From b6626eed577806b1d635616f0481603cbbde737e Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Wed, 9 Dec 2015 18:05:49 -0600 Subject: [PATCH] config: friendlier error message on resource arity mismatch closes #2072 --- config/loader_hcl.go | 5 +++-- config/loader_test.go | 11 +++++++++++ config/test-fixtures/resource-arity-mistake.tf | 5 +++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 config/test-fixtures/resource-arity-mistake.tf diff --git a/config/loader_hcl.go b/config/loader_hcl.go index c62ca3731..59fe81795 100644 --- a/config/loader_hcl.go +++ b/config/loader_hcl.go @@ -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) diff --git a/config/loader_test.go b/config/loader_test.go index 4c291f6e9..6dbcfbede 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -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") diff --git a/config/test-fixtures/resource-arity-mistake.tf b/config/test-fixtures/resource-arity-mistake.tf new file mode 100644 index 000000000..14e49ddf8 --- /dev/null +++ b/config/test-fixtures/resource-arity-mistake.tf @@ -0,0 +1,5 @@ +# I forgot the resource name! +resource "aws_instance" { + ami = "ami-abc123" + instance_type = "t2.micro" +}