github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/arguments/test_test.go (about)

     1  package arguments
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/apparentlymart/go-shquot/shquot"
     7  	"github.com/google/go-cmp/cmp"
     8  	"github.com/hashicorp/terraform/internal/tfdiags"
     9  )
    10  
    11  func TestParseTest(t *testing.T) {
    12  	tests := []struct {
    13  		Input     []string
    14  		Want      Test
    15  		WantError string
    16  	}{
    17  		{
    18  			nil,
    19  			Test{
    20  				Output: TestOutput{
    21  					JUnitXMLFile: "",
    22  				},
    23  			},
    24  			``,
    25  		},
    26  		{
    27  			[]string{"-invalid"},
    28  			Test{
    29  				Output: TestOutput{
    30  					JUnitXMLFile: "",
    31  				},
    32  			},
    33  			`flag provided but not defined: -invalid`,
    34  		},
    35  		{
    36  			[]string{"-junit-xml=result.xml"},
    37  			Test{
    38  				Output: TestOutput{
    39  					JUnitXMLFile: "result.xml",
    40  				},
    41  			},
    42  			``,
    43  		},
    44  		{
    45  			[]string{"baz"},
    46  			Test{
    47  				Output: TestOutput{
    48  					JUnitXMLFile: "",
    49  				},
    50  			},
    51  			`Invalid command arguments`,
    52  		},
    53  	}
    54  
    55  	baseCmdline := []string{"terraform", "test"}
    56  	for _, test := range tests {
    57  		name := shquot.POSIXShell(append(baseCmdline, test.Input...))
    58  		t.Run(name, func(t *testing.T) {
    59  			t.Log(name)
    60  			got, diags := ParseTest(test.Input)
    61  
    62  			if test.WantError != "" {
    63  				if len(diags) != 1 {
    64  					t.Fatalf("got %d diagnostics; want exactly 1\n%s", len(diags), diags.Err().Error())
    65  				}
    66  				if diags[0].Severity() != tfdiags.Error {
    67  					t.Fatalf("got a warning; want an error\n%s", diags.Err().Error())
    68  				}
    69  				if desc := diags[0].Description(); desc.Summary != test.WantError {
    70  					t.Fatalf("wrong error\ngot:  %s\nwant: %s", desc.Summary, test.WantError)
    71  				}
    72  			} else {
    73  				if len(diags) != 0 {
    74  					t.Fatalf("got %d diagnostics; want none\n%s", len(diags), diags.Err().Error())
    75  				}
    76  			}
    77  
    78  			if diff := cmp.Diff(test.Want, got); diff != "" {
    79  				t.Errorf("wrong result\n%s", diff)
    80  			}
    81  		})
    82  	}
    83  }