forked from blakesmith/ar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_test.go
71 lines (60 loc) · 1.36 KB
/
writer_test.go
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
69
70
71
package ar
import (
"bytes"
"io/ioutil"
"os"
"testing"
"time"
)
func TestGlobalHeaderWrite(t *testing.T) {
var buf bytes.Buffer
writer := NewWriter(&buf)
if err := writer.WriteGlobalHeader(); err != nil {
t.Errorf(err.Error())
}
globalHeader := buf.Bytes()
expectedHeader := []byte("!<arch>\n")
if !bytes.Equal(globalHeader, expectedHeader) {
t.Errorf("Global header should be %s but is %s", expectedHeader, globalHeader)
}
}
func TestSimpleFile(t *testing.T) {
hdr := new(Header)
body := "Hello world!\n"
hdr.ModTime = time.Unix(1361157466, 0)
hdr.Name = "hello.txt"
hdr.Size = int64(len(body))
hdr.Mode = 0644
hdr.Uid = 501
hdr.Gid = 20
var buf bytes.Buffer
writer := NewWriter(&buf)
writer.WriteGlobalHeader()
writer.WriteHeader(hdr)
_, err := writer.Write([]byte(body))
if err != nil {
t.Errorf(err.Error())
}
f, _ := os.Open("./fixtures/hello.a")
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
t.Errorf(err.Error())
}
actual := buf.Bytes()
if !bytes.Equal(b, actual) {
t.Errorf("Expected %s to equal %s", actual, b)
}
}
func TestWriteTooLong(t *testing.T) {
body := "Hello world!\n"
hdr := new(Header)
hdr.Size = 1
var buf bytes.Buffer
writer := NewWriter(&buf)
writer.WriteHeader(hdr)
_, err := writer.Write([]byte(body))
if err != ErrWriteTooLong {
t.Errorf("Error should have been: %s", ErrWriteTooLong)
}
}