blob: 8bcdd8f1258a9d30b9be7a5eb16eee7086028c2a [file] [log] [blame]
Earl Lee2e463fb2025-04-17 11:22:22 -07001package bashkit
2
3import (
4 "strings"
5 "testing"
6)
7
8func 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...'
66git config user.name 'John Doe'
67echo '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}