hashTemplateFilesAndTestName generates a unique hash based on test name and template files.
(t *testing.T, testName string, templateFiles map[string]string)
| 28 | |
| 29 | // hashTemplateFilesAndTestName generates a unique hash based on test name and template files. |
| 30 | func hashTemplateFilesAndTestName(t *testing.T, testName string, templateFiles map[string]string) string { |
| 31 | t.Helper() |
| 32 | |
| 33 | sortedFileNames := make([]string, 0, len(templateFiles)) |
| 34 | for fileName := range templateFiles { |
| 35 | sortedFileNames = append(sortedFileNames, fileName) |
| 36 | } |
| 37 | slices.Sort(sortedFileNames) |
| 38 | |
| 39 | // Inserting a delimiter between the file name and the file content |
| 40 | // ensures that a file named `ab` with content `cd` |
| 41 | // will not hash to the same value as a file named `abc` with content `d`. |
| 42 | // This can still happen if the file name or content include the delimiter, |
| 43 | // but hopefully they won't. |
| 44 | delimiter := []byte("🎉 🌱 🌷") |
| 45 | |
| 46 | hasher := sha256.New() |
| 47 | for _, fileName := range sortedFileNames { |
| 48 | file := templateFiles[fileName] |
| 49 | _, err := hasher.Write([]byte(fileName)) |
| 50 | require.NoError(t, err) |
| 51 | _, err = hasher.Write(delimiter) |
| 52 | require.NoError(t, err) |
| 53 | _, err = hasher.Write([]byte(file)) |
| 54 | require.NoError(t, err) |
| 55 | } |
| 56 | _, err := hasher.Write(delimiter) |
| 57 | require.NoError(t, err) |
| 58 | _, err = hasher.Write([]byte(testName)) |
| 59 | require.NoError(t, err) |
| 60 | |
| 61 | return hex.EncodeToString(hasher.Sum(nil)) |
| 62 | } |
| 63 | |
| 64 | // WriteTFCliConfig writes a Terraform CLI config file (`terraform.rc`) in `dir` to enforce using the local provider mirror. |
| 65 | // This blocks network access for providers, forcing Terraform to use only what's cached in `dir`. |
no test coverage detected