-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathinit_test.go
More file actions
246 lines (225 loc) · 5.62 KB
/
init_test.go
File metadata and controls
246 lines (225 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package cmd
import (
"codacy/cli-v2/config"
"codacy/cli-v2/domain"
"codacy/cli-v2/utils"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestConfigFileTemplate(t *testing.T) {
tests := []struct {
name string
tools []domain.Tool
expected []string
notExpected []string
}{
{
name: "empty tools list uses defaults",
tools: []domain.Tool{},
expected: []string{
"node@22.2.0",
"python@3.11.11",
"eslint@9.3.0",
"trivy@0.59.1",
"pylint@3.3.6",
"pmd@6.55.0",
},
notExpected: []string{},
},
{
name: "only eslint enabled",
tools: []domain.Tool{
{
Uuid: ESLint,
Name: "eslint",
Version: "9.4.0",
},
},
expected: []string{
"node@22.2.0",
"eslint@9.4.0",
},
notExpected: []string{
"python@3.11.11",
"pylint",
"pmd",
"trivy",
},
},
{
name: "only pylint enabled",
tools: []domain.Tool{
{
Uuid: PyLint,
Name: "pylint",
Version: "3.4.0",
},
},
expected: []string{
"python@3.11.11",
"pylint@3.4.0",
},
notExpected: []string{
"node@22.2.0",
"eslint",
"pmd",
"trivy",
},
},
{
name: "eslint and trivy enabled",
tools: []domain.Tool{
{
Uuid: ESLint,
Name: "eslint",
Version: "9.4.0",
},
{
Uuid: Trivy,
Name: "trivy",
Version: "0.60.0",
},
},
expected: []string{
"node@22.2.0",
"eslint@9.4.0",
"trivy@0.60.0",
},
notExpected: []string{
"python@3.11.11",
"pylint",
"pmd",
},
},
{
name: "all tools enabled",
tools: []domain.Tool{
{
Uuid: ESLint,
Name: "eslint",
Version: "9.4.0",
},
{
Uuid: Trivy,
Name: "trivy",
Version: "0.60.0",
},
{
Uuid: PyLint,
Name: "pylint",
Version: "3.4.0",
},
{
Uuid: PMD,
Name: "pmd",
Version: "6.56.0",
},
},
expected: []string{
"node@22.2.0",
"python@3.11.11",
"eslint@9.4.0",
"trivy@0.60.0",
"pylint@3.4.0",
"pmd@6.56.0",
},
notExpected: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := configFileTemplate(tt.tools)
// Check that expected strings are present
for _, exp := range tt.expected {
assert.Contains(t, result, exp, "Config file should contain %s", exp)
}
// Check that not-expected strings are absent
for _, notExp := range tt.notExpected {
assert.NotContains(t, result, notExp, "Config file should not contain %s", notExp)
}
})
}
}
func TestCleanConfigDirectory(t *testing.T) {
// Create a temporary directory for testing
tempDir := t.TempDir()
// Create some test files in the temp dir
testFiles := []string{
"eslint.config.mjs",
"pylint.rc",
"ruleset.xml",
"trivy.yaml",
}
for _, file := range testFiles {
filePath := filepath.Join(tempDir, file)
err := os.WriteFile(filePath, []byte("test content"), utils.DefaultFilePerms)
assert.NoError(t, err, "Failed to create test file: %s", filePath)
}
// Verify files exist
files, err := os.ReadDir(tempDir)
assert.NoError(t, err)
assert.Equal(t, len(testFiles), len(files), "Expected %d files before cleaning", len(testFiles))
// Run the clean function
err = cleanConfigDirectory(tempDir)
assert.NoError(t, err, "cleanConfigDirectory should not return an error")
// Verify all files are gone
files, err = os.ReadDir(tempDir)
assert.NoError(t, err)
assert.Equal(t, 0, len(files), "Expected 0 files after cleaning, got %d", len(files))
}
func TestInitCommand_NoToken(t *testing.T) {
tempDir := t.TempDir()
originalWD, err := os.Getwd()
assert.NoError(t, err, "Failed to get current working directory")
defer os.Chdir(originalWD)
// Use the real plugins/tools/semgrep/rules.yaml file
rulesPath := filepath.Join("plugins", "tools", "semgrep", "rules.yaml")
if _, err := os.Stat(rulesPath); os.IsNotExist(err) {
t.Skip("plugins/tools/semgrep/rules.yaml not found; skipping test")
}
// Change to the temp directory to simulate a new project
err = os.Chdir(tempDir)
assert.NoError(t, err, "Failed to change working directory to tempDir")
// Simulate running init with no token
initFlags.ApiToken = ""
initFlags.Provider = ""
initFlags.Organization = ""
initFlags.Repository = ""
// Call the Run logic from initCmd
if err := config.Config.CreateLocalCodacyDir(); err != nil {
t.Fatalf("Failed to create local codacy directory: %v", err)
}
toolsConfigDir := config.Config.ToolsConfigDirectory()
if err := os.MkdirAll(toolsConfigDir, utils.DefaultFilePerms); err != nil {
t.Fatalf("Failed to create tools-configs directory: %v", err)
}
cliLocalMode := len(initFlags.ApiToken) == 0
if cliLocalMode {
noTools := []domain.Tool{}
err := createConfigurationFiles(noTools, cliLocalMode)
assert.NoError(t, err, "createConfigurationFiles should not return an error")
if err := buildDefaultConfigurationFiles(toolsConfigDir); err != nil {
t.Fatalf("Failed to build default configuration files: %v", err)
}
}
// Assert that the expected config files are created
codacyDir := config.Config.LocalCodacyDirectory()
expectedFiles := []string{
"tools-configs/eslint.config.mjs",
"tools-configs/trivy.yaml",
"tools-configs/ruleset.xml",
"tools-configs/pylint.rc",
"tools-configs/analysis_options.yaml",
"tools-configs/semgrep.yaml",
"tools-configs/lizard.yaml",
"codacy.yaml",
"cli-config.yaml",
}
for _, file := range expectedFiles {
filePath := filepath.Join(codacyDir, file)
_, err := os.Stat(filePath)
assert.NoError(t, err, "Expected config file %s to be created", file)
}
}