Skip to content

Commit dc9d829

Browse files
added test
1 parent f8c1355 commit dc9d829

File tree

2 files changed

+203
-39
lines changed

2 files changed

+203
-39
lines changed

cmd/analyze_integration_test.go

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

cmd/analyze_test.go

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path/filepath"
77
"testing"
88

9+
"codacy/cli-v2/config"
910
"codacy/cli-v2/domain"
1011

1112
"github.com/stretchr/testify/assert"
@@ -269,9 +270,15 @@ func TestValidatePaths(t *testing.T) {
269270
}
270271

271272
func TestCheckIfConfigExistsAndIsNeeded(t *testing.T) {
272-
// Save original initFlags
273+
// Save original initFlags and config
273274
originalFlags := initFlags
274-
defer func() { initFlags = originalFlags }()
275+
originalConfig := config.Config
276+
originalWorkingDir, _ := os.Getwd()
277+
defer func() {
278+
initFlags = originalFlags
279+
config.Config = originalConfig
280+
os.Chdir(originalWorkingDir)
281+
}()
275282

276283
tests := []struct {
277284
name string
@@ -300,15 +307,6 @@ func TestCheckIfConfigExistsAndIsNeeded(t *testing.T) {
300307
expectError: false,
301308
description: "When config file exists, should find it successfully",
302309
},
303-
{
304-
name: "remote_mode_with_token_no_config",
305-
toolName: "eslint",
306-
cliLocalMode: false,
307-
apiToken: "test-token",
308-
configFileExists: false,
309-
expectError: false,
310-
description: "Remote mode with token should attempt to create config",
311-
},
312310
{
313311
name: "remote_mode_without_token_no_config",
314312
toolName: "eslint",
@@ -331,11 +329,15 @@ func TestCheckIfConfigExistsAndIsNeeded(t *testing.T) {
331329

332330
for _, tt := range tests {
333331
t.Run(tt.name, func(t *testing.T) {
334-
// Setup temporary directory
332+
// Setup temporary directory and change to it to avoid creating files in project dir
335333
tmpDir, err := os.MkdirTemp("", "codacy-test-*")
336334
require.NoError(t, err)
337335
defer os.RemoveAll(tmpDir)
338336

337+
// Change to temp directory to prevent config file creation in project
338+
err = os.Chdir(tmpDir)
339+
require.NoError(t, err)
340+
339341
// Create config file if needed
340342
if tt.configFileExists && toolConfigFileName[tt.toolName] != "" {
341343
configPath := filepath.Join(tmpDir, toolConfigFileName[tt.toolName])
@@ -348,8 +350,8 @@ func TestCheckIfConfigExistsAndIsNeeded(t *testing.T) {
348350
ApiToken: tt.apiToken,
349351
}
350352

351-
// Mock config directory (this would need actual mocking in real implementation)
352-
// For now, we'll create a simple test that verifies the function doesn't panic
353+
// Mock config to use our temporary directory
354+
config.Config = *config.NewConfigType(tmpDir, tmpDir, tmpDir)
353355

354356
// Execute the function
355357
err = checkIfConfigExistsAndIsNeeded(tt.toolName, tt.cliLocalMode)
@@ -384,28 +386,3 @@ func TestToolConfigFileNameMap(t *testing.T) {
384386
})
385387
}
386388
}
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)