@@ -14,7 +14,11 @@ func TestTerraform(t *testing.T) {
1414 cases := []struct {
1515 Case string
1616 Template string
17+ Command string
1718 WorkspaceName string
19+ TenvEnv string
20+ TenvFile string
21+ TenvFileContent string
1822 ExpectedString string
1923 HasTfCommand bool
2024 HasTfFolder bool
@@ -79,21 +83,120 @@ func TestTerraform(t *testing.T) {
7983 HasTfFiles : true ,
8084 HasTfCommand : true ,
8185 },
86+ {
87+ Case : "terraform version from .terraform-version file" ,
88+ ExpectedString : "1.7.0" ,
89+ ExpectedEnabled : true ,
90+ WorkspaceName : "default" ,
91+ Template : "{{ .Version }}" ,
92+ HasTfFolder : true ,
93+ HasTfCommand : true ,
94+ FetchVersion : true ,
95+ TenvFile : ".terraform-version" ,
96+ TenvFileContent : "1.7.0\n " ,
97+ },
98+ {
99+ Case : "terraform version from TFENV_TERRAFORM_VERSION env" ,
100+ ExpectedString : "1.8.0" ,
101+ ExpectedEnabled : true ,
102+ WorkspaceName : "default" ,
103+ Template : "{{ .Version }}" ,
104+ HasTfFolder : true ,
105+ HasTfCommand : true ,
106+ FetchVersion : true ,
107+ TenvEnv : "1.8.0" ,
108+ },
109+ {
110+ Case : "env takes precedence over .terraform-version file" ,
111+ ExpectedString : "1.8.0" ,
112+ ExpectedEnabled : true ,
113+ WorkspaceName : "default" ,
114+ Template : "{{ .Version }}" ,
115+ HasTfFolder : true ,
116+ HasTfCommand : true ,
117+ FetchVersion : true ,
118+ TenvEnv : "1.8.0" ,
119+ TenvFile : ".terraform-version" ,
120+ TenvFileContent : "1.7.0" ,
121+ },
122+ {
123+ Case : "tenv file takes precedence over terraform files" ,
124+ ExpectedString : "1.7.0" ,
125+ ExpectedEnabled : true ,
126+ WorkspaceName : "default" ,
127+ Template : "{{ .Version }}" ,
128+ HasTfVersionFiles : true ,
129+ HasTfCommand : true ,
130+ FetchVersion : true ,
131+ TenvFile : ".terraform-version" ,
132+ TenvFileContent : "1.7.0" ,
133+ },
134+ {
135+ Case : "enabled by .terraform-version alone" ,
136+ ExpectedString : "1.7.0" ,
137+ ExpectedEnabled : true ,
138+ WorkspaceName : "default" ,
139+ Template : "{{ .Version }}" ,
140+ HasTfCommand : true ,
141+ FetchVersion : true ,
142+ TenvFile : ".terraform-version" ,
143+ TenvFileContent : "1.7.0" ,
144+ },
145+ {
146+ Case : "tofu version from .opentofu-version file" ,
147+ ExpectedString : "1.8.5" ,
148+ ExpectedEnabled : true ,
149+ WorkspaceName : "default" ,
150+ Template : "{{ .Version }}" ,
151+ Command : "tofu" ,
152+ HasTfFolder : true ,
153+ HasTfCommand : true ,
154+ FetchVersion : true ,
155+ TenvFile : ".opentofu-version" ,
156+ TenvFileContent : "1.8.5" ,
157+ },
158+ {
159+ Case : "tofu version from TOFUENV_TOFU_VERSION env" ,
160+ ExpectedString : "1.9.0" ,
161+ ExpectedEnabled : true ,
162+ WorkspaceName : "default" ,
163+ Template : "{{ .Version }}" ,
164+ Command : "tofu" ,
165+ HasTfFolder : true ,
166+ HasTfCommand : true ,
167+ FetchVersion : true ,
168+ TenvEnv : "1.9.0" ,
169+ },
82170 }
83171
84172 for _ , tc := range cases {
85173 env := new (mock.Environment )
86174
87- env .On ("HasCommand" , "terraform" ).Return (tc .HasTfCommand )
175+ cmd := tc .Command
176+ if cmd == "" {
177+ cmd = "terraform"
178+ }
179+
180+ tenvEnvVar := "TFENV_TERRAFORM_VERSION"
181+ tenvFile := ".terraform-version"
182+ if cmd == "tofu" {
183+ tenvEnvVar = "TOFUENV_TOFU_VERSION"
184+ tenvFile = ".opentofu-version"
185+ }
186+
187+ env .On ("HasCommand" , cmd ).Return (tc .HasTfCommand )
88188 env .On ("HasFolder" , ".terraform" ).Return (tc .HasTfFolder )
89189 env .On ("HasFiles" , ".tf" ).Return (tc .HasTfFiles )
90190 env .On ("HasFiles" , ".tfplan" ).Return (tc .HasTfFiles )
91191 env .On ("HasFiles" , ".tfstate" ).Return (tc .HasTfFiles )
92192 env .On ("Pwd" ).Return ("" )
93- env .On ("RunCommand" , "terraform" , []string {"workspace" , "show" }).Return (tc .WorkspaceName , nil )
193+ env .On ("RunCommand" , cmd , []string {"workspace" , "show" }).Return (tc .WorkspaceName , nil )
94194 env .On ("HasFiles" , "versions.tf" ).Return (tc .HasTfVersionFiles )
95195 env .On ("HasFiles" , "main.tf" ).Return (tc .HasTfVersionFiles )
96196 env .On ("HasFiles" , "terraform.tfstate" ).Return (tc .HasTfStateFile )
197+ env .On ("Getenv" , tenvEnvVar ).Return (tc .TenvEnv )
198+ env .On ("HasFiles" , tenvFile ).Return (tc .TenvFile == tenvFile )
199+ env .On ("FileContent" , tenvFile ).Return (tc .TenvFileContent )
97200 if tc .HasTfVersionFiles {
98201 content , _ := os .ReadFile ("../test/versions.tf" )
99202 env .On ("FileContent" , "versions.tf" ).Return (string (content ))
@@ -105,6 +208,7 @@ func TestTerraform(t *testing.T) {
105208
106209 props := options.Map {
107210 options .FetchVersion : tc .FetchVersion ,
211+ Command : cmd ,
108212 }
109213
110214 tf := & Terraform {}
0 commit comments