github.com/bazelbuild/remote-apis-sdks@v0.0.0-20240425170053-8a36686a6350/go/pkg/moreflag/moreflag_test.go (about)

     1  package moreflag
     2  
     3  import (
     4  	"flag"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/google/go-cmp/cmp"
     9  )
    10  
    11  func TestParseUnset(t *testing.T) {
    12  	cleanup := setCommandLine(t, []string{"cmd"})
    13  	defer cleanup()
    14  	f := flag.String("value", "", "Some value")
    15  
    16  	Parse()
    17  	if *f != "" {
    18  		t.Errorf("Flag has wrong value, want '', got %q", *f)
    19  	}
    20  }
    21  
    22  func TestParseSet(t *testing.T) {
    23  	cleanup := setCommandLine(t, []string{"cmd"})
    24  	defer cleanup()
    25  	f := flag.String("value", "", "Some value")
    26  	os.Setenv("FLAG_value", "test")
    27  	defer os.Setenv("FLAG_value", "")
    28  	Parse()
    29  	if *f != "test" {
    30  		t.Errorf("Flag has wrong value, want 'test', got %q", *f)
    31  	}
    32  }
    33  
    34  func TestParseCommandLineWins(t *testing.T) {
    35  	cleanup := setCommandLine(t, []string{"cmd", "--value=cmd"})
    36  	defer cleanup()
    37  	f := flag.String("value", "", "Some value")
    38  	os.Setenv("FLAG_value", "test")
    39  	defer os.Setenv("FLAG_value", "")
    40  	Parse()
    41  	if *f != "cmd" {
    42  		t.Errorf("Flag has wrong value, want 'cmd', got %q", *f)
    43  	}
    44  }
    45  
    46  func setCommandLine(t *testing.T, args []string) func() {
    47  	t.Helper()
    48  	oldArgs := os.Args
    49  	os.Args = args
    50  	flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
    51  	return func() { os.Args = oldArgs }
    52  }
    53  
    54  func TestMapValueSet(t *testing.T) {
    55  	tests := []struct {
    56  		name    string
    57  		str     string
    58  		wantMap map[string]string
    59  		wantStr string
    60  	}{
    61  		{
    62  			name:    "ok - single pair",
    63  			str:     "type=compile",
    64  			wantMap: map[string]string{"type": "compile"},
    65  			wantStr: "type=compile",
    66  		},
    67  		{
    68  			name:    "ok - multiple pairs",
    69  			str:     "type=compile,lang=cpp",
    70  			wantMap: map[string]string{"type": "compile", "lang": "cpp"},
    71  			wantStr: "lang=cpp,type=compile",
    72  		},
    73  		{
    74  			name:    "ok - extra comma",
    75  			str:     "type=compile,",
    76  			wantMap: map[string]string{"type": "compile"},
    77  			wantStr: "type=compile",
    78  		},
    79  		{
    80  			name:    "empty",
    81  			str:     "",
    82  			wantMap: map[string]string{},
    83  		},
    84  	}
    85  	for _, test := range tests {
    86  		t.Run(test.name, func(t *testing.T) {
    87  			var m map[string]string
    88  			mv := (*StringMapValue)(&m)
    89  			if err := mv.Set(test.str); err != nil {
    90  				t.Errorf("StringMapValue.Set(%v) returned error: %v", test.str, err)
    91  			}
    92  			if diff := cmp.Diff(test.wantMap, (map[string]string)(*mv)); diff != "" {
    93  				t.Errorf("StringMapValue.Set(%v) produced diff in map, (-want +got): %s", test.str, diff)
    94  			}
    95  			got := mv.String()
    96  			if test.wantStr != got {
    97  				t.Errorf("StringMapValue.String() produced diff. Want %s, got %s", test.wantStr, got)
    98  			}
    99  		})
   100  	}
   101  }
   102  
   103  func TestMapValueMultipleSet(t *testing.T) {
   104  	var m map[string]string
   105  	mv := (*StringMapValue)(&m)
   106  	pair1 := "key1=value1"
   107  	if err := mv.Set(pair1); err != nil {
   108  		t.Errorf("StringMapValue.Set(%v) returned error: %v", pair1, err)
   109  	}
   110  	if diff := cmp.Diff(map[string]string{"key1": "value1"}, (map[string]string)(*mv)); diff != "" {
   111  		t.Errorf("StringMapValue.Set(%v) produced diff in map, (-want +got): %s", pair1, diff)
   112  	}
   113  	pair2 := "key2=value2"
   114  	if err := mv.Set(pair2); err != nil {
   115  		t.Errorf("StringMapValue.Set(%v) returned error: %v", pair2, err)
   116  	}
   117  	if diff := cmp.Diff(map[string]string{"key2": "value2"}, (map[string]string)(*mv)); diff != "" {
   118  		t.Errorf("StringMapValue.Set(%v) produced diff in map, (-want +got): %s", pair2, diff)
   119  	}
   120  }
   121  
   122  func TestMapValueMultipleSetDuplicate(t *testing.T) {
   123  	var m map[string]string
   124  	mv := (*StringMapValue)(&m)
   125  	pair1 := "key1=value1"
   126  	if err := mv.Set(pair1); err != nil {
   127  		t.Errorf("StringMapValue.Set(%v) returned error: %v", pair1, err)
   128  	}
   129  	if diff := cmp.Diff(map[string]string{"key1": "value1"}, (map[string]string)(*mv)); diff != "" {
   130  		t.Errorf("StringMapValue.Set(%v) produced diff in map, (-want +got): %s", pair1, diff)
   131  	}
   132  	pair2 := "key1=value2"
   133  	if err := mv.Set(pair2); err != nil {
   134  		t.Errorf("StringMapValue.Set(%v) returned error: %v", pair2, err)
   135  	}
   136  	if diff := cmp.Diff(map[string]string{"key1": "value2"}, (map[string]string)(*mv)); diff != "" {
   137  		t.Errorf("StringMapValue.Set(%v) produced diff in map, (-want +got): %s", pair2, diff)
   138  	}
   139  }
   140  
   141  func TestMapValueSetErrors(t *testing.T) {
   142  	tests := []struct {
   143  		name string
   144  		str  string
   145  	}{
   146  		{
   147  			name: "bad format",
   148  			str:  "type=compile,langcpp",
   149  		},
   150  		{
   151  			name: "no key",
   152  			str:  "=val",
   153  		},
   154  		{
   155  			name: "multiple equalities",
   156  			str:  "type=a=b",
   157  		},
   158  		{
   159  			name: "duplicate keys",
   160  			str:  "type=compile,type=link",
   161  		},
   162  	}
   163  	for _, test := range tests {
   164  		t.Run(test.name, func(t *testing.T) {
   165  			var m map[string]string
   166  			mv := (*StringMapValue)(&m)
   167  			if err := mv.Set(test.str); err == nil {
   168  				t.Errorf("StringMapValue.Set(%v) = nil, want error", test.str)
   169  			}
   170  		})
   171  	}
   172  }
   173  
   174  func TestListValueSet(t *testing.T) {
   175  	tests := []struct {
   176  		name     string
   177  		str      string
   178  		wantList []string
   179  		wantStr  string
   180  	}{
   181  		{
   182  			name:     "ok - single val",
   183  			str:      "foo",
   184  			wantList: []string{"foo"},
   185  			wantStr:  "foo",
   186  		},
   187  		{
   188  			name:     "ok - multiple vals",
   189  			str:      "foo,bar",
   190  			wantList: []string{"foo", "bar"},
   191  			wantStr:  "foo,bar",
   192  		},
   193  		{
   194  			name:     "ok - extra comma",
   195  			str:      "foo,",
   196  			wantList: []string{"foo"},
   197  			wantStr:  "foo",
   198  		},
   199  		{
   200  			name:     "ok - double comma",
   201  			str:      "foo,,bar",
   202  			wantList: []string{"foo", "bar"},
   203  			wantStr:  "foo,bar",
   204  		},
   205  		{
   206  			name:     "empty",
   207  			str:      "",
   208  			wantList: []string{},
   209  		},
   210  	}
   211  	for _, test := range tests {
   212  		t.Run(test.name, func(t *testing.T) {
   213  			var l []string
   214  			lv := (*StringListValue)(&l)
   215  			if err := lv.Set(test.str); err != nil {
   216  				t.Errorf("StringListValue.Set(%v) returned error: %v", test.str, err)
   217  			}
   218  			if diff := cmp.Diff(test.wantList, ([]string)(*lv)); diff != "" {
   219  				t.Errorf("StringListValue.Set(%v) produced diff in map, (-want +got): %s", test.str, diff)
   220  			}
   221  			got := lv.String()
   222  			if test.wantStr != got {
   223  				t.Errorf("StringListValue.String() produced diff. Want %s, got %s", test.wantStr, got)
   224  			}
   225  		})
   226  	}
   227  }