Skip to content

Commit

Permalink
Merge pull request #4244 from hashicorp/phinze/resource-arity-errmsg
Browse files Browse the repository at this point in the history
config: friendlier error message on resource arity mismatch
  • Loading branch information
phinze committed Dec 10, 2015
2 parents 07f398f + b6626ee commit bf9af17
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions config/loader_hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,9 @@ func loadResourcesHcl(list *ast.ObjectList) ([]*Resource, error) {
// all of the actual resources.
for _, item := range list.Items {
if len(item.Keys) != 2 {
// TODO: bad error message
return nil, fmt.Errorf("resource needs exactly 2 names")
return nil, fmt.Errorf(
"position %s: resource must be followed by exactly two strings, a type and a name",
item.Pos())
}

t := item.Keys[0].Token.Value().(string)
Expand Down
11 changes: 11 additions & 0 deletions config/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ func TestLoadFile_badType(t *testing.T) {
}
}

func TestLoadFile_resourceArityMistake(t *testing.T) {
_, err := LoadFile(filepath.Join(fixtureDir, "resource-arity-mistake.tf"))
if err == nil {
t.Fatal("should have error")
}
expected := "Error loading test-fixtures/resource-arity-mistake.tf: position 2:10: resource must be followed by exactly two strings, a type and a name"
if err.Error() != expected {
t.Fatalf("expected:\n%s\ngot:\n%s", expected, err)
}
}

func TestLoadFileWindowsLineEndings(t *testing.T) {
testFile := filepath.Join(fixtureDir, "windows-line-endings.tf")

Expand Down
5 changes: 5 additions & 0 deletions config/test-fixtures/resource-arity-mistake.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# I forgot the resource name!
resource "aws_instance" {
ami = "ami-abc123"
instance_type = "t2.micro"
}

0 comments on commit bf9af17

Please sign in to comment.