Skip to content

chore(testutil): extract testutil.CreateZip and testutil.CreateTar helpers #15540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 18, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add directories to archive
  • Loading branch information
johnstcn committed Nov 18, 2024
commit a2969b873e7ed1c19c4261c756e8eca8b889435c
15 changes: 15 additions & 0 deletions testutil/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testutil
import (
"archive/tar"
"bytes"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -16,7 +17,21 @@ import (
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)),
Expand Down
Loading