Skip to content

Commit 387f6b5

Browse files
Add replace program utility (#94)
This adds a ReplaceProgram utility to pulumitest which replaces the Pulumi program used by the test. I've written this quite a few times now, so it seems like a good candidate as a utility - should be useful when testing updates.
1 parent c0ea332 commit 387f6b5

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

pulumitest/pulumiYAML.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package pulumitest
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"strings"
7+
)
8+
9+
// WritePulumiYaml writes the contents of the program string to the Pulumi.yaml file in the current testing directory.
10+
// YAML does not allow tabs, so this function will error if the program contains tabs.
11+
func (a *PulumiTest) WritePulumiYaml(t PT, program string) {
12+
t.Helper()
13+
14+
// find the line of the program that contains tabs
15+
lines := strings.Split(program, "\n")
16+
for i, line := range lines {
17+
if strings.Contains(line, "\t") {
18+
ptFatalF(t, "program contains tabs on line %d: %s\nTry replacing it with:\n%s", i+1, line, strings.ReplaceAll(program, "\t", " "))
19+
}
20+
}
21+
22+
pulumiYamlPath := filepath.Join(a.CurrentStack().Workspace().WorkDir(), "Pulumi.yaml")
23+
err := os.WriteFile(pulumiYamlPath, []byte(program), 0o600)
24+
if err != nil {
25+
ptFatalF(t, "failed to replace program %s", err)
26+
}
27+
}
28+
29+
// ReadPulumiYaml reads the contents of the Pulumi.yaml file in the current testing directory.
30+
func (a *PulumiTest) ReadPulumiYaml(t PT) string {
31+
t.Helper()
32+
33+
pulumiYamlPath := filepath.Join(a.CurrentStack().Workspace().WorkDir(), "Pulumi.yaml")
34+
program, err := os.ReadFile(pulumiYamlPath)
35+
if err != nil {
36+
ptFatalF(t, "failed to read program %s", err)
37+
}
38+
return string(program)
39+
}

pulumitest/pulumiYAML_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package pulumitest_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pulumi/providertest/pulumitest"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestReplaceProgram(t *testing.T) {
11+
t.Parallel()
12+
13+
test := pulumitest.NewPulumiTest(t, "testdata/yaml_empty")
14+
15+
test.WritePulumiYaml(t, `
16+
name: yaml_empty
17+
runtime: yaml
18+
outputs:
19+
output: "output"`)
20+
21+
res := test.Up(t)
22+
23+
require.Equal(t, "output", res.Outputs["output"].Value)
24+
}
25+
26+
func TestReadProgram(t *testing.T) {
27+
t.Parallel()
28+
29+
test := pulumitest.NewPulumiTest(t, "testdata/yaml_empty")
30+
31+
program := `
32+
name: yaml_empty
33+
runtime: yaml
34+
outputs:
35+
output: "output"`
36+
37+
test.WritePulumiYaml(t, program)
38+
39+
res := test.Up(t)
40+
require.Equal(t, "output", res.Outputs["output"].Value)
41+
42+
require.Equal(t, program, test.ReadPulumiYaml(t))
43+
}

0 commit comments

Comments
 (0)