[gitfame] Add format tests.

This commit is contained in:
Arseny Balobanov 2021-03-01 17:10:47 +03:00
parent 72498e46ac
commit e074ac385c
7 changed files with 91 additions and 1 deletions

View file

@ -1,6 +1,7 @@
package integration package integration
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
@ -57,7 +58,7 @@ func TestGitFame(t *testing.T) {
output, err := cmd.Output() output, err := cmd.Output()
if !tc.Error { if !tc.Error {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, string(tc.Expected), string(output)) CompareResults(t, tc.Expected, output, tc.Format)
} else { } else {
require.Error(t, err) require.Error(t, err)
_, ok := err.(*exec.ExitError) _, ok := err.(*exec.ExitError)
@ -115,6 +116,7 @@ type TestDescription struct {
Args []string `yaml:"args"` Args []string `yaml:"args"`
Bundle string `yaml:"bundle"` Bundle string `yaml:"bundle"`
Error bool `yaml:"error"` Error bool `yaml:"error"`
Format string `yaml:"format,omitempty"`
} }
func ReadTestDescription(t *testing.T, path string) *TestDescription { 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) cmd := exec.Command("git", "clone", src, dst)
require.NoError(t, cmd.Run()) 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"))
}

View file

@ -0,0 +1,5 @@
# go-cmp, HEAD, tabular
name: go-cmp HEAD tabular
args: []
bundle: go-cmp.bundle

View file

@ -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

View file

@ -0,0 +1,6 @@
# go-cmp, HEAD, json
name: go-cmp HEAD json
args: [--format, json]
bundle: go-cmp.bundle
format: json

View file

@ -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}]

View file

@ -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

View file

@ -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}