Skip to content

Commit 47ce922

Browse files
added test
1 parent ab59a6b commit 47ce922

File tree

2 files changed

+170
-25
lines changed

2 files changed

+170
-25
lines changed

cmd/analyze_integration_test.go

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package cmd
2+
3+
import (
4+
"path/filepath"
5+
"strings"
6+
"testing"
7+
8+
"codacy/cli-v2/config"
9+
"codacy/cli-v2/domain"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
// TestCheckIfConfigExistsAndIsNeededBehavior tests the behavior of checkIfConfigExistsAndIsNeeded
15+
func TestCheckIfConfigExistsAndIsNeededBehavior(t *testing.T) {
16+
// Save original state
17+
originalFlags := initFlags
18+
defer func() {
19+
initFlags = originalFlags
20+
}()
21+
22+
tests := []struct {
23+
name string
24+
toolName string
25+
cliLocalMode bool
26+
apiToken string
27+
description string
28+
expectNoError bool
29+
}{
30+
{
31+
name: "tool_without_config_file",
32+
toolName: "unsupported-tool",
33+
cliLocalMode: false,
34+
apiToken: "test-token",
35+
description: "Tool that doesn't use config files should return without error",
36+
expectNoError: true,
37+
},
38+
{
39+
name: "eslint_remote_mode_with_token",
40+
toolName: "eslint",
41+
cliLocalMode: false,
42+
apiToken: "test-token",
43+
description: "ESLint in remote mode with token should not error",
44+
expectNoError: true,
45+
},
46+
{
47+
name: "eslint_remote_mode_without_token",
48+
toolName: "eslint",
49+
cliLocalMode: false,
50+
apiToken: "",
51+
description: "ESLint in remote mode without token should not error",
52+
expectNoError: true,
53+
},
54+
{
55+
name: "eslint_local_mode",
56+
toolName: "eslint",
57+
cliLocalMode: true,
58+
apiToken: "",
59+
description: "ESLint in local mode should not error",
60+
expectNoError: true,
61+
},
62+
{
63+
name: "trivy_local_mode",
64+
toolName: "trivy",
65+
cliLocalMode: true,
66+
apiToken: "",
67+
description: "Trivy in local mode should not error",
68+
expectNoError: true,
69+
},
70+
{
71+
name: "pmd_remote_mode",
72+
toolName: "pmd",
73+
cliLocalMode: false,
74+
apiToken: "test-token",
75+
description: "PMD in remote mode should not error",
76+
expectNoError: true,
77+
},
78+
}
79+
80+
for _, tt := range tests {
81+
t.Run(tt.name, func(t *testing.T) {
82+
// Setup initFlags
83+
initFlags = domain.InitFlags{
84+
ApiToken: tt.apiToken,
85+
}
86+
87+
// Execute the function - this tests it doesn't panic or return unexpected errors
88+
err := checkIfConfigExistsAndIsNeeded(tt.toolName, tt.cliLocalMode)
89+
90+
// Verify results
91+
if tt.expectNoError {
92+
assert.NoError(t, err, "Function should not return error: %s", tt.description)
93+
} else {
94+
assert.Error(t, err, "Function should return error: %s", tt.description)
95+
}
96+
})
97+
}
98+
}
99+
100+
// TestToolConfigFileNameMapCompleteness ensures all expected tools have config mappings
101+
func TestToolConfigFileNameMapCompleteness(t *testing.T) {
102+
expectedTools := map[string]string{
103+
"eslint": "eslint.config.mjs",
104+
"trivy": "trivy.yaml",
105+
"pmd": "ruleset.xml",
106+
"pylint": "pylint.rc",
107+
"dartanalyzer": "analysis_options.yaml",
108+
"semgrep": "semgrep.yaml",
109+
"revive": "revive.toml",
110+
"lizard": "lizard.yaml",
111+
}
112+
113+
t.Run("all_expected_tools_present", func(t *testing.T) {
114+
for toolName, expectedFileName := range expectedTools {
115+
actualFileName, exists := toolConfigFileName[toolName]
116+
assert.True(t, exists, "Tool %s should exist in toolConfigFileName map", toolName)
117+
assert.Equal(t, expectedFileName, actualFileName, "Config filename for %s should match expected", toolName)
118+
}
119+
})
120+
121+
t.Run("no_unexpected_tools", func(t *testing.T) {
122+
for toolName := range toolConfigFileName {
123+
_, expected := expectedTools[toolName]
124+
assert.True(t, expected, "Unexpected tool %s found in toolConfigFileName map", toolName)
125+
}
126+
})
127+
128+
t.Run("config_files_have_proper_extensions", func(t *testing.T) {
129+
validExtensions := map[string]bool{
130+
".mjs": true,
131+
".js": true,
132+
".yaml": true,
133+
".yml": true,
134+
".xml": true,
135+
".rc": true,
136+
".toml": true,
137+
}
138+
139+
for toolName, fileName := range toolConfigFileName {
140+
ext := filepath.Ext(fileName)
141+
assert.True(t, validExtensions[ext], "Tool %s has config file %s with unexpected extension %s", toolName, fileName, ext)
142+
}
143+
})
144+
}
145+
146+
// TestCheckIfConfigExistsAndIsNeededEdgeCases tests edge cases and error conditions
147+
func TestCheckIfConfigExistsAndIsNeededEdgeCases(t *testing.T) {
148+
originalFlags := initFlags
149+
originalConfig := config.Config
150+
defer func() {
151+
initFlags = originalFlags
152+
config.Config = originalConfig
153+
}()
154+
155+
t.Run("empty_tool_name", func(t *testing.T) {
156+
err := checkIfConfigExistsAndIsNeeded("", false)
157+
assert.NoError(t, err, "Empty tool name should not cause error")
158+
})
159+
160+
t.Run("tool_name_with_special_characters", func(t *testing.T) {
161+
err := checkIfConfigExistsAndIsNeeded("tool-with-dashes_and_underscores", false)
162+
assert.NoError(t, err, "Tool name with special characters should not cause error")
163+
})
164+
165+
t.Run("very_long_tool_name", func(t *testing.T) {
166+
longToolName := strings.Repeat("verylongtoolname", 10)
167+
err := checkIfConfigExistsAndIsNeeded(longToolName, false)
168+
assert.NoError(t, err, "Very long tool name should not cause error")
169+
})
170+
}

cmd/analyze_test.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -384,28 +384,3 @@ func TestToolConfigFileNameMap(t *testing.T) {
384384
})
385385
}
386386
}
387-
388-
func TestGetFileExtension(t *testing.T) {
389-
tests := []struct {
390-
filePath string
391-
expected string
392-
}{
393-
{"test.js", ".js"},
394-
{"test.jsx", ".jsx"},
395-
{"test.ts", ".ts"},
396-
{"test.tsx", ".tsx"},
397-
{"test.py", ".py"},
398-
{"test.java", ".java"},
399-
{"test", ""},
400-
{"test.JS", ".js"}, // Should be lowercase
401-
{"/path/to/test.py", ".py"},
402-
{"", ""},
403-
}
404-
405-
for _, tt := range tests {
406-
t.Run(tt.filePath, func(t *testing.T) {
407-
result := GetFileExtension(tt.filePath)
408-
assert.Equal(t, tt.expected, result)
409-
})
410-
}
411-
}

0 commit comments

Comments
 (0)