Skip to content

Commit

Permalink
Add replace program utility (#94)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
VenelinMartinov authored Oct 3, 2024
1 parent c0ea332 commit 387f6b5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
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))
}

0 comments on commit 387f6b5

Please sign in to comment.