github.phpd.cn/thought-machine/please@v12.2.0+incompatible/tools/jarcat/tar/tar_test.go (about) 1 package tar 2 3 import ( 4 "archive/tar" 5 "bytes" 6 "compress/gzip" 7 "io" 8 "os" 9 "strings" 10 "testing" 11 "time" 12 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 ) 16 17 var testInputs = []string{"tools/jarcat/tar/test_data/dir1", "tools/jarcat/tar/test_data/dir2"} 18 19 func TestNoCompression(t *testing.T) { 20 filename := "test_no_compression.tar" 21 err := Write(filename, testInputs, "", false) 22 require.NoError(t, err) 23 24 m := ReadTar(t, filename, false) 25 assert.EqualValues(t, map[string]string{ 26 "dir1/file1.txt": "test file 1", 27 "dir2/file2.txt": "test file 2", 28 }, toFilenameMap(m)) 29 30 // All the timestamps should be fixated and there should be no user/group id. 31 var zeroTime time.Time 32 for h := range m { 33 assert.EqualValues(t, mtime, h.ModTime.In(time.UTC)) 34 // These two seem to always be zero regardless of what we send in. 35 // We don't really care as long as they're always the same. 36 assert.EqualValues(t, zeroTime, h.AccessTime) 37 assert.EqualValues(t, zeroTime, h.ChangeTime) 38 assert.EqualValues(t, 0, h.Uid) 39 assert.EqualValues(t, 0, h.Gid) 40 } 41 } 42 43 func TestCompression(t *testing.T) { 44 filename := "test_compression.tar.gz" 45 err := Write(filename, testInputs, "", true) 46 require.NoError(t, err) 47 48 m := ReadTar(t, filename, true) 49 assert.EqualValues(t, map[string]string{ 50 "dir1/file1.txt": "test file 1", 51 "dir2/file2.txt": "test file 2", 52 }, toFilenameMap(m)) 53 } 54 55 func TestWithPrefix(t *testing.T) { 56 filename := "test_prefix.tar" 57 err := Write(filename, testInputs, "/", false) 58 require.NoError(t, err) 59 60 m := ReadTar(t, filename, false) 61 assert.EqualValues(t, map[string]string{ 62 "/dir1/file1.txt": "test file 1", 63 "/dir2/file2.txt": "test file 2", 64 }, toFilenameMap(m)) 65 } 66 67 // ReadTar is a test utility that reads all the files from a tarball and returns a map of 68 // their headers -> their contents. 69 func ReadTar(t *testing.T, filename string, compress bool) map[*tar.Header]string { 70 f, err := os.Open(filename) 71 require.NoError(t, err) 72 if compress { 73 r, err := gzip.NewReader(f) 74 require.NoError(t, err) 75 return readTar(t, r) 76 } 77 return readTar(t, f) 78 } 79 80 // readTar is a test utility that reads all the files from a tarball and returns a map of 81 // their headers -> their contents. 82 func readTar(t *testing.T, r io.Reader) map[*tar.Header]string { 83 tr := tar.NewReader(r) 84 m := map[*tar.Header]string{} 85 for { 86 hdr, err := tr.Next() 87 if err == io.EOF { 88 break 89 } 90 require.NoError(t, err) 91 var buf bytes.Buffer 92 _, err = io.Copy(&buf, tr) 93 require.NoError(t, err) 94 m[hdr] = strings.TrimSpace(buf.String()) // Don't worry about newline, they're just test files... 95 } 96 return m 97 } 98 99 // toFilenameMap converts one of the maps returned by above to a map of filenames to contents. 100 func toFilenameMap(m map[*tar.Header]string) map[string]string { 101 r := map[string]string{} 102 for hdr, contents := range m { 103 r[hdr.Name] = contents 104 } 105 return r 106 }