From fa806f39c831c75d7dbfa3ac406eb2d14fe04cd3 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Tue, 31 Aug 2021 18:14:12 -0600 Subject: Add a failing testcase [ci-skip] --- go.mod | 5 ++- go.sum | 10 +++++ parse_fastimport_test.go | 107 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 parse_fastimport_test.go diff --git a/go.mod b/go.mod index c622260..5a701ed 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module git.lukeshu.com/go/libfastimport go 1.15 -require github.com/pkg/errors v0.9.1 +require ( + github.com/pkg/errors v0.9.1 + github.com/stretchr/testify v1.7.0 // indirect +) diff --git a/go.sum b/go.sum index 7c401c3..c2a26e7 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,12 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/parse_fastimport_test.go b/parse_fastimport_test.go new file mode 100644 index 0000000..d327d4c --- /dev/null +++ b/parse_fastimport_test.go @@ -0,0 +1,107 @@ +package libfastimport_test + +import ( + "io" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + + "git.lukeshu.com/go/libfastimport" +) + +func IdentPtr(ident libfastimport.Ident) *libfastimport.Ident { + return &ident +} + +func TestParser(t *testing.T) { + MustParseIdent := func(str string) libfastimport.Ident { + t.Helper() + ident, err := libfastimport.ParseIdent(str) + if err != nil { + t.Fatal(err) + } + return ident + } + type resp struct { + libfastimport.Cmd + error + } + type testcase struct { + input string + expected []resp + } + testcases := map[string]testcase{ + "TerminatingGetMark": { + input: "\n" + + "commit refs/heads/master\n" + + "mark :1\n" + + "author Luke Shumaker 1561083373 -0400\n" + + "committer Luke Shumaker 1561083373 -0400\n" + + "data 4\n" + + "foo\n" + + "from 0000000000000000000000000000000000000000\n" + + "deleteall\n" + + "M 100755 3bd4eeca3db66d8349fb0963f2caf2808a66e556 README.md\n" + + "commit refs/heads/master\n" + + "mark :2\n" + + "author Luke Shumaker 1561083374 -0400\n" + + "committer Luke Shumaker 1561083374 -0400\n" + + "data 4\n" + + "bar\n" + + "deleteall\n" + + "M 100755 505928d6061b54e26b48a9b1e391a7530e27b7ca README.md\n" + + "get-mark :1\n" + + "get-mark :2\n", + expected: []resp{ + {libfastimport.CmdCommit{ + Ref: "refs/heads/master", + Mark: 1, + Author: IdentPtr(MustParseIdent("Luke Shumaker 1561083373 -0400")), + Committer: MustParseIdent("Luke Shumaker 1561083373 -0400"), + Msg: "foo\n", + From: "0000000000000000000000000000000000000000", + }, nil}, + {libfastimport.FileDeleteAll{}, nil}, + {libfastimport.FileModify{Mode: 0100755, Path: "README.md", DataRef: "3bd4eeca3db66d8349fb0963f2caf2808a66e556"}, nil}, + {libfastimport.CmdCommitEnd{}, nil}, + {libfastimport.CmdCommit{ + Ref: "refs/heads/master", + Mark: 2, + Author: IdentPtr(MustParseIdent("Luke Shumaker 1561083374 -0400")), + Committer: MustParseIdent("Luke Shumaker 1561083374 -0400"), + Msg: "bar\n", + }, nil}, + {libfastimport.FileDeleteAll{}, nil}, + {libfastimport.FileModify{Mode: 0100755, Path: "README.md", DataRef: "505928d6061b54e26b48a9b1e391a7530e27b7ca"}, nil}, + // OK, here's the interesting part of the testcase: `get-mark :1` + // doesn't trigger CommitEnd, as get-mark is allowed inside of a + // commit; BUT `get-mark :2` does, because the in-progress commit is + // :2 and so asking for it implies that it's done. + {libfastimport.CmdGetMark{Mark: 1}, nil}, + {libfastimport.CmdCommitEnd{}, nil}, + {libfastimport.CmdGetMark{Mark: 2}, nil}, + {nil, io.EOF}, + }, + }, + } + + t.Parallel() + for tcName, tcData := range testcases { + tcData := tcData + t.Run(tcName, func(t *testing.T) { + t.Parallel() + parser := libfastimport.NewFrontend(strings.NewReader(tcData.input), nil, nil) + var actual []resp + for { + cmd, err := parser.ReadCmd() + actual = append(actual, resp{cmd, err}) + if err != nil { + break + } + } + assert.Equal(t, tcData.expected, actual) + }) + } + +} -- cgit v1.2.3-54-g00ecf