Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't ignore YAML parsing errors #496

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion actions/recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ func (y *YamlAction) UnmarshalYAML(unmarshal func(interface{}) error) error {
return fmt.Errorf("Unknown action: %v", aux.Action)
}

unmarshal(y.Action)
err = unmarshal(y.Action)
if err != nil {
return err
}

return nil
}
Expand Down
35 changes: 31 additions & 4 deletions actions/recipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ type subRecipe struct {
type testSubRecipe struct {
recipe string
subrecipe subRecipe
err string
err string
parseErr string
}

func TestSubRecipe(t *testing.T) {
Expand Down Expand Up @@ -242,6 +243,7 @@ actions:
`,
recipeAmd64,
"", // Do not expect failure
"", // Do not expect parse failure
},
{
// Test recipe with inherited architecture OK
Expand All @@ -254,6 +256,7 @@ actions:
`,
recipeInheritedArch,
"", // Do not expect failure
"", // Do not expect parse failure
},
{
// Fail with unknown recipe
Expand All @@ -266,6 +269,7 @@ actions:
`,
recipeAmd64,
"stat /tmp/unknown_recipe.yaml: no such file or directory",
"", // Do not expect parse failure
},
{
// Fail with different architecture recipe
Expand All @@ -278,6 +282,22 @@ actions:
`,
recipeArmhf,
"Expect architecture 'amd64' but got 'armhf'",
"", // Do not expect parse failure
},
{
// Fail with type mismatch during parse
`
architecture: armhf

actions:
- action: recipe
recipe: armhf.yaml
variables:
- foo
`,
recipeArmhf,
"",
"yaml: unmarshal errors:\n line 8: cannot unmarshal !!seq into map[string]string",
},
}

Expand Down Expand Up @@ -306,17 +326,24 @@ func runTestWithSubRecipes(t *testing.T, test testSubRecipe, templateVars ...map
file_subrecipe.WriteString(test.subrecipe.recipe)
file_subrecipe.Close()

failed := false

r := actions.Recipe{}
if len(templateVars) == 0 {
err = r.Parse(file.Name(), false, false)
} else {
err = r.Parse(file.Name(), false, false, templateVars[0])
}

// Should not expect error during parse
failed := !assert.Empty(t, err)
if len(test.parseErr) > 0 {
// Expected parse error?
failed = !assert.EqualError(t, err, test.parseErr)
} else {
// Unexpected error
failed = !assert.Empty(t, err)
}

if !failed {
if err == nil {
context.Architecture = r.Architecture
context.RecipeDir = dir

Expand Down