Skip to content

Commit

Permalink
add read yam
Browse files Browse the repository at this point in the history
  • Loading branch information
VenelinMartinov committed Oct 3, 2024
1 parent 689440f commit 0f4c5cd
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
37 changes: 37 additions & 0 deletions pulumitest/pulumiYAML.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package pulumitest

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

// 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)
}
}

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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,29 @@ func TestReplaceProgram(t *testing.T) {

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

// Note the forbidden tab character in the program
test.WritePulumiYaml(t, `
name: yaml_empty
runtime: yaml
outputs:
output: "output"`)
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_prog
runtime: yaml
outputs:
output: "output"`

test.WritePulumiYaml(t, program)

require.Equal(t, program, test.ReadPulumiYaml(t))
}
20 changes: 0 additions & 20 deletions pulumitest/writePulumiYAML.go

This file was deleted.

0 comments on commit 0f4c5cd

Please sign in to comment.