github.com/adevinta/lava@v0.7.2/cmd/lava/internal/run/runflag_test.go (about)

     1  // Copyright 2024 Adevinta
     2  
     3  package run
     4  
     5  import (
     6  	"bytes"
     7  	"os"
     8  	"testing"
     9  
    10  	types "github.com/adevinta/vulcan-types"
    11  	"github.com/google/go-cmp/cmp"
    12  
    13  	"github.com/adevinta/lava/internal/assettypes"
    14  )
    15  
    16  func TestTypeFlag_Set(t *testing.T) {
    17  	tests := []struct {
    18  		name       string
    19  		values     []string
    20  		want       typeFlag
    21  		wantNilErr []bool
    22  	}{
    23  		{
    24  			name:       "valid",
    25  			values:     []string{"Path"},
    26  			want:       typeFlag(assettypes.Path),
    27  			wantNilErr: []bool{true},
    28  		},
    29  		{
    30  			name:       "invalid",
    31  			values:     []string{"Invalid"},
    32  			want:       typeFlag(""),
    33  			wantNilErr: []bool{false},
    34  		},
    35  		{
    36  			name:       "empty",
    37  			values:     []string{""},
    38  			want:       typeFlag(""),
    39  			wantNilErr: []bool{false},
    40  		},
    41  		{
    42  			name:       "multiple",
    43  			values:     []string{"Path", "Hostname"},
    44  			want:       typeFlag(types.Hostname),
    45  			wantNilErr: []bool{true, true},
    46  		},
    47  		{
    48  			name:       "multiple valid invalid",
    49  			values:     []string{"Path", "Invalid", "Hostname", "Invalid"},
    50  			want:       typeFlag(types.Hostname),
    51  			wantNilErr: []bool{true, false, true, false},
    52  		},
    53  	}
    54  
    55  	for _, tt := range tests {
    56  		if len(tt.values) != len(tt.wantNilErr) {
    57  			panic("values and wantNilErr arrays must have the same length")
    58  		}
    59  
    60  		t.Run(tt.name, func(t *testing.T) {
    61  			var got typeFlag
    62  			for i, v := range tt.values {
    63  				if err := got.Set(v); (err == nil) != tt.wantNilErr[i] {
    64  					t.Errorf("unexpected error: %v", err)
    65  				}
    66  			}
    67  			if diff := cmp.Diff(tt.want, got); diff != "" {
    68  				t.Errorf("asset type mismatch (-want +got):\n%v", diff)
    69  			}
    70  		})
    71  	}
    72  }
    73  
    74  func TestVarFlag_Set(t *testing.T) {
    75  	tests := []struct {
    76  		name       string
    77  		values     []string
    78  		env        map[string]string
    79  		want       varFlag
    80  		wantNilErr []bool
    81  	}{
    82  		{
    83  			name:       "valid",
    84  			values:     []string{"name1=value1"},
    85  			want:       varFlag{"name1": "value1"},
    86  			wantNilErr: []bool{true},
    87  		},
    88  		{
    89  			name:       "multiple equals",
    90  			values:     []string{"name1=value1=value2"},
    91  			want:       varFlag{"name1": "value1=value2"},
    92  			wantNilErr: []bool{true},
    93  		},
    94  		{
    95  			name:       "empty",
    96  			values:     []string{""},
    97  			want:       varFlag{},
    98  			wantNilErr: []bool{false},
    99  		},
   100  		{
   101  			name:       "empty value",
   102  			values:     []string{"name1="},
   103  			want:       varFlag{"name1": ""},
   104  			wantNilErr: []bool{true},
   105  		},
   106  		{
   107  			name:       "empty name",
   108  			values:     []string{"=value1"},
   109  			want:       varFlag{},
   110  			wantNilErr: []bool{false},
   111  		},
   112  		{
   113  			name:   "known env",
   114  			values: []string{"name1"},
   115  			env: map[string]string{
   116  				"name1": "env1",
   117  			},
   118  			want:       varFlag{"name1": "env1"},
   119  			wantNilErr: []bool{true},
   120  		},
   121  		{
   122  			name:       "unknown env",
   123  			values:     []string{"unknown"},
   124  			want:       varFlag{"unknown": ""},
   125  			wantNilErr: []bool{true},
   126  		},
   127  		{
   128  			name:   "same env name",
   129  			values: []string{"name1=value1", "name2="},
   130  			env: map[string]string{
   131  				"name1": "env1",
   132  				"name2": "env2",
   133  			},
   134  			want:       varFlag{"name1": "value1", "name2": ""},
   135  			wantNilErr: []bool{true, true},
   136  		},
   137  		{
   138  			name:       "multiple",
   139  			values:     []string{"name1=", "name2=value2"},
   140  			want:       varFlag{"name1": "", "name2": "value2"},
   141  			wantNilErr: []bool{true, true},
   142  		},
   143  		{
   144  			name:   "multiple valid invalid env",
   145  			values: []string{"name1=value1", "=value2", "name3=", "=value4", "name5"},
   146  			env: map[string]string{
   147  				"name1": "env1",
   148  				"name3": "env3",
   149  				"name5": "env5",
   150  			},
   151  			want:       varFlag{"name1": "value1", "name3": "", "name5": "env5"},
   152  			wantNilErr: []bool{true, false, true, false, true},
   153  		},
   154  	}
   155  
   156  	for _, tt := range tests {
   157  		if len(tt.values) != len(tt.wantNilErr) {
   158  			panic("values and wantNilErr arrays must have the same length")
   159  		}
   160  
   161  		t.Run(tt.name, func(t *testing.T) {
   162  			for k, v := range tt.env {
   163  				t.Setenv(k, v)
   164  			}
   165  
   166  			var got varFlag
   167  			for i, v := range tt.values {
   168  				if err := got.Set(v); (err == nil) != tt.wantNilErr[i] {
   169  					t.Errorf("unexpected error: %v", err)
   170  				}
   171  			}
   172  			if diff := cmp.Diff(tt.want, got); diff != "" {
   173  				t.Errorf("envvars mismatch (-want +got):\n%v", diff)
   174  			}
   175  		})
   176  	}
   177  }
   178  
   179  func TestUserFlag_Set(t *testing.T) {
   180  	tests := []struct {
   181  		name       string
   182  		values     []string
   183  		stdin      string
   184  		want       userFlag
   185  		wantNilErr []bool
   186  	}{
   187  		{
   188  			name:   "valid",
   189  			values: []string{"user:pass"},
   190  			want: userFlag{
   191  				Username: "user",
   192  				Password: "pass",
   193  			},
   194  			wantNilErr: []bool{true},
   195  		},
   196  		{
   197  			name:       "empty",
   198  			values:     []string{""},
   199  			want:       userFlag{},
   200  			wantNilErr: []bool{false},
   201  		},
   202  		{
   203  			name: "multiple",
   204  			values: []string{
   205  				"user1:pass1",
   206  				"user2:pass2",
   207  			},
   208  			want: userFlag{
   209  				Username: "user2",
   210  				Password: "pass2",
   211  			},
   212  			wantNilErr: []bool{true, true},
   213  		},
   214  		{
   215  			name: "multiple valid empty",
   216  			values: []string{
   217  				"user1:pass1",
   218  				"",
   219  				"user3:pass3",
   220  				"",
   221  			},
   222  			want: userFlag{
   223  				Username: "user3",
   224  				Password: "pass3",
   225  			},
   226  			wantNilErr: []bool{true, false, true, false},
   227  		},
   228  		{
   229  			name: "empty password",
   230  			values: []string{
   231  				"user1:",
   232  			},
   233  			want: userFlag{
   234  				Username: "user1",
   235  				Password: "",
   236  			},
   237  			wantNilErr: []bool{true},
   238  		},
   239  		{
   240  			name: "stdin",
   241  			values: []string{
   242  				"user1",
   243  			},
   244  			stdin: "pass1",
   245  			want: userFlag{
   246  				Username: "user1",
   247  				Password: "pass1",
   248  			},
   249  			wantNilErr: []bool{true},
   250  		},
   251  		{
   252  			name: "multiple stdin",
   253  			values: []string{
   254  				"user1",
   255  				"user2",
   256  			},
   257  			stdin: "pass1",
   258  			want: userFlag{
   259  				Username: "user2",
   260  				Password: "",
   261  			},
   262  			wantNilErr: []bool{true, true},
   263  		},
   264  	}
   265  
   266  	for _, tt := range tests {
   267  		if len(tt.values) != len(tt.wantNilErr) {
   268  			panic("values and wantNilErr arrays must have the same length")
   269  		}
   270  
   271  		t.Run(tt.name, func(t *testing.T) {
   272  			if tt.stdin != "" {
   273  				osStdin = bytes.NewBufferString(tt.stdin)
   274  				defer func() { osStdin = os.Stdin }()
   275  			}
   276  
   277  			var got userFlag
   278  			for i, v := range tt.values {
   279  				if err := got.Set(v); (err == nil) != tt.wantNilErr[i] {
   280  					t.Errorf("unexpected error: %v", err)
   281  				}
   282  			}
   283  			if diff := cmp.Diff(tt.want, got); diff != "" {
   284  				t.Errorf("userinfo mismatch (-want +got):\n%v", diff)
   285  			}
   286  		})
   287  	}
   288  }