1
1
package server_test
2
2
3
3
import (
4
- "fmt"
5
4
"testing"
6
5
7
6
"github.com/google/go-github/github"
8
7
"github.com/hootsuite/atlantis/server"
9
8
. "github.com/hootsuite/atlantis/testing_util"
9
+ "strings"
10
10
)
11
11
12
12
func TestDetermineCommandInvalid (t * testing.T ) {
@@ -22,8 +22,6 @@ func TestDetermineCommandInvalid(t *testing.T) {
22
22
"atlantis slkjd" ,
23
23
"@user slkjd" ,
24
24
"atlantis plans" ,
25
- // whitespace
26
- " atlantis plan" ,
27
25
// misc
28
26
"related comment mentioning atlantis" ,
29
27
}
@@ -55,29 +53,50 @@ func TestDetermineCommandPermutations(t *testing.T) {
55
53
execNames := []string {"run" , "atlantis" , "@user" }
56
54
commandNames := []server.CommandName {server .Plan , server .Apply }
57
55
envs := []string {"" , "default" , "env" , "env-dash" , "env_underscore" , "camelEnv" }
58
- verboses := []bool {true , false }
56
+ flagCases := [][]string {
57
+ []string {},
58
+ []string {"--verbose" },
59
+ []string {"-key=value" },
60
+ []string {"-key" , "value" },
61
+ []string {"-key1=value1" , "-key2=value2" },
62
+ []string {"-key1=value1" , "-key2" , "value2" },
63
+ []string {"-key1" , "value1" , "-key2=value2" },
64
+ []string {"--verbose" , "key2=value2" },
65
+ []string {"-key1=value1" , "--verbose" },
66
+ }
59
67
60
68
// test all permutations
61
69
for _ , exec := range execNames {
62
70
for _ , name := range commandNames {
63
71
for _ , env := range envs {
64
- for _ , v := range verboses {
65
- vFlag := ""
66
- if v == true {
67
- vFlag = "--verbose"
68
- }
72
+ for _ , flags := range flagCases {
73
+ // If github comments end in a newline they get \r\n appended.
74
+ // Ensure that we parse commands properly either way.
75
+ for _ , lineEnding := range []string {"" , "\r \n " } {
76
+ comment := strings .Join (append ([]string {exec , name .String (), env }, flags ... ), " " ) + lineEnding
77
+ t .Log ("testing comment: " + comment )
78
+ c , err := e .DetermineCommand (buildComment (comment ))
79
+ Ok (t , err )
80
+ Equals (t , name , c .Name )
81
+ if env == "" {
82
+ Equals (t , "default" , c .Environment )
83
+ } else {
84
+ Equals (t , env , c .Environment )
85
+ }
86
+ Equals (t , stringInSlice ("--verbose" , flags ), c .Verbose )
69
87
70
- comment := fmt .Sprintf ("%s %s %s %s" , exec , name .String (), env , vFlag )
71
- t .Log ("testing comment: " + comment )
72
- c , err := e .DetermineCommand (buildComment (comment ))
73
- Ok (t , err )
74
- Equals (t , name , c .Name )
75
- if env == "" {
76
- Equals (t , "default" , c .Environment )
77
- } else {
78
- Equals (t , env , c .Environment )
88
+ // ensure --verbose never shows up in flags
89
+ for _ , f := range c .Flags {
90
+ Assert (t , f != "--verbose" , "Should not pass on the --verbose flag: %v" , flags )
91
+ }
92
+
93
+ // check all flags are present
94
+ for _ , f := range flags {
95
+ if f != "--verbose" {
96
+ Contains (t , f , c .Flags )
97
+ }
98
+ }
79
99
}
80
- Equals (t , v , c .Verbose )
81
100
}
82
101
}
83
102
}
@@ -91,3 +110,13 @@ func buildComment(c string) *github.IssueCommentEvent {
91
110
},
92
111
}
93
112
}
113
+
114
+ func stringInSlice (a string , list []string ) bool {
115
+ for _ , b := range list {
116
+ if b == a {
117
+ return true
118
+ }
119
+ }
120
+ return false
121
+ }
122
+
0 commit comments