-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathinit_test.go
More file actions
183 lines (171 loc) · 3.66 KB
/
init_test.go
File metadata and controls
183 lines (171 loc) · 3.66 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
package cmd
import (
"codacy/cli-v2/cmd/configsetup"
"codacy/cli-v2/constants"
"codacy/cli-v2/domain"
"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@8.57.0",
"trivy@0.66.0",
"pylint@3.3.6",
"pmd@7.11.0",
},
notExpected: []string{},
},
{
name: "only eslint enabled",
tools: []domain.Tool{
{
Uuid: domain.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: domain.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: domain.ESLint,
Name: "eslint",
Version: "9.4.0",
},
{
Uuid: domain.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: domain.ESLint,
Name: "eslint",
Version: "9.4.0",
},
{
Uuid: domain.Trivy,
Name: "trivy",
Version: "0.60.0",
},
{
Uuid: domain.PyLint,
Name: "pylint",
Version: "3.4.0",
},
{
Uuid: domain.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 := configsetup.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"), constants.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 = configsetup.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))
}