-
Notifications
You must be signed in to change notification settings - Fork 26
/
jsonpp_test.go
53 lines (47 loc) · 1.2 KB
/
jsonpp_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
package main
import (
"bytes"
"io/ioutil"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestMultipleObjectFiles(t *testing.T) {
testCmd(t, "./testdata/multiple")
}
func TestAlreadyFormattedSingleJSON(t *testing.T) {
testCmd(t, "./testdata/one", "-s")
}
func testCmd(t *testing.T, dir string, argPrefix ...string) {
t.Logf("Reminder: This tests a jsonpp binary in the current directory.")
infos, err := ioutil.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
seen := false
for _, i := range infos {
if strings.HasPrefix(i.Name(), "expected_") {
continue
}
seen = true
path := filepath.Join(dir, i.Name())
expectedPath := filepath.Join(dir, "expected_"+i.Name())
args := append(argPrefix, path)
cmd := exec.Command("./jsonpp", args...)
expected, err := ioutil.ReadFile(expectedPath)
if err != nil {
t.Errorf("unable to read %s: %s", expectedPath, err)
}
out, cerr := cmd.CombinedOutput()
if !bytes.Equal(expected, out) {
if cerr != nil {
t.Logf("jsonpp cmd errored: %s", cerr)
}
t.Errorf("On %#v, expected:\n%s\n=====\nGot:\n%s\n=====", strings.Join(cmd.Args, " "), string(expected), string(out))
}
}
if !seen {
t.Fatalf("no test files in %#v", dir)
}
}