@@ -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+
1219func 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
1973func 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}
0 commit comments