-
-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathbase_path_resolution_test.go
More file actions
797 lines (665 loc) · 30.4 KB
/
Copy pathbase_path_resolution_test.go
File metadata and controls
797 lines (665 loc) · 30.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
package config
import (
"errors"
"os"
"path/filepath"
"testing"
errUtils "github.com/cloudposse/atmos/errors"
"github.com/cloudposse/atmos/pkg/schema"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestInitCliConfig_ExplicitBasePath_DotSlash_ResolvesRelativeToCWD verifies that when
// AtmosBasePath uses a dot-slash prefix (e.g., "./.terraform/modules/monorepo"), it resolves
// relative to CWD. This is Tyler's scenario: the dot-slash explicitly anchors to CWD.
func TestInitCliConfig_ExplicitBasePath_DotSlash_ResolvesRelativeToCWD(t *testing.T) {
// Create a temp directory to simulate a project layout.
tmpDir := t.TempDir()
// Resolve symlinks (macOS /var -> /private/var).
tmpDir, err := filepath.EvalSymlinks(tmpDir)
require.NoError(t, err)
// Create a subdirectory to simulate CWD being different from project root.
subDir := filepath.Join(tmpDir, "components", "terraform", "vpc")
require.NoError(t, os.MkdirAll(subDir, 0o755))
// Create a relative base path target with dot-slash prefix.
relBasePath := filepath.Join(".", ".terraform", "modules", "monorepo")
absTarget := filepath.Join(subDir, ".terraform", "modules", "monorepo")
require.NoError(t, os.MkdirAll(absTarget, 0o755))
// Create minimal atmos.yaml in the target.
atmosYaml := filepath.Join(absTarget, "atmos.yaml")
require.NoError(t, os.WriteFile(atmosYaml, []byte("base_path: ./\nstacks:\n base_path: stacks\n"), 0o644))
// Create stacks directory so config loading doesn't fail.
require.NoError(t, os.MkdirAll(filepath.Join(absTarget, "stacks"), 0o755))
// Change to the subdirectory (simulating terraform-provider-utils context).
t.Chdir(subDir)
// Provide AtmosBasePath with dot-slash prefix — this is a runtime source.
configAndStacksInfo := schema.ConfigAndStacksInfo{
AtmosBasePath: relBasePath,
}
atmosConfig, err := InitCliConfig(configAndStacksInfo, false)
require.NoError(t, err)
// The base path should resolve to CWD + relative path (dot-slash = CWD anchor).
assert.True(t, filepath.IsAbs(atmosConfig.BasePathAbsolute),
"BasePathAbsolute should be absolute, got: %s", atmosConfig.BasePathAbsolute)
assert.Equal(t, absTarget, atmosConfig.BasePathAbsolute,
"Dot-slash AtmosBasePath should resolve relative to CWD, not config dir")
}
// TestInitCliConfig_ExplicitBasePath_AbsolutePassedThrough verifies that an absolute
// AtmosBasePath is passed through without modification.
func TestInitCliConfig_ExplicitBasePath_AbsolutePassedThrough(t *testing.T) {
tmpDir := t.TempDir()
// Resolve symlinks (macOS /var -> /private/var).
tmpDir, err := filepath.EvalSymlinks(tmpDir)
require.NoError(t, err)
// Create minimal atmos.yaml.
require.NoError(t, os.WriteFile(
filepath.Join(tmpDir, "atmos.yaml"),
[]byte("base_path: ./\nstacks:\n base_path: stacks\n"),
0o644,
))
require.NoError(t, os.MkdirAll(filepath.Join(tmpDir, "stacks"), 0o755))
configAndStacksInfo := schema.ConfigAndStacksInfo{
AtmosBasePath: tmpDir,
}
atmosConfig, err := InitCliConfig(configAndStacksInfo, false)
require.NoError(t, err)
assert.Equal(t, tmpDir, atmosConfig.BasePath,
"Absolute AtmosBasePath should be used as-is")
}
// TestInitCliConfig_EmptyBasePath_DefaultsToAbsolute verifies that when AtmosBasePath is empty,
// the default resolution produces an absolute path.
func TestInitCliConfig_EmptyBasePath_DefaultsToAbsolute(t *testing.T) {
configAndStacksInfo := schema.ConfigAndStacksInfo{
AtmosBasePath: "",
}
// InitCliConfig with processStacks=false — BasePath gets populated by AtmosConfigAbsolutePaths.
atmosConfig, err := InitCliConfig(configAndStacksInfo, false)
require.NoError(t, err)
// After AtmosConfigAbsolutePaths, BasePath should be absolute (from git root, config dir, or CWD).
if atmosConfig.BasePath != "" {
assert.True(t, filepath.IsAbs(atmosConfig.BasePath),
"Default BasePath should be absolute, got: %s", atmosConfig.BasePath)
}
}
// TestInitCliConfig_EnvVarBasePath_DotSlash_ResolvesRelativeToCWD verifies that when
// ATMOS_BASE_PATH is set with a dot-slash prefix, it resolves relative to CWD.
// This is Tyler's scenario: ATMOS_BASE_PATH=./.terraform/modules/monorepo on Spacelift.
func TestInitCliConfig_EnvVarBasePath_DotSlash_ResolvesRelativeToCWD(t *testing.T) {
tmpDir := t.TempDir()
// Resolve symlinks (macOS /var -> /private/var).
tmpDir, err := filepath.EvalSymlinks(tmpDir)
require.NoError(t, err)
// Create a subdirectory to simulate CWD being a component directory.
subDir := filepath.Join(tmpDir, "components", "terraform", "iam-delegated-roles")
require.NoError(t, os.MkdirAll(subDir, 0o755))
// Create the relative base path target with dot-slash prefix.
relBasePath := filepath.Join(".", ".terraform", "modules", "monorepo")
absTarget := filepath.Join(subDir, ".terraform", "modules", "monorepo")
require.NoError(t, os.MkdirAll(absTarget, 0o755))
// Create minimal atmos.yaml in the target.
require.NoError(t, os.WriteFile(
filepath.Join(absTarget, "atmos.yaml"),
[]byte("base_path: ./\nstacks:\n base_path: stacks\n"),
0o644,
))
require.NoError(t, os.MkdirAll(filepath.Join(absTarget, "stacks"), 0o755))
// Change to the component directory.
t.Chdir(subDir)
// Set ATMOS_BASE_PATH with dot-slash prefix (Tyler's fix).
// Resolves to CWD (not config dir). In shell context, "." means "here" is CWD.
t.Setenv("ATMOS_BASE_PATH", relBasePath)
// No AtmosBasePath in struct — the env var is the source.
configAndStacksInfo := schema.ConfigAndStacksInfo{}
atmosConfig, err := InitCliConfig(configAndStacksInfo, false)
require.NoError(t, err)
// After AtmosConfigAbsolutePaths, BasePathAbsolute should resolve to CWD + relBasePath.
assert.True(t, filepath.IsAbs(atmosConfig.BasePathAbsolute),
"BasePathAbsolute should be absolute, got: %s", atmosConfig.BasePathAbsolute)
assert.Equal(t, absTarget, atmosConfig.BasePathAbsolute,
"ATMOS_BASE_PATH env var with dot-slash should resolve relative to CWD")
}
// TestInitCliConfig_EnvVarBasePath_Dot_ResolvesToCWD verifies that ATMOS_BASE_PATH=.
// Resolves to CWD (not config dir). In shell context, "." means "here" is CWD.
func TestInitCliConfig_EnvVarBasePath_Dot_ResolvesToCWD(t *testing.T) {
tmpDir := t.TempDir()
tmpDir, err := filepath.EvalSymlinks(tmpDir)
require.NoError(t, err)
// Create project layout: config in one dir, CWD in another.
configDir := filepath.Join(tmpDir, "config")
cwdDir := filepath.Join(tmpDir, "workdir")
require.NoError(t, os.MkdirAll(configDir, 0o755))
require.NoError(t, os.MkdirAll(cwdDir, 0o755))
// Create atmos.yaml in configDir.
require.NoError(t, os.WriteFile(
filepath.Join(configDir, "atmos.yaml"),
[]byte("base_path: ./\nstacks:\n base_path: stacks\n"),
0o644,
))
require.NoError(t, os.MkdirAll(filepath.Join(cwdDir, "stacks"), 0o755))
t.Chdir(cwdDir)
t.Setenv("ATMOS_CLI_CONFIG_PATH", configDir)
t.Setenv("ATMOS_BASE_PATH", ".")
t.Setenv("ATMOS_GIT_ROOT_BASEPATH", "false")
atmosConfig, err := InitCliConfig(schema.ConfigAndStacksInfo{}, false)
require.NoError(t, err)
// ATMOS_BASE_PATH=. in shell context should resolve to CWD, not config dir.
assert.Equal(t, cwdDir, atmosConfig.BasePathAbsolute,
"ATMOS_BASE_PATH=. should resolve to CWD (shell convention), not config dir")
}
// TestResolveAbsolutePath_DotPrefix_SourceAware verifies that dot-prefixed paths
// resolve differently based on source: config → config dir, runtime → CWD.
func TestResolveAbsolutePath_DotPrefix_SourceAware(t *testing.T) {
tmpDir := t.TempDir()
tmpDir, err := filepath.EvalSymlinks(tmpDir)
require.NoError(t, err)
configDir := filepath.Join(tmpDir, "config")
cwdDir := filepath.Join(tmpDir, "workdir")
require.NoError(t, os.MkdirAll(configDir, 0o755))
require.NoError(t, os.MkdirAll(cwdDir, 0o755))
t.Chdir(cwdDir)
tests := []struct {
name string
path string
source string
expected string
}{
{
name: "dot from config resolves to config dir",
path: ".",
source: "",
expected: configDir,
},
{
name: "dot from runtime resolves to CWD",
path: ".",
source: "runtime",
expected: cwdDir,
},
{
name: "dot-slash-foo from config resolves to config dir",
path: "./foo",
source: "",
expected: filepath.Join(configDir, "foo"),
},
{
name: "dot-slash-foo from runtime resolves to CWD",
path: "./foo",
source: "runtime",
expected: filepath.Join(cwdDir, "foo"),
},
{
name: "dot-dot from config resolves relative to config dir",
path: "..",
source: "",
expected: tmpDir,
},
{
name: "dot-dot from runtime resolves relative to CWD",
path: "..",
source: "runtime",
expected: tmpDir,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := resolveAbsolutePath(tt.path, configDir, tt.source)
require.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
}
}
// TestResolveAbsolutePath_BarePath_SourceIndependent verifies that bare paths
// (no dot prefix) go through the same git root search regardless of source.
func TestResolveAbsolutePath_BarePath_SourceIndependent(t *testing.T) {
gitRoot := getGitRootOrEmpty()
require.NotEmpty(t, gitRoot, "test requires git root discovery")
// "go.mod" exists at git root — bare path should find it regardless of source.
_, err := os.Stat(filepath.Join(gitRoot, "go.mod"))
require.NoError(t, err)
t.Chdir(filepath.Join(gitRoot, "pkg", "config"))
configResult, err := resolveAbsolutePath("go.mod", "", "")
require.NoError(t, err)
runtimeResult, err := resolveAbsolutePath("go.mod", "", "runtime")
require.NoError(t, err)
// Both should resolve to git root — bare paths are source-independent.
assert.Equal(t, filepath.Join(gitRoot, "go.mod"), configResult,
"bare path from config should resolve via git root")
assert.Equal(t, filepath.Join(gitRoot, "go.mod"), runtimeResult,
"bare path from runtime should resolve via git root (same as config)")
assert.Equal(t, configResult, runtimeResult,
"bare paths must resolve identically regardless of source")
}
// TestTryResolveWithGitRoot_ExistingPathAtGitRoot verifies that when a simple relative path
// exists at the git root, resolveAbsolutePath returns the git-root-joined path.
func TestTryResolveWithGitRoot_ExistingPathAtGitRoot(t *testing.T) {
gitRoot := getGitRootOrEmpty()
require.NotEmpty(t, gitRoot, "test requires git root discovery")
pathAtGitRoot := "go.mod"
_, err := os.Stat(filepath.Join(gitRoot, pathAtGitRoot))
require.NoError(t, err)
t.Chdir(filepath.Join(gitRoot, "pkg", "config"))
resolved, err := resolveAbsolutePath(pathAtGitRoot, "", "")
require.NoError(t, err)
assert.Equal(t, filepath.Join(gitRoot, pathAtGitRoot), resolved)
}
// TestTryResolveWithGitRoot_CWDFallback verifies that when a simple relative path does NOT
// exist at the git root but DOES exist relative to CWD, the resolver falls back to the
// CWD-relative path.
func TestTryResolveWithGitRoot_CWDFallback(t *testing.T) {
gitRoot := getGitRootOrEmpty()
require.NotEmpty(t, gitRoot, "test requires git root discovery")
cwdDir := filepath.Join(gitRoot, "pkg", "config", "testdata-cwd-fallback")
require.NoError(t, os.MkdirAll(cwdDir, 0o755))
t.Cleanup(func() { os.RemoveAll(cwdDir) })
cwdOnlyPath := "test-cwd-fallback-unique-dir-12345"
absExpected := filepath.Join(cwdDir, cwdOnlyPath)
require.NoError(t, os.MkdirAll(absExpected, 0o755))
t.Cleanup(func() { os.RemoveAll(absExpected) })
_, statErr := os.Stat(filepath.Join(gitRoot, cwdOnlyPath))
require.True(t, os.IsNotExist(statErr), "path should not exist at git root")
t.Chdir(cwdDir)
resolved, err := resolveAbsolutePath(cwdOnlyPath, "", "")
require.NoError(t, err)
assert.Equal(t, absExpected, resolved,
"should fall back to CWD-relative path when git root path doesn't exist")
}
// TestTryResolveWithGitRoot_NeitherExists verifies that when a simple relative path exists
// at neither git root nor CWD, the resolver returns the git-root-joined path.
func TestTryResolveWithGitRoot_NeitherExists(t *testing.T) {
gitRoot := getGitRootOrEmpty()
require.NotEmpty(t, gitRoot, "test requires git root discovery")
nonexistentPath := "nonexistent-path-that-should-not-exist-anywhere-12345"
resolved, err := resolveAbsolutePath(nonexistentPath, "", "")
require.NoError(t, err)
assert.Equal(t, filepath.Join(gitRoot, nonexistentPath), resolved,
"should return git-root-joined path when neither location exists")
}
// TestResolveDotPrefixPath_NoConfigPath_FallsToCWD verifies that when source is config
// but no cliConfigPath is provided, dot-prefixed paths fall back to CWD.
func TestResolveDotPrefixPath_NoConfigPath_FallsToCWD(t *testing.T) {
tmpDir := t.TempDir()
tmpDir, err := filepath.EvalSymlinks(tmpDir)
require.NoError(t, err)
cwdDir := filepath.Join(tmpDir, "workdir")
require.NoError(t, os.MkdirAll(cwdDir, 0o755))
t.Chdir(cwdDir)
// Config source with empty cliConfigPath — should fall back to CWD.
result, err := resolveAbsolutePath(".", "", "")
require.NoError(t, err)
assert.Equal(t, cwdDir, result,
"dot from config with no cliConfigPath should fall back to CWD")
// Same for dot-slash-foo.
result, err = resolveAbsolutePath("./sub", "", "")
require.NoError(t, err)
assert.Equal(t, filepath.Join(cwdDir, "sub"), result,
"dot-slash from config with no cliConfigPath should fall back to CWD")
}
// TestResolveDotPrefixPath_DotDotSlash_Runtime verifies "../foo" from runtime resolves to CWD.
func TestResolveDotPrefixPath_DotDotSlash_Runtime(t *testing.T) {
tmpDir := t.TempDir()
tmpDir, err := filepath.EvalSymlinks(tmpDir)
require.NoError(t, err)
cwdDir := filepath.Join(tmpDir, "a", "b")
require.NoError(t, os.MkdirAll(cwdDir, 0o755))
t.Chdir(cwdDir)
result, err := resolveAbsolutePath("../c", filepath.Join(tmpDir, "config"), "runtime")
require.NoError(t, err)
assert.Equal(t, filepath.Join(tmpDir, "a", "c"), result,
"../c from runtime should resolve relative to CWD")
}
// TestResolveAbsolutePath_AbsolutePassThrough verifies absolute paths pass through unchanged.
func TestResolveAbsolutePath_AbsolutePassThrough(t *testing.T) {
// Use a real absolute path from the OS to avoid Windows drive-letter issues.
tmpDir := t.TempDir()
tmpDir, err := filepath.EvalSymlinks(tmpDir)
require.NoError(t, err)
absPath := filepath.Join(tmpDir, "some", "absolute", "path")
configPath := filepath.Join(tmpDir, "config")
result, err := resolveAbsolutePath(absPath, configPath, "")
require.NoError(t, err)
assert.Equal(t, absPath, result, "absolute path should pass through unchanged")
result, err = resolveAbsolutePath(absPath, "", "runtime")
require.NoError(t, err)
assert.Equal(t, absPath, result, "absolute path should pass through regardless of source")
}
// TestTryResolveWithConfigPath_AllBranches covers tryResolveWithConfigPath branches.
func TestTryResolveWithConfigPath_AllBranches(t *testing.T) {
tmpDir := t.TempDir()
tmpDir, err := filepath.EvalSymlinks(tmpDir)
require.NoError(t, err)
configDir := filepath.Join(tmpDir, "config")
require.NoError(t, os.MkdirAll(configDir, 0o755))
cwdDir := filepath.Join(tmpDir, "cwd")
require.NoError(t, os.MkdirAll(cwdDir, 0o755))
t.Chdir(cwdDir)
tests := []struct {
name string
path string
configPath string
expected string
}{
{
name: "empty path with config path returns config path",
path: "",
configPath: configDir,
expected: configDir,
},
{
name: "relative path with config path joins them",
path: "stacks",
configPath: configDir,
expected: filepath.Join(configDir, "stacks"),
},
{
name: "empty path and empty config path returns CWD",
path: "",
configPath: "",
expected: cwdDir,
},
{
name: "relative path with empty config path resolves to CWD",
path: "stacks",
configPath: "",
expected: filepath.Join(cwdDir, "stacks"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := tryResolveWithConfigPath(tt.path, tt.configPath, "")
require.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
}
}
// TestResolveAbsolutePath_BarePathNoGitRoot verifies that bare paths without git root
// fall back to config dir, then CWD.
func TestResolveAbsolutePath_BarePathNoGitRoot(t *testing.T) {
tmpDir := t.TempDir()
tmpDir, err := filepath.EvalSymlinks(tmpDir)
require.NoError(t, err)
configDir := filepath.Join(tmpDir, "config")
require.NoError(t, os.MkdirAll(configDir, 0o755))
t.Chdir(tmpDir)
t.Setenv("ATMOS_GIT_ROOT_BASEPATH", "false")
// Bare path with config dir and no git root → config dir join.
result, err := resolveAbsolutePath("stacks", configDir, "")
require.NoError(t, err)
assert.Equal(t, filepath.Join(configDir, "stacks"), result,
"bare path without git root should resolve via config dir")
// Bare path with no config dir and no git root → CWD join.
result, err = resolveAbsolutePath("stacks", "", "")
require.NoError(t, err)
assert.Equal(t, filepath.Join(tmpDir, "stacks"), result,
"bare path without git root and config dir should resolve via CWD")
}
// =============================================================================
// Bug reproduction tests for: 2026-03-19-failed-to-find-import-no-git-root-fallback
//
// These tests reproduce the scenario where ATMOS_BASE_PATH is set to a bare
// relative path (e.g., ".terraform/modules/monorepo") on a CI worker WITHOUT
// a .git directory. The v1.210.1 fix added os.Stat + CWD fallback to
// tryResolveWithGitRoot, but when git root is unavailable, the code falls
// through to tryResolveWithConfigPath which lacks the same fallback.
// =============================================================================
// TestTryResolveWithConfigPath_CWDFallback_BarePathExistsAtCWD reproduces the
// core bug: when cliConfigPath is set but the config-dir-joined path doesn't
// exist, and the CWD-relative path DOES exist, tryResolveWithConfigPath should
// fall back to the CWD path. Currently it unconditionally returns the
// config-dir-joined path (which doesn't exist).
//
// This is the exact CI scenario:
// - CWD = /workspace/components/terraform/iam-delegated-roles/
// - cliConfigPath = /workspace (where atmos.yaml was found walking up)
// - path = ".terraform/modules/monorepo"
// - /workspace/.terraform/modules/monorepo → DOES NOT EXIST
// - /workspace/components/terraform/iam-delegated-roles/.terraform/modules/monorepo → EXISTS
func TestTryResolveWithConfigPath_CWDFallback_BarePathExistsAtCWD(t *testing.T) {
tmpDir := t.TempDir()
tmpDir, err := filepath.EvalSymlinks(tmpDir)
require.NoError(t, err)
// Simulate CI layout:
// /workspace/
// ├── atmos.yaml (config dir = /workspace)
// └── components/terraform/iam-delegated-roles/
// └── .terraform/modules/monorepo/ (target — exists only here)
workspaceDir := filepath.Join(tmpDir, "workspace")
componentDir := filepath.Join(workspaceDir, "components", "terraform", "iam-delegated-roles")
targetAtCWD := filepath.Join(componentDir, ".terraform", "modules", "monorepo")
require.NoError(t, os.MkdirAll(workspaceDir, 0o755))
require.NoError(t, os.MkdirAll(targetAtCWD, 0o755))
// Verify the path does NOT exist at config dir (workspace root).
targetAtConfigDir := filepath.Join(workspaceDir, ".terraform", "modules", "monorepo")
_, statErr := os.Stat(targetAtConfigDir)
require.True(t, os.IsNotExist(statErr), "path must NOT exist at config dir for this test")
// CWD is the component directory.
t.Chdir(componentDir)
// tryResolveWithConfigPath should fall back to CWD when config-dir path doesn't exist.
// Source is "runtime" because the path comes from ATMOS_BASE_PATH env var.
resolved, err := tryResolveWithConfigPath(".terraform/modules/monorepo", workspaceDir, "runtime")
require.NoError(t, err)
// BUG: currently returns /workspace/.terraform/modules/monorepo (doesn't exist).
// EXPECTED: /workspace/components/terraform/iam-delegated-roles/.terraform/modules/monorepo
assert.Equal(t, targetAtCWD, resolved,
"tryResolveWithConfigPath should fall back to CWD when config-dir path doesn't exist")
}
// TestTryResolveWithConfigPath_ConfigDirPathExists verifies that when the
// config-dir-joined path DOES exist, it is returned (unchanged behavior).
func TestTryResolveWithConfigPath_ConfigDirPathExists(t *testing.T) {
tmpDir := t.TempDir()
tmpDir, err := filepath.EvalSymlinks(tmpDir)
require.NoError(t, err)
configDir := filepath.Join(tmpDir, "config")
targetDir := filepath.Join(configDir, "stacks")
require.NoError(t, os.MkdirAll(targetDir, 0o755))
cwdDir := filepath.Join(tmpDir, "cwd")
require.NoError(t, os.MkdirAll(cwdDir, 0o755))
t.Chdir(cwdDir)
resolved, err := tryResolveWithConfigPath("stacks", configDir, "")
require.NoError(t, err)
assert.Equal(t, targetDir, resolved,
"should return config-dir-joined path when it exists")
}
// TestTryResolveWithConfigPath_NeitherExists verifies that when neither the
// config-dir-joined path nor the CWD-relative path exist, the config-dir-joined
// path is returned (for consistent error messages).
func TestTryResolveWithConfigPath_NeitherExists(t *testing.T) {
tmpDir := t.TempDir()
tmpDir, err := filepath.EvalSymlinks(tmpDir)
require.NoError(t, err)
configDir := filepath.Join(tmpDir, "config")
require.NoError(t, os.MkdirAll(configDir, 0o755))
cwdDir := filepath.Join(tmpDir, "cwd")
require.NoError(t, os.MkdirAll(cwdDir, 0o755))
t.Chdir(cwdDir)
resolved, err := tryResolveWithConfigPath("nonexistent-path-xyz", configDir, "")
require.NoError(t, err)
assert.Equal(t, filepath.Join(configDir, "nonexistent-path-xyz"), resolved,
"should return config-dir-joined path when neither location exists")
}
// TestResolveAbsolutePath_BarePathNoGitRoot_CWDFallback is the end-to-end
// reproduction of the CI bug. It disables git root discovery and verifies that
// a bare path resolves to CWD when the config-dir path doesn't exist.
//
// This reproduces the exact user scenario:
// - ATMOS_GIT_ROOT_BASEPATH=false (no .git on CI)
// - ATMOS_BASE_PATH=.terraform/modules/monorepo (bare path, not dot-prefixed)
// - CWD = component directory (where .terraform/modules/monorepo exists)
// - atmos.yaml found at workspace root (cliConfigPath = workspace root)
// - Expected: resolves to CWD/.terraform/modules/monorepo
// - Actual (bug): resolves to workspace/.terraform/modules/monorepo (doesn't exist)
func TestResolveAbsolutePath_BarePathNoGitRoot_CWDFallback(t *testing.T) {
tmpDir := t.TempDir()
tmpDir, err := filepath.EvalSymlinks(tmpDir)
require.NoError(t, err)
// Layout:
// /tmp/workspace/ (config dir — atmos.yaml location)
// /tmp/workspace/components/terraform/vpc/ (CWD)
// /tmp/workspace/components/terraform/vpc/.terraform/modules/monorepo/ (target)
workspaceDir := filepath.Join(tmpDir, "workspace")
componentDir := filepath.Join(workspaceDir, "components", "terraform", "vpc")
targetDir := filepath.Join(componentDir, ".terraform", "modules", "monorepo")
require.NoError(t, os.MkdirAll(workspaceDir, 0o755))
require.NoError(t, os.MkdirAll(targetDir, 0o755))
// Target must NOT exist at workspace root.
_, statErr := os.Stat(filepath.Join(workspaceDir, ".terraform", "modules", "monorepo"))
require.True(t, os.IsNotExist(statErr))
t.Chdir(componentDir)
t.Setenv("ATMOS_GIT_ROOT_BASEPATH", "false")
resolved, err := resolveAbsolutePath(".terraform/modules/monorepo", workspaceDir, "runtime")
require.NoError(t, err)
// BUG: returns workspace/.terraform/modules/monorepo (doesn't exist).
// EXPECTED: CWD/.terraform/modules/monorepo (exists).
assert.Equal(t, targetDir, resolved,
"bare path with no git root should fall back to CWD when config-dir path doesn't exist")
}
// TestInitCliConfig_BareBasePath_NoGitRoot_CWDFallback is the full integration
// test reproducing the user's CI scenario end-to-end through InitCliConfig.
//
// Simulates:
// - A Spacelift CI worker with no .git directory
// - ATMOS_BASE_PATH=.terraform/modules/monorepo (env var, bare path)
// - CWD is a component directory inside the workspace
// - atmos.yaml exists at the workspace root AND inside the monorepo
// - The monorepo is cloned inside CWD/.terraform/modules/monorepo/
func TestInitCliConfig_BareBasePath_NoGitRoot_CWDFallback(t *testing.T) {
tmpDir := t.TempDir()
tmpDir, err := filepath.EvalSymlinks(tmpDir)
require.NoError(t, err)
// Layout:
// /workspace/
// ├── atmos.yaml (workspace-level config)
// └── components/terraform/iam-delegated-roles/
// └── .terraform/modules/monorepo/
// ├── atmos.yaml (monorepo config)
// ├── stacks/
// │ └── deploy.yaml
// └── components/terraform/
workspaceDir := filepath.Join(tmpDir, "workspace")
componentDir := filepath.Join(workspaceDir, "components", "terraform", "iam-delegated-roles")
monorepoDir := filepath.Join(componentDir, ".terraform", "modules", "monorepo")
// Create workspace-level atmos.yaml.
require.NoError(t, os.MkdirAll(workspaceDir, 0o755))
require.NoError(t, os.WriteFile(
filepath.Join(workspaceDir, "atmos.yaml"),
[]byte("base_path: ./\nstacks:\n base_path: stacks\n included_paths:\n - \"**/*\"\n excluded_paths: []\n name_pattern: \"{stage}\"\n"),
0o644,
))
require.NoError(t, os.MkdirAll(filepath.Join(workspaceDir, "stacks"), 0o755))
// Create monorepo inside component dir.
require.NoError(t, os.MkdirAll(componentDir, 0o755))
require.NoError(t, os.MkdirAll(monorepoDir, 0o755))
require.NoError(t, os.WriteFile(
filepath.Join(monorepoDir, "atmos.yaml"),
[]byte("base_path: ./\nstacks:\n base_path: stacks\n included_paths:\n - \"**/*\"\n excluded_paths: []\n name_pattern: \"{stage}\"\n"),
0o644,
))
require.NoError(t, os.MkdirAll(filepath.Join(monorepoDir, "stacks"), 0o755))
require.NoError(t, os.MkdirAll(filepath.Join(monorepoDir, "components", "terraform"), 0o755))
// Create a minimal stack file so processing doesn't fail.
require.NoError(t, os.WriteFile(
filepath.Join(monorepoDir, "stacks", "deploy.yaml"),
[]byte("vars:\n stage: dev\n"),
0o644,
))
// Set up the CI environment.
t.Chdir(componentDir)
t.Setenv("ATMOS_GIT_ROOT_BASEPATH", "false") // No .git on Spacelift
t.Setenv("ATMOS_BASE_PATH", ".terraform/modules/monorepo")
configAndStacksInfo := schema.ConfigAndStacksInfo{}
atmosConfig, err := InitCliConfig(configAndStacksInfo, false)
require.NoError(t, err)
// BUG: BasePathAbsolute resolves to workspace/.terraform/modules/monorepo (doesn't exist).
// EXPECTED: componentDir/.terraform/modules/monorepo (exists).
assert.Equal(t, monorepoDir, atmosConfig.BasePathAbsolute,
"ATMOS_BASE_PATH bare relative path should resolve to CWD on CI without .git")
// Stacks path should be inside the monorepo.
expectedStacksPath := filepath.Join(monorepoDir, "stacks")
assert.Equal(t, expectedStacksPath, atmosConfig.StacksBaseAbsolutePath,
"stacks base path should be inside the resolved monorepo")
}
// TestInitCliConfig_BasePathSource_SetForStructField verifies that BasePathSource
// is set to "runtime" when AtmosBasePath is provided via struct field.
func TestInitCliConfig_BasePathSource_SetForStructField(t *testing.T) {
tmpDir := t.TempDir()
tmpDir, err := filepath.EvalSymlinks(tmpDir)
require.NoError(t, err)
require.NoError(t, os.WriteFile(
filepath.Join(tmpDir, "atmos.yaml"),
[]byte("base_path: ./\nstacks:\n base_path: stacks\n"),
0o644,
))
require.NoError(t, os.MkdirAll(filepath.Join(tmpDir, "stacks"), 0o755))
configAndStacksInfo := schema.ConfigAndStacksInfo{
AtmosBasePath: tmpDir,
}
atmosConfig, err := InitCliConfig(configAndStacksInfo, false)
require.NoError(t, err)
assert.Equal(t, "runtime", atmosConfig.BasePathSource,
"BasePathSource should be 'runtime' when AtmosBasePath is set via struct field")
}
// TestInitCliConfig_BasePathSource_SetForEnvVar verifies that BasePathSource
// is set to "runtime" when ATMOS_BASE_PATH env var is provided.
func TestInitCliConfig_BasePathSource_SetForEnvVar(t *testing.T) {
tmpDir := t.TempDir()
tmpDir, err := filepath.EvalSymlinks(tmpDir)
require.NoError(t, err)
require.NoError(t, os.WriteFile(
filepath.Join(tmpDir, "atmos.yaml"),
[]byte("base_path: ./\nstacks:\n base_path: stacks\n"),
0o644,
))
require.NoError(t, os.MkdirAll(filepath.Join(tmpDir, "stacks"), 0o755))
t.Chdir(tmpDir)
t.Setenv("ATMOS_BASE_PATH", tmpDir)
atmosConfig, err := InitCliConfig(schema.ConfigAndStacksInfo{}, false)
require.NoError(t, err)
assert.Equal(t, "runtime", atmosConfig.BasePathSource,
"BasePathSource should be 'runtime' when ATMOS_BASE_PATH env var is set")
}
// TestFindAllStackConfigsInPathsForStack_ErrorWrapping verifies that when every
// included_paths entry matches nothing (e.g. the whole stacks directory doesn't exist),
// the function still errors -- with ErrNoStackManifestsFound, not ErrFailedToFindImport.
//
// Prior to cloudposse/atmos#2867's fix, a single entry matching nothing surfaced
// ErrFailedToFindImport directly from inside the per-path loop, which also meant a SECOND,
// valid included_paths entry earlier in the list would have its already-found matches
// discarded by this same hard error. Now, an individual entry matching nothing is treated as
// "nothing here, keep looking" and only the aggregate "nothing matched at all" case (this
// test: the only entry present matches nothing) errors, with a sentinel describing that
// outcome directly instead of leaking the glob-matching implementation's own error identity.
func TestFindAllStackConfigsInPathsForStack_ErrorWrapping(t *testing.T) {
atmosConfig := schema.AtmosConfiguration{
StacksBaseAbsolutePath: filepath.Join(os.TempDir(), "nonexistent-stacks-dir-test"),
}
includeStackPaths := []string{
filepath.Join(os.TempDir(), "nonexistent-stacks-dir-test", "**", "*.yaml"),
}
_, _, _, err := FindAllStackConfigsInPathsForStack(
atmosConfig,
"test-stack",
includeStackPaths,
nil,
)
require.Error(t, err)
assert.True(t, errors.Is(err, errUtils.ErrNoStackManifestsFound),
"Error should wrap ErrNoStackManifestsFound, got: %v", err)
}
// TestFindAllStackConfigsInPaths_ErrorWrapping verifies error wrapping in the non-stack
// variant. See TestFindAllStackConfigsInPathsForStack_ErrorWrapping above for why this is
// ErrNoStackManifestsFound rather than ErrFailedToFindImport post-#2867.
func TestFindAllStackConfigsInPaths_ErrorWrapping(t *testing.T) {
atmosConfig := schema.AtmosConfiguration{
StacksBaseAbsolutePath: filepath.Join(os.TempDir(), "nonexistent-stacks-dir-test2"),
}
includeStackPaths := []string{
filepath.Join(os.TempDir(), "nonexistent-stacks-dir-test2", "**", "*.yaml"),
}
_, _, err := FindAllStackConfigsInPaths(
&atmosConfig,
includeStackPaths,
nil,
)
require.Error(t, err)
assert.True(t, errors.Is(err, errUtils.ErrNoStackManifestsFound),
"Error should wrap ErrNoStackManifestsFound, got: %v", err)
}