Skip to content

Commit b97707f

Browse files
committed
test: Add tests to the tool configs generation PLUTO-1384
1 parent 547c211 commit b97707f

File tree

3 files changed

+159
-8
lines changed

3 files changed

+159
-8
lines changed

cmd/init.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,7 @@ func buildRepositoryConfigurationFiles(token string) error {
247247
}
248248

249249
// Filter out any tools that use configuration file
250-
var configuredToolsWithUI []tools.Tool
251-
for _, tool := range apiTools {
252-
if !tool.Settings.UsesConfigFile {
253-
configuredToolsWithUI = append(configuredToolsWithUI, tool)
254-
} else {
255-
fmt.Printf("Skipping config generation for %s - configured to use repo's config file\n", tool.Name)
256-
}
257-
}
250+
configuredToolsWithUI := tools.FilterToolsByConfigUsage(apiTools)
258251

259252
// Create main config files with all enabled API tools
260253
err = createConfigurationFiles(apiTools, false)

tools/getTools.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,17 @@ type Tool struct {
138138
UsesConfigFile bool `json:"hasConfigurationFile"`
139139
} `json:"settings"`
140140
}
141+
142+
// FilterToolsByConfigUsage filters out tools that use their own configuration files
143+
// Returns only tools that need configuration to be generated for them (UsesConfigFile = false)
144+
func FilterToolsByConfigUsage(tools []Tool) []Tool {
145+
var filtered []Tool
146+
for _, tool := range tools {
147+
if !tool.Settings.UsesConfigFile {
148+
filtered = append(filtered, tool)
149+
} else {
150+
fmt.Printf("Skipping config generation for %s - configured to use repo's config file\n", tool.Name)
151+
}
152+
}
153+
return filtered
154+
}

tools/getTools_test.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package tools
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestFilterToolsByConfigUsage(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
inputTools []Tool
13+
expectedCount int
14+
expectedTools []string
15+
}{
16+
{
17+
name: "tools with UsesConfigFile=true should be filtered out",
18+
inputTools: []Tool{
19+
{
20+
Uuid: "eslint-uuid",
21+
Name: "eslint",
22+
Settings: struct {
23+
Enabled bool `json:"isEnabled"`
24+
UsesConfigFile bool `json:"hasConfigurationFile"`
25+
}{
26+
Enabled: true,
27+
UsesConfigFile: true,
28+
},
29+
},
30+
{
31+
Uuid: "trivy-uuid",
32+
Name: "trivy",
33+
Settings: struct {
34+
Enabled bool `json:"isEnabled"`
35+
UsesConfigFile bool `json:"hasConfigurationFile"`
36+
}{
37+
Enabled: true,
38+
UsesConfigFile: false,
39+
},
40+
},
41+
{
42+
Uuid: "pylint-uuid",
43+
Name: "pylint",
44+
Settings: struct {
45+
Enabled bool `json:"isEnabled"`
46+
UsesConfigFile bool `json:"hasConfigurationFile"`
47+
}{
48+
Enabled: true,
49+
UsesConfigFile: false,
50+
},
51+
},
52+
},
53+
expectedCount: 2,
54+
expectedTools: []string{"trivy", "pylint"},
55+
},
56+
{
57+
name: "all tools using config should be filtered out",
58+
inputTools: []Tool{
59+
{
60+
Uuid: "eslint-uuid",
61+
Name: "eslint",
62+
Settings: struct {
63+
Enabled bool `json:"isEnabled"`
64+
UsesConfigFile bool `json:"hasConfigurationFile"`
65+
}{
66+
Enabled: true,
67+
UsesConfigFile: true,
68+
},
69+
},
70+
{
71+
Uuid: "trivy-uuid",
72+
Name: "trivy",
73+
Settings: struct {
74+
Enabled bool `json:"isEnabled"`
75+
UsesConfigFile bool `json:"hasConfigurationFile"`
76+
}{
77+
Enabled: true,
78+
UsesConfigFile: true,
79+
},
80+
},
81+
},
82+
expectedCount: 0,
83+
expectedTools: []string{},
84+
},
85+
{
86+
name: "no tools using config should all pass through",
87+
inputTools: []Tool{
88+
{
89+
Uuid: "eslint-uuid",
90+
Name: "eslint",
91+
Settings: struct {
92+
Enabled bool `json:"isEnabled"`
93+
UsesConfigFile bool `json:"hasConfigurationFile"`
94+
}{
95+
Enabled: true,
96+
UsesConfigFile: false,
97+
},
98+
},
99+
{
100+
Uuid: "pylint-uuid",
101+
Name: "pylint",
102+
Settings: struct {
103+
Enabled bool `json:"isEnabled"`
104+
UsesConfigFile bool `json:"hasConfigurationFile"`
105+
}{
106+
Enabled: true,
107+
UsesConfigFile: false,
108+
},
109+
},
110+
},
111+
expectedCount: 2,
112+
expectedTools: []string{"eslint", "pylint"},
113+
},
114+
}
115+
116+
for _, tt := range tests {
117+
t.Run(tt.name, func(t *testing.T) {
118+
// Use the actual function being tested
119+
result := FilterToolsByConfigUsage(tt.inputTools)
120+
121+
// Verify the count matches
122+
assert.Equal(t, tt.expectedCount, len(result),
123+
"Expected %d tools after filtering, got %d", tt.expectedCount, len(result))
124+
125+
// Verify each expected tool is in the result
126+
for _, expectedTool := range tt.expectedTools {
127+
found := false
128+
for _, tool := range result {
129+
if tool.Name == expectedTool {
130+
found = true
131+
break
132+
}
133+
}
134+
assert.True(t, found, "Expected tool %s not found in filtered results", expectedTool)
135+
}
136+
137+
// Verify no tools with UsesConfigFile=true are in the result
138+
for _, tool := range result {
139+
assert.False(t, tool.Settings.UsesConfigFile,
140+
"Tool %s with UsesConfigFile=true should not be in filtered results", tool.Name)
141+
}
142+
})
143+
}
144+
}

0 commit comments

Comments
 (0)