mirror of
https://github.com/hashicorp/terraform.git
synced 2026-03-21 18:10:30 -04:00
This commit moves some code around to fix configuration loading during the (legacy) import command. And add vars to the show command.
109 lines
2.2 KiB
Go
109 lines
2.2 KiB
Go
// Copyright IBM Corp. 2014, 2026
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package arguments
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
|
)
|
|
|
|
func TestParseShow_valid(t *testing.T) {
|
|
testCases := map[string]struct {
|
|
args []string
|
|
want *Show
|
|
}{
|
|
"defaults": {
|
|
nil,
|
|
&Show{
|
|
Path: "",
|
|
ViewType: ViewHuman,
|
|
Vars: &Vars{},
|
|
},
|
|
},
|
|
"json": {
|
|
[]string{"-json"},
|
|
&Show{
|
|
Path: "",
|
|
ViewType: ViewJSON,
|
|
Vars: &Vars{},
|
|
},
|
|
},
|
|
"path": {
|
|
[]string{"-json", "foo"},
|
|
&Show{
|
|
Path: "foo",
|
|
ViewType: ViewJSON,
|
|
Vars: &Vars{},
|
|
},
|
|
},
|
|
}
|
|
|
|
cmpOpts := cmpopts.IgnoreUnexported(Operation{}, Vars{}, State{})
|
|
|
|
for name, tc := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
got, diags := ParseShow(tc.args)
|
|
if len(diags) > 0 && diags.HasErrors() {
|
|
t.Fatalf("unexpected diags: %v", diags)
|
|
}
|
|
if diff := cmp.Diff(tc.want, got, cmpOpts); diff != "" {
|
|
t.Errorf("unexpected result\n%s", diff)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseShow_invalid(t *testing.T) {
|
|
testCases := map[string]struct {
|
|
args []string
|
|
want *Show
|
|
wantDiags tfdiags.Diagnostics
|
|
}{
|
|
"unknown flag": {
|
|
[]string{"-boop"},
|
|
&Show{
|
|
Path: "",
|
|
ViewType: ViewHuman,
|
|
Vars: &Vars{},
|
|
},
|
|
tfdiags.Diagnostics{
|
|
tfdiags.Sourceless(
|
|
tfdiags.Error,
|
|
"Failed to parse command-line flags",
|
|
"flag provided but not defined: -boop",
|
|
),
|
|
},
|
|
},
|
|
"too many arguments": {
|
|
[]string{"-json", "bar", "baz"},
|
|
&Show{
|
|
Path: "bar",
|
|
ViewType: ViewJSON,
|
|
Vars: &Vars{},
|
|
},
|
|
tfdiags.Diagnostics{
|
|
tfdiags.Sourceless(
|
|
tfdiags.Error,
|
|
"Too many command line arguments",
|
|
"Expected at most one positional argument.",
|
|
),
|
|
},
|
|
},
|
|
}
|
|
|
|
cmpOpts := cmpopts.IgnoreUnexported(Operation{}, Vars{}, State{})
|
|
|
|
for name, tc := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
got, gotDiags := ParseShow(tc.args)
|
|
if diff := cmp.Diff(tc.want, got, cmpOpts); diff != "" {
|
|
t.Errorf("unexpected result\n%s", diff)
|
|
}
|
|
tfdiags.AssertDiagnosticsMatch(t, gotDiags, tc.wantDiags)
|
|
})
|
|
}
|
|
}
|