Skip to content

Commit d79b3c0

Browse files
fix(scaffold): sanitize package name (#396)
When scaffolling a new environment, if the env name has `-` in it, since it is used as go package name, the generated code has compile errors as go pkg does no support `-` as part of its name. This commit update the logic to sanitize the name replacing it with `_` instead
1 parent 4e22bb1 commit d79b3c0

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

.changeset/evil-hairs-bet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"chainlink-deployments-framework": patch
3+
---
4+
5+
fix(scaffold): sanitize env name for go package name

engine/cld/scaffold/scaffold.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func ScaffoldEnvDir(envdir enginedomain.EnvDir) error {
117117

118118
// Setup all the args for the templates
119119
renderArgs := map[string]string{
120-
"package": envdir.Key(),
120+
"package": sanitizePackageName(envdir.Key()),
121121
}
122122

123123
// Define the structure
@@ -289,3 +289,9 @@ func runGoModTidy(dir string) error {
289289

290290
return nil
291291
}
292+
293+
// sanitizePackageName converts a string to a valid Go package name by replacing hyphens with underscores.
294+
// Go package names cannot contain hyphens, so this function ensures compatibility.
295+
func sanitizePackageName(name string) string {
296+
return strings.ReplaceAll(name, "-", "_")
297+
}

engine/cld/scaffold/scaffold_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,76 @@ func Test_getRepositoryName(t *testing.T) {
168168
result := getRepositoryName(rootDir)
169169
assert.Equal(t, "repo_name", result)
170170
}
171+
172+
func Test_ScaffoldEnvDir_WithHyphens(t *testing.T) {
173+
t.Parallel()
174+
175+
var (
176+
rootDir = t.TempDir()
177+
domKey = "ccip"
178+
envKey = "my-test-env"
179+
)
180+
181+
// First scaffold the domain
182+
dom := domain.NewDomain(rootDir, domKey)
183+
err := ScaffoldDomain(dom)
184+
require.NoError(t, err)
185+
186+
// Then scaffold the environment
187+
envdir := domain.NewEnvDir(rootDir, domKey, envKey)
188+
err = ScaffoldEnvDir(envdir)
189+
require.NoError(t, err)
190+
191+
// Check that pipelines.go uses sanitized package name
192+
pipelinesPath := filepath.Join(envdir.DirPath(), "pipelines.go")
193+
pipelinesContent, err := os.ReadFile(pipelinesPath)
194+
require.NoError(t, err)
195+
196+
// Should contain sanitized package name (underscores instead of hyphens)
197+
assert.Contains(t, string(pipelinesContent), "my_test_env")
198+
assert.NotContains(t, string(pipelinesContent), "my-test-env")
199+
}
200+
201+
func Test_sanitizePackageName(t *testing.T) {
202+
t.Parallel()
203+
204+
tests := []struct {
205+
name string
206+
input string
207+
expected string
208+
}{
209+
{
210+
name: "no hyphens",
211+
input: "mypackage",
212+
expected: "mypackage",
213+
},
214+
{
215+
name: "single hyphen",
216+
input: "my-package",
217+
expected: "my_package",
218+
},
219+
{
220+
name: "multiple hyphens",
221+
input: "my-complex-package-name",
222+
expected: "my_complex_package_name",
223+
},
224+
{
225+
name: "empty string",
226+
input: "",
227+
expected: "",
228+
},
229+
{
230+
name: "only hyphens",
231+
input: "---",
232+
expected: "___",
233+
},
234+
}
235+
236+
for _, tt := range tests {
237+
t.Run(tt.name, func(t *testing.T) {
238+
t.Parallel()
239+
result := sanitizePackageName(tt.input)
240+
assert.Equal(t, tt.expected, result)
241+
})
242+
}
243+
}

0 commit comments

Comments
 (0)