MCPcopy Index your code
hub / github.com/coder/coder / TestGetModulesArchive

Function TestGetModulesArchive

provisioner/terraform/modules_internal_test.go:24–243  ·  view source on GitHub ↗

The .tar archive is different on Windows because of git converting LF line endings to CRLF line endings, so many of the assertions in this test are platform specific.

(t *testing.T)

Source from the content-addressed store, hash-verified

22// endings to CRLF line endings, so many of the assertions in this test are
23// platform specific.
24func TestGetModulesArchive(t *testing.T) {
25 t.Parallel()
26 if runtime.GOOS == "windows" {
27 t.Skip("Windows path separators and newline handling make this test unreliable.")
28 }
29
30 t.Run("Success", func(t *testing.T) {
31 t.Parallel()
32
33 archive, skipped, err := GetModulesArchive(os.DirFS(filepath.Join("testdata", "modules-source-caching")))
34 require.NoError(t, err)
35 require.Len(t, skipped, 0)
36
37 // Check that all of the files it should contain are correct
38 b := bytes.NewBuffer(archive)
39 tarfs := archivefs.FromTarReader(b)
40
41 content, err := fs.ReadFile(tarfs, ".terraform/modules/modules.json")
42 require.NoError(t, err)
43 require.True(t, strings.HasPrefix(string(content), `{"Modules":[{"Key":"","Source":"","Dir":"."},`))
44
45 dirFiles, err := fs.ReadDir(tarfs, ".terraform/modules/example_module")
46 require.NoError(t, err)
47 require.Len(t, dirFiles, 1)
48 require.Equal(t, "main.tf", dirFiles[0].Name())
49
50 content, err = fs.ReadFile(tarfs, ".terraform/modules/example_module/main.tf")
51 require.NoError(t, err)
52 require.True(t, strings.HasPrefix(string(content), "terraform {"))
53 if runtime.GOOS != "windows" {
54 require.Len(t, content, 3691)
55 } else {
56 require.Len(t, content, 3812)
57 }
58
59 _, err = fs.ReadFile(tarfs, ".terraform/modules/stuff_that_should_not_be_included/nothing.txt")
60 require.Error(t, err)
61
62 // It should always be byte-identical to optimize storage
63 hashBytes := sha256.Sum256(archive)
64 hash := hex.EncodeToString(hashBytes[:])
65 if runtime.GOOS != "windows" {
66 require.Equal(t, "edcccdd4db68869552542e66bad87a51e2e455a358964912805a32b06123cb5c", hash)
67 } else {
68 require.Equal(t, "67027a27452d60ce2799fcfd70329c185f9aee7115b0944e3aa00b4776be9d92", hash)
69 }
70 })
71
72 t.Run("EmptyDirectory", func(t *testing.T) {
73 t.Parallel()
74
75 root := afero.NewMemMapFs()
76 afero.WriteFile(root, ".terraform/modules/modules.json", []byte(`{"Modules":[{"Key":"","Source":"","Dir":"."}]}`), 0o644)
77
78 archive, skipped, err := GetModulesArchive(afero.NewIOFS(root))
79 require.NoError(t, err)
80 require.Len(t, skipped, 0)
81 require.Equal(t, []byte{}, archive)

Callers

nothing calls this directly

Calls 13

GetModulesArchiveFunction · 0.85
moduleArchiveFSFunction · 0.85
SkipMethod · 0.80
ReadDirMethod · 0.80
RunMethod · 0.65
ReadFileMethod · 0.65
NameMethod · 0.65
WriteFileMethod · 0.65
LenMethod · 0.45
EqualMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected