mirror of
https://github.com/coder/coder.git
synced 2025-07-12 00:14:10 +00:00
chore(testutil): extract testutil.CreateZip and testutil.CreateTar helpers (#15540)
Extracts `testutil.CreateTar` and `testutil.CreateZip` test helpers.
This commit is contained in:
61
testutil/archive.go
Normal file
61
testutil/archive.go
Normal file
@ -0,0 +1,61 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/coder/v2/archive"
|
||||
)
|
||||
|
||||
// Creates an in-memory tar of the files provided.
|
||||
// Files in the archive are written with nobody
|
||||
// owner/group, and -rw-rw-rw- permissions.
|
||||
func CreateTar(t testing.TB, files map[string]string) []byte {
|
||||
var buffer bytes.Buffer
|
||||
writer := tar.NewWriter(&buffer)
|
||||
// Keep track of directories previously added.
|
||||
addedDirs := make(map[string]bool)
|
||||
for path, content := range files {
|
||||
// Add parent directories if they don't exist
|
||||
dir := filepath.Dir(path)
|
||||
if dir != "." && !addedDirs[dir] {
|
||||
err := writer.WriteHeader(&tar.Header{
|
||||
Name: dir + "/", // Directory names must end with /
|
||||
Mode: 0o755,
|
||||
Typeflag: tar.TypeDir,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
addedDirs[dir] = true
|
||||
}
|
||||
|
||||
err := writer.WriteHeader(&tar.Header{
|
||||
Name: path,
|
||||
Size: int64(len(content)),
|
||||
Uid: 65534, // nobody
|
||||
Gid: 65534, // nogroup
|
||||
Mode: 0o666, // -rw-rw-rw-
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = writer.Write([]byte(content))
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
err := writer.Flush()
|
||||
require.NoError(t, err)
|
||||
return buffer.Bytes()
|
||||
}
|
||||
|
||||
// Creates an in-memory zip of the files provided.
|
||||
// Uses archive.CreateZipFromTar under the hood.
|
||||
func CreateZip(t testing.TB, files map[string]string) []byte {
|
||||
ta := CreateTar(t, files)
|
||||
tr := tar.NewReader(bytes.NewReader(ta))
|
||||
za, err := archive.CreateZipFromTar(tr, int64(len(ta)))
|
||||
require.NoError(t, err)
|
||||
return za
|
||||
}
|
Reference in New Issue
Block a user