|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "codacy/cli-v2/config" |
| 10 | + "codacy/cli-v2/domain" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +// TestCheckIfConfigExistsAndIsNeededBehavior tests the behavior of checkIfConfigExistsAndIsNeeded |
| 17 | +// without creating actual config files in the project directory |
| 18 | +func TestCheckIfConfigExistsAndIsNeededBehavior(t *testing.T) { |
| 19 | + // Save original state |
| 20 | + originalFlags := initFlags |
| 21 | + originalConfig := config.Config |
| 22 | + originalWorkingDir, _ := os.Getwd() |
| 23 | + defer func() { |
| 24 | + initFlags = originalFlags |
| 25 | + config.Config = originalConfig |
| 26 | + os.Chdir(originalWorkingDir) |
| 27 | + }() |
| 28 | + |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + toolName string |
| 32 | + cliLocalMode bool |
| 33 | + apiToken string |
| 34 | + description string |
| 35 | + expectNoError bool |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "tool_without_config_file", |
| 39 | + toolName: "unsupported-tool", |
| 40 | + cliLocalMode: false, |
| 41 | + apiToken: "test-token", |
| 42 | + description: "Tool that doesn't use config files should return without error", |
| 43 | + expectNoError: true, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "eslint_local_mode", |
| 47 | + toolName: "eslint", |
| 48 | + cliLocalMode: true, |
| 49 | + apiToken: "", |
| 50 | + description: "ESLint in local mode should not error", |
| 51 | + expectNoError: true, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "eslint_remote_mode_without_token", |
| 55 | + toolName: "eslint", |
| 56 | + cliLocalMode: false, |
| 57 | + apiToken: "", |
| 58 | + description: "ESLint in remote mode without token should not error", |
| 59 | + expectNoError: true, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "trivy_local_mode", |
| 63 | + toolName: "trivy", |
| 64 | + cliLocalMode: true, |
| 65 | + apiToken: "", |
| 66 | + description: "Trivy in local mode should not error", |
| 67 | + expectNoError: true, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + for _, tt := range tests { |
| 72 | + t.Run(tt.name, func(t *testing.T) { |
| 73 | + // Create temporary directory and change to it to avoid creating files in project dir |
| 74 | + tmpDir, err := os.MkdirTemp("", "codacy-test-isolated-*") |
| 75 | + require.NoError(t, err) |
| 76 | + defer os.RemoveAll(tmpDir) |
| 77 | + |
| 78 | + // Change to temp directory to avoid creating config files in project |
| 79 | + err = os.Chdir(tmpDir) |
| 80 | + require.NoError(t, err) |
| 81 | + |
| 82 | + // Setup initFlags |
| 83 | + initFlags = domain.InitFlags{ |
| 84 | + ApiToken: tt.apiToken, |
| 85 | + } |
| 86 | + |
| 87 | + // Mock config to use our temporary directory |
| 88 | + config.Config = *config.NewConfigType(tmpDir, tmpDir, tmpDir) |
| 89 | + |
| 90 | + // Execute the function - this tests it doesn't panic or return unexpected errors |
| 91 | + err = checkIfConfigExistsAndIsNeeded(tt.toolName, tt.cliLocalMode) |
| 92 | + |
| 93 | + // Verify results |
| 94 | + if tt.expectNoError { |
| 95 | + assert.NoError(t, err, "Function should not return error: %s", tt.description) |
| 96 | + } else { |
| 97 | + assert.Error(t, err, "Function should return error: %s", tt.description) |
| 98 | + } |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// TestToolConfigFileNameMapCompleteness ensures all expected tools have config mappings |
| 104 | +func TestToolConfigFileNameMapCompleteness(t *testing.T) { |
| 105 | + expectedTools := map[string]string{ |
| 106 | + "eslint": "eslint.config.mjs", |
| 107 | + "trivy": "trivy.yaml", |
| 108 | + "pmd": "ruleset.xml", |
| 109 | + "pylint": "pylint.rc", |
| 110 | + "dartanalyzer": "analysis_options.yaml", |
| 111 | + "semgrep": "semgrep.yaml", |
| 112 | + "revive": "revive.toml", |
| 113 | + "lizard": "lizard.yaml", |
| 114 | + } |
| 115 | + |
| 116 | + t.Run("all_expected_tools_present", func(t *testing.T) { |
| 117 | + for toolName, expectedFileName := range expectedTools { |
| 118 | + actualFileName, exists := toolConfigFileName[toolName] |
| 119 | + assert.True(t, exists, "Tool %s should exist in toolConfigFileName map", toolName) |
| 120 | + assert.Equal(t, expectedFileName, actualFileName, "Config filename for %s should match expected", toolName) |
| 121 | + } |
| 122 | + }) |
| 123 | + |
| 124 | + t.Run("no_unexpected_tools", func(t *testing.T) { |
| 125 | + for toolName := range toolConfigFileName { |
| 126 | + _, expected := expectedTools[toolName] |
| 127 | + assert.True(t, expected, "Unexpected tool %s found in toolConfigFileName map", toolName) |
| 128 | + } |
| 129 | + }) |
| 130 | + |
| 131 | + t.Run("config_files_have_proper_extensions", func(t *testing.T) { |
| 132 | + validExtensions := map[string]bool{ |
| 133 | + ".mjs": true, |
| 134 | + ".js": true, |
| 135 | + ".yaml": true, |
| 136 | + ".yml": true, |
| 137 | + ".xml": true, |
| 138 | + ".rc": true, |
| 139 | + ".toml": true, |
| 140 | + } |
| 141 | + |
| 142 | + for toolName, fileName := range toolConfigFileName { |
| 143 | + ext := filepath.Ext(fileName) |
| 144 | + assert.True(t, validExtensions[ext], "Tool %s has config file %s with unexpected extension %s", toolName, fileName, ext) |
| 145 | + } |
| 146 | + }) |
| 147 | +} |
| 148 | + |
| 149 | +// TestCheckIfConfigExistsAndIsNeededEdgeCases tests edge cases and error conditions |
| 150 | +func TestCheckIfConfigExistsAndIsNeededEdgeCases(t *testing.T) { |
| 151 | + originalFlags := initFlags |
| 152 | + originalConfig := config.Config |
| 153 | + originalWorkingDir, _ := os.Getwd() |
| 154 | + defer func() { |
| 155 | + initFlags = originalFlags |
| 156 | + config.Config = originalConfig |
| 157 | + os.Chdir(originalWorkingDir) |
| 158 | + }() |
| 159 | + |
| 160 | + // Create temporary directory for edge case tests |
| 161 | + tmpDir, err := os.MkdirTemp("", "codacy-test-edge-*") |
| 162 | + require.NoError(t, err) |
| 163 | + defer os.RemoveAll(tmpDir) |
| 164 | + |
| 165 | + // Change to temp directory |
| 166 | + err = os.Chdir(tmpDir) |
| 167 | + require.NoError(t, err) |
| 168 | + |
| 169 | + // Mock config |
| 170 | + config.Config = *config.NewConfigType(tmpDir, tmpDir, tmpDir) |
| 171 | + |
| 172 | + t.Run("empty_tool_name", func(t *testing.T) { |
| 173 | + err := checkIfConfigExistsAndIsNeeded("", false) |
| 174 | + assert.NoError(t, err, "Empty tool name should not cause error") |
| 175 | + }) |
| 176 | + |
| 177 | + t.Run("tool_name_with_special_characters", func(t *testing.T) { |
| 178 | + err := checkIfConfigExistsAndIsNeeded("tool-with-dashes_and_underscores", false) |
| 179 | + assert.NoError(t, err, "Tool name with special characters should not cause error") |
| 180 | + }) |
| 181 | + |
| 182 | + t.Run("very_long_tool_name", func(t *testing.T) { |
| 183 | + longToolName := strings.Repeat("verylongtoolname", 10) |
| 184 | + err := checkIfConfigExistsAndIsNeeded(longToolName, false) |
| 185 | + assert.NoError(t, err, "Very long tool name should not cause error") |
| 186 | + }) |
| 187 | +} |
0 commit comments