Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add replace program utility #94

Merged
merged 10 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions pulumitest/pulumiYAML.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package pulumitest

import (
"os"
"path/filepath"
"strings"
)

// WritePulumiYaml writes the contents of the program string to the Pulumi.yaml file in the current testing directory.
// YAML does not allow tabs, so this function will error if the program contains tabs.
func (a *PulumiTest) WritePulumiYaml(t PT, program string) {
t.Helper()

// find the line of the program that contains tabs
lines := strings.Split(program, "\n")
for i, line := range lines {
if strings.Contains(line, "\t") {
ptFatalF(t, "program contains tabs on line %d: %s\nTry replacing it with:\n%s", i+1, line, strings.ReplaceAll(program, "\t", " "))
}
}

pulumiYamlPath := filepath.Join(a.CurrentStack().Workspace().WorkDir(), "Pulumi.yaml")
err := os.WriteFile(pulumiYamlPath, []byte(program), 0o600)
if err != nil {
ptFatalF(t, "failed to replace program %s", err)
}
}

// ReadPulumiYaml reads the contents of the Pulumi.yaml file in the current testing directory.
func (a *PulumiTest) ReadPulumiYaml(t PT) string {
t.Helper()

pulumiYamlPath := filepath.Join(a.CurrentStack().Workspace().WorkDir(), "Pulumi.yaml")
program, err := os.ReadFile(pulumiYamlPath)
if err != nil {
ptFatalF(t, "failed to read program %s", err)
}
return string(program)
}
43 changes: 43 additions & 0 deletions pulumitest/pulumiYAML_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package pulumitest_test

import (
"testing"

"github.com/pulumi/providertest/pulumitest"
"github.com/stretchr/testify/require"
)

func TestReplaceProgram(t *testing.T) {
t.Parallel()

test := pulumitest.NewPulumiTest(t, "testdata/yaml_empty")

test.WritePulumiYaml(t, `
name: yaml_empty
runtime: yaml
outputs:
output: "output"`)

res := test.Up(t)

require.Equal(t, "output", res.Outputs["output"].Value)
}

func TestReadProgram(t *testing.T) {
t.Parallel()

test := pulumitest.NewPulumiTest(t, "testdata/yaml_empty")

program := `
name: yaml_empty
runtime: yaml
outputs:
output: "output"`

test.WritePulumiYaml(t, program)

res := test.Up(t)
require.Equal(t, "output", res.Outputs["output"].Value)

require.Equal(t, program, test.ReadPulumiYaml(t))
}
Loading