forked from peterebden/ar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader_test.go
More file actions
68 lines (62 loc) · 1.72 KB
/
Copy pathreader_test.go
File metadata and controls
68 lines (62 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package ar
import (
"bytes"
"fmt"
"io"
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestReadHeader(t *testing.T) {
f, err := os.Open("./test_data/hello.a")
require.NoError(t, err)
defer f.Close()
reader, err := NewReader(f)
require.NoError(t, err)
header, err := reader.Next()
require.NoError(t, err)
assert.Equal(t, "hello.txt", header.Name)
assert.Equal(t, time.Unix(1361157466, 0), header.ModTime)
assert.Equal(t, 501, header.Uid)
assert.Equal(t, 20, header.Gid)
assert.Equal(t, int64(0100644), header.Mode)
}
func TestLongFilenames(t *testing.T) {
for _, tc := range []struct {
Description string
Variant Variant
ArchivePath string
}{
{"BSD format", BSD, "./test_data/long_filenames_bsd.a"},
{"GNU format", GNU, "./test_data/long_filenames_gnu.a"},
} {
t.Run(tc.Description, func(t *testing.T) {
f, err := os.Open(tc.ArchivePath)
require.NoError(t, err)
defer f.Close()
reader, err := NewReader(f)
require.NoError(t, err)
assert.Equal(t, tc.Variant, reader.Variant())
for i := 1; i <= 20; i++ {
t.Run("File "+strconv.Itoa(i), func(t *testing.T) {
hdr, err := reader.Next()
require.NoError(t, err)
var buf bytes.Buffer
assert.Equal(t, fmt.Sprintf("%d%s", i, strings.Repeat("x", i - len(strconv.Itoa(i)))), hdr.Name)
io.Copy(&buf, reader)
expected := []byte(fmt.Sprintf("The name of this file contains %d character(s).\n", i))
assert.Equal(t, hdr.Size, int64(len(expected)))
actual := buf.Bytes()
assert.Equal(t, expected, actual)
})
}
hdr, err := reader.Next()
assert.Nil(t, hdr, "No files left to read")
assert.ErrorIs(t, err, io.EOF)
})
}
}