| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 1 | package bashkit |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestCheck(t *testing.T) { |
| 9 | tests := []struct { |
| 10 | name string |
| 11 | script string |
| 12 | wantErr bool |
| 13 | errMatch string // string to match in error message, if wantErr is true |
| 14 | }{ |
| 15 | { |
| 16 | name: "valid script", |
| 17 | script: "echo hello world", |
| 18 | wantErr: false, |
| 19 | errMatch: "", |
| 20 | }, |
| 21 | { |
| 22 | name: "invalid syntax", |
| 23 | script: "echo 'unterminated string", |
| 24 | wantErr: false, // As per implementation, syntax errors are not flagged |
| 25 | errMatch: "", |
| 26 | }, |
| 27 | { |
| 28 | name: "git config user.name", |
| 29 | script: "git config user.name 'John Doe'", |
| 30 | wantErr: true, |
| 31 | errMatch: "changing git config username/email is not allowed", |
| 32 | }, |
| 33 | { |
| 34 | name: "git config user.email", |
| 35 | script: "git config user.email 'john@example.com'", |
| 36 | wantErr: true, |
| 37 | errMatch: "changing git config username/email is not allowed", |
| 38 | }, |
| 39 | { |
| 40 | name: "git config with flag user.name", |
| 41 | script: "git config --global user.name 'John Doe'", |
| 42 | wantErr: true, |
| 43 | errMatch: "changing git config username/email is not allowed", |
| 44 | }, |
| 45 | { |
| 46 | name: "git config with other setting", |
| 47 | script: "git config core.editor vim", |
| 48 | wantErr: false, |
| 49 | errMatch: "", |
| 50 | }, |
| 51 | { |
| 52 | name: "git without config", |
| 53 | script: "git commit -m 'Add feature'", |
| 54 | wantErr: false, |
| 55 | errMatch: "", |
| 56 | }, |
| 57 | { |
| 58 | name: "multiline script with proper escaped newlines", |
| 59 | script: "echo 'Setting up git...' && git config user.name 'John Doe' && echo 'Done!'", |
| 60 | wantErr: true, |
| 61 | errMatch: "changing git config username/email is not allowed", |
| 62 | }, |
| 63 | { |
| 64 | name: "multiline script with backticks", |
| 65 | script: `echo 'Setting up git...' |
| 66 | git config user.name 'John Doe' |
| 67 | echo 'Done!'`, |
| 68 | wantErr: true, |
| 69 | errMatch: "changing git config username/email is not allowed", |
| 70 | }, |
| 71 | { |
| 72 | name: "git config with variable", |
| 73 | script: "NAME='John Doe'\ngit config user.name $NAME", |
| 74 | wantErr: true, |
| 75 | errMatch: "changing git config username/email is not allowed", |
| 76 | }, |
| 77 | { |
| 78 | name: "only git command", |
| 79 | script: "git", |
| 80 | wantErr: false, |
| 81 | errMatch: "", |
| 82 | }, |
| 83 | { |
| 84 | name: "read git config", |
| 85 | script: "git config user.name", |
| 86 | wantErr: false, |
| 87 | errMatch: "", |
| 88 | }, |
| 89 | { |
| 90 | name: "commented git config", |
| 91 | script: "# git config user.name 'John Doe'", |
| 92 | wantErr: false, |
| 93 | errMatch: "", |
| 94 | }, |
| 95 | } |
| 96 | |
| 97 | for _, tc := range tests { |
| 98 | t.Run(tc.name, func(t *testing.T) { |
| 99 | err := Check(tc.script) |
| 100 | if (err != nil) != tc.wantErr { |
| 101 | t.Errorf("Check() error = %v, wantErr %v", err, tc.wantErr) |
| 102 | return |
| 103 | } |
| 104 | if tc.wantErr && err != nil && !strings.Contains(err.Error(), tc.errMatch) { |
| 105 | t.Errorf("Check() error message = %v, want containing %v", err, tc.errMatch) |
| 106 | } |
| 107 | }) |
| 108 | } |
| 109 | } |
| Josh Bleecher Snyder | dae1907 | 2025-04-30 01:08:57 +0000 | [diff] [blame] | 110 | |
| 111 | func TestWillRunGitCommit(t *testing.T) { |
| 112 | tests := []struct { |
| 113 | name string |
| 114 | script string |
| 115 | wantCommit bool |
| 116 | }{ |
| 117 | { |
| 118 | name: "simple git commit", |
| 119 | script: "git commit -m 'Add feature'", |
| 120 | wantCommit: true, |
| 121 | }, |
| 122 | { |
| 123 | name: "git command without commit", |
| 124 | script: "git status", |
| 125 | wantCommit: false, |
| 126 | }, |
| 127 | { |
| 128 | name: "multiline script with git commit", |
| 129 | script: "echo 'Making changes' && git add . && git commit -m 'Update files'", |
| 130 | wantCommit: true, |
| 131 | }, |
| 132 | { |
| 133 | name: "multiline script without git commit", |
| 134 | script: "echo 'Checking status' && git status", |
| 135 | wantCommit: false, |
| 136 | }, |
| 137 | { |
| 138 | name: "script with commented git commit", |
| 139 | script: "# git commit -m 'This is commented out'", |
| 140 | wantCommit: false, |
| 141 | }, |
| 142 | { |
| 143 | name: "git commit with variables", |
| 144 | script: "MSG='Fix bug' && git commit -m 'Using variable'", |
| 145 | wantCommit: true, |
| 146 | }, |
| 147 | { |
| 148 | name: "only git command", |
| 149 | script: "git", |
| 150 | wantCommit: false, |
| 151 | }, |
| 152 | { |
| 153 | name: "script with invalid syntax", |
| 154 | script: "git commit -m 'unterminated string", |
| 155 | wantCommit: false, |
| 156 | }, |
| 157 | { |
| 158 | name: "commit used in different context", |
| 159 | script: "echo 'commit message'", |
| 160 | wantCommit: false, |
| 161 | }, |
| 162 | { |
| 163 | name: "git with flags before commit", |
| 164 | script: "git -C /path/to/repo commit -m 'Update'", |
| 165 | wantCommit: true, |
| 166 | }, |
| 167 | { |
| 168 | name: "git with multiple flags", |
| 169 | script: "git --git-dir=.git -C repo commit -a -m 'Update'", |
| 170 | wantCommit: true, |
| 171 | }, |
| 172 | { |
| 173 | name: "git with env vars", |
| 174 | script: "GIT_AUTHOR_NAME=\"Josh Bleecher Snyder\" GIT_AUTHOR_EMAIL=\"josharian@gmail.com\" git commit -am \"Updated code\"", |
| 175 | wantCommit: true, |
| 176 | }, |
| 177 | { |
| 178 | name: "git with redirections", |
| 179 | script: "git commit -m 'Fix issue' > output.log 2>&1", |
| 180 | wantCommit: true, |
| 181 | }, |
| 182 | { |
| 183 | name: "git with piped commands", |
| 184 | script: "echo 'Committing' | git commit -F -", |
| 185 | wantCommit: true, |
| 186 | }, |
| 187 | } |
| 188 | |
| 189 | for _, tc := range tests { |
| 190 | t.Run(tc.name, func(t *testing.T) { |
| 191 | gotCommit, err := WillRunGitCommit(tc.script) |
| 192 | if err != nil { |
| 193 | t.Errorf("WillRunGitCommit() error = %v", err) |
| 194 | return |
| 195 | } |
| 196 | if gotCommit != tc.wantCommit { |
| 197 | t.Errorf("WillRunGitCommit() = %v, want %v", gotCommit, tc.wantCommit) |
| 198 | } |
| 199 | }) |
| 200 | } |
| 201 | } |