Merge pull request #4244 from hashicorp/phinze/resource-arity-errmsg

config: friendlier error message on resource arity mismatch
This commit is contained in:
Paul Hinze 2015-12-10 08:38:04 -06:00
commit bf9af17e4f
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. // all of the actual resources.
for _, item := range list.Items { for _, item := range list.Items {
if len(item.Keys) != 2 { if len(item.Keys) != 2 {
// TODO: bad error message return nil, fmt.Errorf(
return nil, fmt.Errorf("resource needs exactly 2 names") "position %s: resource must be followed by exactly two strings, a type and a name",
item.Pos())
} }
t := item.Keys[0].Token.Value().(string) 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) { func TestLoadFileWindowsLineEndings(t *testing.T) {
testFile := filepath.Join(fixtureDir, "windows-line-endings.tf") 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"
}