1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
package cli
import (
"flag"
"os"
"testing"
)
var cfsslFlagSet = flag.NewFlagSet("cfssl", flag.ExitOnError)
// The testing style from this package is borrowed from the Go flag
// library's methodology for testing this. We set flag.Usage to nil,
// then replace it with a closure to ensure the usage function was
// called appropriately.
// 'cfssl -help' should be supported.
func TestHelp(t *testing.T) {
called := false
ResetForTesting(func() { called = true })
os.Args = []string{"cfssl", "-help"}
Start(nil)
if !called {
t.Fatal("flag -help is not recognized correctly.")
}
}
// 'cfssl -badflag' should trigger parse error and usage invocation.
func TestUnknownFlag(t *testing.T) {
called := false
os.Args = []string{"cfssl", "-badflag"}
ResetForTesting(func() { called = true })
Start(nil)
if !called {
t.Fatal("Bad flag is not caught.")
}
}
// 'cfssl badcommand' should trigger parse error and usage invocation.
func TestBadCommand(t *testing.T) {
called := false
ResetForTesting(func() { called = true })
os.Args = []string{"cfssl", "badcommand"}
Start(nil)
if !called {
t.Fatal("Bad command is not caught.")
}
}
func TestCommandHelp(t *testing.T) {
called := false
ResetCFSSLFlagSetForTesting(func() { called = true })
args := []string{"-help"}
cfsslFlagSet.Parse(args)
if !called {
t.Fatal("sub-command -help is not recognized.")
}
}
func TestCommandBadFlag(t *testing.T) {
called := false
ResetCFSSLFlagSetForTesting(func() { called = true })
args := []string{"-help", "-badflag"}
cfsslFlagSet.Parse(args)
if !called {
t.Fatal("bad flag for sub-command is not caught.")
}
}
// Additional routines derived from flag unit testing
// ResetForTesting clears all flag state and sets the usage function as directed.
// After calling ResetForTesting, parse errors in flag handling will not
// exit the program.
func ResetForTesting(usage func()) {
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
flag.Usage = usage
}
// ResetCFSSLFlagSetForTesting reset cfsslFlagSet with flag.ContinueOnError so parse
// errors in flag will not exit the program
func ResetCFSSLFlagSetForTesting(usage func()) {
var c Config
cfsslFlagSet = flag.NewFlagSet("cfssl", flag.ContinueOnError)
registerFlags(&c, cfsslFlagSet)
cfsslFlagSet.Usage = usage
}
func TestReadStdin(t *testing.T) {
fn, err := ReadStdin("./testdata/test.txt")
if err != nil {
t.Fatal(err)
}
if string(fn) != "This is a test file" {
t.Fatal(err)
}
fn, err = ReadStdin("-")
if err != nil {
t.Fatal(err)
}
}
func TestPopFirstArg(t *testing.T) {
s, str, err := PopFirstArgument([]string{"a", "b", "c"})
if s != "a" {
t.Fatal("Did not pop first argument successfully")
}
if str == nil {
t.Fatal("Did not return the rest of argument successfully")
}
if err != nil {
t.Fatal(err)
}
//test invalid argument
_, _, err = PopFirstArgument([]string{})
if err == nil {
t.Fatal("No argument given, should return error")
}
}
|