-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstattocsv_test.go
107 lines (86 loc) · 2.16 KB
/
stattocsv_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
package datareader
import (
"crypto/md5"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// runStattocsv runs stattocsv on all the files in the test_files
// directory, and calculates an md5 checksum on each output file.
func runStattocsv(filenames []string) map[string][16]byte {
checksums := make(map[string][16]byte)
cmdName := filepath.Join(os.Getenv("GOBIN"), "stattocsv")
for _, file := range filenames {
infile := filepath.Join("test_files", "data", file)
args := []string{infile}
cmd := exec.Command(cmdName, args...)
cmd.Stderr = os.Stderr
rslt, err := cmd.Output()
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("runStattocsv:: %v %v\n", cmdName, infile))
panic(err)
}
checksums[file] = md5.Sum(rslt)
}
return checksums
}
func refChecksums(filenames []string) map[string][16]byte {
checksums := make(map[string][16]byte)
for _, file := range filenames {
file1 := strings.Replace(file, ".dta", ".csv", -1)
file1 = strings.Replace(file1, ".sas7bdat", ".csv", -1)
var b []byte
infile := filepath.Join("test_files", "ref", file1)
fid, err := os.Open(infile)
if os.IsNotExist(err) {
fid, err = os.Open(infile)
if err != nil {
panic(err)
}
b, err = ioutil.ReadAll(fid)
if err != nil {
panic(err)
}
} else if err != nil {
panic(err)
} else {
b, err = ioutil.ReadAll(fid)
if err != nil {
panic(err)
}
}
checksums[file] = md5.Sum(b)
}
return checksums
}
func getFilenames() []string {
files, err := ioutil.ReadDir(filepath.Join("test_files", "data"))
if err != nil {
panic(err)
}
var filenames []string
for _, f := range files {
name := f.Name()
if strings.HasSuffix(name, ".dta") || strings.HasSuffix(name, ".sas7bdat") {
filenames = append(filenames, name)
}
}
return filenames
}
func TestStattocsv1(t *testing.T) {
testFiles := getFilenames()
newChecksums := runStattocsv(testFiles)
oldChecksums := refChecksums(testFiles)
for ky := range oldChecksums {
for j := 0; j < 16; j++ {
if newChecksums[ky][j] != oldChecksums[ky][j] {
fmt.Printf("%v\n%v\n%v\n\n", ky, newChecksums[ky], oldChecksums[ky])
t.Fail()
}
}
}
}