|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "codacy/cli-v2/config" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestConfigFileExistsInToolsConfigDirectory(t *testing.T) { |
| 13 | + // Create a test directory structure |
| 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") |
| 20 | + |
| 21 | + // Create .codacy/tools-configs directory |
| 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") |
| 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") |
| 49 | + err := os.MkdirAll(configDir, 0755) |
| 50 | + assert.NoError(t, err, "Failed to create test directory structure") |
| 51 | + |
| 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) |
| 55 | + assert.NoError(t, err, "Failed to create test config file") |
| 56 | + |
| 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, |
| 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") |
| 103 | + |
| 104 | + // Test case: Config file does not exist |
| 105 | + configPath, exists := ConfigFileExists(config, "non-existent-config.yaml") |
| 106 | + assert.False(t, exists, "Config file should not exist") |
| 107 | + assert.Equal(t, "", configPath, "Config path should be empty for non-existent file") |
| 108 | +} |
0 commit comments