Skip to content

Commit d7d3b21

Browse files
authored
fix(catalog): construct valid module source URLs for version-pinned modules #5173 (#5174)
* fix(5173): construct valid module source URLs * add tests for TerraformSourcePath URL formatting * use external test package pattern * add test case for multiple query params
1 parent f83bbf4 commit d7d3b21

2 files changed

Lines changed: 88 additions & 1 deletion

File tree

internal/services/catalog/module/module.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,22 @@ func (module *Module) URL() string {
9595
return module.url
9696
}
9797

98+
// TerraformSourcePath returns the module source URL in the format expected by go-getter:
99+
// baseURL//moduleDir?query (e.g., git::https://github.com/org/repo.git//modules/foo?ref=v1.0.0)
98100
func (module *Module) TerraformSourcePath() string {
99-
return module.cloneURL + "//" + module.moduleDir
101+
if module.moduleDir == "" {
102+
return module.cloneURL
103+
}
104+
105+
// Split on ? to separate base URL from query string
106+
base, query, _ := strings.Cut(module.cloneURL, "?")
107+
108+
result := base + "//" + module.moduleDir
109+
if query != "" {
110+
result += "?" + query
111+
}
112+
113+
return result
100114
}
101115

102116
func (module *Module) isValid() (bool, error) {
@@ -126,3 +140,11 @@ func (module *Module) isValid() (bool, error) {
126140
func (module *Module) ModuleDir() string {
127141
return module.moduleDir
128142
}
143+
144+
// NewModuleForTest creates a Module for testing purposes.
145+
func NewModuleForTest(cloneURL, moduleDir string) *Module {
146+
return &Module{
147+
cloneURL: cloneURL,
148+
moduleDir: moduleDir,
149+
}
150+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package module_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gruntwork-io/terragrunt/internal/services/catalog/module"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestTerraformSourcePath(t *testing.T) {
11+
t.Parallel()
12+
13+
testCases := []struct {
14+
name string
15+
cloneURL string
16+
moduleDir string
17+
expected string
18+
}{
19+
{
20+
name: "root module without ref",
21+
cloneURL: "git::https://github.com/org/repo.git",
22+
moduleDir: "",
23+
expected: "git::https://github.com/org/repo.git",
24+
},
25+
{
26+
name: "root module with ref",
27+
cloneURL: "git::https://github.com/org/repo.git?ref=v1.0.0",
28+
moduleDir: "",
29+
expected: "git::https://github.com/org/repo.git?ref=v1.0.0",
30+
},
31+
{
32+
name: "submodule without ref",
33+
cloneURL: "git::https://github.com/org/repo.git",
34+
moduleDir: "modules/foo",
35+
expected: "git::https://github.com/org/repo.git//modules/foo",
36+
},
37+
{
38+
name: "submodule with ref",
39+
cloneURL: "git::https://github.com/org/repo.git?ref=v1.0.0",
40+
moduleDir: "modules/foo",
41+
expected: "git::https://github.com/org/repo.git//modules/foo?ref=v1.0.0",
42+
},
43+
{
44+
name: "ssh url with ref",
45+
cloneURL: "git::ssh://git@github.com/org/repo.git?ref=v1.0.0",
46+
moduleDir: "modules/bar",
47+
expected: "git::ssh://git@github.com/org/repo.git//modules/bar?ref=v1.0.0",
48+
},
49+
{
50+
name: "multiple query params",
51+
cloneURL: "git::https://github.com/org/repo.git?ref=v1.0.0&depth=1",
52+
moduleDir: "modules/foo",
53+
expected: "git::https://github.com/org/repo.git//modules/foo?ref=v1.0.0&depth=1",
54+
},
55+
}
56+
57+
for _, tc := range testCases {
58+
t.Run(tc.name, func(t *testing.T) {
59+
t.Parallel()
60+
61+
m := module.NewModuleForTest(tc.cloneURL, tc.moduleDir)
62+
assert.Equal(t, tc.expected, m.TerraformSourcePath())
63+
})
64+
}
65+
}

0 commit comments

Comments
 (0)