Skip to content

Commit 547c211

Browse files
committed
test: Add tests for the codacy config file generation
1 parent 54c7d3a commit 547c211

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed

cmd/init_test.go

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package cmd
2+
3+
import (
4+
"codacy/cli-v2/tools"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestConfigFileTemplate(t *testing.T) {
13+
tests := []struct {
14+
name string
15+
tools []tools.Tool
16+
expected []string
17+
notExpected []string
18+
}{
19+
{
20+
name: "empty tools list uses defaults",
21+
tools: []tools.Tool{},
22+
expected: []string{
23+
24+
25+
26+
27+
28+
29+
},
30+
notExpected: []string{},
31+
},
32+
{
33+
name: "only eslint enabled",
34+
tools: []tools.Tool{
35+
{
36+
Uuid: ESLint,
37+
Name: "eslint",
38+
Version: "9.4.0",
39+
},
40+
},
41+
expected: []string{
42+
43+
44+
},
45+
notExpected: []string{
46+
47+
"pylint",
48+
"pmd",
49+
"trivy",
50+
},
51+
},
52+
{
53+
name: "only pylint enabled",
54+
tools: []tools.Tool{
55+
{
56+
Uuid: PyLint,
57+
Name: "pylint",
58+
Version: "3.4.0",
59+
},
60+
},
61+
expected: []string{
62+
63+
64+
},
65+
notExpected: []string{
66+
67+
"eslint",
68+
"pmd",
69+
"trivy",
70+
},
71+
},
72+
{
73+
name: "eslint and trivy enabled",
74+
tools: []tools.Tool{
75+
{
76+
Uuid: ESLint,
77+
Name: "eslint",
78+
Version: "9.4.0",
79+
},
80+
{
81+
Uuid: Trivy,
82+
Name: "trivy",
83+
Version: "0.60.0",
84+
},
85+
},
86+
expected: []string{
87+
88+
89+
90+
},
91+
notExpected: []string{
92+
93+
"pylint",
94+
"pmd",
95+
},
96+
},
97+
{
98+
name: "all tools enabled",
99+
tools: []tools.Tool{
100+
{
101+
Uuid: ESLint,
102+
Name: "eslint",
103+
Version: "9.4.0",
104+
},
105+
{
106+
Uuid: Trivy,
107+
Name: "trivy",
108+
Version: "0.60.0",
109+
},
110+
{
111+
Uuid: PyLint,
112+
Name: "pylint",
113+
Version: "3.4.0",
114+
},
115+
{
116+
Uuid: PMD,
117+
Name: "pmd",
118+
Version: "6.56.0",
119+
},
120+
},
121+
expected: []string{
122+
123+
124+
125+
126+
127+
128+
},
129+
notExpected: []string{},
130+
},
131+
}
132+
133+
for _, tt := range tests {
134+
t.Run(tt.name, func(t *testing.T) {
135+
result := configFileTemplate(tt.tools)
136+
137+
// Check that expected strings are present
138+
for _, exp := range tt.expected {
139+
assert.Contains(t, result, exp, "Config file should contain %s", exp)
140+
}
141+
142+
// Check that not-expected strings are absent
143+
for _, notExp := range tt.notExpected {
144+
assert.NotContains(t, result, notExp, "Config file should not contain %s", notExp)
145+
}
146+
})
147+
}
148+
}
149+
150+
func TestCleanConfigDirectory(t *testing.T) {
151+
// Create a temporary directory for testing
152+
tempDir := t.TempDir()
153+
154+
// Create some test files in the temp dir
155+
testFiles := []string{
156+
"eslint.config.mjs",
157+
"pylint.rc",
158+
"ruleset.xml",
159+
"trivy.yaml",
160+
}
161+
162+
for _, file := range testFiles {
163+
filePath := filepath.Join(tempDir, file)
164+
err := os.WriteFile(filePath, []byte("test content"), 0644)
165+
assert.NoError(t, err, "Failed to create test file: %s", filePath)
166+
}
167+
168+
// Verify files exist
169+
files, err := os.ReadDir(tempDir)
170+
assert.NoError(t, err)
171+
assert.Equal(t, len(testFiles), len(files), "Expected %d files before cleaning", len(testFiles))
172+
173+
// Run the clean function
174+
err = cleanConfigDirectory(tempDir)
175+
assert.NoError(t, err, "cleanConfigDirectory should not return an error")
176+
177+
// Verify all files are gone
178+
files, err = os.ReadDir(tempDir)
179+
assert.NoError(t, err)
180+
assert.Equal(t, 0, len(files), "Expected 0 files after cleaning, got %d", len(files))
181+
}

0 commit comments

Comments
 (0)