config: friendlier error message on resource arity mismatch

closes #2072
This commit is contained in:
Paul Hinze 2015-12-09 18:05:49 -06:00
parent 3041920d37
commit b6626eed57
3 changed files with 19 additions and 2 deletions

View File

@ -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)

View File

@ -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")

View File

@ -0,0 +1,5 @@
# I forgot the resource name!
resource "aws_instance" {
ami = "ami-abc123"
instance_type = "t2.micro"
}