|
1 | 1 | package tools |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "codacy/cli-v2/config" |
4 | 5 | "os" |
5 | 6 | "path/filepath" |
6 | 7 | "testing" |
7 | 8 |
|
8 | 9 | "github.com/stretchr/testify/assert" |
9 | 10 | ) |
10 | 11 |
|
11 | | -func TestConfigFileExists(t *testing.T) { |
| 12 | +func TestConfigFileExistsInToolsConfigDirectory(t *testing.T) { |
12 | 13 | // Create a test directory structure |
13 | 14 | tempDir := t.TempDir() |
| 15 | + repoDir := filepath.Join(tempDir, "src") |
| 16 | + repositoryCache := filepath.Join(repoDir, ".codacy") |
| 17 | + |
| 18 | + // Create configuration |
| 19 | + config := *config.NewConfigType(repoDir, repositoryCache, "unused-global-cache") |
14 | 20 |
|
15 | 21 | // Create .codacy/tools-configs directory |
16 | | - configDir := filepath.Join(tempDir, ".codacy", "tools-configs") |
| 22 | + configDir := filepath.Join(repoDir, ".codacy", "tools-configs") |
| 23 | + err := os.MkdirAll(configDir, 0755) |
| 24 | + assert.NoError(t, err, "Failed to create test directory structure") |
| 25 | + |
| 26 | + // Create a test config file on the configDir |
| 27 | + generatedConfigFile := filepath.Join(configDir, "generated-config.yaml") |
| 28 | + err = os.WriteFile(generatedConfigFile, []byte("test content"), 0644) |
| 29 | + assert.NoError(t, err, "Failed to create test config file") |
17 | 30 |
|
| 31 | + // Test case: Config file exists in tools config directory |
| 32 | + configPath, exists := ConfigFileExists(config, "generated-config.yaml") |
| 33 | + assert.True(t, exists, "Config file should exist in tools config directory") |
| 34 | + assert.Equal(t, filepath.Join(config.ToolsConfigDirectory(), "generated-config.yaml"), configPath, |
| 35 | + "Config path should be correctly formed relative path") |
| 36 | +} |
| 37 | + |
| 38 | +func TestConfigFileExistsInRepositoryDirectory(t *testing.T) { |
| 39 | + // Create a test directory structure |
| 40 | + tempDir := t.TempDir() |
| 41 | + repoDir := filepath.Join(tempDir, "src") |
| 42 | + repositoryCache := filepath.Join(repoDir, ".codacy") |
| 43 | + |
| 44 | + // Create configuration |
| 45 | + config := *config.NewConfigType(repoDir, repositoryCache, "unused-global-cache") |
| 46 | + |
| 47 | + // Create .codacy/tools-configs directory |
| 48 | + configDir := filepath.Join(repoDir, ".codacy", "tools-configs") |
18 | 49 | err := os.MkdirAll(configDir, 0755) |
19 | 50 | assert.NoError(t, err, "Failed to create test directory structure") |
20 | 51 |
|
21 | | - // Create a test config file |
22 | | - testConfigFile := filepath.Join(configDir, "test-config.yaml") |
23 | | - err = os.WriteFile(testConfigFile, []byte("test content"), 0644) |
| 52 | + // Create a test config file on the repository directory |
| 53 | + existingConfigFile := filepath.Join(repoDir, "existing-config.yaml") |
| 54 | + err = os.WriteFile(existingConfigFile, []byte("test content"), 0644) |
24 | 55 | assert.NoError(t, err, "Failed to create test config file") |
25 | 56 |
|
26 | | - // Test case 1: Config file exists |
27 | | - configPath, exists := ConfigFileExists(tempDir, "test-config.yaml") |
28 | | - assert.True(t, exists, "Config file should exist") |
29 | | - assert.Equal(t, filepath.Join(".codacy", "tools-configs", "test-config.yaml"), configPath, |
| 57 | + // Test case: The existing config file gets picked up |
| 58 | + configPath, exists := ConfigFileExists(config, "existing-config.yaml") |
| 59 | + assert.True(t, exists, "Config file should exist in tools config directory") |
| 60 | + assert.Equal(t, filepath.Join(config.RepositoryDirectory(), "existing-config.yaml"), configPath, |
30 | 61 | "Config path should be correctly formed relative path") |
| 62 | +} |
| 63 | + |
| 64 | +func TestConfigFilePrefersToolsConfigDirectory(t *testing.T) { |
| 65 | + // Create a test directory structure |
| 66 | + tempDir := t.TempDir() |
| 67 | + repoDir := filepath.Join(tempDir, "src") |
| 68 | + repositoryCache := filepath.Join(repoDir, ".codacy") |
| 69 | + |
| 70 | + // Create configuration |
| 71 | + config := *config.NewConfigType(repoDir, repositoryCache, "unused-global-cache") |
| 72 | + |
| 73 | + // Create .codacy/tools-configs directory |
| 74 | + configDir := filepath.Join(repoDir, ".codacy", "tools-configs") |
| 75 | + err := os.MkdirAll(configDir, 0755) |
| 76 | + assert.NoError(t, err, "Failed to create test directory structure") |
| 77 | + |
| 78 | + // Create a test config file in both locations |
| 79 | + generatedConfigFile := filepath.Join(configDir, "some-config.yaml") |
| 80 | + existingConfigFile := filepath.Join(repoDir, "some-config.yaml") |
| 81 | + |
| 82 | + err = os.WriteFile(generatedConfigFile, []byte("tools config content"), 0644) |
| 83 | + assert.NoError(t, err, "Failed to create test config file in tools config directory") |
| 84 | + |
| 85 | + err = os.WriteFile(existingConfigFile, []byte("repository config content"), 0644) |
| 86 | + assert.NoError(t, err, "Failed to create test config file in repository directory") |
| 87 | + |
| 88 | + // Test case: Config file in tools config directory is preferred |
| 89 | + configPath, exists := ConfigFileExists(config, "some-config.yaml") |
| 90 | + assert.True(t, exists, "Config file should exist") |
| 91 | + assert.Equal(t, filepath.Join(config.ToolsConfigDirectory(), "some-config.yaml"), configPath, |
| 92 | + "Config path should prefer tools config directory") |
| 93 | +} |
| 94 | + |
| 95 | +func TestConfigFileDoesNotExist(t *testing.T) { |
| 96 | + // Create a test directory structure |
| 97 | + tempDir := t.TempDir() |
| 98 | + repoDir := filepath.Join(tempDir, "src") |
| 99 | + repositoryCache := filepath.Join(repoDir, ".codacy") |
| 100 | + |
| 101 | + // Create configuration |
| 102 | + config := *config.NewConfigType(repoDir, repositoryCache, "unused-global-cache") |
31 | 103 |
|
32 | | - // Test case 2: Config file doesn't exist |
33 | | - configPath, exists = ConfigFileExists(tempDir, "non-existent-config.yaml") |
| 104 | + // Test case: Config file does not exist |
| 105 | + configPath, exists := ConfigFileExists(config, "non-existent-config.yaml") |
34 | 106 | assert.False(t, exists, "Config file should not exist") |
35 | 107 | assert.Equal(t, "", configPath, "Config path should be empty for non-existent file") |
36 | 108 | } |
0 commit comments