-
Notifications
You must be signed in to change notification settings - Fork 10
fix: Only pass configuration file if created, otherwise fallback to native tool PLUTO-1383 #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,8 @@ | |
| ) | ||
|
|
||
| type ConfigType struct { | ||
| homePath string | ||
| repositoryDirectory string | ||
|
|
||
| globalCacheDirectory string | ||
| runtimesDirectory string | ||
| toolsDirectory string | ||
|
|
@@ -24,8 +25,8 @@ | |
| tools map[string]*plugins.ToolInfo | ||
| } | ||
|
|
||
| func (c *ConfigType) HomePath() string { | ||
| return c.homePath | ||
| func (c *ConfigType) RepositoryDirectory() string { | ||
| return c.repositoryDirectory | ||
| } | ||
|
|
||
| func (c *ConfigType) CodacyDirectory() string { | ||
|
|
@@ -98,15 +99,42 @@ | |
| return c.toolsConfigDirectory | ||
| } | ||
|
|
||
| func (c *ConfigType) setupCodacyPaths() { | ||
| c.globalCacheDirectory = filepath.Join(c.homePath, ".cache", "codacy") | ||
| func NewConfigType(repositoryDirectory string, repositoryCache string, globalCache string) *ConfigType { | ||
| c := &ConfigType{} | ||
|
|
||
| c.repositoryDirectory = repositoryDirectory | ||
| c.localCodacyDirectory = repositoryCache | ||
| c.globalCacheDirectory = globalCache | ||
|
|
||
| c.runtimesDirectory = filepath.Join(c.globalCacheDirectory, "runtimes") | ||
| c.toolsDirectory = filepath.Join(c.globalCacheDirectory, "tools") | ||
| c.localCodacyDirectory = ".codacy" | ||
| c.toolsConfigDirectory = filepath.Join(c.localCodacyDirectory, "tools-configs") | ||
|
|
||
| c.projectConfigFile = filepath.Join(c.localCodacyDirectory, "codacy.yaml") | ||
| c.cliConfigFile = filepath.Join(c.localCodacyDirectory, "cli-config.yaml") | ||
|
|
||
| c.runtimes = make(map[string]*plugins.RuntimeInfo) | ||
| c.tools = make(map[string]*plugins.ToolInfo) | ||
| return c | ||
| } | ||
|
|
||
| // TODO: Consider not having a global config and instead pass the config object around | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now, let's keep it, but it is now easier to remove, as it only depends at the NewConfigType method to generate the values for the config |
||
| func setupGlobalConfig(repositoryDirectory string, repositoryCache string, globalCache string) { | ||
| newConfig := NewConfigType(repositoryDirectory, repositoryCache, globalCache) | ||
|
|
||
| Config.repositoryDirectory = newConfig.repositoryDirectory | ||
| Config.localCodacyDirectory = newConfig.localCodacyDirectory | ||
| Config.globalCacheDirectory = newConfig.globalCacheDirectory | ||
|
|
||
| Config.runtimesDirectory = newConfig.runtimesDirectory | ||
| Config.toolsDirectory = newConfig.toolsDirectory | ||
| Config.toolsConfigDirectory = newConfig.toolsConfigDirectory | ||
|
|
||
| Config.projectConfigFile = newConfig.projectConfigFile | ||
| Config.cliConfigFile = newConfig.cliConfigFile | ||
|
|
||
| Config.runtimes = newConfig.runtimes | ||
| Config.tools = newConfig.tools | ||
| } | ||
|
|
||
| func (c *ConfigType) CreateCodacyDirs() error { | ||
|
|
@@ -136,11 +164,12 @@ | |
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| Config.homePath = homePath | ||
| // Repository directory is the current working directory | ||
| repositoryDirectory := "" | ||
| repositoryCache := ".codacy" | ||
| globalCache := filepath.Join(homePath, ".cache", "codacy") | ||
|
|
||
| Config.setupCodacyPaths() | ||
| Config.runtimes = make(map[string]*plugins.RuntimeInfo) | ||
| Config.tools = make(map[string]*plugins.ToolInfo) | ||
| setupGlobalConfig(repositoryDirectory, repositoryCache, globalCache) | ||
| } | ||
|
|
||
| // IsRuntimeInstalled checks if a runtime is already installed | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package tools | ||
|
|
||
| import ( | ||
| "codacy/cli-v2/config" | ||
| "encoding/json" | ||
| "log" | ||
| "os" | ||
|
|
@@ -51,19 +52,24 @@ func TestRunPmdToFile(t *testing.T) { | |
|
|
||
| // Use the correct path relative to tools directory | ||
| testDirectory := filepath.Join(currentDirectory, "testdata", "repositories", "pmd") | ||
| repositoryCache := filepath.Join(testDirectory, ".codacy") | ||
| globalCache := filepath.Join(homeDirectory, ".cache/codacy") | ||
|
|
||
| tempResultFile := filepath.Join(os.TempDir(), "pmd.sarif") | ||
| defer os.Remove(tempResultFile) | ||
|
|
||
| config := *config.NewConfigType(testDirectory, repositoryCache, globalCache) | ||
|
|
||
| // Use absolute paths | ||
| repositoryToAnalyze := testDirectory | ||
| // Use the standard ruleset file for testing the PMD runner functionality | ||
| rulesetFile := filepath.Join(testDirectory, "pmd-ruleset.xml") | ||
| //rulesetFile := filepath.Join(testDirectory, "ruleset.xml") | ||
|
|
||
| // Use the same path as defined in plugin.yaml | ||
| pmdBinary := filepath.Join(homeDirectory, ".cache/codacy/tools/[email protected]/pmd-bin-6.55.0/bin/run.sh") | ||
| pmdBinary := filepath.Join(globalCache, "tools/[email protected]/pmd-bin-6.55.0/bin/run.sh") | ||
|
|
||
| // Run PMD | ||
| err = RunPmd(repositoryToAnalyze, pmdBinary, nil, tempResultFile, "sarif", rulesetFile) | ||
| err = RunPmd(repositoryToAnalyze, pmdBinary, nil, tempResultFile, "sarif", config) | ||
| if err != nil { | ||
| t.Fatalf("Failed to run pmd: %v", err) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package tools | ||
|
|
||
| import ( | ||
| "codacy/cli-v2/config" | ||
| "os" | ||
| "path/filepath" | ||
| ) | ||
|
|
||
| // ConfigFileExists checks if a specific configuration file exists in the .codacy/tools-configs/ | ||
| // or on the root of the repository directory. | ||
| // | ||
| // Parameters: | ||
| // - conf: The configuration object containing the tools config directory | ||
| // - fileName: The configuration file name to check for | ||
| // | ||
| // Returns: | ||
| // - string: The relative path to the configuration file (for cmd args) | ||
| // - bool: True if the file exists, false otherwise | ||
| func ConfigFileExists(conf config.ConfigType, fileName string) (string, bool) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Each tool runner should now depend on this when generating its command to pass a configuration file |
||
| generatedConfigFile := filepath.Join(conf.ToolsConfigDirectory(), fileName) | ||
| existingConfigFile := filepath.Join(conf.RepositoryDirectory(), fileName) | ||
|
|
||
| if _, err := os.Stat(generatedConfigFile); err == nil { | ||
| return generatedConfigFile, true | ||
| } else if _, err := os.Stat(existingConfigFile); err == nil { | ||
| return existingConfigFile, true | ||
| } | ||
|
|
||
| return "", false | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| package tools | ||
|
|
||
| import ( | ||
| "codacy/cli-v2/config" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestConfigFileExistsInToolsConfigDirectory(t *testing.T) { | ||
| // Create a test directory structure | ||
| tempDir := t.TempDir() | ||
| repoDir := filepath.Join(tempDir, "src") | ||
| repositoryCache := filepath.Join(repoDir, ".codacy") | ||
|
|
||
| // Create configuration | ||
| config := *config.NewConfigType(repoDir, repositoryCache, "unused-global-cache") | ||
|
|
||
| // Create .codacy/tools-configs directory | ||
| configDir := filepath.Join(repoDir, ".codacy", "tools-configs") | ||
| err := os.MkdirAll(configDir, 0755) | ||
|
Check warning on line 23 in tools/runnerUtils_test.go
|
||
| assert.NoError(t, err, "Failed to create test directory structure") | ||
|
|
||
| // Create a test config file on the configDir | ||
| generatedConfigFile := filepath.Join(configDir, "generated-config.yaml") | ||
| err = os.WriteFile(generatedConfigFile, []byte("test content"), 0644) | ||
| assert.NoError(t, err, "Failed to create test config file") | ||
|
|
||
| // Test case: Config file exists in tools config directory | ||
| configPath, exists := ConfigFileExists(config, "generated-config.yaml") | ||
| assert.True(t, exists, "Config file should exist in tools config directory") | ||
| assert.Equal(t, filepath.Join(config.ToolsConfigDirectory(), "generated-config.yaml"), configPath, | ||
| "Config path should be correctly formed relative path") | ||
| } | ||
|
|
||
| func TestConfigFileExistsInRepositoryDirectory(t *testing.T) { | ||
| // Create a test directory structure | ||
| tempDir := t.TempDir() | ||
| repoDir := filepath.Join(tempDir, "src") | ||
| repositoryCache := filepath.Join(repoDir, ".codacy") | ||
|
|
||
| // Create configuration | ||
| config := *config.NewConfigType(repoDir, repositoryCache, "unused-global-cache") | ||
|
|
||
| // Create .codacy/tools-configs directory | ||
| configDir := filepath.Join(repoDir, ".codacy", "tools-configs") | ||
| err := os.MkdirAll(configDir, 0755) | ||
| assert.NoError(t, err, "Failed to create test directory structure") | ||
|
|
||
| // Create a test config file on the repository directory | ||
| existingConfigFile := filepath.Join(repoDir, "existing-config.yaml") | ||
| err = os.WriteFile(existingConfigFile, []byte("test content"), 0644) | ||
| assert.NoError(t, err, "Failed to create test config file") | ||
|
|
||
| // Test case: The existing config file gets picked up | ||
| configPath, exists := ConfigFileExists(config, "existing-config.yaml") | ||
| assert.True(t, exists, "Config file should exist in tools config directory") | ||
| assert.Equal(t, filepath.Join(config.RepositoryDirectory(), "existing-config.yaml"), configPath, | ||
| "Config path should be correctly formed relative path") | ||
| } | ||
|
|
||
| func TestConfigFilePrefersToolsConfigDirectory(t *testing.T) { | ||
| // Create a test directory structure | ||
| tempDir := t.TempDir() | ||
| repoDir := filepath.Join(tempDir, "src") | ||
| repositoryCache := filepath.Join(repoDir, ".codacy") | ||
|
|
||
| // Create configuration | ||
| config := *config.NewConfigType(repoDir, repositoryCache, "unused-global-cache") | ||
|
|
||
| // Create .codacy/tools-configs directory | ||
| configDir := filepath.Join(repoDir, ".codacy", "tools-configs") | ||
| err := os.MkdirAll(configDir, 0755) | ||
| assert.NoError(t, err, "Failed to create test directory structure") | ||
|
|
||
| // Create a test config file in both locations | ||
| generatedConfigFile := filepath.Join(configDir, "some-config.yaml") | ||
| existingConfigFile := filepath.Join(repoDir, "some-config.yaml") | ||
|
|
||
| err = os.WriteFile(generatedConfigFile, []byte("tools config content"), 0644) | ||
| assert.NoError(t, err, "Failed to create test config file in tools config directory") | ||
|
|
||
| err = os.WriteFile(existingConfigFile, []byte("repository config content"), 0644) | ||
| assert.NoError(t, err, "Failed to create test config file in repository directory") | ||
|
|
||
| // Test case: Config file in tools config directory is preferred | ||
| configPath, exists := ConfigFileExists(config, "some-config.yaml") | ||
| assert.True(t, exists, "Config file should exist") | ||
| assert.Equal(t, filepath.Join(config.ToolsConfigDirectory(), "some-config.yaml"), configPath, | ||
| "Config path should prefer tools config directory") | ||
| } | ||
|
|
||
| func TestConfigFileDoesNotExist(t *testing.T) { | ||
| // Create a test directory structure | ||
| tempDir := t.TempDir() | ||
| repoDir := filepath.Join(tempDir, "src") | ||
| repositoryCache := filepath.Join(repoDir, ".codacy") | ||
|
|
||
| // Create configuration | ||
| config := *config.NewConfigType(repoDir, repositoryCache, "unused-global-cache") | ||
|
|
||
| // Test case: Config file does not exist | ||
| configPath, exists := ConfigFileExists(config, "non-existent-config.yaml") | ||
| assert.False(t, exists, "Config file should not exist") | ||
| assert.Equal(t, "", configPath, "Config path should be empty for non-existent file") | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created a builder, so it can used both on the Global initialization and tests