code.gitea.io/gitea@v1.22.3/modules/git/parse_gogit_test.go (about)

     1  // Copyright 2018 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  //go:build gogit
     5  
     6  package git
     7  
     8  import (
     9  	"fmt"
    10  	"testing"
    11  
    12  	"github.com/go-git/go-git/v5/plumbing"
    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  
    18  func 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: plumbing.Hash(MustIDFromString("61ab7345a1a3bbc590068ccae37b8515cfc5843c").RawValue()),
    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: plumbing.Hash(MustIDFromString("61ab7345a1a3bbc590068ccae37b8515cfc5843c").RawValue()),
    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: plumbing.Hash(MustIDFromString("1d01fb729fb0db5881daaa6030f9f2d3cd3d5ae8").RawValue()),
    61  						Name: "example",
    62  						Mode: filemode.Dir,
    63  					},
    64  				},
    65  			},
    66  		},
    67  	}
    68  
    69  	for _, testCase := range testCases {
    70  		entries, err := ParseTreeEntries(Sha1ObjectFormat, []byte(testCase.Input))
    71  		assert.NoError(t, err)
    72  		if len(entries) > 1 {
    73  			fmt.Println(testCase.Expected[0].ID)
    74  			fmt.Println(entries[0].ID)
    75  		}
    76  		assert.EqualValues(t, testCase.Expected, entries)
    77  	}
    78  }