summaryrefslogtreecommitdiff
path: root/parse_fastimport_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'parse_fastimport_test.go')
-rw-r--r--parse_fastimport_test.go107
1 files changed, 107 insertions, 0 deletions
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 <lukeshu@lukeshu.com> 1561083373 -0400\n" +
+ "committer Luke Shumaker <lukeshu@lukeshu.com> 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 <lukeshu@lukeshu.com> 1561083374 -0400\n" +
+ "committer Luke Shumaker <lukeshu@lukeshu.com> 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 <lukeshu@lukeshu.com> 1561083373 -0400")),
+ Committer: MustParseIdent("Luke Shumaker <lukeshu@lukeshu.com> 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 <lukeshu@lukeshu.com> 1561083374 -0400")),
+ Committer: MustParseIdent("Luke Shumaker <lukeshu@lukeshu.com> 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)
+ })
+ }
+
+}