Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Uk1288 committed Apr 16, 2024
1 parent ffabb5e commit d660496
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 27 deletions.
114 changes: 104 additions & 10 deletions internal/command/arguments/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

func TestParseInit_basicValid(t *testing.T) {
var flagNameValue []FlagNameValue
testCases := map[string]struct {
args []string
want *Init
Expand All @@ -34,13 +36,21 @@ func TestParseInit_basicValid(t *testing.T) {
Upgrade: false,
Json: false,
IgnoreRemoteVersion: false,
BackendConfig: FlagNameValueSlice{
FlagName: "-backend-config",
Items: &flagNameValue,
},
Vars: &Vars{},
InputEnabled: true,
CompactWarnings: false,
TargetFlags: nil,
},
},
"setting multiple options": {
[]string{"-backend=false", "-force-copy=true",
"-from-module=./main-dir", "-json", "-get=false",
"-lock=false", "-lock-timeout=10s", "-reconfigure=true",
"-upgrade=true", "-lockfile=readonly",
"-upgrade=true", "-lockfile=readonly", "-compact-warnings=true",
"-ignore-remote-version=true", "-test-directory=./test-dir"},
&Init{
FromModule: "./main-dir",
Expand All @@ -58,10 +68,19 @@ func TestParseInit_basicValid(t *testing.T) {
Upgrade: true,
Json: true,
IgnoreRemoteVersion: true,
BackendConfig: FlagNameValueSlice{
FlagName: "-backend-config",
Items: &flagNameValue,
},
Vars: &Vars{},
InputEnabled: true,
Args: []string{},
CompactWarnings: true,
TargetFlags: nil,
},
},
"with cloud option": {
[]string{"-cloud=false"},
[]string{"-cloud=false", "-input=false", "-target=foo_bar.baz", "-backend-config", "backend.config"},
&Init{
FromModule: "",
Lockfile: "",
Expand All @@ -78,18 +97,29 @@ func TestParseInit_basicValid(t *testing.T) {
Upgrade: false,
Json: false,
IgnoreRemoteVersion: false,
BackendConfig: FlagNameValueSlice{
FlagName: "-backend-config",
Items: &[]FlagNameValue{{Name: "-backend-config", Value: "backend.config"}},
},
Vars: &Vars{},
InputEnabled: false,
Args: []string{},
CompactWarnings: false,
TargetFlags: []string{"foo_bar.baz"},
},
},
}

cmpOpts := cmpopts.IgnoreUnexported(Vars{})

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got, diags := ParseInit(tc.args)
if len(diags) > 0 {
t.Fatalf("unexpected diags: %v", diags)
}

if diff := cmp.Diff(tc.want, got); diff != "" {
if diff := cmp.Diff(tc.want, got, cmpOpts); diff != "" {
t.Errorf("unexpected result\n%s", diff)
}
})
Expand All @@ -98,16 +128,29 @@ func TestParseInit_basicValid(t *testing.T) {

func TestParseInit_invalid(t *testing.T) {
testCases := map[string]struct {
args []string
wantErr string
args []string
wantErr string
wantViewType ViewType
}{
"with unsupported options": {
args: []string{"-raw"},
wantErr: "flag provided but not defined",
args: []string{"-raw"},
wantErr: "flag provided but not defined",
wantViewType: ViewHuman,
},
"with both -backend and -cloud options set": {
args: []string{"-backend=false", "-cloud=false"},
wantErr: "The -backend and -cloud options are aliases of one another and mutually-exclusive in their use",
args: []string{"-backend=false", "-cloud=false"},
wantErr: "The -backend and -cloud options are aliases of one another and mutually-exclusive in their use",
wantViewType: ViewHuman,
},
"with both -migrate-state and -json options set": {
args: []string{"-migrate-state", "-json"},
wantErr: "Terraform cannot ask for interactive approval when -json is set. To use the -migrate-state option, disable the -json option.",
wantViewType: ViewJSON,
},
"with both -migrate-state and -reconfigure options set": {
args: []string{"-migrate-state", "-reconfigure"},
wantErr: "The -migrate-state and -reconfigure options are mutually-exclusive.",
wantViewType: ViewHuman,
},
}

Expand All @@ -120,9 +163,60 @@ func TestParseInit_invalid(t *testing.T) {
if got, want := diags.Err().Error(), tc.wantErr; !strings.Contains(got, want) {
t.Fatalf("wrong diags\n got: %s\nwant: %s", got, want)
}
if got.ViewType != ViewHuman {
if got.ViewType != tc.wantViewType {
t.Fatalf("wrong view type, got %#v, want %#v", got.ViewType, ViewHuman)
}
})
}
}

func TestParseInit_vars(t *testing.T) {
testCases := map[string]struct {
args []string
want []FlagNameValue
}{
"no var flags by default": {
args: nil,
want: nil,
},
"one var": {
args: []string{"-var", "foo=bar"},
want: []FlagNameValue{
{Name: "-var", Value: "foo=bar"},
},
},
"one var-file": {
args: []string{"-var-file", "cool.tfvars"},
want: []FlagNameValue{
{Name: "-var-file", Value: "cool.tfvars"},
},
},
"ordering preserved": {
args: []string{
"-var", "foo=bar",
"-var-file", "cool.tfvars",
"-var", "boop=beep",
},
want: []FlagNameValue{
{Name: "-var", Value: "foo=bar"},
{Name: "-var-file", Value: "cool.tfvars"},
{Name: "-var", Value: "boop=beep"},
},
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got, diags := ParseInit(tc.args)
if len(diags) > 0 {
t.Fatalf("unexpected diags: %v", diags)
}
if vars := got.Vars.All(); !cmp.Equal(vars, tc.want) {
t.Fatalf("unexpected result\n%s", cmp.Diff(vars, tc.want))
}
if got, want := got.Vars.Empty(), len(tc.want) == 0; got != want {
t.Fatalf("expected Empty() to return %t, but was %t", want, got)
}
})
}
}
10 changes: 5 additions & 5 deletions internal/command/testdata/init-get/output.jsonlog
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{"@level":"info","@message":"Terraform 1.9.0-dev","@module":"terraform.ui","terraform":"1.9.0-dev","type":"version","ui":"1.2"}
{"@level":"info","@message":"Initializing the backend...","@module":"terraform.ui","type":"init_output"}
{"@level":"info","@message":"Initializing modules...","@module":"terraform.ui","type":"init_output"}
{"@level":"info","@message":"Initializing the backend...","@module":"terraform.ui","message_code": "initializing_backend_message","type":"init_output"}
{"@level":"info","@message":"Initializing modules...","@module":"terraform.ui","message_code": "initializing_modules_message","type":"init_output"}
{"@level":"info","@message":"- foo in foo","@module":"terraform.ui","type":"log"}
{"@level":"info","@message":"Initializing provider plugins...","@module":"terraform.ui","type":"init_output"}
{"@level":"info","@message":"Terraform has been successfully initialized!","@module":"terraform.ui","type":"init_output"}
{"@level":"info","@message":"You may now begin working with Terraform. Try running \"terraform plan\" to see\nany changes that are required for your infrastructure. All Terraform commands\nshould now work.\n\nIf you ever set or change modules or backend configuration for Terraform,\nrerun this command to reinitialize your working directory. If you forget, other\ncommands will detect it and remind you to do so if necessary.","@module":"terraform.ui","type":"init_output"}
{"@level":"info","@message":"Initializing provider plugins...","@module":"terraform.ui","message_code": "initializing_provider_plugin_message","type":"init_output"}
{"@level":"info","@message":"Terraform has been successfully initialized!","@module":"terraform.ui","message_code": "output_init_success_message","type":"init_output"}
{"@level":"info","@message":"You may now begin working with Terraform. Try running \"terraform plan\" to see\nany changes that are required for your infrastructure. All Terraform commands\nshould now work.\n\nIf you ever set or change modules or backend configuration for Terraform,\nrerun this command to reinitialize your working directory. If you forget, other\ncommands will detect it and remind you to do so if necessary.","@module":"terraform.ui","message_code": "output_init_success_cli_message","type":"init_output"}
27 changes: 15 additions & 12 deletions internal/command/views/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,11 @@ func TestNewInit_jsonViewOutput(t *testing.T) {
"ui": JSON_UI_VERSION,
},
{
"@level": "info",
"@message": "Initializing provider plugins...",
"@module": "terraform.ui",
"type": "init_output",
"@level": "info",
"@message": "Initializing provider plugins...",
"message_code": "initializing_provider_plugin_message",
"@module": "terraform.ui",
"type": "init_output",
},
}

Expand Down Expand Up @@ -175,10 +176,11 @@ func TestNewInit_jsonViewOutput(t *testing.T) {
"ui": JSON_UI_VERSION,
},
{
"@level": "info",
"@message": fmt.Sprintf("- Finding latest version of %s...", packageName),
"@module": "terraform.ui",
"type": "init_output",
"@level": "info",
"@message": fmt.Sprintf("- Finding latest version of %s...", packageName),
"@module": "terraform.ui",
"message_code": "finding_latest_version_message",
"type": "init_output",
},
}

Expand Down Expand Up @@ -208,10 +210,11 @@ func TestNewInit_jsonViewOutput(t *testing.T) {
"ui": JSON_UI_VERSION,
},
{
"@level": "info",
"@message": fmt.Sprintf("- Using previously-installed %s v%s", packageName, packageVersion),
"@module": "terraform.ui",
"type": "init_output",
"@level": "info",
"@message": fmt.Sprintf("- Using previously-installed %s v%s", packageName, packageVersion),
"@module": "terraform.ui",
"message_code": "provider_already_installed_message",
"type": "init_output",
},
}

Expand Down

0 comments on commit d660496

Please sign in to comment.