github.com/rainforestapp/rainforest-cli@v2.12.0+incompatible/rainforest-cli_test.go (about) 1 package main 2 3 import ( 4 "errors" 5 "os" 6 "os/exec" 7 "reflect" 8 "testing" 9 10 "github.com/urfave/cli" 11 ) 12 13 func TestMain(t *testing.T) { 14 commands := []string{"run", "new", "validate", "upload", "rm", "download", "csv-upload", "mobile-upload", "report", "sites", "environments", "folders", "filters", "browsers", "features", "run-groups", "update"} 15 16 for _, command := range commands { 17 if os.Getenv("TEST_EXIT") == "1" { 18 os.Args = []string{"./rainforest", command, "--not-real-flag"} 19 main() 20 return 21 } 22 23 cmd := exec.Command(os.Args[0], "-test.run=Main") 24 cmd.Env = append(os.Environ(), "TEST_EXIT=1") 25 err := cmd.Run() 26 27 if err == nil { 28 t.Error("Expected exit error was not received") 29 } else if e, ok := err.(*exec.ExitError); !ok || e.Success() { 30 t.Errorf("Unexpected error. Expected an exit error with non-zero status. Got %#v", e.Error()) 31 } 32 } 33 } 34 35 func TestShuffleFlags(t *testing.T) { 36 var testCases = []struct { 37 testArgs []string 38 want []string 39 }{ 40 { 41 testArgs: []string{"./rainforest", "--token", "foobar", "run", "--tags", "tag,bag"}, 42 want: []string{"./rainforest", "--token", "foobar", "run", "--tags", "tag,bag"}, 43 }, 44 { 45 testArgs: []string{"./rainforest", "run", "--tags", "tag,bag", "--token", "foobar"}, 46 want: []string{"./rainforest", "--token", "foobar", "run", "--tags", "tag,bag"}, 47 }, 48 { 49 testArgs: []string{"./rainforest", "run", "--tags", "tag,bag", "--token", "foobar", "--site", "123"}, 50 want: []string{"./rainforest", "--token", "foobar", "run", "--tags", "tag,bag", "--site", "123"}, 51 }, 52 { 53 testArgs: []string{"./rainforest", "run", "--tags", "tag,bag", "--token", "foobar", "--site", "123", "--debug"}, 54 want: []string{"./rainforest", "--token", "foobar", "--debug", "run", "--tags", "tag,bag", "--site", "123"}, 55 }, 56 { 57 testArgs: []string{"./rainforest", "run", "--tags", "tag,bag", "--token", "foobar", "--site", "123", "--run-group-id", "255"}, 58 want: []string{"./rainforest", "--token", "foobar", "run", "--tags", "tag,bag", "--site", "123", "--run-group-id", "255"}, 59 }, 60 { 61 testArgs: []string{"./rainforest", "--skip-update", "run", "--tags", "tag,bag", "--token", "foobar", "--site", "123"}, 62 want: []string{"./rainforest", "--skip-update", "--token", "foobar", "run", "--tags", "tag,bag", "--site", "123"}, 63 }, 64 { 65 testArgs: []string{"./rainforest", "run", "-f", "foo.rfml", "bar.rfml", "--token", "foobar"}, 66 want: []string{"./rainforest", "--token", "foobar", "run", "-f", "foo.rfml", "bar.rfml"}, 67 }, 68 { 69 testArgs: []string{"./rainforest", "run", "-f", "foo.rfml"}, 70 want: []string{"./rainforest", "run", "-f", "foo.rfml"}, 71 }, 72 } 73 74 for _, tCase := range testCases { 75 got := shuffleFlags(tCase.testArgs) 76 if !reflect.DeepEqual(tCase.want, got) { 77 t.Errorf("shuffleFlags returned %+v, want %+v", got, tCase.want) 78 } 79 } 80 } 81 82 var errStub = errors.New("STUB") 83 84 // fakeContext is a helper for testing the cli interfacing functions 85 type fakeContext struct { 86 mappings map[string]interface{} 87 args cli.Args 88 } 89 90 func (f fakeContext) String(s string) string { 91 val, ok := f.mappings[s].(string) 92 93 if ok { 94 return val 95 } 96 return "" 97 } 98 99 func (f fakeContext) StringSlice(s string) []string { 100 val, ok := f.mappings[s].([]string) 101 102 if ok { 103 return val 104 } 105 return []string{} 106 } 107 108 func (f fakeContext) Bool(s string) bool { 109 val, ok := f.mappings[s].(bool) 110 111 if ok { 112 return val 113 } 114 return false 115 } 116 117 func (f fakeContext) Int(s string) int { 118 val, ok := f.mappings[s].(int) 119 120 if ok { 121 return val 122 } 123 return 0 124 } 125 126 func (f fakeContext) Args() cli.Args { 127 return f.args 128 } 129 130 func newFakeContext(mappings map[string]interface{}, args cli.Args) *fakeContext { 131 return &fakeContext{mappings, args} 132 }