summaryrefslogtreecommitdiff
path: root/parse_fastimport_test.go
blob: d327d4c3025457f52d4849aa8ce4212f9eeff2ec (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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)
		})
	}

}