Skip to content

Commit 2d7b1d8

Browse files
(fix): (go/v4): Fixed: Init command incorrectly rejects directories with non-conflicting configuration files
Assisted-by: Cursor
1 parent 78fbec7 commit 2d7b1d8

File tree

2 files changed

+259
-18
lines changed

2 files changed

+259
-18
lines changed

pkg/plugins/golang/v4/init.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -179,33 +179,25 @@ func checkDir() error {
179179
if strings.HasSuffix(info.Name(), ".md") && !info.IsDir() {
180180
return nil
181181
}
182-
// Allow capitalized files except PROJECT
182+
// Deny PROJECT file (reserved for scaffolding)
183+
if info.Name() == "PROJECT" {
184+
return fmt.Errorf("target directory is not empty and contains the PROJECT file. " +
185+
"This file is reserved and will be generated by the scaffolding tool")
186+
}
187+
// Allow capitalized files
183188
isCapitalized := true
184189
for _, l := range info.Name() {
185190
if !unicode.IsUpper(l) {
186191
isCapitalized = false
187192
break
188193
}
189194
}
190-
if isCapitalized && info.Name() != "PROJECT" {
195+
if isCapitalized {
191196
return nil
192197
}
193-
disallowedExtensions := []string{
194-
".go",
195-
".yaml",
196-
".mod",
197-
".sum",
198-
}
199-
// Deny files with .go or .yaml or .mod or .sum extensions
200-
for _, ext := range disallowedExtensions {
201-
if strings.HasSuffix(info.Name(), ext) {
202-
return nil
203-
}
204-
}
205-
// Do not allow any other file
206-
return fmt.Errorf("target directory is not empty and contains a disallowed file %q. "+
207-
"files with the following extensions [%s] are not allowed to avoid conflicts with the tooling",
208-
path, strings.Join(disallowedExtensions, ", "))
198+
// Allow all other files (including .go, .mod, .sum, .yaml, config files, etc.)
199+
// The directory doesn't need to be completely empty - we just block PROJECT
200+
return nil
209201
})
210202
if err != nil {
211203
return fmt.Errorf("error walking directory: %w", err)

pkg/plugins/golang/v4/init_test.go

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v4
18+
19+
import (
20+
"os"
21+
"path/filepath"
22+
"testing"
23+
24+
. "github.com/onsi/ginkgo/v2"
25+
. "github.com/onsi/gomega"
26+
)
27+
28+
func TestCheckDir(t *testing.T) {
29+
RegisterFailHandler(Fail)
30+
RunSpecs(t, "CheckDir Suite")
31+
}
32+
33+
var _ = Describe("checkDir", func() {
34+
var testDir string
35+
36+
BeforeEach(func() {
37+
var err error
38+
testDir, err = os.MkdirTemp("", "checkdir-test-*")
39+
Expect(err).NotTo(HaveOccurred())
40+
41+
// Change to test directory
42+
err = os.Chdir(testDir)
43+
Expect(err).NotTo(HaveOccurred())
44+
})
45+
46+
AfterEach(func() {
47+
// Clean up test directory
48+
_ = os.RemoveAll(testDir)
49+
})
50+
51+
Context("when the directory is empty", func() {
52+
It("should succeed", func() {
53+
err := checkDir()
54+
Expect(err).NotTo(HaveOccurred())
55+
})
56+
})
57+
58+
Context("when the directory contains allowed files", func() {
59+
It("should succeed with .md files", func() {
60+
err := os.WriteFile("README.md", []byte("# Test"), 0o644)
61+
Expect(err).NotTo(HaveOccurred())
62+
63+
err = checkDir()
64+
Expect(err).NotTo(HaveOccurred())
65+
})
66+
67+
It("should succeed with dot files", func() {
68+
err := os.WriteFile(".gitignore", []byte("*.log"), 0o644)
69+
Expect(err).NotTo(HaveOccurred())
70+
71+
err = checkDir()
72+
Expect(err).NotTo(HaveOccurred())
73+
})
74+
75+
It("should succeed with dot directories", func() {
76+
err := os.Mkdir(".git", 0o755)
77+
Expect(err).NotTo(HaveOccurred())
78+
err = os.WriteFile(".git/config", []byte("[core]"), 0o644)
79+
Expect(err).NotTo(HaveOccurred())
80+
81+
err = checkDir()
82+
Expect(err).NotTo(HaveOccurred())
83+
})
84+
85+
It("should succeed with capitalized files", func() {
86+
err := os.WriteFile("LICENSE", []byte("MIT"), 0o644)
87+
Expect(err).NotTo(HaveOccurred())
88+
89+
err = checkDir()
90+
Expect(err).NotTo(HaveOccurred())
91+
})
92+
93+
It("should succeed with multiple capitalized files", func() {
94+
err := os.WriteFile("LICENSE", []byte("MIT"), 0o644)
95+
Expect(err).NotTo(HaveOccurred())
96+
err = os.WriteFile("CONTRIBUTING", []byte("contribute"), 0o644)
97+
Expect(err).NotTo(HaveOccurred())
98+
99+
err = checkDir()
100+
Expect(err).NotTo(HaveOccurred())
101+
})
102+
103+
It("should succeed with mise.toml file", func() {
104+
err := os.WriteFile("mise.toml", []byte("[tools]"), 0o644)
105+
Expect(err).NotTo(HaveOccurred())
106+
107+
err = checkDir()
108+
Expect(err).NotTo(HaveOccurred())
109+
})
110+
111+
It("should succeed with .tool-versions file", func() {
112+
err := os.WriteFile(".tool-versions", []byte("golang 1.23.0"), 0o644)
113+
Expect(err).NotTo(HaveOccurred())
114+
115+
err = checkDir()
116+
Expect(err).NotTo(HaveOccurred())
117+
})
118+
119+
It("should succeed with .envrc file", func() {
120+
err := os.WriteFile(".envrc", []byte("export FOO=bar"), 0o644)
121+
Expect(err).NotTo(HaveOccurred())
122+
123+
err = checkDir()
124+
Expect(err).NotTo(HaveOccurred())
125+
})
126+
127+
It("should succeed with config.toml file", func() {
128+
err := os.WriteFile("config.toml", []byte("[config]"), 0o644)
129+
Expect(err).NotTo(HaveOccurred())
130+
131+
err = checkDir()
132+
Expect(err).NotTo(HaveOccurred())
133+
})
134+
135+
It("should succeed with settings.json file", func() {
136+
err := os.WriteFile("settings.json", []byte("{}"), 0o644)
137+
Expect(err).NotTo(HaveOccurred())
138+
139+
err = checkDir()
140+
Expect(err).NotTo(HaveOccurred())
141+
})
142+
143+
It("should succeed with .go files", func() {
144+
err := os.WriteFile("main.go", []byte("package main"), 0o644)
145+
Expect(err).NotTo(HaveOccurred())
146+
147+
err = checkDir()
148+
Expect(err).NotTo(HaveOccurred())
149+
})
150+
151+
It("should succeed with .yaml files", func() {
152+
err := os.WriteFile("config.yaml", []byte("key: value"), 0o644)
153+
Expect(err).NotTo(HaveOccurred())
154+
155+
err = checkDir()
156+
Expect(err).NotTo(HaveOccurred())
157+
})
158+
159+
It("should succeed with .mod files", func() {
160+
err := os.WriteFile("go.mod", []byte("module test"), 0o644)
161+
Expect(err).NotTo(HaveOccurred())
162+
163+
err = checkDir()
164+
Expect(err).NotTo(HaveOccurred())
165+
})
166+
167+
It("should succeed with .sum files", func() {
168+
err := os.WriteFile("go.sum", []byte("checksums"), 0o644)
169+
Expect(err).NotTo(HaveOccurred())
170+
171+
err = checkDir()
172+
Expect(err).NotTo(HaveOccurred())
173+
})
174+
175+
It("should succeed with a combination of allowed files", func() {
176+
err := os.WriteFile("README.md", []byte("# Test"), 0o644)
177+
Expect(err).NotTo(HaveOccurred())
178+
err = os.WriteFile(".gitignore", []byte("*.log"), 0o644)
179+
Expect(err).NotTo(HaveOccurred())
180+
err = os.WriteFile("LICENSE", []byte("MIT"), 0o644)
181+
Expect(err).NotTo(HaveOccurred())
182+
err = os.WriteFile("mise.toml", []byte("[tools]"), 0o644)
183+
Expect(err).NotTo(HaveOccurred())
184+
err = os.WriteFile("go.mod", []byte("module test"), 0o644)
185+
Expect(err).NotTo(HaveOccurred())
186+
187+
err = checkDir()
188+
Expect(err).NotTo(HaveOccurred())
189+
})
190+
})
191+
192+
Context("when the directory contains disallowed files", func() {
193+
It("should fail with PROJECT file (reserved for scaffolding)", func() {
194+
err := os.WriteFile("PROJECT", []byte("version: 3"), 0o644)
195+
Expect(err).NotTo(HaveOccurred())
196+
197+
err = checkDir()
198+
Expect(err).To(HaveOccurred())
199+
Expect(err.Error()).To(ContainSubstring("PROJECT"))
200+
})
201+
})
202+
203+
Context("edge cases", func() {
204+
It("should handle files in dot directories correctly", func() {
205+
err := os.Mkdir(".hidden", 0o755)
206+
Expect(err).NotTo(HaveOccurred())
207+
// Files in dot directories should be skipped entirely
208+
err = os.WriteFile(".hidden/main.go", []byte("package main"), 0o644)
209+
Expect(err).NotTo(HaveOccurred())
210+
211+
err = checkDir()
212+
Expect(err).NotTo(HaveOccurred())
213+
})
214+
215+
It("should allow empty subdirectories", func() {
216+
err := os.Mkdir("emptydir", 0o755)
217+
Expect(err).NotTo(HaveOccurred())
218+
219+
err = checkDir()
220+
Expect(err).NotTo(HaveOccurred())
221+
})
222+
223+
It("should allow files with multiple extensions", func() {
224+
err := os.WriteFile("config.backup.yaml", []byte("key: value"), 0o644)
225+
Expect(err).NotTo(HaveOccurred())
226+
227+
err = checkDir()
228+
Expect(err).NotTo(HaveOccurred())
229+
})
230+
231+
It("should allow partially capitalized files", func() {
232+
err := os.WriteFile("READme", []byte("test"), 0o644)
233+
Expect(err).NotTo(HaveOccurred())
234+
235+
err = checkDir()
236+
Expect(err).NotTo(HaveOccurred())
237+
})
238+
239+
It("should allow go files in subdirectories", func() {
240+
err := os.Mkdir("subdir", 0o755)
241+
Expect(err).NotTo(HaveOccurred())
242+
err = os.WriteFile(filepath.Join("subdir", "main.go"), []byte("package main"), 0o644)
243+
Expect(err).NotTo(HaveOccurred())
244+
245+
err = checkDir()
246+
Expect(err).NotTo(HaveOccurred())
247+
})
248+
})
249+
})

0 commit comments

Comments
 (0)