File tree 1 file changed +15
-0
lines changed
1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package testutil
3
3
import (
4
4
"archive/tar"
5
5
"bytes"
6
+ "path/filepath"
6
7
"testing"
7
8
8
9
"github.com/stretchr/testify/require"
@@ -16,7 +17,21 @@ import (
16
17
func CreateTar (t testing.TB , files map [string ]string ) []byte {
17
18
var buffer bytes.Buffer
18
19
writer := tar .NewWriter (& buffer )
20
+ // Keep track of directories previously added.
21
+ addedDirs := make (map [string ]bool )
19
22
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
+
20
35
err := writer .WriteHeader (& tar.Header {
21
36
Name : path ,
22
37
Size : int64 (len (content )),
You can’t perform that action at this time.
0 commit comments