Skip to content

Commit 066bee6

Browse files
committed
rewrite the e2e test to adhere to project style
1 parent b52ac9a commit 066bee6

File tree

3 files changed

+200
-113
lines changed

3 files changed

+200
-113
lines changed

pkg/plugins/golang/v4/scaffolds/api.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package scaffolds
1919
import (
2020
"errors"
2121
"fmt"
22-
log "log/slog"
22+
"log/slog"
2323
"path/filepath"
2424

2525
"github.com/spf13/afero"
@@ -65,13 +65,13 @@ func (s *apiScaffolder) InjectFS(fs machinery.Filesystem) {
6565

6666
// Scaffold implements cmdutil.Scaffolder
6767
func (s *apiScaffolder) Scaffold() error {
68-
log.Info("Writing scaffold for you to edit...")
68+
slog.Info("Writing scaffold for you to edit...")
6969

7070
// Load the boilerplate
7171
boilerplate, err := afero.ReadFile(s.fs.FS, hack.DefaultBoilerplatePath)
7272
if err != nil {
7373
if errors.Is(err, afero.ErrFileNotFound) {
74-
log.Warn("Unable to find boilerplate file."+
74+
slog.Warn("Unable to find boilerplate file."+
7575
"This file is used to generate the license header in the project.\n"+
7676
"Note that controller-gen will also use this. Therefore, ensure that you "+
7777
"add the license file or configure your project accordingly.",
@@ -143,24 +143,25 @@ func (s *apiScaffolder) discoverFeatureGates() []string {
143143
apiDir := "api"
144144
if s.config.IsMultiGroup() && s.resource.Group != "" {
145145
apiDir = filepath.Join("api", s.resource.Group)
146+
}
146147

147148
// Check if the directory exists before trying to parse it
148149
if _, err := s.fs.FS.Stat(apiDir); err != nil {
149-
log.Debugf("API directory %s does not exist yet, skipping feature gate discovery", apiDir)
150+
slog.Debug("API directory does not exist yet, skipping feature gate discovery", "apiDir", apiDir)
150151
return []string{}
151152
}
152153

153154
markers, err := parser.ParseDirectory(apiDir)
154155
if err != nil {
155-
log.Debugf("Failed to parse feature gates from %s: %v", apiDir, err)
156+
slog.Debug("Failed to parse feature gates from directory", "apiDir", apiDir, "error", err)
156157
return []string{}
157158
}
158159

159160
featureGates := machinery.ExtractFeatureGates(markers)
160161
if len(featureGates) > 0 {
161-
log.Infof("Discovered feature gates: %v", featureGates)
162+
slog.Info("Discovered feature gates", "featureGates", featureGates)
162163
} else {
163-
log.Debugf("No feature gates found in %s", apiDir)
164+
slog.Debug("No feature gates found in directory", "apiDir", apiDir)
164165
}
165166

166167
return featureGates

pkg/plugins/golang/v4/scaffolds/api_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ import (
2323

2424
. "github.com/onsi/gomega"
2525

26+
"github.com/spf13/afero"
2627
"sigs.k8s.io/kubebuilder/v4/pkg/config"
2728
v3 "sigs.k8s.io/kubebuilder/v4/pkg/config/v3"
29+
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
2830
"sigs.k8s.io/kubebuilder/v4/pkg/model/resource"
2931
)
3032

@@ -68,6 +70,8 @@ func (m *mockConfig) MarshalYAML() ([]byte, error) { return nil, nil }
6870
func (m *mockConfig) UnmarshalYAML([]byte) error { return nil }
6971

7072
func TestAPIScaffolder_discoverFeatureGates(t *testing.T) {
73+
RegisterTestingT(t)
74+
7175
tests := []struct {
7276
name string
7377
config config.Config
@@ -112,6 +116,9 @@ func TestAPIScaffolder_discoverFeatureGates(t *testing.T) {
112116
t.Fatal("Expected apiScaffolder type")
113117
}
114118

119+
// Initialize filesystem to prevent nil pointer dereference
120+
apiScaffolder.fs = machinery.Filesystem{FS: afero.NewMemMapFs()}
121+
115122
gates := apiScaffolder.discoverFeatureGates()
116123

117124
// In a real test environment, we'd create test files with feature gates
@@ -123,6 +130,8 @@ func TestAPIScaffolder_discoverFeatureGates(t *testing.T) {
123130
}
124131

125132
func TestAPIScaffolder_NewAPIScaffolder(t *testing.T) {
133+
RegisterTestingT(t)
134+
126135
cfg := v3.New()
127136
res := resource.Resource{
128137
GVK: resource.GVK{
@@ -151,8 +160,8 @@ func TestAPIScaffolder_discoverFeatureGates_Testdata(t *testing.T) {
151160
}
152161
defer os.Chdir(oldDir)
153162

154-
// Change to testdata/project-v4 directory
155-
testdataDir := filepath.Join("testdata", "project-v4")
163+
// Change to testdata/project-v4 directory (from pkg/plugins/golang/v4/scaffolds/ to ../../../../../testdata/project-v4)
164+
testdataDir := filepath.Join("../../../../../testdata", "project-v4")
156165
if err := os.Chdir(testdataDir); err != nil {
157166
t.Fatalf("Failed to change to testdata directory: %v", err)
158167
}
@@ -172,6 +181,9 @@ func TestAPIScaffolder_discoverFeatureGates_Testdata(t *testing.T) {
172181
t.Fatal("Expected apiScaffolder type")
173182
}
174183

184+
// Initialize filesystem to prevent nil pointer dereference
185+
apiScaffolder.fs = machinery.Filesystem{FS: afero.NewOsFs()}
186+
175187
featureGates := apiScaffolder.discoverFeatureGates()
176188
if len(featureGates) > 0 {
177189
t.Errorf("Expected no feature gates from testdata, but found %d: %v", len(featureGates), featureGates)

0 commit comments

Comments
 (0)