mirror of
https://github.com/opentffoundation/opentf.git
synced 2025-12-25 01:00:16 -05:00
Add exclude flag support (#1900)
Signed-off-by: RLRabinowitz <rlrabinowitz2@gmail.com>
This commit is contained in:
@@ -9,6 +9,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/opentofu/opentofu/internal/tfdiags"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/opentofu/opentofu/internal/addrs"
|
||||
)
|
||||
@@ -119,13 +121,16 @@ func TestParseRefresh_targets(t *testing.T) {
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, diags := ParseRefresh(tc.args)
|
||||
if len(diags) > 0 {
|
||||
if tc.wantErr == "" {
|
||||
t.Fatalf("unexpected diags: %v", diags)
|
||||
if tc.wantErr == "" && len(diags) > 0 {
|
||||
t.Fatalf("unexpected diags: %v", diags)
|
||||
} else if tc.wantErr != "" {
|
||||
if len(diags) == 0 {
|
||||
t.Fatalf("expected diags but got none")
|
||||
} else if got := diags.Err().Error(); !strings.Contains(got, tc.wantErr) {
|
||||
t.Fatalf("wrong diags\n got: %s\nwant: %s", got, tc.wantErr)
|
||||
}
|
||||
}
|
||||
|
||||
if !cmp.Equal(got.Operation.Targets, tc.want) {
|
||||
t.Fatalf("unexpected result\n%s", cmp.Diff(got.Operation.Targets, tc.want))
|
||||
}
|
||||
@@ -133,6 +138,80 @@ func TestParseRefresh_targets(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRefresh_excludes(t *testing.T) {
|
||||
foobarbaz, _ := addrs.ParseTargetStr("foo_bar.baz")
|
||||
boop, _ := addrs.ParseTargetStr("module.boop")
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
want []addrs.Targetable
|
||||
wantErr string
|
||||
}{
|
||||
"no excludes by default": {
|
||||
args: nil,
|
||||
want: nil,
|
||||
},
|
||||
"one exclude": {
|
||||
args: []string{"-exclude=foo_bar.baz"},
|
||||
want: []addrs.Targetable{foobarbaz.Subject},
|
||||
},
|
||||
"two excludes": {
|
||||
args: []string{"-exclude=foo_bar.baz", "-exclude", "module.boop"},
|
||||
want: []addrs.Targetable{foobarbaz.Subject, boop.Subject},
|
||||
},
|
||||
"invalid traversal": {
|
||||
args: []string{"-exclude=foo."},
|
||||
want: nil,
|
||||
wantErr: "Dot must be followed by attribute name",
|
||||
},
|
||||
"invalid target": {
|
||||
args: []string{"-exclude=data[0].foo"},
|
||||
want: nil,
|
||||
wantErr: "A data source name is required",
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, diags := ParseRefresh(tc.args)
|
||||
if tc.wantErr == "" && len(diags) > 0 {
|
||||
t.Fatalf("unexpected diags: %v", diags)
|
||||
} else if tc.wantErr != "" {
|
||||
if len(diags) == 0 {
|
||||
t.Fatalf("expected diags but got none")
|
||||
} else if got := diags.Err().Error(); !strings.Contains(got, tc.wantErr) {
|
||||
t.Fatalf("wrong diags\n got: %s\nwant: %s", got, tc.wantErr)
|
||||
}
|
||||
}
|
||||
if !cmp.Equal(got.Operation.Excludes, tc.want) {
|
||||
t.Fatalf("unexpected result\n%s", cmp.Diff(got.Operation.Excludes, tc.want))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRefresh_excludeAndTarget(t *testing.T) {
|
||||
got, gotDiags := ParseRefresh([]string{"-exclude=foo_bar.baz", "-target=foo_bar.bar"})
|
||||
if len(gotDiags) == 0 {
|
||||
t.Fatalf("expected error, but there was none")
|
||||
}
|
||||
|
||||
wantDiags := tfdiags.Diagnostics{
|
||||
tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Invalid combination of arguments",
|
||||
"-target and -exclude flags cannot be used together. Please remove one of the flags",
|
||||
),
|
||||
}
|
||||
if diff := cmp.Diff(wantDiags.ForRPC(), gotDiags.ForRPC()); diff != "" {
|
||||
t.Errorf("wrong diagnostics\n%s", diff)
|
||||
}
|
||||
if len(got.Operation.Targets) > 0 {
|
||||
t.Errorf("Did not expect operation to parse targets, but it parsed %d targets", len(got.Operation.Targets))
|
||||
}
|
||||
if len(got.Operation.Excludes) > 0 {
|
||||
t.Errorf("Did not expect operation to parse excludes, but it parsed %d targets", len(got.Operation.Excludes))
|
||||
}
|
||||
}
|
||||
func TestParseRefresh_vars(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
|
||||
Reference in New Issue
Block a user