@@ -11,22 +11,15 @@ import (
1111 "github.com/stretchr/testify/assert"
1212)
1313
14- // setupTestModule copies the module to a temp directory and injects a provider file.
15- // This avoids race conditions when running tests in parallel and keeps the source clean.
1614func setupTestModule (t * testing.T , vars map [string ]any ) string {
1715 moduleDir := "../"
1816
19- // Copy all .tf files to temp directory
2017 tempDir , err := files .CopyTerraformFolderToTemp (moduleDir , t .Name ())
2118 if err != nil {
22- // Fallback: manually copy files
2319 tempDir = t .TempDir ()
2420 copyTerraformFiles (t , moduleDir , tempDir )
2521 }
2622
27- // Inject provider configuration
28- // Note: Variable validations are evaluated BEFORE provider authentication,
29- // so these tests work offline. The provider config is only needed for syntax.
3023 // We only need to inject the provider block, as required_providers is now in versions.tf
3124 providerContent := "provider \" azurerm\" {\n features {}\n }\n "
3225 providerPath := filepath .Join (tempDir , "provider_test.tf" )
@@ -35,13 +28,11 @@ func setupTestModule(t *testing.T, vars map[string]any) string {
3528 t .Fatalf ("failed to write provider file: %v" , err )
3629 }
3730
38- // Write variables to a .tfvars file (terraform validate doesn't accept -var flags)
3931 writeVarsFile (t , tempDir , vars )
4032
4133 return tempDir
4234}
4335
44- // writeVarsFile writes variables to a terraform.tfvars file
4536func writeVarsFile (t * testing.T , dir string , vars map [string ]any ) {
4637 var content string
4738 for k , v := range vars {
@@ -68,18 +59,14 @@ func writeVarsFile(t *testing.T, dir string, vars map[string]any) {
6859 }
6960}
7061
71- // copyTerraformFiles copies .tf files from src to dst
7262func copyTerraformFiles (t * testing.T , src , dst string ) {
7363 entries , err := os .ReadDir (src )
7464 if err != nil {
7565 t .Fatalf ("failed to read source directory: %v" , err )
7666 }
7767
7868 for _ , entry := range entries {
79- if entry .IsDir () {
80- continue
81- }
82- if filepath .Ext (entry .Name ()) != ".tf" {
69+ if entry .IsDir () || filepath .Ext (entry .Name ()) != ".tf" {
8370 continue
8471 }
8572
@@ -98,7 +85,6 @@ func copyTerraformFiles(t *testing.T, src, dst string) {
9885 }
9986}
10087
101- // TestManagedIdentity_ValidInputs verifies the module accepts valid configurations.
10288func TestManagedIdentity_ValidInputs (t * testing.T ) {
10389 t .Parallel ()
10490
@@ -114,7 +100,7 @@ func TestManagedIdentity_ValidInputs(t *testing.T) {
114100 },
115101 {
116102 name : "minimum_length_name" ,
117- uaiName : "abc" , // Exactly 3 characters (minimum)
103+ uaiName : "abc" ,
118104 tags : nil ,
119105 },
120106 {
@@ -152,9 +138,7 @@ func TestManagedIdentity_ValidInputs(t *testing.T) {
152138 }
153139}
154140
155- // TestManagedIdentity_InvalidName verifies the module rejects invalid identity names.
156- // Note: Variable validation rules are only evaluated during 'plan' or 'apply', not 'validate'.
157- // Therefore, we use InitAndPlanE to trigger the validation checks.
141+ // Variable validation rules are only evaluated during 'plan' or 'apply', not 'validate'.
158142func TestManagedIdentity_InvalidName (t * testing.T ) {
159143 t .Parallel ()
160144
@@ -166,12 +150,12 @@ func TestManagedIdentity_InvalidName(t *testing.T) {
166150 {
167151 name : "too_short_one_char" ,
168152 uaiName : "a" ,
169- description : "single character should be rejected (minimum is 3) " ,
153+ description : "single character should be rejected" ,
170154 },
171155 {
172156 name : "too_short_two_chars" ,
173157 uaiName : "ab" ,
174- description : "two characters should be rejected (minimum is 3) " ,
158+ description : "two characters should be rejected" ,
175159 },
176160 {
177161 name : "starts_with_hyphen" ,
@@ -206,7 +190,6 @@ func TestManagedIdentity_InvalidName(t *testing.T) {
206190 NoColor : true ,
207191 }
208192
209- // Use InitAndPlanE because variable validations are only evaluated during plan/apply
210193 _ , err := terraform .InitAndPlanE (t , terraformOptions )
211194
212195 assert .Error (t , err , tc .description )
0 commit comments