Skip to content

Commit 7772b2b

Browse files
committed
test(cmd): add unit tests for editorconfig validation logic
Add comprehensive unit tests for the editorconfig validation command, including dry-run logic, version checking, and integration with atmos configuration. Tests cover config path parsing, flag overrides, and various config scenarios to ensure robust behavior.
1 parent f032467 commit 7772b2b

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed

cmd/validate_editorconfig_test.go

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package cmd
33
import (
44
"testing"
55

6+
"github.com/editorconfig-checker/editorconfig-checker/v3/pkg/config"
67
"github.com/spf13/cobra"
78
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
10+
11+
"github.com/cloudposse/atmos/pkg/schema"
912
)
1013

1114
// TestParseConfigPaths tests the pure parseConfigPaths function.
@@ -70,3 +73,213 @@ func TestInitConfig(t *testing.T) {
7073
// Call function with no assertions - test passes if no panic occurs.
7174
initializeConfig(editorConfigCmd)
7275
}
76+
77+
// TestRunMainLogicDryRun tests the dry-run path in runMainLogic.
78+
func TestRunMainLogicDryRun(t *testing.T) {
79+
// Save original state.
80+
originalConfig := currentConfig
81+
originalCliConfig := cliConfig
82+
defer func() {
83+
currentConfig = originalConfig
84+
cliConfig = originalCliConfig
85+
}()
86+
87+
// Create a minimal config for dry-run mode.
88+
cfg := config.NewConfig([]string{})
89+
cfg.DryRun = true
90+
currentConfig = cfg
91+
cliConfig = config.Config{DryRun: true}
92+
93+
// This should not panic and should list files (if any match).
94+
// In dry-run mode, runMainLogic just lists files without validation.
95+
runMainLogic()
96+
}
97+
98+
// TestCheckVersion tests the version checking logic.
99+
func TestCheckVersion(t *testing.T) {
100+
tests := []struct {
101+
name string
102+
config config.Config
103+
expectError bool
104+
}{
105+
{
106+
name: "no config file exists",
107+
config: config.Config{
108+
Path: "/nonexistent/path",
109+
Version: "",
110+
},
111+
expectError: false,
112+
},
113+
{
114+
name: "empty version in config",
115+
config: config.Config{
116+
Path: ".",
117+
Version: "",
118+
},
119+
expectError: false,
120+
},
121+
{
122+
name: "version mismatch",
123+
config: config.Config{
124+
Path: ".", // Current directory exists
125+
Version: "999.999.999",
126+
},
127+
expectError: true,
128+
},
129+
}
130+
131+
for _, tt := range tests {
132+
t.Run(tt.name, func(t *testing.T) {
133+
err := checkVersion(tt.config)
134+
if tt.expectError {
135+
assert.Error(t, err)
136+
} else {
137+
assert.NoError(t, err)
138+
}
139+
})
140+
}
141+
}
142+
143+
// TestReplaceAtmosConfigInConfig tests that atmos config values are properly applied.
144+
func TestReplaceAtmosConfigInConfig(t *testing.T) {
145+
// Reset module-level variables before test.
146+
originalConfigFilePaths := configFilePaths
147+
originalTmpExclude := tmpExclude
148+
originalInitEditorConfig := initEditorConfig
149+
originalCliConfig := cliConfig
150+
defer func() {
151+
configFilePaths = originalConfigFilePaths
152+
tmpExclude = originalTmpExclude
153+
initEditorConfig = originalInitEditorConfig
154+
cliConfig = originalCliConfig
155+
}()
156+
157+
tests := []struct {
158+
name string
159+
atmosConfig schema.AtmosConfiguration
160+
flagChanged map[string]bool
161+
setup func(*cobra.Command)
162+
validate func(t *testing.T)
163+
}{
164+
{
165+
name: "applies config file paths from atmos config",
166+
atmosConfig: schema.AtmosConfiguration{
167+
Validate: schema.Validate{
168+
EditorConfig: schema.EditorConfig{
169+
ConfigFilePaths: []string{".custom-editorconfig"},
170+
},
171+
},
172+
},
173+
validate: func(t *testing.T) {
174+
assert.Equal(t, []string{".custom-editorconfig"}, configFilePaths)
175+
},
176+
},
177+
{
178+
name: "applies exclude patterns from atmos config",
179+
atmosConfig: schema.AtmosConfiguration{
180+
Validate: schema.Validate{
181+
EditorConfig: schema.EditorConfig{
182+
Exclude: []string{"vendor/**", "node_modules/**"},
183+
},
184+
},
185+
},
186+
validate: func(t *testing.T) {
187+
assert.Equal(t, "vendor/**,node_modules/**", tmpExclude)
188+
},
189+
},
190+
{
191+
name: "applies init flag from atmos config",
192+
atmosConfig: schema.AtmosConfiguration{
193+
Validate: schema.Validate{
194+
EditorConfig: schema.EditorConfig{
195+
Init: true,
196+
},
197+
},
198+
},
199+
validate: func(t *testing.T) {
200+
assert.True(t, initEditorConfig)
201+
},
202+
},
203+
{
204+
name: "applies ignore defaults from atmos config",
205+
atmosConfig: schema.AtmosConfiguration{
206+
Validate: schema.Validate{
207+
EditorConfig: schema.EditorConfig{
208+
IgnoreDefaults: true,
209+
},
210+
},
211+
},
212+
validate: func(t *testing.T) {
213+
assert.True(t, cliConfig.IgnoreDefaults)
214+
},
215+
},
216+
{
217+
name: "applies dry run from atmos config",
218+
atmosConfig: schema.AtmosConfiguration{
219+
Validate: schema.Validate{
220+
EditorConfig: schema.EditorConfig{
221+
DryRun: true,
222+
},
223+
},
224+
},
225+
validate: func(t *testing.T) {
226+
assert.True(t, cliConfig.DryRun)
227+
},
228+
},
229+
{
230+
name: "applies disable flags from atmos config",
231+
atmosConfig: schema.AtmosConfiguration{
232+
Validate: schema.Validate{
233+
EditorConfig: schema.EditorConfig{
234+
DisableTrimTrailingWhitespace: true,
235+
DisableEndOfLine: true,
236+
DisableInsertFinalNewline: true,
237+
DisableIndentation: true,
238+
DisableIndentSize: true,
239+
DisableMaxLineLength: true,
240+
},
241+
},
242+
},
243+
validate: func(t *testing.T) {
244+
assert.True(t, cliConfig.Disable.TrimTrailingWhitespace)
245+
assert.True(t, cliConfig.Disable.EndOfLine)
246+
assert.True(t, cliConfig.Disable.InsertFinalNewline)
247+
assert.True(t, cliConfig.Disable.Indentation)
248+
assert.True(t, cliConfig.Disable.IndentSize)
249+
assert.True(t, cliConfig.Disable.MaxLineLength)
250+
},
251+
},
252+
{
253+
name: "applies no color from atmos config",
254+
atmosConfig: schema.AtmosConfiguration{
255+
Settings: schema.AtmosSettings{
256+
Terminal: schema.Terminal{
257+
NoColor: true,
258+
},
259+
},
260+
},
261+
validate: func(t *testing.T) {
262+
assert.True(t, cliConfig.NoColor)
263+
},
264+
},
265+
}
266+
267+
for _, tt := range tests {
268+
t.Run(tt.name, func(t *testing.T) {
269+
// Reset state.
270+
configFilePaths = nil
271+
tmpExclude = ""
272+
initEditorConfig = false
273+
274+
cmd := &cobra.Command{}
275+
addPersistentFlags(cmd)
276+
277+
if tt.setup != nil {
278+
tt.setup(cmd)
279+
}
280+
281+
replaceAtmosConfigInConfig(cmd, tt.atmosConfig)
282+
tt.validate(t)
283+
})
284+
}
285+
}

0 commit comments

Comments
 (0)