Skip to content

Commit a2969b8

Browse files
committed
add directories to archive
1 parent fcfd5bc commit a2969b8

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

testutil/archive.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package testutil
33
import (
44
"archive/tar"
55
"bytes"
6+
"path/filepath"
67
"testing"
78

89
"github.com/stretchr/testify/require"
@@ -16,7 +17,21 @@ import (
1617
func CreateTar(t testing.TB, files map[string]string) []byte {
1718
var buffer bytes.Buffer
1819
writer := tar.NewWriter(&buffer)
20+
// Keep track of directories previously added.
21+
addedDirs := make(map[string]bool)
1922
for path, content := range files {
23+
// Add parent directories if they don't exist
24+
dir := filepath.Dir(path)
25+
if dir != "." && !addedDirs[dir] {
26+
err := writer.WriteHeader(&tar.Header{
27+
Name: dir + "/", // Directory names must end with /
28+
Mode: 0o755,
29+
Typeflag: tar.TypeDir,
30+
})
31+
require.NoError(t, err)
32+
addedDirs[dir] = true
33+
}
34+
2035
err := writer.WriteHeader(&tar.Header{
2136
Name: path,
2237
Size: int64(len(content)),

0 commit comments

Comments
 (0)