Skip to content

Commit 9966815

Browse files
alexlovelltroydavidallendj
authored andcommitted
Refactor registration file handling and add regeneration tests
Signed-off-by: Alex Lovell-Troy <alovelltroy@lanl.gov> Signed-off-by: David Allen <davidallendj@gmail.com>
1 parent d2bfd06 commit 9966815

File tree

2 files changed

+113
-9
lines changed

2 files changed

+113
-9
lines changed

cmd/fabrica/generate.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,9 @@ Examples:
121121

122122
fmt.Printf("📦 Found %d resource(s): %s\n", len(resources), strings.Join(resources, ", "))
123123

124-
// Check if registration file exists or needs regenerating
125-
regFile := "pkg/resources/register_generated.go"
126-
needsRegistration := false
127-
if _, err := os.Stat(regFile); os.IsNotExist(err) {
128-
needsRegistration = true
129-
}
130-
131124
// Check version compatibility before regenerating (only if generated code exists)
132-
// Skip version check for fresh projects (where registration file doesn't exist)
133-
if !needsRegistration {
125+
regFile := "pkg/resources/register_generated.go"
126+
if _, err := os.Stat(regFile); err == nil {
134127
generatedVersion := detectGeneratedVersion()
135128
if generatedVersion != "" && debug {
136129
fmt.Printf("🔍 Detected generated code version: %s\n", generatedVersion)
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// SPDX-FileCopyrightText: 2025 Copyright © 2025 OpenCHAMI a Series of LF Projects, LLC
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
package integration
6+
7+
import (
8+
"os"
9+
"path/filepath"
10+
"testing"
11+
12+
"github.com/stretchr/testify/suite"
13+
)
14+
15+
type RegenerationTestSuite struct {
16+
suite.Suite
17+
tempDir string
18+
fabricaBinary string
19+
}
20+
21+
func (s *RegenerationTestSuite) SetupSuite() {
22+
// Find fabrica binary
23+
wd, err := os.Getwd()
24+
s.Require().NoError(err)
25+
26+
// Assuming test is run from test/integration
27+
projectRoot := filepath.Join(wd, "..", "..")
28+
s.fabricaBinary = filepath.Join(projectRoot, "bin", "fabrica")
29+
30+
// Verify binary exists
31+
_, err = os.Stat(s.fabricaBinary)
32+
s.Require().NoError(err, "fabrica binary not found at %s. Run 'make build' first.", s.fabricaBinary)
33+
}
34+
35+
func (s *RegenerationTestSuite) SetupTest() {
36+
var err error
37+
s.tempDir, err = os.MkdirTemp("", "fabrica-regeneration-test-*")
38+
s.Require().NoError(err)
39+
}
40+
41+
func (s *RegenerationTestSuite) TearDownTest() {
42+
os.RemoveAll(s.tempDir)
43+
}
44+
45+
func (s *RegenerationTestSuite) TestRegenerationAddsNewResources() {
46+
projectName := "regeneration-test"
47+
project := NewTestProject(&s.Suite, s.tempDir, projectName, "github.com/example/regeneration", "file")
48+
49+
// 1. Initialize project
50+
err := project.Initialize(s.fabricaBinary)
51+
s.Require().NoError(err)
52+
53+
// 2. Add first resource
54+
err = project.AddResource(s.fabricaBinary, "DeviceProfile")
55+
s.Require().NoError(err)
56+
57+
// 3. Generate
58+
err = project.Generate(s.fabricaBinary)
59+
s.Require().NoError(err)
60+
61+
// 4. Verify first resource routes exist
62+
routesFile := filepath.Join(project.Dir, "cmd", "server", "routes_generated.go")
63+
content, err := os.ReadFile(routesFile)
64+
s.Require().NoError(err)
65+
s.Contains(string(content), "DeviceProfile", "Routes should contain DeviceProfile")
66+
67+
// 5. Add second resource
68+
err = project.AddResource(s.fabricaBinary, "UpdateProfile")
69+
s.Require().NoError(err)
70+
71+
// 6. Generate again
72+
err = project.Generate(s.fabricaBinary)
73+
s.Require().NoError(err)
74+
75+
// 7. Verify both resources exist in routes
76+
content, err = os.ReadFile(routesFile)
77+
s.Require().NoError(err)
78+
s.Contains(string(content), "DeviceProfile", "Routes should still contain DeviceProfile")
79+
s.Contains(string(content), "UpdateProfile", "Routes should now contain UpdateProfile")
80+
81+
// 8. Verify registration file contains both resources
82+
regFile := filepath.Join(project.Dir, "pkg", "resources", "register_generated.go")
83+
regContent, err := os.ReadFile(regFile)
84+
s.Require().NoError(err)
85+
s.Contains(string(regContent), "DeviceProfile", "Registration should contain DeviceProfile")
86+
s.Contains(string(regContent), "UpdateProfile", "Registration should contain UpdateProfile")
87+
88+
// 9. Verify other global files contain the new resource
89+
filesToCheck := []string{
90+
filepath.Join("cmd", "server", "models_generated.go"),
91+
filepath.Join("internal", "storage", "storage_generated.go"),
92+
filepath.Join("pkg", "client", "client_generated.go"),
93+
filepath.Join("cmd", "server", "openapi_generated.go"),
94+
}
95+
96+
for _, relPath := range filesToCheck {
97+
fullPath := filepath.Join(project.Dir, relPath)
98+
content, err := os.ReadFile(fullPath)
99+
s.Require().NoError(err, "Failed to read %s", relPath)
100+
s.Contains(string(content), "UpdateProfile", "%s should contain UpdateProfile", relPath)
101+
}
102+
103+
// 10. Verify new handler file exists
104+
handlerFile := filepath.Join(project.Dir, "cmd", "server", "updateprofile_handlers_generated.go")
105+
_, err = os.Stat(handlerFile)
106+
s.Require().NoError(err, "Handler file for UpdateProfile should exist")
107+
}
108+
109+
func TestRegenerationSuite(t *testing.T) {
110+
suite.Run(t, new(RegenerationTestSuite))
111+
}

0 commit comments

Comments
 (0)