Skip to content

Commit

Permalink
Add tests for code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
cgetzen committed May 29, 2020
1 parent ef1f2da commit c4a87eb
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
6 changes: 6 additions & 0 deletions configs/testdata/invalid-files/variable-validation-bad-msg.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
variable "validation" {
validation {
condition = var.validation != 4
# ERROR: Missing required argument
}
}
74 changes: 74 additions & 0 deletions terraform/eval_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,77 @@ variable "test" {
t.Fatalf("expected error to contain %q\nerror was:\n%s", want, got)
}
}

func TestContext2Apply_variableCustomValidationsNilStringError(t *testing.T) {
// This test is for custom validation rules associated with root module
// variables, and specifically that we handle the situation where their
// values are unknown during validation, skipping the validation check
// altogether. (Root module variables are never known during validation.)
m := testModuleInline(t, map[string]string{
"main.tf": `
variable "test" {
type = string
default = "four"
validation {
condition = length(var.test) > 5
error_message = ""
}
}
`,
})

p := testProvider("test")
ctx := testContext2(t, &ContextOpts{
Config: m,
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
})

_, diags := ctx.Apply()
want := `Invalid validation error message: An empty string is not a valid nor useful error message.`
if !diags.HasErrors() {
t.Fatalf("expected an error that contained %q", want)
}
if got := diags.Err().Error(); !strings.Contains(got, want) {
t.Fatalf("expected error to contain %q\nerror was:\n%s", want, got)
}
}

func TestContext2Apply_variableCustomValidationsNonStringError(t *testing.T) {
// This test is for custom validation rules associated with root module
// variables, and specifically that we handle the situation where their
// values are unknown during validation, skipping the validation check
// altogether. (Root module variables are never known during validation.)
m := testModuleInline(t, map[string]string{
"main.tf": `
variable "test" {
type = string
default = "four"
validation {
condition = length(var.test) > 5
error_message = [4, 3]
}
}
`,
})

p := testProvider("test")
ctx := testContext2(t, &ContextOpts{
Config: m,
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
})

_, diags := ctx.Apply()
want := `Invalid validation error message: Invalid validation error message result value: string required.`
if !diags.HasErrors() {
t.Fatalf("expected an error that contained %q", want)
}
if got := diags.Err().Error(); !strings.Contains(got, want) {
t.Fatalf("expected error to contain %q\nerror was:\n%s", want, got)
}
}

0 comments on commit c4a87eb

Please sign in to comment.