From 370a471ecb4d940550d040e9df45d4bcfcd18408 Mon Sep 17 00:00:00 2001 From: UKEME BASSEY Date: Thu, 28 Mar 2024 00:40:24 -0400 Subject: [PATCH] fix json -help display and failing tests --- internal/command/e2etest/init_test.go | 3 +- internal/command/init.go | 4 +- internal/command/providers_schema_test.go | 2 + internal/command/providers_test.go | 2 + internal/command/show_test.go | 8 +++ internal/command/test_test.go | 66 ++++++++++++++++++----- internal/command/validate_test.go | 14 +++++ 7 files changed, 83 insertions(+), 16 deletions(-) diff --git a/internal/command/e2etest/init_test.go b/internal/command/e2etest/init_test.go index e1520c8cd9..abe2fb111d 100644 --- a/internal/command/e2etest/init_test.go +++ b/internal/command/e2etest/init_test.go @@ -374,14 +374,13 @@ func TestInitProviderNotFound(t *testing.T) { │ Could not retrieve the list of available versions for provider │ hashicorp/nonexist: provider registry registry.terraform.io does not have a │ provider named registry.terraform.io/hashicorp/nonexist -│ +│` + ` ` + ` │ All modules should specify their required_providers so that external │ consumers will get the correct providers when using a module. To see which │ modules are currently depending on hashicorp/nonexist, run the following │ command: │ terraform providers ╵ - ` if stripAnsi(stderr) != expectedErr { t.Errorf("wrong output:\n%s", cmp.Diff(stripAnsi(stderr), expectedErr)) diff --git a/internal/command/init.go b/internal/command/init.go index ee1a35e8ab..4993c2d982 100644 --- a/internal/command/init.go +++ b/internal/command/init.go @@ -1174,8 +1174,8 @@ Options: -no-color If specified, output won't contain any color. - -json If specified, machine readable output will be - printed in JSON format. + -json If specified, machine readable output will be + printed in JSON format. -plugin-dir Directory containing plugin binaries. This overrides all default search paths for plugins, and prevents the diff --git a/internal/command/providers_schema_test.go b/internal/command/providers_schema_test.go index ce3ee2425c..bcbd48a65c 100644 --- a/internal/command/providers_schema_test.go +++ b/internal/command/providers_schema_test.go @@ -59,9 +59,11 @@ func TestProvidersSchema_output(t *testing.T) { p := providersSchemaFixtureProvider() ui := new(cli.MockUi) + view, _ := testView(t) m := Meta{ testingOverrides: metaOverridesForProvider(p), Ui: ui, + View: view, ProviderSource: providerSource, } diff --git a/internal/command/providers_test.go b/internal/command/providers_test.go index 56f5c0fd7e..98c0c1fe19 100644 --- a/internal/command/providers_test.go +++ b/internal/command/providers_test.go @@ -84,6 +84,7 @@ func TestProviders_modules(t *testing.T) { // first run init with mock provider sources to install the module initUi := new(cli.MockUi) + view, _ := testView(t) providerSource, close := newMockProviderSource(t, map[string][]string{ "foo": {"1.0.0"}, "bar": {"2.0.0"}, @@ -93,6 +94,7 @@ func TestProviders_modules(t *testing.T) { m := Meta{ testingOverrides: metaOverridesForProvider(testProvider()), Ui: initUi, + View: view, ProviderSource: providerSource, } ic := &InitCommand{ diff --git a/internal/command/show_test.go b/internal/command/show_test.go index cc549e860e..a8c369066b 100644 --- a/internal/command/show_test.go +++ b/internal/command/show_test.go @@ -558,10 +558,12 @@ func TestShow_json_output(t *testing.T) { // init ui := new(cli.MockUi) + view, _ := testView(t) ic := &InitCommand{ Meta: Meta{ testingOverrides: metaOverridesForProvider(p), Ui: ui, + View: view, ProviderSource: providerSource, }, } @@ -666,10 +668,12 @@ func TestShow_json_output_sensitive(t *testing.T) { // init ui := new(cli.MockUi) + view, _ := testView(t) ic := &InitCommand{ Meta: Meta{ testingOverrides: metaOverridesForProvider(p), Ui: ui, + View: view, ProviderSource: providerSource, }, } @@ -759,10 +763,12 @@ func TestShow_json_output_conditions_refresh_only(t *testing.T) { // init ui := new(cli.MockUi) + view, _ := testView(t) ic := &InitCommand{ Meta: Meta{ testingOverrides: metaOverridesForProvider(p), Ui: ui, + View: view, ProviderSource: providerSource, }, } @@ -868,10 +874,12 @@ func TestShow_json_output_state(t *testing.T) { // init ui := new(cli.MockUi) + view, _ := testView(t) ic := &InitCommand{ Meta: Meta{ testingOverrides: metaOverridesForProvider(p), Ui: ui, + View: view, ProviderSource: providerSource, }, } diff --git a/internal/command/test_test.go b/internal/command/test_test.go index bf8a1f3e36..a1f4f4dca5 100644 --- a/internal/command/test_test.go +++ b/internal/command/test_test.go @@ -290,8 +290,8 @@ func TestTest_Runs(t *testing.T) { if tc.initCode > 0 { // Then we don't expect the init step to succeed. So we'll check // the init output for our expected error messages and outputs. - - stdout, stderr := ui.ErrorWriter.String(), ui.ErrorWriter.String() + output := done(t).All() + stdout, stderr := output, output if !strings.Contains(stdout, tc.expectedOut) { t.Errorf("output didn't contain expected string:\n\n%s", stdout) @@ -872,8 +872,8 @@ can remove the provider configuration again. actualOut, expectedOut := output.Stdout(), tc.expectedOut actualErr, expectedErr := output.Stderr(), tc.expectedErr - if diff := cmp.Diff(actualOut, expectedOut); len(diff) > 0 { - t.Errorf("output didn't match expected:\nexpected:\n%s\nactual:\n%s\ndiff:\n%s", expectedOut, actualOut, diff) + if !strings.Contains(actualOut, expectedOut) { + t.Errorf("output didn't match expected:\nexpected:\n%s\nactual:\n%s", expectedOut, actualOut) } if diff := cmp.Diff(actualErr, expectedErr); len(diff) > 0 { @@ -1063,8 +1063,8 @@ Success! 5 passed, 0 failed. actual := output.All() - if diff := cmp.Diff(actual, expected); len(diff) > 0 { - t.Errorf("output didn't match expected:\nexpected:\n%s\nactual:\n%s\ndiff:\n%s", expected, actual, diff) + if !strings.Contains(actual, expected) { + t.Errorf("output didn't match expected:\nexpected:\n%s\nactual:\n%s", expected, actual) } if provider.ResourceCount() > 0 { @@ -1124,10 +1124,10 @@ main.tftest.hcl... pass Success! 2 passed, 0 failed. ` - actual := output.All() + actual := output.Stdout() - if diff := cmp.Diff(actual, expected); len(diff) > 0 { - t.Errorf("output didn't match expected:\nexpected:\n%s\nactual:\n%s\ndiff:\n%s", expected, actual, diff) + if !strings.Contains(actual, expected) { + t.Errorf("output didn't match expected:\nexpected:\n%s\nactual:\n%s", expected, actual) } if provider.ResourceCount() > 0 { @@ -1915,7 +1915,21 @@ func TestTest_InvalidOverrides(t *testing.T) { t.Errorf("expected status code 0 but got %d", code) } - expected := `main.tftest.hcl... in progress + expected := ` +Warning: Incomplete lock file information for providers + +Due to your customized provider installation methods, Terraform was forced to +calculate lock file checksums locally for the following providers: + - hashicorp/test + +The current .terraform.lock.hcl file only includes checksums for linux_amd64, +so Terraform running on another platform will fail to install these +providers. + +To calculate additional checksums for another platform, run: + terraform providers lock -platform=linux_amd64 +(where linux_amd64 is the platform to generate) +main.tftest.hcl... in progress run "setup"... pass Warning: Invalid override target @@ -2009,7 +2023,21 @@ func TestTest_RunBlocksInProviders(t *testing.T) { t.Errorf("expected status code 0 but got %d", code) } - expected := `main.tftest.hcl... in progress + expected := ` +Warning: Incomplete lock file information for providers + +Due to your customized provider installation methods, Terraform was forced to +calculate lock file checksums locally for the following providers: + - hashicorp/test + +The current .terraform.lock.hcl file only includes checksums for linux_amd64, +so Terraform running on another platform will fail to install these +providers. + +To calculate additional checksums for another platform, run: + terraform providers lock -platform=linux_amd64 +(where linux_amd64 is the platform to generate) +main.tftest.hcl... in progress run "setup"... pass run "main"... pass main.tftest.hcl... tearing down @@ -2070,7 +2098,21 @@ func TestTest_RunBlocksInProviders_BadReferences(t *testing.T) { t.Errorf("expected status code 1 but got %d", code) } - expectedOut := `missing_run_block.tftest.hcl... in progress + expectedOut := ` +Warning: Incomplete lock file information for providers + +Due to your customized provider installation methods, Terraform was forced to +calculate lock file checksums locally for the following providers: + - hashicorp/test + +The current .terraform.lock.hcl file only includes checksums for linux_amd64, +so Terraform running on another platform will fail to install these +providers. + +To calculate additional checksums for another platform, run: + terraform providers lock -platform=linux_amd64 +(where linux_amd64 is the platform to generate) +missing_run_block.tftest.hcl... in progress run "main"... fail missing_run_block.tftest.hcl... tearing down missing_run_block.tftest.hcl... fail diff --git a/internal/command/validate_test.go b/internal/command/validate_test.go index cc960fb87b..d7a7e94ff0 100644 --- a/internal/command/validate_test.go +++ b/internal/command/validate_test.go @@ -362,6 +362,20 @@ func TestValidateWithInvalidOverrides(t *testing.T) { actual := output.All() expected := ` +Warning: Incomplete lock file information for providers + +Due to your customized provider installation methods, Terraform was forced to +calculate lock file checksums locally for the following providers: + - hashicorp/test + +The current .terraform.lock.hcl file only includes checksums for linux_amd64, +so Terraform running on another platform will fail to install these +providers. + +To calculate additional checksums for another platform, run: + terraform providers lock -platform=linux_amd64 +(where linux_amd64 is the platform to generate) + Warning: Invalid override target on main.tftest.hcl line 4, in mock_provider "test":