cli_test.go 2.9 KB

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