Skip to content

Commit c7a288d

Browse files
committed
Adds tests for non-json output
1 parent e38b2cd commit c7a288d

10 files changed

Lines changed: 80 additions & 99 deletions

File tree

tfconfig/load_post_init_test.go

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,78 @@ import (
99
"testing"
1010
)
1111

12+
func assertEqual[T comparable](t *testing.T, got, want T, msg string) {
13+
t.Helper()
14+
if got != want {
15+
t.Errorf("%s: got %v, want %v", msg, got, want)
16+
}
17+
}
18+
1219
func TestLoadPostInit(t *testing.T) {
13-
testLoadHelper(t, "testdata-post-init", func(path string) interface{} {
14-
dataDir := filepath.Join(path, ".terraform")
15-
return LoadPostInit(path, dataDir)
16-
})
20+
path := "testdata-post-init"
21+
dataDir := filepath.Join(path, ".terraform")
22+
23+
cfg := LoadPostInit(path, dataDir)
24+
25+
if cfg == nil {
26+
t.Fatal("result is nil")
27+
}
28+
29+
assertEqual(t, cfg.Path, path, "path")
30+
assertEqual(t, len(cfg.Providers), 2, "providers count")
31+
assertEqual(t, len(cfg.Modules), 4, "modules count")
32+
33+
// Verify providers
34+
expectedProviders := map[string]LiveProviderInstance{
35+
"registry.terraform.io/hashicorp/aws": {Source: "registry.terraform.io/hashicorp/aws", Version: "5.31.0"},
36+
"registry.terraform.io/hashicorp/random": {Source: "registry.terraform.io/hashicorp/random", Version: "3.6.0"},
37+
}
38+
39+
for name, want := range expectedProviders {
40+
got, ok := cfg.Providers[name]
41+
if !ok {
42+
t.Errorf("provider %q: not found", name)
43+
continue
44+
}
45+
assertEqual(t, got.Source, want.Source, "provider "+name+" source")
46+
assertEqual(t, got.Version, want.Version, "provider "+name+" version")
47+
}
48+
49+
// Verify modules
50+
expectedModules := map[string]LiveModuleInstance{
51+
"vpc": {Source: "registry.terraform.io/terraform-aws-modules/vpc/aws", Version: "5.1.0"},
52+
"ec2": {Source: "registry.terraform.io/terraform-aws-modules/ec2-instance/aws", Version: "5.5.0"},
53+
"local_module": {Source: "./modules/local", Version: ""},
54+
"git_module": {Source: "git::https://example.com/module.git", Version: ""},
55+
}
56+
57+
for name, want := range expectedModules {
58+
got, ok := cfg.Modules[name]
59+
if !ok {
60+
t.Errorf("module %q: not found", name)
61+
continue
62+
}
63+
assertEqual(t, got.Source, want.Source, "module "+name+" source")
64+
assertEqual(t, got.Version, want.Version, "module "+name+" version")
65+
}
66+
67+
// Verify root module (empty key) is skipped
68+
if _, ok := cfg.Modules[""]; ok {
69+
t.Error("root module (empty key) should not be included")
70+
}
1771
}
1872

1973
func TestLoadPostInitFromFilesystem(t *testing.T) {
20-
testLoadHelper(t, "testdata-post-init", func(path string) interface{} {
21-
fs := os.DirFS(".")
22-
dataDir := filepath.Join(path, ".terraform")
23-
return LoadPostInitFromFilesystem(WrapFS(fs), path, WrapFS(fs), dataDir)
24-
})
74+
path := "testdata-post-init"
75+
dataDir := filepath.Join(path, ".terraform")
76+
77+
fs := os.DirFS(".")
78+
cfg := LoadPostInitFromFilesystem(WrapFS(fs), path, WrapFS(fs), dataDir)
79+
80+
if cfg == nil {
81+
t.Fatal("result is nil")
82+
}
83+
84+
assertEqual(t, len(cfg.Providers), 2, "providers count")
85+
assertEqual(t, len(cfg.Modules), 4, "modules count")
2586
}
File renamed without changes.

tfconfig/testdata-post-init/basic/.terraform/modules/modules.json renamed to tfconfig/testdata-post-init/.terraform/modules/modules.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
"Key": "ec2",
1515
"Source": "registry.terraform.io/terraform-aws-modules/ec2-instance/aws",
1616
"Version": "5.5.0"
17+
},
18+
{
19+
"Key": "local_module",
20+
"Source": "./modules/local",
21+
"Version": ""
22+
},
23+
{
24+
"Key": "git_module",
25+
"Source": "git::https://example.com/module.git",
26+
"Version": ""
1727
}
1828
]
1929
}

tfconfig/testdata-post-init/basic/basic.out.json

Lines changed: 0 additions & 23 deletions
This file was deleted.

tfconfig/testdata-post-init/modules-only/.terraform.lock.hcl

Lines changed: 0 additions & 2 deletions
This file was deleted.

tfconfig/testdata-post-init/modules-only/.terraform/modules/modules.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

tfconfig/testdata-post-init/modules-only/modules-only.out.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

tfconfig/testdata-post-init/providers-only/.terraform.lock.hcl

Lines changed: 0 additions & 14 deletions
This file was deleted.

tfconfig/testdata-post-init/providers-only/.terraform/modules/modules.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

tfconfig/testdata-post-init/providers-only/providers-only.out.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)