From 4a5f47bc2cfbe4264ad959d94b089e6848bf4491 Mon Sep 17 00:00:00 2001 From: Matt Stofko Date: Thu, 19 Nov 2020 12:31:30 -0800 Subject: [PATCH 1/2] Adding support for errors that get line wrapped --- tfexec/errors.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tfexec/errors.go b/tfexec/errors.go index 010b3887..6515033f 100644 --- a/tfexec/errors.go +++ b/tfexec/errors.go @@ -11,7 +11,7 @@ import ( var ( // The "Required variable not set:" case is for 0.11 missingVarErrRegexp = regexp.MustCompile(`Error: No value for required variable|Error: Required variable not set:`) - missingVarNameRegexp = regexp.MustCompile(`The root module input variable "(.+)" is not set, and has no default|Error: Required variable not set: (.+)`) + missingVarNameRegexp = regexp.MustCompile(`The root module input variable\s"(.+)"\sis\snot\sset,\sand\shas\sno\sdefault|Error: Required variable not set: (.+)`) usageRegexp = regexp.MustCompile(`Too many command line arguments|^Usage: .*Options:.*|Error: Invalid -\d+ option`) From 0f2b22371ffef2a5b01344e60a9302bae9baacdb Mon Sep 17 00:00:00 2001 From: Matt Stofko Date: Thu, 19 Nov 2020 12:32:17 -0800 Subject: [PATCH 2/2] Adding test for long var names in error message --- tfexec/internal/e2etest/errors_test.go | 27 +++++++++++++++++--- tfexec/internal/e2etest/testdata/var/main.tf | 5 +++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/tfexec/internal/e2etest/errors_test.go b/tfexec/internal/e2etest/errors_test.go index cd7b97df..d032c12b 100644 --- a/tfexec/internal/e2etest/errors_test.go +++ b/tfexec/internal/e2etest/errors_test.go @@ -43,7 +43,12 @@ func TestMissingVar(t *testing.T) { t.Fatalf("err during init: %s", err) } - _, err = tf.Plan(context.Background()) + // Variable names from testdata/var/main.tf + shortVarName := "no_default" + longVarName := "no_default_really_long_variable_name_that_will_line_wrap_tf_output" + + // Test for ErrMissingVar and properly formatted error message on shorter variable names + _, err = tf.Plan(context.Background(), tfexec.Var(longVarName+"=foo")) if err == nil { t.Fatalf("expected error running Plan, none returned") } @@ -52,11 +57,25 @@ func TestMissingVar(t *testing.T) { t.Fatalf("expected ErrMissingVar, got %T, %s", err, err) } - if e.VariableName != "no_default" { - t.Fatalf("expected missing no_default, got %q", e.VariableName) + if e.VariableName != shortVarName { + t.Fatalf("expected missing %s, got %q", shortVarName, e.VariableName) + } + + // Test for ErrMissingVar and properly formatted error message on long variable names + _, err = tf.Plan(context.Background(), tfexec.Var(shortVarName+"=foo")) + if err == nil { + t.Fatalf("expected error running Plan, none returned") + } + if !errors.As(err, &e) { + t.Fatalf("expected ErrMissingVar, got %T, %s", err, err) + } + + if e.VariableName != longVarName { + t.Fatalf("expected missing %s, got %q", longVarName, e.VariableName) } - _, err = tf.Plan(context.Background(), tfexec.Var("no_default=foo")) + // Test for no error when all variables have a value + _, err = tf.Plan(context.Background(), tfexec.Var(shortVarName+"=foo"), tfexec.Var(longVarName+"=foo")) if err != nil { t.Fatalf("expected no error, got %s", err) } diff --git a/tfexec/internal/e2etest/testdata/var/main.tf b/tfexec/internal/e2etest/testdata/var/main.tf index 4abb28c3..4911386a 100644 --- a/tfexec/internal/e2etest/testdata/var/main.tf +++ b/tfexec/internal/e2etest/testdata/var/main.tf @@ -3,4 +3,7 @@ variable "default" { } variable "no_default" { -} \ No newline at end of file +} + +variable "no_default_really_long_variable_name_that_will_line_wrap_tf_output" { +}