/
cli_test.go
111 lines (93 loc) · 2.85 KB
/
cli_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
package main
import (
. "github.com/smartystreets/goconvey/convey"
"io/ioutil"
"os"
"testing"
)
func TestCli(t *testing.T) {
Convey("Cli", t, func() {
cli := &Cli{
outputDir: "./.tmp",
schemaDir: "./.tmp",
encoding: "auto",
silent: true,
}
Convey("#run", func() {
os.MkdirAll("./.tmp", 0777)
data, _ := ioutil.ReadFile("./fixtures/masterdata.csv")
ioutil.WriteFile("./.tmp/masterdata.csv", data, 0777)
cli.file = "./.tmp/masterdata.csv"
Convey("should output JSON files", func() {
cli.run()
actual, err := ioutil.ReadFile("./.tmp/masterdata.json")
So(err, ShouldBeNil)
expected, err := ioutil.ReadFile("./fixtures/masterdata.json")
So(err, ShouldBeNil)
So(actual, ShouldResemble, expected)
})
Convey("with fixEncoding option", func() {
cli.fixEncoding = true
Convey("should fix CSV file encoding", func() {
cli.run()
actual, err := ioutil.ReadFile("./.tmp/masterdata.csv")
So(err, ShouldBeNil)
expected, err := ioutil.ReadFile("./fixtures/masterdata-utf-8-bom.csv")
So(err, ShouldBeNil)
So(actual, ShouldResemble, expected)
})
Convey("should add BOM to UTF-8 CSV", func() {
data, _ := ioutil.ReadFile("./fixtures/masterdata-utf-8.csv")
ioutil.WriteFile("./.tmp/masterdata-utf-8.csv", data, 0777)
cli.file = "./.tmp/masterdata-utf-8.csv"
cli.run()
actual, err := ioutil.ReadFile("./.tmp/masterdata-utf-8.csv")
So(err, ShouldBeNil)
expected, err := ioutil.ReadFile("./fixtures/masterdata-utf-8-bom.csv")
So(err, ShouldBeNil)
So(actual, ShouldResemble, expected)
})
})
Convey("with outputSchema option", func() {
cli.outputSchema = true
Convey("should output JSON Schema files", func() {
cli.run()
actual, err := ioutil.ReadFile("./.tmp/masterdata.schema.json")
So(err, ShouldBeNil)
expected, err := ioutil.ReadFile("./fixtures/masterdata.schema.json")
So(err, ShouldBeNil)
So(actual, ShouldResemble, expected)
})
})
Convey("with noOutputFile option", func() {
cli.noOutputFile = true
Convey("should not output JSON files", func() {
cli.run()
_, err := ioutil.ReadFile("./.tmp/masterdata.json")
So(err, ShouldNotBeNil)
})
})
Reset(func() {
os.RemoveAll("./.tmp")
})
})
Convey("#masterDataList", func() {
Convey("should return master data list", func() {
cli.file = "./fixtures/masterdata.csv"
actual := cli.masterDataList()[0]
So(actual.json(), ShouldContainSubstring, "ムーミン")
})
})
Convey("#csvFilePaths", func() {
Convey("should return target csv file paths", func() {
cli.dir = "./fixtures"
actual := cli.csvFilePaths()
So(actual, ShouldResemble, []string{
"fixtures/masterdata-utf-8-bom.csv",
"fixtures/masterdata-utf-8.csv",
"fixtures/masterdata.csv",
})
})
})
})
}