Improve error message and add test for #1440 (#8268)

* Improve error message and add test for #1440

* lint

* lint

* Use %w instead of errors.Wrap
This commit is contained in:
Fraser Waters 2021-10-26 16:55:52 +01:00 committed by GitHub
parent 22d2ef247f
commit b39c81e4cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 1 deletions

View file

@ -16,6 +16,7 @@ package encoding
import (
"encoding/json"
"fmt"
"path/filepath"
yaml "gopkg.in/yaml.v2"
@ -106,5 +107,14 @@ func (m *yamlMarshaler) Unmarshal(data []byte, v interface{}) error {
// IDEA: use a "strict" marshaler, so that we can warn on unrecognized keys (avoiding silly mistakes). We should
// set aside an officially sanctioned area in the metadata for extensibility by 3rd parties.
return yaml.Unmarshal(data, v)
err := yaml.Unmarshal(data, v)
if err != nil {
// Return type errors directly
if _, ok := err.(*yaml.TypeError); ok {
return err
}
// Other errors will be parse errors due to invalid syntax
return fmt.Errorf("invalid YAML file: %w", err)
}
return nil
}

View file

@ -106,6 +106,36 @@ func TestStackTagValidation(t *testing.T) {
})
}
// TestStackInitValidation verifies various error scenarios related to init'ing a stack.
func TestStackInitValidation(t *testing.T) {
t.Run("Error_InvalidStackYaml", func(t *testing.T) {
e := ptesting.NewEnvironment(t)
defer func() {
if !t.Failed() {
e.DeleteEnvironment()
}
}()
e.RunCommand("git", "init")
e.ImportDirectory("stack_project_name")
e.RunCommand("pulumi", "login", "--cloud-url", e.LocalURL())
// Starting a yaml value with a quote string and then more data is invalid
invalidYaml := "\"this is invalid\" yaml because of trailing data after quote string"
// Change the contents of the Description property of Pulumi.yaml.
yamlPath := filepath.Join(e.CWD, "Pulumi.yaml")
err := integration.ReplaceInFile("description: ", "description: "+invalidYaml, yamlPath)
assert.NoError(t, err)
stdout, stderr := e.RunCommandExpectError("pulumi", "stack", "init", "valid-name")
assert.Equal(t, "", stdout)
assert.Contains(t, stderr,
"error: could not get cloud url: could not load current project: "+
"invalid YAML file: yaml: line 1: did not find expected key")
})
}
// TestConfigSave ensures that config commands in the Pulumi CLI work as expected.
func TestConfigSave(t *testing.T) {
e := ptesting.NewEnvironment(t)