Skip to content

Commit f363fe3

Browse files
fix test
1 parent 079cb60 commit f363fe3

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

cmd/analyze_test.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,24 +338,42 @@ func TestCheckIfConfigExistsAndIsNeeded(t *testing.T) {
338338
err = os.Chdir(tmpDir)
339339
require.NoError(t, err)
340340

341-
// Create config file if needed
341+
// Mock config to use our temporary directory BEFORE creating files
342+
config.Config = *config.NewConfigType(tmpDir, tmpDir, tmpDir)
343+
344+
// Create config file if needed - using the same path logic as the function under test
342345
if tt.configFileExists && toolConfigFileName[tt.toolName] != "" {
343-
configPath := filepath.Join(tmpDir, toolConfigFileName[tt.toolName])
344-
err := os.WriteFile(configPath, []byte("test config"), 0644)
346+
// Use config.Config.ToolsConfigDirectory() to get the exact same path the function will use
347+
toolsConfigDir := config.Config.ToolsConfigDirectory()
348+
err := os.MkdirAll(toolsConfigDir, 0755)
345349
require.NoError(t, err)
350+
351+
configPath := filepath.Join(toolsConfigDir, toolConfigFileName[tt.toolName])
352+
err = os.WriteFile(configPath, []byte("test config"), 0644)
353+
require.NoError(t, err)
354+
355+
// Ensure the file was created and can be found
356+
_, err = os.Stat(configPath)
357+
require.NoError(t, err, "Config file should exist at %s", configPath)
346358
}
347359

348360
// Setup initFlags
349361
initFlags = domain.InitFlags{
350362
ApiToken: tt.apiToken,
351363
}
352364

353-
// Mock config to use our temporary directory
354-
config.Config = *config.NewConfigType(tmpDir, tmpDir, tmpDir)
355-
356365
// Execute the function
357366
err = checkIfConfigExistsAndIsNeeded(tt.toolName, tt.cliLocalMode)
358367

368+
// Clean up any files that might have been created by the function under test
369+
if !tt.configFileExists && toolConfigFileName[tt.toolName] != "" {
370+
toolsConfigDir := config.Config.ToolsConfigDirectory()
371+
configPath := filepath.Join(toolsConfigDir, toolConfigFileName[tt.toolName])
372+
if _, statErr := os.Stat(configPath); statErr == nil {
373+
os.Remove(configPath)
374+
}
375+
}
376+
359377
// Verify results
360378
if tt.expectError {
361379
assert.Error(t, err, tt.description)

0 commit comments

Comments
 (0)