1// Copyright 2018 The Gitea Authors. All rights reserved.
2// Use of this source code is governed by a MIT-style
3// license that can be found in the LICENSE file.
4
5//go:build gogit
6// +build gogit
7
8package git
9
10import (
11	"testing"
12
13	"github.com/go-git/go-git/v5/plumbing/filemode"
14	"github.com/go-git/go-git/v5/plumbing/object"
15	"github.com/stretchr/testify/assert"
16)
17
18func TestParseTreeEntries(t *testing.T) {
19	testCases := []struct {
20		Input    string
21		Expected []*TreeEntry
22	}{
23		{
24			Input:    "",
25			Expected: []*TreeEntry{},
26		},
27		{
28			Input: "100644 blob 61ab7345a1a3bbc590068ccae37b8515cfc5843c    1022\texample/file2.txt\n",
29			Expected: []*TreeEntry{
30				{
31					ID: MustIDFromString("61ab7345a1a3bbc590068ccae37b8515cfc5843c"),
32					gogitTreeEntry: &object.TreeEntry{
33						Hash: MustIDFromString("61ab7345a1a3bbc590068ccae37b8515cfc5843c"),
34						Name: "example/file2.txt",
35						Mode: filemode.Regular,
36					},
37					size:  1022,
38					sized: true,
39				},
40			},
41		},
42		{
43			Input: "120000 blob 61ab7345a1a3bbc590068ccae37b8515cfc5843c  234131\t\"example/\\n.txt\"\n" +
44				"040000 tree 1d01fb729fb0db5881daaa6030f9f2d3cd3d5ae8       -\texample\n",
45			Expected: []*TreeEntry{
46				{
47					ID: MustIDFromString("61ab7345a1a3bbc590068ccae37b8515cfc5843c"),
48					gogitTreeEntry: &object.TreeEntry{
49						Hash: MustIDFromString("61ab7345a1a3bbc590068ccae37b8515cfc5843c"),
50						Name: "example/\n.txt",
51						Mode: filemode.Symlink,
52					},
53					size:  234131,
54					sized: true,
55				},
56				{
57					ID:    MustIDFromString("1d01fb729fb0db5881daaa6030f9f2d3cd3d5ae8"),
58					sized: true,
59					gogitTreeEntry: &object.TreeEntry{
60						Hash: MustIDFromString("1d01fb729fb0db5881daaa6030f9f2d3cd3d5ae8"),
61						Name: "example",
62						Mode: filemode.Dir,
63					},
64				},
65			},
66		},
67	}
68
69	for _, testCase := range testCases {
70		entries, err := ParseTreeEntries([]byte(testCase.Input))
71		assert.NoError(t, err)
72		assert.EqualValues(t, testCase.Expected, entries)
73	}
74}
75