github.com/slackhq/nebula@v1.9.0/cmd/nebula-cert/main_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "errors" 6 "io" 7 "os" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 //TODO: all flag parsing continueOnError will print to stderr on its own currently 14 15 func Test_help(t *testing.T) { 16 expected := "Usage of " + os.Args[0] + " <global flags> <mode>:\n" + 17 " Global flags:\n" + 18 " -version: Prints the version\n" + 19 " -h, -help: Prints this help message\n\n" + 20 " Modes:\n" + 21 " " + caSummary() + "\n" + 22 " " + keygenSummary() + "\n" + 23 " " + signSummary() + "\n" + 24 " " + printSummary() + "\n" + 25 " " + verifySummary() + "\n" + 26 "\n" + 27 " To see usage for a given mode, use " + os.Args[0] + " <mode> -h\n" 28 29 ob := &bytes.Buffer{} 30 31 // No error test 32 help("", ob) 33 assert.Equal( 34 t, 35 expected, 36 ob.String(), 37 ) 38 39 // Error test 40 ob.Reset() 41 help("test error", ob) 42 assert.Equal( 43 t, 44 "Error: test error\n\n"+expected, 45 ob.String(), 46 ) 47 } 48 49 func Test_handleError(t *testing.T) { 50 ob := &bytes.Buffer{} 51 52 // normal error 53 handleError("", errors.New("test error"), ob) 54 assert.Equal(t, "Error: test error\n", ob.String()) 55 56 // unknown mode help error 57 ob.Reset() 58 handleError("", newHelpErrorf("test %s", "error"), ob) 59 assert.Equal(t, "Error: test error\n", ob.String()) 60 61 // test all modes with help error 62 modes := map[string]func(io.Writer){"ca": caHelp, "print": printHelp, "sign": signHelp, "verify": verifyHelp} 63 eb := &bytes.Buffer{} 64 for mode, fn := range modes { 65 ob.Reset() 66 eb.Reset() 67 fn(eb) 68 69 handleError(mode, newHelpErrorf("test %s", "error"), ob) 70 assert.Equal(t, "Error: test error\n"+eb.String(), ob.String()) 71 } 72 73 } 74 75 func assertHelpError(t *testing.T, err error, msg string) { 76 switch err.(type) { 77 case *helpError: 78 // good 79 default: 80 t.Fatal("err was not a helpError") 81 } 82 83 assert.EqualError(t, err, msg) 84 }