1package gitattributes
2
3import (
4 "strings"
5 "testing"
6
7 "github.com/stretchr/testify/suite"
8)
9
10type AttributesSuite struct {
11 suite.Suite
12}
13
14func TestAttributesSuite(t *testing.T) {
15 suite.Run(t, new(AttributesSuite))
16}
17
18func (s *AttributesSuite) TestAttributes_ReadAttributes() {
19 lines := []string{
20 "[attr]sub -a",
21 "[attr]add a",
22 "* sub a",
23 "* !a foo=bar -b c",
24 }
25
26 mas, err := ReadAttributes(strings.NewReader(strings.Join(lines, "\n")), nil, true)
27 s.NoError(err)
28 s.Len(mas, 4)
29
30 s.Equal("sub", mas[0].Name)
31 s.Nil(mas[0].Pattern)
32 s.True(mas[0].Attributes[0].IsUnset())
33
34 s.Equal("add", mas[1].Name)
35 s.Nil(mas[1].Pattern)
36 s.True(mas[1].Attributes[0].IsSet())
37
38 s.Equal("*", mas[2].Name)
39 s.NotNil(mas[2].Pattern)
40 s.True(mas[2].Attributes[0].IsSet())
41
42 s.Equal("*", mas[3].Name)
43 s.NotNil(mas[3].Pattern)
44 s.True(mas[3].Attributes[0].IsUnspecified())
45 s.True(mas[3].Attributes[1].IsValueSet())
46 s.Equal("bar", mas[3].Attributes[1].Value())
47 s.True(mas[3].Attributes[2].IsUnset())
48 s.True(mas[3].Attributes[3].IsSet())
49 s.Equal("a: unspecified", mas[3].Attributes[0].String())
50 s.Equal("foo: bar", mas[3].Attributes[1].String())
51 s.Equal("b: unset", mas[3].Attributes[2].String())
52 s.Equal("c: set", mas[3].Attributes[3].String())
53}
54
55func (s *AttributesSuite) TestAttributes_ReadAttributesDisallowMacro() {
56 lines := []string{
57 "[attr]sub -a",
58 "* a add",
59 }
60
61 _, err := ReadAttributes(strings.NewReader(strings.Join(lines, "\n")), nil, false)
62 s.ErrorIs(err, ErrMacroNotAllowed)
63}
64
65func (s *AttributesSuite) TestAttributes_ReadAttributesInvalidName() {
66 lines := []string{
67 "[attr]foo!bar -a",
68 }
69
70 _, err := ReadAttributes(strings.NewReader(strings.Join(lines, "\n")), nil, true)
71 s.ErrorIs(err, ErrInvalidAttributeName)
72}