Skip to content

Commit

Permalink
Merge pull request #496 from fooishbar/no-silent-yaml-parse-fail
Browse files Browse the repository at this point in the history
Don't ignore YAML parsing errors
  • Loading branch information
obbardc authored May 20, 2024
2 parents 6550de9 + 27da56f commit 09629ab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
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

0 comments on commit 09629ab

Please sign in to comment.