-
Notifications
You must be signed in to change notification settings - Fork 384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow flag or file based configuration #620
Changes from 1 commit
4712f74
ddcc786
4c06b2e
a6656ac
cc9922c
4fd46e5
d2cc80d
54850a1
661daaf
c49bc08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2017 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"flag" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestParseFlags(t *testing.T) { | ||
var a, b string | ||
flag.StringVar(&a, "a", "", "") | ||
flag.StringVar(&b, "b", "", "") | ||
|
||
flag.CommandLine.Init(os.Args[0], flag.ContinueOnError) | ||
|
||
tests := []struct { | ||
contents string | ||
env map[string]string | ||
cliArgs []string | ||
expectedErr string | ||
expectedA string | ||
expectedB string | ||
}{ | ||
{ | ||
contents: "-a one -b two", | ||
expectedA: "one", | ||
expectedB: "two", | ||
}, | ||
{ | ||
contents: "-a one\n-b two", | ||
expectedA: "one", | ||
expectedB: "two", | ||
}, | ||
{ | ||
contents: "-a one \\\n-b two", | ||
expectedA: "one", | ||
expectedB: "two", | ||
}, | ||
{ | ||
contents: "-a one", | ||
cliArgs: []string{"-b", "two"}, | ||
expectedA: "one", | ||
expectedB: "two", | ||
}, | ||
{ | ||
contents: "-a one\n-b two", | ||
cliArgs: []string{"-b", "three"}, | ||
expectedA: "one", | ||
expectedB: "three", | ||
}, | ||
{ | ||
contents: "-a one\n-b $TEST_VAR", | ||
env: map[string]string{"TEST_VAR": "from env"}, | ||
expectedA: "one", | ||
expectedB: "from env", | ||
}, | ||
{ | ||
contents: "-a one -b two -c three", | ||
expectedErr: "flag provided but not defined: -c", | ||
}, | ||
} | ||
|
||
initalArgs := os.Args[:] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: s/initalArgs/initialArgs/ |
||
for _, tc := range tests { | ||
a, b = "", "" | ||
os.Args = initalArgs[:] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines 80-83 could be condensed into |
||
if len(tc.cliArgs) > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for this check - |
||
os.Args = append(os.Args, tc.cliArgs...) | ||
} | ||
for k, v := range tc.env { | ||
if err := os.Setenv(k, v); err != nil { | ||
t.Errorf("os.SetEnv failed: %s", err) | ||
} | ||
} | ||
err := parseFlags(tc.contents) | ||
if err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could combine the above two lines. |
||
if err.Error() == tc.expectedErr { | ||
continue | ||
} | ||
t.Errorf("parseFlags failed: wanted: %q, got: %q", tc.expectedErr, err) | ||
continue | ||
} | ||
if tc.expectedA != a { | ||
t.Errorf("flag 'a' not properly set: wanted: %q, got %q", tc.expectedA, a) | ||
} | ||
if tc.expectedB != b { | ||
t.Errorf("flag 'b' not properly set: wanted: %q, got %q", tc.expectedB, b) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a
name
field and include it in all of thet.Errorf()
calls so that it's easy to identify which test case failed.