Skip to content

Commit

Permalink
chore: Add tests for project and stack edits
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronFriel committed Dec 10, 2022
1 parent a748861 commit 12a8743
Showing 1 changed file with 184 additions and 0 deletions.
184 changes: 184 additions & 0 deletions tests/roundtrip_test.go
@@ -0,0 +1,184 @@
// Copyright 2016-2021, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// nolint: lll
package tests

import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/hexops/autogold"
"github.com/stretchr/testify/require"

"github.com/pulumi/pulumi/pkg/v3/testing/integration"
ptesting "github.com/pulumi/pulumi/sdk/v3/go/common/testing"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
)

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

e := ptesting.NewEnvironment(t)
defer func() {
if !t.Failed() {
e.DeleteEnvironment()
}
}()

pulumiProject := `
# 馃敶 header comment
name: pulumi-test
runtime: yaml
config:
first-value:
type: string
default: first
second-value:
type: string
third-value:
type: array
items:
type: string
default: [third] # 馃煚 comment after array
# 馃煛 comment before resources
resources:
my-bucket:
type: aws:s3:bucket
# 馃煝 comment before props, note the indentation is excessive, will change to 2 spaces
properties:
# 馃數 comment before prop
bucket: test-123 # 馃煟 comment after prop
# 馃煡 footer comment
`

integration.CreatePulumiRepo(e, pulumiProject)
projFilename := filepath.Join(e.CWD, fmt.Sprintf("%s.yaml", workspace.ProjectFile))
// TODO: Replace this with config set --project after implemented.
proj, err := workspace.LoadProject(projFilename)
require.NoError(t, err)
ty := "string"
proj.Config["new-value"] = workspace.ProjectConfigType{
Type: &ty,
Description: "馃挏 a new value added to config, expect unicode to be escaped",
}
err = proj.Save(projFilename)
require.NoError(t, err)

projData, err := os.ReadFile(projFilename)
require.NoError(t, err)

// Project file:
want := autogold.Want("roundtrip-project", `# 馃敶 header comment
name: pulumi-test
runtime: yaml
config:
first-value:
type: string
default: first
new-value:
type: string
description: "\U0001F49C a new value added to config, expect unicode to be escaped"
second-value:
type: string
third-value:
type: array
items:
type: string
default: [third] # 馃煚 comment after array
# 馃煛 comment before resources
resources:
my-bucket:
# 馃煝 comment before props, note the indentation is excessive, will change to 2 spaces
properties:
# 馃數 comment before prop
bucket: test-123 # 馃煟 comment after prop
type: aws:s3:bucket
# 馃煡 footer comment
`)
want.Equal(t, string(projData))
}

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

e := ptesting.NewEnvironment(t)
defer func() {
if !t.Failed() {
e.DeleteEnvironment()
}
}()

pulumiProject := `
name: foo
runtime: yaml
`

integration.CreatePulumiRepo(e, pulumiProject)
e.SetBackend(e.LocalURL())
e.RunCommand("pulumi", "stack", "init", "test")
e.Passphrase = "TestConfigRoundtripComments"
configFilename := filepath.Join(e.CWD, fmt.Sprintf("%s.test.yaml", workspace.ProjectFile))

err := os.WriteFile(configFilename, []byte(`
encryptionsalt: v1:ThS5UPxP9qc=:v1:UZYAXF+ylaJ0rGhv:9OTvBnOEDFgxs7btjzSu+LZ470vLpg==
# 馃敶 header comment
config:
foo:a: some-value # 馃煚 comment after value
# 馃煛 comment before key
foo:b: some-value
foo:c:
a: A
b: B
c:
- 1
- 2
- 3 # 馃煝 comment in array
# 馃數 comment after array
foo:d:
secure: v1:T1ftqhY0hqr+EJK6:+jvd5PMecFx80tcavzuZY4tLatgIfoe/xR72GA== # 馃煟 comment on secret
# 馃煡 footer comment`), 0600)
require.NoError(t, err)
e.RunCommand("pulumi", "config", "set", "e", "E")
e.RunCommand("pulumi", "config", "set", "--path", "c.c[2]", "three")

projData, err := os.ReadFile(configFilename)
require.NoError(t, err)

// Project file:
want := autogold.Want("roundtrip-config", `encryptionsalt: v1:ThS5UPxP9qc=:v1:UZYAXF+ylaJ0rGhv:9OTvBnOEDFgxs7btjzSu+LZ470vLpg==
# 馃敶 header comment
config:
foo:a: some-value # 馃煚 comment after value
# 馃煛 comment before key
foo:b: some-value
foo:c:
a: A
b: B
c:
- 1
- 2
- three # 馃煝 comment in array
# 馃數 comment after array
foo:d:
secure: v1:T1ftqhY0hqr+EJK6:+jvd5PMecFx80tcavzuZY4tLatgIfoe/xR72GA== # 馃煟 comment on secret
foo:e: E
# 馃煡 footer comment
`)
want.Equal(t, string(projData))
}

0 comments on commit 12a8743

Please sign in to comment.