| 62 | } |
| 63 | |
| 64 | func TestParseVariableValuesFromVarsFiles(t *testing.T) { |
| 65 | t.Parallel() |
| 66 | |
| 67 | // Given |
| 68 | const ( |
| 69 | hclFilename1 = "file1.tfvars" |
| 70 | hclFilename2 = "file2.tfvars" |
| 71 | jsonFilename3 = "file3.tfvars.json" |
| 72 | jsonFilename4 = "file4.tfvars.json" |
| 73 | |
| 74 | hclContent1 = `region = "us-east-1" |
| 75 | cores = 2` |
| 76 | hclContent2 = `region = "us-west-2" |
| 77 | go_image = ["1.19","1.20","1.21"]` |
| 78 | jsonContent3 = `{"cat": "foobar", "cores": 3}` |
| 79 | jsonContent4 = `{"dog": 4, "go_image": "[\"1.19\",\"1.20\"]"}` |
| 80 | ) |
| 81 | |
| 82 | // Prepare the .tfvars files |
| 83 | tempDir, err := os.MkdirTemp(os.TempDir(), "test-parse-variable-values-from-vars-files-*") |
| 84 | require.NoError(t, err) |
| 85 | t.Cleanup(func() { |
| 86 | _ = os.RemoveAll(tempDir) |
| 87 | }) |
| 88 | |
| 89 | err = os.WriteFile(filepath.Join(tempDir, hclFilename1), []byte(hclContent1), 0o600) |
| 90 | require.NoError(t, err) |
| 91 | err = os.WriteFile(filepath.Join(tempDir, hclFilename2), []byte(hclContent2), 0o600) |
| 92 | require.NoError(t, err) |
| 93 | err = os.WriteFile(filepath.Join(tempDir, jsonFilename3), []byte(jsonContent3), 0o600) |
| 94 | require.NoError(t, err) |
| 95 | err = os.WriteFile(filepath.Join(tempDir, jsonFilename4), []byte(jsonContent4), 0o600) |
| 96 | require.NoError(t, err) |
| 97 | |
| 98 | // When |
| 99 | actual, err := codersdk.ParseUserVariableValues([]string{ |
| 100 | filepath.Join(tempDir, hclFilename1), |
| 101 | filepath.Join(tempDir, hclFilename2), |
| 102 | filepath.Join(tempDir, jsonFilename3), |
| 103 | filepath.Join(tempDir, jsonFilename4), |
| 104 | }, "", nil) |
| 105 | require.NoError(t, err) |
| 106 | |
| 107 | // Then |
| 108 | expected := []codersdk.VariableValue{ |
| 109 | {Name: "cat", Value: "foobar"}, |
| 110 | {Name: "cores", Value: "3"}, |
| 111 | {Name: "dog", Value: "4"}, |
| 112 | {Name: "go_image", Value: "[\"1.19\",\"1.20\"]"}, |
| 113 | {Name: "region", Value: "us-west-2"}, |
| 114 | } |
| 115 | require.Equal(t, expected, actual) |
| 116 | } |
| 117 | |
| 118 | func TestParseVariableValuesFromVarsFiles_InvalidJSON(t *testing.T) { |
| 119 | t.Parallel() |