44package configs
55
66import (
7- "os"
87 "path/filepath"
98 "testing"
109
1110 "github.com/hashicorp/go-slug/sourceaddrs"
1211 "github.com/hashicorp/go-slug/sourcebundle"
1312)
1413
15- // TestSourceBundleParser_LoadConfigDir_WithAbsolutePath tests that when
16- // LocalPathForSource returns an absolute path, LoadConfigDir correctly converts
17- // it to a relative path from the current working directory and sets it on the Module.
18- func TestSourceBundleParser_LoadConfigDir_WithAbsolutePath (t * testing.T ) {
19- tmpDir := t .TempDir ()
20- bundleRoot := filepath .Join (tmpDir , "bundle" )
21- configDir := filepath .Join (bundleRoot , "root" )
22-
23- err := os .MkdirAll (configDir , 0755 )
24- if err != nil {
25- t .Fatalf ("failed to create config directory: %s" , err )
26- }
27-
28- configContent := []byte (`
29- resource "test_resource" "example" {
30- name = "test"
31- }
32- ` )
33- err = os .WriteFile (filepath .Join (configDir , "main.tf" ), configContent , 0644 )
34- if err != nil {
35- t .Fatalf ("failed to write config file: %s" , err )
36- }
37-
38- manifestContent := []byte (`{
39- "terraform_source_bundle": 1,
40- "packages": [
41- {
42- "source": "git::https://example.com/test.git",
43- "local": "root",
44- "meta": {}
45- }
46- ]
47- }` )
48- err = os .WriteFile (filepath .Join (bundleRoot , "terraform-sources.json" ), manifestContent , 0644 )
49- if err != nil {
50- t .Fatalf ("failed to write manifest file: %s" , err )
51- }
52-
53- sources , err := sourcebundle .OpenDir (bundleRoot )
14+ // TestSourceBundleParser_LoadConfigDir_WithRelativePath tests that when
15+ // LocalPathForSource returns a relative path, LoadConfigDir correctly uses
16+ // it as-is without attempting to convert it.
17+ func TestSourceBundleParser_LoadConfigDir_WithRelativePath (t * testing.T ) {
18+ // Use the basics-bundle from stacks testdata which has a component with has a relative source.
19+ bundlePath := "../stacks/stackconfig/testdata/basics-bundle"
20+ bundle , err := sourcebundle .OpenDir (bundlePath )
5421 if err != nil {
5522 t .Fatalf ("failed to open source bundle: %s" , err )
5623 }
5724
58- source := sourceaddrs .MustParseSource ("git::https://example.com/test.git " ).(sourceaddrs.FinalSource )
25+ source := sourceaddrs .MustParseSource ("../stacks/stackconfig/testdata/basics-bundle " ).(sourceaddrs.FinalSource )
5926
60- sourcePath , err := sources .LocalPathForSource (source )
61- if err != nil {
62- t .Fatalf ("failed to get local path for source: %s" , err )
63- }
64- if ! filepath .IsAbs (sourcePath ) {
65- t .Fatalf ("LocalPathForSource returned relative path %q" , sourcePath )
66- }
67-
68- parser := NewSourceBundleParser (sources )
27+ // Create a SourceBundleParser and load the config directory.
28+ parser := NewSourceBundleParser (bundle )
6929 mod , diags := parser .LoadConfigDir (source )
7030
7131 if diags .HasErrors () {
@@ -76,134 +36,11 @@ resource "test_resource" "example" {
7636 t .Fatal ("expected non-nil module" )
7737 }
7838
39+ // Verify that the SourceDir is set and that it's a relative path.
40+ if mod .SourceDir == "" {
41+ t .Error ("expected SourceDir to be set, but it was empty" )
42+ }
7943 if filepath .IsAbs (mod .SourceDir ) {
8044 t .Errorf ("expected SourceDir to be relative, but got absolute path: %s" , mod .SourceDir )
8145 }
82-
83- workDir , err := os .Getwd ()
84- if err != nil {
85- t .Fatalf ("failed to get working directory: %s" , err )
86- }
87-
88- expectedRelPath , err := filepath .Rel (workDir , sourcePath )
89- if err != nil {
90- t .Fatalf ("failed to compute expected relative path: %s" , err )
91- }
92-
93- if mod .SourceDir != expectedRelPath {
94- t .Errorf ("expected SourceDir to be %q, got %q" , expectedRelPath , mod .SourceDir )
95- }
96- }
97-
98- // mockSourceBundleForRelativePath is a test helper that wraps sourcebundle.Bundle
99- // to simulate LocalPathForSource returning a relative path.
100- type mockSourceBundleForRelativePath struct {
101- realBundle * sourcebundle.Bundle
102- workDir string
103- }
104-
105- // We can't reliably ensure that LocalPathForSource will return a relative path - so we can mock it
106- // to ensure that the core logic of keeping that path relative is working.
107- func (m * mockSourceBundleForRelativePath ) LocalPathForSource (source sourceaddrs.FinalSource ) (string , error ) {
108- path , err := m .realBundle .LocalPathForSource (source )
109- if err != nil {
110- return "" , err
111- }
112-
113- // Convert absolute path to relative for testing
114- if filepath .IsAbs (path ) {
115- relPath , err := filepath .Rel (m .workDir , path )
116- if err != nil {
117- return "" , err
118- }
119- return relPath , nil
120- }
121-
122- return path , nil
123- }
124-
125- // TestSourceBundleParser_LoadConfigDir_WithRelativePath tests that when
126- // LocalPathForSource returns a relative path (already relative to the working
127- // directory), the code uses it as-is without attempting to convert it. This
128- // ensures we don't break the case where the path is already in the correct form.
129- func TestSourceBundleParser_LoadConfigDir_WithRelativePath (t * testing.T ) {
130- tmpDir := t .TempDir ()
131- bundleRoot := filepath .Join (tmpDir , "bundle" )
132- configDir := filepath .Join (bundleRoot , "root" )
133-
134- err := os .MkdirAll (configDir , 0755 )
135- if err != nil {
136- t .Fatalf ("failed to create config directory: %s" , err )
137- }
138-
139- configContent := []byte (`
140- resource "test_resource" "example" {
141- name = "test"
142- }
143- ` )
144- err = os .WriteFile (filepath .Join (configDir , "main.tf" ), configContent , 0644 )
145- if err != nil {
146- t .Fatalf ("failed to write config file: %s" , err )
147- }
148-
149- // Create the source bundle manifest
150- manifestContent := []byte (`{
151- "terraform_source_bundle": 1,
152- "packages": [
153- {
154- "source": "git::https://example.com/relative-test.git",
155- "local": "root",
156- "meta": {}
157- }
158- ]
159- }` )
160-
161- err = os .WriteFile (filepath .Join (bundleRoot , "terraform-sources.json" ), manifestContent , 0644 )
162- if err != nil {
163- t .Fatalf ("failed to write manifest file: %s" , err )
164- }
165-
166- realSources , err := sourcebundle .OpenDir (bundleRoot )
167- if err != nil {
168- t .Fatalf ("failed to open source bundle: %s" , err )
169- }
170-
171- workDir , err := os .Getwd ()
172- if err != nil {
173- t .Fatalf ("failed to get working directory: %s" , err )
174- }
175-
176- mockSources := & mockSourceBundleForRelativePath {
177- realBundle : realSources ,
178- workDir : workDir ,
179- }
180-
181- source := sourceaddrs .MustParseSource ("git::https://example.com/relative-test.git" ).(sourceaddrs.FinalSource )
182-
183- sourcePath , err := mockSources .LocalPathForSource (source )
184- if err != nil {
185- t .Fatalf ("failed to get local path for source: %s" , err )
186- }
187-
188- if filepath .IsAbs (sourcePath ) {
189- t .Fatalf ("mock should return relative path but got absolute: %q" , sourcePath )
190- }
191-
192- testPath := sourcePath
193- var relativeSourceDir string
194-
195- // This mimics the logic in LoadConfigDir
196- if filepath .IsAbs (testPath ) {
197- t .Fatal ("unexpected absolute path from mock" )
198- } else {
199- relativeSourceDir = testPath
200- }
201-
202- if relativeSourceDir != sourcePath {
203- t .Errorf ("expected relative path to be used as-is: got %q, want %q" , relativeSourceDir , sourcePath )
204- }
205-
206- if filepath .IsAbs (relativeSourceDir ) {
207- t .Errorf ("expected relativeSourceDir to be relative, but got absolute: %s" , relativeSourceDir )
208- }
20946}
0 commit comments