1// Copyright 2018 The Bazel Authors. All rights reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// Unit test for the flagfile module. 16package flagfile 17 18import ( 19 "bufio" 20 "reflect" 21 "strings" 22 "testing" 23) 24 25func TestParseFlags(t *testing.T) { 26 tcs := []struct { 27 name string 28 in string 29 want map[string]string 30 wantErr string 31 }{ 32 { 33 name: "SingleLineFlagDefinitions", 34 in: ` 35--a=b 36'--1=2' 37-foo=bar 38--enable 39"--baz=qux=quux" 40`, 41 want: map[string]string{ 42 "a": "b", 43 "1": "2", 44 "foo": "bar", 45 "enable": "", 46 "baz": "qux=quux", 47 }, 48 }, 49 { 50 name: "MultiLineFlagDefinitions", 51 in: ` 52--a 53b 54--1 552 56-foo 57bar 58--enable 59--baz 60qux=quux 61`, 62 want: map[string]string{ 63 "a": "b", 64 "1": "2", 65 "foo": "bar", 66 "enable": "", 67 "baz": "qux=quux", 68 }, 69 }, 70 { 71 name: "MixedMultiSingleLineFlagDefinitions", 72 in: ` 73--a 74b 75"-1=2" 76-foo 77bar 78--enable 79'--baz=--qux=quux' 80`, 81 want: map[string]string{ 82 "a": "b", 83 "1": "2", 84 "foo": "bar", 85 "enable": "", 86 "baz": "--qux=quux", 87 }, 88 }, 89 { 90 name: "NoFlags", 91 in: "", 92 want: map[string]string{}, 93 }, 94 { 95 name: "MalformedFlagMissingDash", 96 in: "a=b", 97 wantErr: "expected flag start definition ('-' or '--')", 98 }, 99 { 100 name: "MalformedFlagTooManyDashes", 101 in: "---a=b", 102 wantErr: "expected flag start definition ('-' or '--')", 103 }, 104 { 105 name: "UnbalancedQuotationsAroundFlag", 106 in: "'--a=b", 107 wantErr: "found unbalanced quotation marks around flag entry", 108 }, 109 } 110 for _, tc := range tcs { 111 t.Run(tc.name, func(t *testing.T) { 112 got, err := parseFlags(bufio.NewReader(strings.NewReader(tc.in))) 113 if err != nil { 114 if tc.wantErr != "" { 115 if !strings.Contains(err.Error(), tc.wantErr) { 116 t.Errorf("error got error: %s wanted to contain: %s", err, tc.wantErr) 117 } 118 return 119 } 120 t.Errorf("got unexpected error: %s", err) 121 return 122 } 123 if eq := reflect.DeepEqual(got, tc.want); !eq { 124 t.Errorf("error got: %v wanted: %v", got, tc.want) 125 } 126 }) 127 } 128} 129