(t *testing.T)
| 11 | ) |
| 12 | |
| 13 | func TestDiscoverVarsFiles(t *testing.T) { |
| 14 | t.Parallel() |
| 15 | |
| 16 | // Given |
| 17 | tempDir, err := os.MkdirTemp(os.TempDir(), "test-discover-vars-files-*") |
| 18 | require.NoError(t, err) |
| 19 | |
| 20 | t.Cleanup(func() { |
| 21 | _ = os.RemoveAll(tempDir) |
| 22 | }) |
| 23 | |
| 24 | testFiles := []string{ |
| 25 | "terraform.tfvars", // ok |
| 26 | "terraform.tfvars.json", // ok |
| 27 | "aaa.tf", // not Terraform vars |
| 28 | "bbb.tf", // not Terraform vars |
| 29 | "example.auto.tfvars", // ok |
| 30 | "example.auto.tfvars.bak", // not Terraform vars |
| 31 | "example.auto.tfvars.json", // ok |
| 32 | "example.auto.tfvars.json.bak", // not Terraform vars |
| 33 | "other_file.txt", // not Terraform vars |
| 34 | "random_file1.tfvars", // should be .auto.tfvars, otherwise ignored |
| 35 | "random_file2.tf", // not Terraform vars |
| 36 | "random_file2.tfvars.json", // should be .auto.tfvars.json, otherwise ignored |
| 37 | "random_file3.auto.tfvars", // ok |
| 38 | "random_file3.tf", // not Terraform vars |
| 39 | "random_file4.auto.tfvars.json", // ok |
| 40 | } |
| 41 | |
| 42 | for _, file := range testFiles { |
| 43 | filePath := filepath.Join(tempDir, file) |
| 44 | err := os.WriteFile(filePath, []byte(""), 0o600) |
| 45 | require.NoError(t, err) |
| 46 | } |
| 47 | |
| 48 | // When |
| 49 | found, err := codersdk.DiscoverVarsFiles(tempDir) |
| 50 | require.NoError(t, err) |
| 51 | |
| 52 | // Then |
| 53 | expected := []string{ |
| 54 | filepath.Join(tempDir, "terraform.tfvars"), |
| 55 | filepath.Join(tempDir, "terraform.tfvars.json"), |
| 56 | filepath.Join(tempDir, "example.auto.tfvars"), |
| 57 | filepath.Join(tempDir, "example.auto.tfvars.json"), |
| 58 | filepath.Join(tempDir, "random_file3.auto.tfvars"), |
| 59 | filepath.Join(tempDir, "random_file4.auto.tfvars.json"), |
| 60 | } |
| 61 | require.EqualValues(t, expected, found) |
| 62 | } |
| 63 | |
| 64 | func TestParseVariableValuesFromVarsFiles(t *testing.T) { |
| 65 | t.Parallel() |
nothing calls this directly
no test coverage detected