Skip to content

Commit

Permalink
feat: improve error message for invalid extra atlantis dependencies (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bmbferreira authored Feb 5, 2025
1 parent 56eb7cb commit f74a96e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
22 changes: 22 additions & 0 deletions cmd/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,28 @@ func TestExtraDeclaredDependencies(t *testing.T) {
})
}

func TestNonStringErrorOnExtraDeclaredDependencies(t *testing.T) {
err := resetForRun()
if err != nil {
t.Error("Failed to reset default flags")
return
}

rootCmd.SetArgs([]string{
"generate",
"--root",
filepath.Join("..", "test_examples_errors", "extra_dependency_error"),
})
err = rootCmd.Execute()

expectedError := "extra_atlantis_dependencies contains non-string value at position 4"
if err == nil || err.Error() != expectedError {
t.Errorf("Expected error '%s', got '%v'", expectedError, err)
return
}
return
}

func TestLocalTerraformModuleSource(t *testing.T) {
runTest(t, filepath.Join("golden", "local_terraform_module.yaml"), []string{
"--root",
Expand Down
22 changes: 15 additions & 7 deletions cmd/parse_locals.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/zclconf/go-cty/cty"


"fmt"
"path/filepath"
)

Expand Down Expand Up @@ -126,17 +127,19 @@ func parseLocals(path string, terragruntOptions *options.TerragruntOptions, incl
mergedParentLocals = mergeResolvedLocals(mergedParentLocals, parentLocals)
}
}
childLocals := resolveLocals(*localsAsCty)

childLocals, err := resolveLocals(*localsAsCty)
if err != nil {
return ResolvedLocals{}, err
}
return mergeResolvedLocals(mergedParentLocals, childLocals), nil
}

func resolveLocals(localsAsCty cty.Value) ResolvedLocals {
func resolveLocals(localsAsCty cty.Value) (ResolvedLocals,error) {
resolved := ResolvedLocals{}

// Return an empty set of locals if no `locals` block was present
if localsAsCty == cty.NilVal {
return resolved
return resolved, nil
}
rawLocals := localsAsCty.AsValueMap()

Expand Down Expand Up @@ -182,13 +185,18 @@ func resolveLocals(localsAsCty cty.Value) ResolvedLocals {
if ok {
it := extraDependenciesAsCty.ElementIterator()
for it.Next() {
_, val := it.Element()
pos, val := it.Element()
if !val.Type().Equals(cty.String) {
posInt, _ := pos.AsBigFloat().Int64()
return resolved, fmt.Errorf("extra_atlantis_dependencies contains non-string value at position %d", posInt)
}

resolved.ExtraAtlantisDependencies = append(
resolved.ExtraAtlantisDependencies,
filepath.ToSlash(val.AsString()),
)
}
}

return resolved
return resolved, nil
}
15 changes: 15 additions & 0 deletions test_examples_errors/extra_dependency_error/child/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
terraform {
source = "git::[email protected]:transcend-io/terraform-aws-fargate-container?ref=v0.0.4"
}

locals {
tg_config = read_terragrunt_config(find_in_parent_folders("config.hcl"))

extra_atlantis_dependencies = [
"some_extra_dep0",
"some_extra_dep1",
"some_extra_dep2",
"some_extra_dep3",
local.tg_config
]
}
3 changes: 3 additions & 0 deletions test_examples_errors/extra_dependency_error/config.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
locals {
aws_account_id = "111131111219"
}

0 comments on commit f74a96e

Please sign in to comment.