-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
74 lines (68 loc) · 1.32 KB
/
integration_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
72
73
74
package main
import (
"bytes"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"testing"
)
const INPUT = "gutenberg/gutenberg.tar"
func mustReadFile(path string) []byte {
f, err := os.Open(path)
if err != nil {
panic(err)
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
return b
}
func findFile(root dir, name string) file {
for _, f := range root.files() {
if strings.Contains(f.name(), name) {
return f
}
}
for _, d := range root.dirs() {
f := findFile(d, name)
if f != nil {
return f
}
}
return nil
}
func TestGutenbergContents(t *testing.T) {
b := mustReadFile(INPUT)
dir := mustNewDirFromTar(b)
expected, err := filepath.Glob("gutenberg/expected/*.txt")
if err != nil {
panic(err)
}
contents := map[string][]byte{}
for _, p := range expected {
name := path.Base(p)
contents[name] = mustReadFile(p)
}
for name, content := range contents {
f := findFile(dir, name)
if f == nil {
t.Fatalf("Did not found '%v'", name)
}
rc, err := f.readCloser()
if err != nil {
t.Fatalf("failed to open file '%v': %v", f.name(), err)
}
b, err := ioutil.ReadAll(rc)
if err != nil {
t.Fatalf("failed to read file '%v': %v", f.name(), err)
}
if bytes.Compare(content, b) != 0 {
t.Fatalf("Unexpected content for %v", name)
}
rc.Close()
}
}