Skip to content
Merged
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
2 changes: 1 addition & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func runConfigResetLogic(cmd *cobra.Command, args []string, flags domain.InitFla
fmt.Println()
fmt.Println("ℹ️ Resetting to local default configurations.")
noTools := []domain.Tool{} // Empty slice for tools as we are in local mode without specific toolset from API initially
if err := configsetup.CreateConfigurationFiles(noTools, cliLocalMode); err != nil {
if err := configsetup.CreateConfigurationFiles(noTools, cliLocalMode, flags); err != nil {
log.Fatalf("Failed to create base configuration files: %v", err)
}
// Create default configuration files for tools
Expand Down
14 changes: 7 additions & 7 deletions cmd/configsetup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,15 @@ func CreateGitIgnoreFile() error {
return writeConfigFile(gitIgnorePath, []byte(content))
}

func CreateConfigurationFiles(tools []domain.Tool, cliLocalMode bool) error {
func CreateConfigurationFiles(tools []domain.Tool, cliLocalMode bool, flags domain.InitFlags) error {
// Create project config file
configContent := ConfigFileTemplate(tools)
if err := writeConfigFile(config.Config.ProjectConfigFile(), []byte(configContent)); err != nil {
return fmt.Errorf("failed to write project config file: %w", err)
}

// Create CLI config file
cliConfigContent := buildCliConfigContent(cliLocalMode)
cliConfigContent := buildCliConfigContent(cliLocalMode, flags)
if err := writeConfigFile(config.Config.CliConfigFile(), []byte(cliConfigContent)); err != nil {
return fmt.Errorf("failed to write CLI config file: %w", err)
}
Expand All @@ -229,12 +229,12 @@ func CreateConfigurationFiles(tools []domain.Tool, cliLocalMode bool) error {
}

// buildCliConfigContent creates the CLI configuration content
func buildCliConfigContent(cliLocalMode bool) string {
mode := "remote"
func buildCliConfigContent(cliLocalMode bool, initFlags domain.InitFlags) string {
if cliLocalMode {
mode = "local"
return fmt.Sprintf("mode: local")
} else {
return fmt.Sprintf("mode: remote\nprovider: %s\norganization: %s\nrepository: %s", initFlags.Provider, initFlags.Organization, initFlags.Repository)
}
return fmt.Sprintf("mode: %s", mode)
}

// RuntimePluginConfig holds the structure of the runtime plugin.yaml file
Expand Down Expand Up @@ -425,7 +425,7 @@ func BuildRepositoryConfigurationFiles(flags domain.InitFlags) error {
logVersionConflicts(familyToVersions, toolsWithLatestVersion)

// Create main config files with all enabled API tools (including cli-config.yaml)
if err := CreateConfigurationFiles(toolsWithLatestVersion, false); err != nil {
if err := CreateConfigurationFiles(toolsWithLatestVersion, false, flags); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var initCmd = &cobra.Command{
fmt.Println()
fmt.Println("ℹ️ No project token was specified, fetching codacy default configurations")
noTools := []domain.Tool{}
err := configsetup.CreateConfigurationFiles(noTools, cliLocalMode)
err := configsetup.CreateConfigurationFiles(noTools, cliLocalMode, initFlags)
if err != nil {
log.Fatal(err)
}
Expand Down
75 changes: 0 additions & 75 deletions cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"codacy/cli-v2/cmd/configsetup"
"codacy/cli-v2/config"
"codacy/cli-v2/constants"
"codacy/cli-v2/domain"
"os"
Expand Down Expand Up @@ -182,77 +181,3 @@ func TestCleanConfigDirectory(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 0, len(files), "Expected 0 files after cleaning, got %d", len(files))
}

func TestInitCommand_NoToken(t *testing.T) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removing these tests, they are always being skipped. Integration tests cover these scenarios.

tempDir := t.TempDir()
originalWD, err := os.Getwd()
assert.NoError(t, err, "Failed to get current working directory")
defer os.Chdir(originalWD)

// Use the real plugins/tools/semgrep/rules.yaml file
rulesPath := filepath.Join("..", "plugins", "tools", "semgrep", "rules.yaml")
if _, err := os.Stat(rulesPath); os.IsNotExist(err) {
t.Skipf("plugins/tools/semgrep/rules.yaml not found at %s; skipping test", rulesPath)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Line that made the tests skip always.

}

// Change to the temp directory to simulate a new project
err = os.Chdir(tempDir)
assert.NoError(t, err, "Failed to change working directory to tempDir")

// Simulate running init with no token
currentInitFlags := domain.InitFlags{
ApiToken: "",
Provider: "",
Organization: "",
Repository: "",
}

// Call the Run logic from initCmd
if err := config.Config.CreateLocalCodacyDir(); err != nil {
t.Fatalf("Failed to create local codacy directory: %v", err)
}

toolsConfigDir := config.Config.ToolsConfigDirectory()
if err := os.MkdirAll(toolsConfigDir, constants.DefaultDirPerms); err != nil {
t.Fatalf("Failed to create tools-configs directory: %v", err)
}

cliLocalMode := len(currentInitFlags.ApiToken) == 0
if cliLocalMode {
noTools := []domain.Tool{}
err := configsetup.CreateConfigurationFiles(noTools, cliLocalMode)
assert.NoError(t, err, "CreateConfigurationFiles should not return an error")
if err := configsetup.BuildDefaultConfigurationFiles(toolsConfigDir, currentInitFlags); err != nil {
t.Fatalf("Failed to build default configuration files: %v", err)
}
if err := configsetup.CreateLanguagesConfigFileLocal(toolsConfigDir); err != nil {
t.Fatalf("Failed to create languages config file: %v", err)
}
}

// Assert that the expected config files are created
codacyDir := config.Config.LocalCodacyDirectory()
expectedFiles := []string{
filepath.Join("tools-configs", "eslint.config.mjs"),
filepath.Join("tools-configs", "trivy.yaml"),
filepath.Join("tools-configs", "ruleset.xml"),
filepath.Join("tools-configs", "pylint.rc"),
filepath.Join("tools-configs", "analysis_options.yaml"),
filepath.Join("tools-configs", "semgrep.yaml"),
filepath.Join("tools-configs", "lizard.yaml"),
"codacy.yaml",
"cli-config.yaml",
filepath.Join("tools-configs", "languages-config.yaml"),
".gitignore",
}

for _, file := range expectedFiles {
filePath := filepath.Join(codacyDir, file)
if file == ".gitignore" {
filePath = filepath.Join(config.Config.LocalCodacyDirectory(), file)
}

_, err := os.Stat(filePath)
assert.NoError(t, err, "Expected config file %s to be created at %s", file, filePath)
}
}
5 changes: 4 additions & 1 deletion integration-tests/init-with-token/expected/cli-config.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
mode: remote
mode: remote
provider: gh
organization: troubleshoot-codacy-dev
repository: codacy-cli-test
Loading