From e074ac385c15608fe52af7b75ba2642bc02d3eaf Mon Sep 17 00:00:00 2001 From: Arseny Balobanov Date: Mon, 1 Mar 2021 17:10:47 +0300 Subject: [PATCH] [gitfame] Add format tests. --- gitfame/test/integration/gitfame_test.go | 41 ++++++++++++++++++- .../testdata/tests/27/description.yaml | 5 +++ .../testdata/tests/27/expected.out | 17 ++++++++ .../testdata/tests/28/description.yaml | 6 +++ .../testdata/tests/28/expected.out | 1 + .../testdata/tests/29/description.yaml | 6 +++ .../testdata/tests/29/expected.out | 16 ++++++++ 7 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 gitfame/test/integration/testdata/tests/27/description.yaml create mode 100644 gitfame/test/integration/testdata/tests/27/expected.out create mode 100644 gitfame/test/integration/testdata/tests/28/description.yaml create mode 100644 gitfame/test/integration/testdata/tests/28/expected.out create mode 100644 gitfame/test/integration/testdata/tests/29/description.yaml create mode 100644 gitfame/test/integration/testdata/tests/29/expected.out diff --git a/gitfame/test/integration/gitfame_test.go b/gitfame/test/integration/gitfame_test.go index 08cdbe0..e7e51a5 100644 --- a/gitfame/test/integration/gitfame_test.go +++ b/gitfame/test/integration/gitfame_test.go @@ -1,6 +1,7 @@ package integration import ( + "bytes" "io/ioutil" "os" "os/exec" @@ -57,7 +58,7 @@ func TestGitFame(t *testing.T) { output, err := cmd.Output() if !tc.Error { require.NoError(t, err) - require.Equal(t, string(tc.Expected), string(output)) + CompareResults(t, tc.Expected, output, tc.Format) } else { require.Error(t, err) _, ok := err.(*exec.ExitError) @@ -115,6 +116,7 @@ type TestDescription struct { Args []string `yaml:"args"` Bundle string `yaml:"bundle"` Error bool `yaml:"error"` + Format string `yaml:"format,omitempty"` } func ReadTestDescription(t *testing.T, path string) *TestDescription { @@ -135,3 +137,40 @@ func Unbundle(t *testing.T, src, dst string) { cmd := exec.Command("git", "clone", src, dst) require.NoError(t, cmd.Run()) } + +func CompareResults(t *testing.T, expected, actual []byte, format string) { + t.Helper() + + switch format { + case "json": + if len(expected) == 0 { + require.Empty(t, string(actual)) + } else { + require.JSONEq(t, string(expected), string(actual)) + } + case "json-lines": + if len(expected) == 0 { + require.Empty(t, string(actual)) + } else { + CompareJSONLines(t, expected, actual) + } + default: + require.Equal(t, string(expected), string(actual)) + } +} + +func CompareJSONLines(t *testing.T, expected, actual []byte) { + t.Helper() + + expectedLines := ParseJSONLines(expected) + actualLines := ParseJSONLines(actual) + require.Equal(t, len(expectedLines), len(actualLines)) + + for i, l := range expectedLines { + require.JSONEq(t, string(l), string(actualLines[i])) + } +} + +func ParseJSONLines(data []byte) [][]byte { + return bytes.Split(bytes.TrimSpace(data), []byte("\n")) +} diff --git a/gitfame/test/integration/testdata/tests/27/description.yaml b/gitfame/test/integration/testdata/tests/27/description.yaml new file mode 100644 index 0000000..61dd8e6 --- /dev/null +++ b/gitfame/test/integration/testdata/tests/27/description.yaml @@ -0,0 +1,5 @@ +# go-cmp, HEAD, tabular + +name: go-cmp HEAD tabular +args: [] +bundle: go-cmp.bundle diff --git a/gitfame/test/integration/testdata/tests/27/expected.out b/gitfame/test/integration/testdata/tests/27/expected.out new file mode 100644 index 0000000..ec0dd1f --- /dev/null +++ b/gitfame/test/integration/testdata/tests/27/expected.out @@ -0,0 +1,17 @@ +Name Lines Commits Files +Joe Tsai 13818 94 54 +colinnewell 130 1 1 +A. Ishikawa 92 1 2 +Roger Peppe 59 1 2 +Tobias Klauser 35 2 3 +178inaba 27 2 5 +Kyle Lemons 11 1 1 +Dmitri Shuralyov 8 1 2 +ferhat elmas 7 1 4 +Christian Muehlhaeuser 6 3 4 +k.nakada 5 1 3 +LMMilewski 5 1 2 +Ernest Galbrun 3 1 1 +Ross Light 2 1 1 +Chris Morrow 1 1 1 +Fiisio 1 1 1 diff --git a/gitfame/test/integration/testdata/tests/28/description.yaml b/gitfame/test/integration/testdata/tests/28/description.yaml new file mode 100644 index 0000000..4625c4f --- /dev/null +++ b/gitfame/test/integration/testdata/tests/28/description.yaml @@ -0,0 +1,6 @@ +# go-cmp, HEAD, json + +name: go-cmp HEAD json +args: [--format, json] +bundle: go-cmp.bundle +format: json diff --git a/gitfame/test/integration/testdata/tests/28/expected.out b/gitfame/test/integration/testdata/tests/28/expected.out new file mode 100644 index 0000000..26e55c0 --- /dev/null +++ b/gitfame/test/integration/testdata/tests/28/expected.out @@ -0,0 +1 @@ +[{"name":"Joe Tsai","lines":13818,"commits":94,"files":54},{"name":"colinnewell","lines":130,"commits":1,"files":1},{"name":"A. Ishikawa","lines":92,"commits":1,"files":2},{"name":"Roger Peppe","lines":59,"commits":1,"files":2},{"name":"Tobias Klauser","lines":35,"commits":2,"files":3},{"name":"178inaba","lines":27,"commits":2,"files":5},{"name":"Kyle Lemons","lines":11,"commits":1,"files":1},{"name":"Dmitri Shuralyov","lines":8,"commits":1,"files":2},{"name":"ferhat elmas","lines":7,"commits":1,"files":4},{"name":"Christian Muehlhaeuser","lines":6,"commits":3,"files":4},{"name":"k.nakada","lines":5,"commits":1,"files":3},{"name":"LMMilewski","lines":5,"commits":1,"files":2},{"name":"Ernest Galbrun","lines":3,"commits":1,"files":1},{"name":"Ross Light","lines":2,"commits":1,"files":1},{"name":"Chris Morrow","lines":1,"commits":1,"files":1},{"name":"Fiisio","lines":1,"commits":1,"files":1}] \ No newline at end of file diff --git a/gitfame/test/integration/testdata/tests/29/description.yaml b/gitfame/test/integration/testdata/tests/29/description.yaml new file mode 100644 index 0000000..53d2195 --- /dev/null +++ b/gitfame/test/integration/testdata/tests/29/description.yaml @@ -0,0 +1,6 @@ +# go-cmp, HEAD, json-lines + +name: go-cmp HEAD json +args: [--format, json-lines] +bundle: go-cmp.bundle +format: json-lines diff --git a/gitfame/test/integration/testdata/tests/29/expected.out b/gitfame/test/integration/testdata/tests/29/expected.out new file mode 100644 index 0000000..3230bc7 --- /dev/null +++ b/gitfame/test/integration/testdata/tests/29/expected.out @@ -0,0 +1,16 @@ +{"name":"Joe Tsai","lines":13818,"commits":94,"files":54} +{"name":"colinnewell","lines":130,"commits":1,"files":1} +{"name":"A. Ishikawa","lines":92,"commits":1,"files":2} +{"name":"Roger Peppe","lines":59,"commits":1,"files":2} +{"name":"Tobias Klauser","lines":35,"commits":2,"files":3} +{"name":"178inaba","lines":27,"commits":2,"files":5} +{"name":"Kyle Lemons","lines":11,"commits":1,"files":1} +{"name":"Dmitri Shuralyov","lines":8,"commits":1,"files":2} +{"name":"ferhat elmas","lines":7,"commits":1,"files":4} +{"name":"Christian Muehlhaeuser","lines":6,"commits":3,"files":4} +{"name":"k.nakada","lines":5,"commits":1,"files":3} +{"name":"LMMilewski","lines":5,"commits":1,"files":2} +{"name":"Ernest Galbrun","lines":3,"commits":1,"files":1} +{"name":"Ross Light","lines":2,"commits":1,"files":1} +{"name":"Chris Morrow","lines":1,"commits":1,"files":1} +{"name":"Fiisio","lines":1,"commits":1,"files":1}