-
Notifications
You must be signed in to change notification settings - Fork 12
/
columnize_test.go
157 lines (130 loc) · 2.84 KB
/
columnize_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package datareader
import (
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"testing"
)
const (
// Leave false when testing
generateColumnize = false
)
// Not really a test function, used to generate md5 sums for the results.
func TestGenerateColumnize(t *testing.T) {
if !generateColumnize {
return
}
ms := make(map[string][16]byte)
allTestFiles := getFilenames()
for _, f := range allTestFiles {
for _, mode := range []string{"text", "binary"} {
m := columnizeBase(f, mode)
k := f + "::" + mode
ms[k] = m
}
}
b, err := json.Marshal(ms)
if err != nil {
panic(err)
}
cf, err := os.Create(filepath.Join("test_files", "columnize_checksums.json"))
if err != nil {
panic(err)
}
if _, err := cf.Write(b); err != nil {
panic(err)
}
cf.Close()
}
func columnizeBase(fname, mode string) [16]byte {
// Clear the workspace and set up the subdirectories.
outpath := filepath.Join("test_files", "tmp", "cols")
if err := os.RemoveAll(outpath); err != nil {
panic(err)
}
if err := os.MkdirAll(outpath, os.ModePerm); err != nil {
panic(err)
}
// Run columnize on the file
cmdName := filepath.Join(os.Getenv("GOBIN"), "columnize")
infile := filepath.Join("test_files", "data", fname)
args := []string{
fmt.Sprintf("-in=%s", infile),
fmt.Sprintf("-out=%s", outpath),
fmt.Sprintf("-mode=%s", mode),
}
cmd := exec.Command(cmdName, args...)
cmd.Stderr = os.Stderr
if _, err := cmd.Output(); err != nil {
panic(err)
}
files, err := ioutil.ReadDir(outpath)
if err != nil {
panic(err)
}
var fileNames []string
for _, v := range files {
fileNames = append(fileNames, v.Name())
}
sort.Strings(fileNames)
var buf bytes.Buffer
for _, f := range fileNames {
if strings.HasPrefix(f, ".") {
continue
}
gname := filepath.Join("test_files", "tmp", "cols", f)
g, err := os.Open(gname)
if err != nil {
panic(err)
}
defer g.Close()
ba, err := ioutil.ReadAll(g)
if err != nil {
panic(err)
}
if _, err := buf.Write(ba); err != nil {
panic(err)
}
}
return md5.Sum(buf.Bytes())
}
func TestColumnize1(t *testing.T) {
if generateColumnize {
return
}
cf, err := os.Open(filepath.Join("test_files", "columnize_checksums.json"))
if err != nil {
panic(err)
}
defer cf.Close()
// Read the stored checksums
var checksum map[string][]byte
b, err := ioutil.ReadAll(cf)
if err != nil {
panic(err)
}
if err := json.Unmarshal(b, &checksum); err != nil {
panic(err)
}
allTestFiles := getFilenames()
for _, f := range allTestFiles {
for _, mode := range []string{"text", "binary"} {
m := columnizeBase(f, mode)
m1 := checksum[f+"::"+mode]
for j := range m {
if m[j] != m1[j] {
fmt.Printf("Failing %s mode: %s\n", f, mode)
fmt.Printf("Expected %v, got %v\n", m[j], m1[j])
t.Fail()
}
}
}
}
}