Welcome to mirror list, hosted at ThFree Co, Russian Federation.

check_attr_test.go « gitattributes « git « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3b2a0fd01eff95a5bd6057acb638e3b9f776a52f (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package gitattributes

import (
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/v15/internal/git/gittest"
	"gitlab.com/gitlab-org/gitaly/v15/internal/git/localrepo"
	"gitlab.com/gitlab-org/gitaly/v15/internal/helper/perm"
	"gitlab.com/gitlab-org/gitaly/v15/internal/testhelper"
	"gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testcfg"
)

func TestCheckAttrCmd_Check(t *testing.T) {
	t.Parallel()

	ctx := testhelper.Context(t)
	cfg := testcfg.Build(t)

	repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
		SkipCreationViaService: true,
	})
	repo := localrepo.NewTestRepo(t, cfg, repoProto)

	// Until https://gitlab.com/groups/gitlab-org/-/epics/9006 is completed
	// we rely on info/attributes.
	infoPath := filepath.Join(repoPath, "info")
	require.NoError(t, os.MkdirAll(infoPath, perm.SharedDir))
	attrPath := filepath.Join(infoPath, "attributes")

	for _, tc := range []struct {
		desc         string
		attrContent  string
		path         string
		expectedAttr Attributes
	}{
		{
			desc:         "no attributes",
			path:         "README.md",
			expectedAttr: Attributes{},
		},
		{
			desc:         "attribute on other file",
			attrContent:  "*.go foo",
			path:         "README.md",
			expectedAttr: Attributes{},
		},
		{
			desc:         "comments are ignored",
			attrContent:  "# *.md -foo",
			path:         "README.md",
			expectedAttr: Attributes{},
		},
		{
			desc:         "single attribute",
			attrContent:  "*.md foo",
			path:         "README.md",
			expectedAttr: Attributes{Attribute{Name: "foo", State: "set"}},
		},
		{
			desc:         "double star pattern",
			attrContent:  "doc/** foo",
			path:         "doc/development/readme.md",
			expectedAttr: Attributes{Attribute{Name: "foo", State: "set"}},
		},
		{
			desc:         "comment and single attribute",
			attrContent:  "# these are the gitattributes\n*.md foo",
			path:         "README.md",
			expectedAttr: Attributes{Attribute{Name: "foo", State: "set"}},
		},
		{
			desc:         "quoted pattern",
			attrContent:  `"*.\155\144" foo`,
			path:         "README.md",
			expectedAttr: Attributes{Attribute{Name: "foo", State: "set"}},
		},
		{
			desc:        "multiple attributes",
			attrContent: "*.md foo bar",
			path:        "README.md",
			expectedAttr: Attributes{
				Attribute{Name: "foo", State: "set"},
				Attribute{Name: "bar", State: "set"},
			},
		},
		{
			desc:        "multiple attributes on multiple lines",
			attrContent: "*.md foo\n*.md bar",
			path:        "README.md",
			expectedAttr: Attributes{
				Attribute{Name: "foo", State: "set"},
				Attribute{Name: "bar", State: "set"},
			},
		},
		{
			desc:         "attributes on multiple files",
			attrContent:  "*.md foo\n*.go bar",
			path:         "README.md",
			expectedAttr: Attributes{Attribute{Name: "foo", State: "set"}},
		},
		{
			desc:         "attributes with state",
			attrContent:  "*.md foo=bar",
			path:         "README.md",
			expectedAttr: Attributes{Attribute{Name: "foo", State: "bar"}},
		},
		{
			desc:         "attributes with unset",
			attrContent:  "*.md -foo",
			path:         "README.md",
			expectedAttr: Attributes{Attribute{Name: "foo", State: "unset"}},
		},
		{
			desc:         "attributes with unset overriding",
			attrContent:  "*.md foo\ndoc/*.md -foo",
			path:         "doc/README.md",
			expectedAttr: Attributes{Attribute{Name: "foo", State: "unset"}},
		},
		{
			desc:         "attributes with state overridden",
			attrContent:  "*.md foo=bar\n*.md foo=buzz",
			path:         "README.md",
			expectedAttr: Attributes{Attribute{Name: "foo", State: "buzz"}},
		},
		{
			desc:         "attributes overridden for subdir",
			attrContent:  "*.md foo=bar\ndoc/*.md foo=buzz",
			path:         "doc/README.md",
			expectedAttr: Attributes{Attribute{Name: "foo", State: "buzz"}},
		},
		{
			desc:         "attributes overridden for other subdir",
			attrContent:  "*.md foo=bar\ndoc/*.md foo=buzz",
			path:         "README.md",
			expectedAttr: Attributes{Attribute{Name: "foo", State: "bar"}},
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			require.NoError(t, os.WriteFile(attrPath, []byte(tc.attrContent), perm.SharedFile))

			checkCmd, finish, err := CheckAttr(ctx, repo, []string{"foo", "bar"})
			require.NoError(t, err)
			t.Cleanup(finish)

			actual, err := checkCmd.Check(tc.path)
			require.NoError(t, err)
			require.Equal(t, tc.expectedAttr, actual)
		})
	}
}