-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathrepo_tree_test.go
More file actions
95 lines (81 loc) · 2.1 KB
/
repo_tree_test.go
File metadata and controls
95 lines (81 loc) · 2.1 KB
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
// Copyright 2020 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package git
import (
"os"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestUnescapeChars(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{
name: "no escapes",
in: "normal-filename.txt",
want: "normal-filename.txt",
},
{
name: "escaped quote",
in: `Test \"Word\".md`,
want: `Test "Word".md`,
},
{
name: "escaped backslash",
in: `path\\to\\file.txt`,
want: `path\to\file.txt`,
},
{
name: "escaped tab",
in: `file\twith\ttabs.txt`,
want: "file\twith\ttabs.txt",
},
{
name: "mixed escapes",
in: `\"quoted\\path\t.md`,
want: "\"quoted\\path\t.md",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := UnescapeChars([]byte(tt.in))
assert.Equal(t, tt.want, string(got))
})
}
}
func TestRepository_LsTree(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip(`Windows does not allow '"' in filenames`)
}
path := tempPath()
defer os.RemoveAll(path)
err := Init(path)
require.NoError(t, err)
specialName := `Test "Wiki" Page.md`
err = os.WriteFile(filepath.Join(path, specialName), []byte("content"), 0o644)
require.NoError(t, err)
err = Add(path, AddOptions{All: true})
require.NoError(t, err)
err = CreateCommit(path, &Signature{Name: "test", Email: "[email protected]"}, "initial commit")
require.NoError(t, err)
repo, err := Open(path)
require.NoError(t, err)
commit, err := repo.CatFileCommit("HEAD")
require.NoError(t, err)
// Without Verbatim, Git quotes and escapes the filename.
entries, err := commit.Entries()
require.NoError(t, err)
require.Len(t, entries, 1)
assert.Equal(t, specialName, entries[0].Name())
// With Verbatim, Git outputs the filename as-is.
entries, err = commit.Entries(LsTreeOptions{Verbatim: true})
require.NoError(t, err)
require.Len(t, entries, 1)
assert.Equal(t, specialName, entries[0].Name())
}