Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ warning comment on PROJECT file #3137

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions pkg/config/store/yaml/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ import (
const (
// DefaultPath is the default path for the configuration file
DefaultPath = "PROJECT"

// Comment for 'PROJECT' config file
commentStr = `# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
jason1028kr marked this conversation as resolved.
Show resolved Hide resolved
`
)

// yamlStore implements store.Store using a YAML file as the storage backend
Expand Down Expand Up @@ -133,6 +139,9 @@ func (s yamlStore) SaveTo(path string) error {
return store.SaveError{Err: fmt.Errorf("unable to marshal to YAML: %w", err)}
}

// Prepend warning comment for the 'PROJECT' file
content = append([]byte(commentStr), content...)

// Write the marshalled configuration
err = afero.WriteFile(s.fs, path, content, 0600)
if err != nil {
Expand Down
25 changes: 12 additions & 13 deletions pkg/config/store/yaml/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ layout: ""

Context("Load", func() {
It("should load the Config from an existing file at the default path", func() {
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(v2File), os.ModePerm)).To(Succeed())
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(commentStr+v2File), os.ModePerm)).To(Succeed())

Expect(s.Load()).To(Succeed())
Expect(s.fs).NotTo(BeNil())
Expand All @@ -102,23 +102,23 @@ layout: ""
})

It("should fail if unable to identify the version of the file at the default path", func() {
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(unversionedFile), os.ModePerm)).To(Succeed())
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(commentStr+unversionedFile), os.ModePerm)).To(Succeed())

err := s.Load()
Expect(err).To(HaveOccurred())
Expect(errors.As(err, &store.LoadError{})).To(BeTrue())
})

It("should fail if unable to create a Config for the version of the file at the default path", func() {
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(nonexistentVersionFile), os.ModePerm)).To(Succeed())
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(commentStr+nonexistentVersionFile), os.ModePerm)).To(Succeed())

err := s.Load()
Expect(err).To(HaveOccurred())
Expect(errors.As(err, &store.LoadError{})).To(BeTrue())
})

It("should fail if unable to unmarshal the file at the default path", func() {
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(wrongFile), os.ModePerm)).To(Succeed())
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(commentStr+wrongFile), os.ModePerm)).To(Succeed())

err := s.Load()
Expect(err).To(HaveOccurred())
Expand All @@ -128,7 +128,7 @@ layout: ""

Context("LoadFrom", func() {
It("should load the Config from an existing file from the specified path", func() {
Expect(afero.WriteFile(s.fs, path, []byte(v2File), os.ModePerm)).To(Succeed())
Expect(afero.WriteFile(s.fs, path, []byte(commentStr+v2File), os.ModePerm)).To(Succeed())

Expect(s.LoadFrom(path)).To(Succeed())
Expect(s.fs).NotTo(BeNil())
Expand All @@ -144,23 +144,23 @@ layout: ""
})

It("should fail if unable to identify the version of the file at the specified path", func() {
Expect(afero.WriteFile(s.fs, path, []byte(unversionedFile), os.ModePerm)).To(Succeed())
Expect(afero.WriteFile(s.fs, path, []byte(commentStr+unversionedFile), os.ModePerm)).To(Succeed())

err := s.LoadFrom(path)
Expect(err).To(HaveOccurred())
Expect(errors.As(err, &store.LoadError{})).To(BeTrue())
})

It("should fail if unable to create a Config for the version of the file at the specified path", func() {
Expect(afero.WriteFile(s.fs, path, []byte(nonexistentVersionFile), os.ModePerm)).To(Succeed())
Expect(afero.WriteFile(s.fs, path, []byte(commentStr+nonexistentVersionFile), os.ModePerm)).To(Succeed())

err := s.LoadFrom(path)
Expect(err).To(HaveOccurred())
Expect(errors.As(err, &store.LoadError{})).To(BeTrue())
})

It("should fail if unable to unmarshal the file at the specified path", func() {
Expect(afero.WriteFile(s.fs, path, []byte(wrongFile), os.ModePerm)).To(Succeed())
Expect(afero.WriteFile(s.fs, path, []byte(commentStr+wrongFile), os.ModePerm)).To(Succeed())

err := s.LoadFrom(path)
Expect(err).To(HaveOccurred())
Expand All @@ -175,7 +175,7 @@ layout: ""

cfgBytes, err := afero.ReadFile(s.fs, DefaultPath)
Expect(err).NotTo(HaveOccurred())
Expect(string(cfgBytes)).To(Equal(v2File))
Expect(string(cfgBytes)).To(Equal(commentStr + v2File))
})

It("should succeed for a valid config that must not exist", func() {
Expand All @@ -185,7 +185,7 @@ layout: ""

cfgBytes, err := afero.ReadFile(s.fs, DefaultPath)
Expect(err).NotTo(HaveOccurred())
Expect(string(cfgBytes)).To(Equal(v2File))
Expect(string(cfgBytes)).To(Equal(commentStr + v2File))
})

It("should fail for an empty config", func() {
Expand All @@ -212,8 +212,7 @@ layout: ""

cfgBytes, err := afero.ReadFile(s.fs, path)
Expect(err).NotTo(HaveOccurred())
Expect(string(cfgBytes)).To(Equal(`version: "2"
`))
Expect(string(cfgBytes)).To(Equal(commentStr + v2File))
})

It("should succeed for a valid config that must not exist", func() {
Expand All @@ -223,7 +222,7 @@ layout: ""

cfgBytes, err := afero.ReadFile(s.fs, path)
Expect(err).NotTo(HaveOccurred())
Expect(string(cfgBytes)).To(Equal(v2File))
Expect(string(cfgBytes)).To(Equal(commentStr + v2File))
})

It("should fail for an empty config", func() {
Expand Down
3 changes: 3 additions & 0 deletions testdata/project-v2/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
domain: testproject.org
repo: sigs.k8s.io/kubebuilder/testdata/project-v2
resources:
Expand Down
3 changes: 3 additions & 0 deletions testdata/project-v3-config/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
componentConfig: true
domain: testproject.org
layout:
Expand Down
3 changes: 3 additions & 0 deletions testdata/project-v3-declarative-v1/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
domain: testproject.org
layout:
- go.kubebuilder.io/v3
Expand Down
3 changes: 3 additions & 0 deletions testdata/project-v3-multigroup/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
domain: testproject.org
layout:
- go.kubebuilder.io/v3
Expand Down
3 changes: 3 additions & 0 deletions testdata/project-v3-with-deploy-image/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
domain: testproject.org
layout:
- go.kubebuilder.io/v3
Expand Down
3 changes: 3 additions & 0 deletions testdata/project-v3-with-grafana/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
domain: testproject.org
layout:
- go.kubebuilder.io/v3
Expand Down
3 changes: 3 additions & 0 deletions testdata/project-v3/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
domain: testproject.org
layout:
- go.kubebuilder.io/v3
Expand Down
3 changes: 3 additions & 0 deletions testdata/project-v4-config/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
componentConfig: true
domain: testproject.org
layout:
Expand Down
3 changes: 3 additions & 0 deletions testdata/project-v4-declarative-v1/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
domain: testproject.org
layout:
- go.kubebuilder.io/v4-alpha
Expand Down
3 changes: 3 additions & 0 deletions testdata/project-v4-multigroup/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
domain: testproject.org
layout:
- go.kubebuilder.io/v4-alpha
Expand Down
3 changes: 3 additions & 0 deletions testdata/project-v4-with-deploy-image/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
domain: testproject.org
layout:
- go.kubebuilder.io/v4-alpha
Expand Down
3 changes: 3 additions & 0 deletions testdata/project-v4-with-grafana/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
domain: testproject.org
layout:
- go.kubebuilder.io/v3
Expand Down
3 changes: 3 additions & 0 deletions testdata/project-v4/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
domain: testproject.org
layout:
- go.kubebuilder.io/v4-alpha
Expand Down