github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/arguments/test_test.go (about)

     1  package arguments
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/davecgh/go-spew/spew"
     8  	"github.com/google/go-cmp/cmp"
     9  	"github.com/google/go-cmp/cmp/cmpopts"
    10  
    11  	"github.com/terramate-io/tf/tfdiags"
    12  )
    13  
    14  func TestParseTest_Vars(t *testing.T) {
    15  	tcs := map[string]struct {
    16  		args []string
    17  		want []FlagNameValue
    18  	}{
    19  		"no var flags by default": {
    20  			args: nil,
    21  			want: nil,
    22  		},
    23  		"one var": {
    24  			args: []string{"-var", "foo=bar"},
    25  			want: []FlagNameValue{
    26  				{Name: "-var", Value: "foo=bar"},
    27  			},
    28  		},
    29  		"one var-file": {
    30  			args: []string{"-var-file", "cool.tfvars"},
    31  			want: []FlagNameValue{
    32  				{Name: "-var-file", Value: "cool.tfvars"},
    33  			},
    34  		},
    35  		"ordering preserved": {
    36  			args: []string{
    37  				"-var", "foo=bar",
    38  				"-var-file", "cool.tfvars",
    39  				"-var", "boop=beep",
    40  			},
    41  			want: []FlagNameValue{
    42  				{Name: "-var", Value: "foo=bar"},
    43  				{Name: "-var-file", Value: "cool.tfvars"},
    44  				{Name: "-var", Value: "boop=beep"},
    45  			},
    46  		},
    47  	}
    48  
    49  	for name, tc := range tcs {
    50  		t.Run(name, func(t *testing.T) {
    51  			got, diags := ParseTest(tc.args)
    52  			if len(diags) > 0 {
    53  				t.Fatalf("unexpected diags: %v", diags)
    54  			}
    55  			if vars := got.Vars.All(); !cmp.Equal(vars, tc.want) {
    56  				t.Fatalf("unexpected result\n%s", cmp.Diff(vars, tc.want))
    57  			}
    58  			if got, want := got.Vars.Empty(), len(tc.want) == 0; got != want {
    59  				t.Fatalf("expected Empty() to return %t, but was %t", want, got)
    60  			}
    61  		})
    62  	}
    63  }
    64  
    65  func TestParseTest(t *testing.T) {
    66  	tcs := map[string]struct {
    67  		args      []string
    68  		want      *Test
    69  		wantDiags tfdiags.Diagnostics
    70  	}{
    71  		"defaults": {
    72  			args: nil,
    73  			want: &Test{
    74  				Filter:        nil,
    75  				TestDirectory: "tests",
    76  				ViewType:      ViewHuman,
    77  				Vars:          &Vars{},
    78  			},
    79  			wantDiags: nil,
    80  		},
    81  		"with-filters": {
    82  			args: []string{"-filter=one.tftest.hcl", "-filter=two.tftest.hcl"},
    83  			want: &Test{
    84  				Filter:        []string{"one.tftest.hcl", "two.tftest.hcl"},
    85  				TestDirectory: "tests",
    86  				ViewType:      ViewHuman,
    87  				Vars:          &Vars{},
    88  			},
    89  			wantDiags: nil,
    90  		},
    91  		"json": {
    92  			args: []string{"-json"},
    93  			want: &Test{
    94  				Filter:        nil,
    95  				TestDirectory: "tests",
    96  				ViewType:      ViewJSON,
    97  				Vars:          &Vars{},
    98  			},
    99  			wantDiags: nil,
   100  		},
   101  		"test-directory": {
   102  			args: []string{"-test-directory=other"},
   103  			want: &Test{
   104  				Filter:        nil,
   105  				TestDirectory: "other",
   106  				ViewType:      ViewHuman,
   107  				Vars:          &Vars{},
   108  			},
   109  			wantDiags: nil,
   110  		},
   111  		"verbose": {
   112  			args: []string{"-verbose"},
   113  			want: &Test{
   114  				Filter:        nil,
   115  				TestDirectory: "tests",
   116  				ViewType:      ViewHuman,
   117  				Verbose:       true,
   118  				Vars:          &Vars{},
   119  			},
   120  		},
   121  		"unknown flag": {
   122  			args: []string{"-boop"},
   123  			want: &Test{
   124  				Filter:        nil,
   125  				TestDirectory: "tests",
   126  				ViewType:      ViewHuman,
   127  				Vars:          &Vars{},
   128  			},
   129  			wantDiags: tfdiags.Diagnostics{
   130  				tfdiags.Sourceless(
   131  					tfdiags.Error,
   132  					"Failed to parse command-line flags",
   133  					"flag provided but not defined: -boop",
   134  				),
   135  			},
   136  		},
   137  	}
   138  
   139  	cmpOpts := cmpopts.IgnoreUnexported(Operation{}, Vars{}, State{})
   140  
   141  	for name, tc := range tcs {
   142  		t.Run(name, func(t *testing.T) {
   143  			got, diags := ParseTest(tc.args)
   144  
   145  			if diff := cmp.Diff(tc.want, got, cmpOpts); len(diff) > 0 {
   146  				t.Errorf("diff:\n%s", diff)
   147  			}
   148  
   149  			if !reflect.DeepEqual(diags, tc.wantDiags) {
   150  				t.Errorf("wrong result\ngot: %s\nwant: %s", spew.Sdump(diags), spew.Sdump(tc.wantDiags))
   151  			}
   152  		})
   153  	}
   154  }