Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,7 @@ func runLizardAnalysis(workDirectory string, pathsToCheck []string, outputFile s
}
} else {
fmt.Println("No configuration file found for Lizard, using default patterns, run init with repository token to get a custom configuration")
patterns, err = tools.FetchDefaultEnabledPatterns(Lizard)
if err != nil {
return fmt.Errorf("failed to fetch default patterns: %v", err)
}
patterns = []domain.PatternDefinition{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this change. On the analysis standpoint, the tool should just rely on the patterns file and that is that.

I would leave this as it was or update this tool to actually use the tools.ConfigFileExists( as the other tools use. As this at most should be a failsafe anyway.

tools.ConfigFileExists check for configuration file both generated from the API during init, or if it exists on the root of the repo. So if we initialize from the UI with no patterns, we should have a file created with no rules, and don't even hit this path

}

return lizard.RunLizard(workDirectory, lizardBinary, pathsToCheck, outputFile, outputFormat, patterns)
Expand Down
15 changes: 3 additions & 12 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,19 +541,10 @@ func cleanConfigDirectory(toolsConfigDir string) error {
}

func createLizardConfigFile(toolsConfigDir string, patternConfiguration []domain.PatternConfiguration) error {
var patterns []domain.PatternDefinition

if len(patternConfiguration) == 0 {
var err error
patterns, err = tools.FetchDefaultEnabledPatterns(Lizard)
if err != nil {
return err
}
} else {
patterns = make([]domain.PatternDefinition, len(patternConfiguration))
for i, pattern := range patternConfiguration {
patterns[i] = pattern.PatternDefinition
}
patterns := make([]domain.PatternDefinition, len(patternConfiguration))
for i, pattern := range patternConfiguration {
patterns[i] = pattern.PatternDefinition

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are a bit strange for me, if we check the ifs for other tools, there are things like

	case DartAnalyzer:
		if len(patternConfiguration) > 0 {
			err := createDartAnalyzerConfigFile(patternConfiguration, toolsConfigDir)
			if err != nil {
				return fmt.Errorf("failed to create Dart Analyzer config: %v", err)
			}
			fmt.Println("Dart configuration created based on Codacy settings")
		}
	case Semgrep:
		if len(patternConfiguration) > 0 {
			err := createSemgrepConfigFile(patternConfiguration, toolsConfigDir)
			if err != nil {
				return fmt.Errorf("failed to create Semgrep config: %v", err)
			}
			fmt.Println("Semgrep configuration created based on Codacy settings")
		}
	case Lizard:
		createLizardConfigFile(toolsConfigDir, patternConfiguration)
	}

But for Lizard you only call this method, but then you remove the if?

}

Expand Down
27 changes: 27 additions & 0 deletions cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"codacy/cli-v2/config"
"codacy/cli-v2/domain"
"codacy/cli-v2/tools"
"codacy/cli-v2/utils"
"os"
Expand Down Expand Up @@ -244,3 +245,29 @@ func TestInitCommand_NoToken(t *testing.T) {
assert.NoError(t, err, "Expected config file %s to be created", file)
}
}

func TestCreateLizardConfigFile_EmptyAndNonEmpty(t *testing.T) {
tempDir := t.TempDir()

// Case 1: Empty patternConfiguration
err := createLizardConfigFile(tempDir, []domain.PatternConfiguration{})
assert.NoError(t, err, "Should not error with empty patternConfiguration")
lizardFile := filepath.Join(tempDir, "lizard.yaml")
_, err = os.Stat(lizardFile)
assert.NoError(t, err, "lizard.yaml should be created for empty input")

// Case 2: Non-empty patternConfiguration
patterns := []domain.PatternConfiguration{
{
PatternDefinition: domain.PatternDefinition{
Id: "Lizard_1",
Enabled: true,
},
},
}
err = createLizardConfigFile(tempDir, patterns)
assert.NoError(t, err, "Should not error with non-empty patternConfiguration")
content, err := os.ReadFile(lizardFile)
assert.NoError(t, err, "Should be able to read lizard.yaml")
assert.NotEmpty(t, content, "lizard.yaml should not be empty for non-empty input")
}
Loading