| 1 |
package gitattributes |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"strconv" |
| 6 |
|
| 7 |
"github.com/go-git/go-billy/v5" |
| 8 |
"github.com/go-git/go-billy/v5/memfs" |
| 9 |
. "gopkg.in/check.v1" |
| 10 |
) |
| 11 |
|
| 12 |
type MatcherSuite struct { |
| 13 |
GFS billy.Filesystem // git repository root |
| 14 |
RFS billy.Filesystem // root that contains user home |
| 15 |
MCFS billy.Filesystem // root that contains user home, but missing ~/.gitattributes |
| 16 |
MEFS billy.Filesystem // root that contains user home, but missing attributesfile entry |
| 17 |
MIFS billy.Filesystem // root that contains user home, but missing .gitattributes |
| 18 |
|
| 19 |
SFS billy.Filesystem // root that contains /etc/gitattributes |
| 20 |
} |
| 21 |
|
| 22 |
var _ = Suite(&MatcherSuite{}) |
| 23 |
|
| 24 |
func (s *MatcherSuite) SetUpTest(c *C) { |
| 25 |
home, err := os.UserHomeDir() |
| 26 |
c.Assert(err, IsNil) |
| 27 |
|
| 28 |
gitAttributesGlobal := func(fs billy.Filesystem, filename string) { |
| 29 |
f, err := fs.Create(filename) |
| 30 |
c.Assert(err, IsNil) |
| 31 |
_, err = f.Write([]byte("# IntelliJ\n")) |
| 32 |
c.Assert(err, IsNil) |
| 33 |
_, err = f.Write([]byte(".idea/** text\n")) |
| 34 |
c.Assert(err, IsNil) |
| 35 |
_, err = f.Write([]byte("*.iml -text\n")) |
| 36 |
c.Assert(err, IsNil) |
| 37 |
err = f.Close() |
| 38 |
c.Assert(err, IsNil) |
| 39 |
} |
| 40 |
|
| 41 |
// setup generic git repository root |
| 42 |
fs := memfs.New() |
| 43 |
f, err := fs.Create(".gitattributes") |
| 44 |
c.Assert(err, IsNil) |
| 45 |
_, err = f.Write([]byte("vendor/g*/** foo=bar\n")) |
| 46 |
c.Assert(err, IsNil) |
| 47 |
err = f.Close() |
| 48 |
c.Assert(err, IsNil) |
| 49 |
|
| 50 |
err = fs.MkdirAll("vendor", os.ModePerm) |
| 51 |
c.Assert(err, IsNil) |
| 52 |
f, err = fs.Create("vendor/.gitattributes") |
| 53 |
c.Assert(err, IsNil) |
| 54 |
_, err = f.Write([]byte("github.com/** -foo\n")) |
| 55 |
c.Assert(err, IsNil) |
| 56 |
err = f.Close() |
| 57 |
c.Assert(err, IsNil) |
| 58 |
|
| 59 |
fs.MkdirAll("another", os.ModePerm) |
| 60 |
fs.MkdirAll("vendor/github.com", os.ModePerm) |
| 61 |
fs.MkdirAll("vendor/gopkg.in", os.ModePerm) |
| 62 |
|
| 63 |
gitAttributesGlobal(fs, fs.Join(home, ".gitattributes_global")) |
| 64 |
|
| 65 |
s.GFS = fs |
| 66 |
|
| 67 |
fs = memfs.New() |
| 68 |
err = fs.MkdirAll(home, os.ModePerm) |
| 69 |
c.Assert(err, IsNil) |
| 70 |
|
| 71 |
f, err = fs.Create(fs.Join(home, gitconfigFile)) |
| 72 |
c.Assert(err, IsNil) |
| 73 |
_, err = f.Write([]byte("[core]\n")) |
| 74 |
c.Assert(err, IsNil) |
| 75 |
_, err = f.Write([]byte(" attributesfile = " + strconv.Quote(fs.Join(home, ".gitattributes_global")) + "\n")) |
| 76 |
c.Assert(err, IsNil) |
| 77 |
err = f.Close() |
| 78 |
c.Assert(err, IsNil) |
| 79 |
|
| 80 |
gitAttributesGlobal(fs, fs.Join(home, ".gitattributes_global")) |
| 81 |
|
| 82 |
s.RFS = fs |
| 83 |
|
| 84 |
// root that contains user home, but missing ~/.gitconfig |
| 85 |
fs = memfs.New() |
| 86 |
gitAttributesGlobal(fs, fs.Join(home, ".gitattributes_global")) |
| 87 |
|
| 88 |
s.MCFS = fs |
| 89 |
|
| 90 |
// setup root that contains user home, but missing attributesfile entry |
| 91 |
fs = memfs.New() |
| 92 |
err = fs.MkdirAll(home, os.ModePerm) |
| 93 |
c.Assert(err, IsNil) |
| 94 |
|
| 95 |
f, err = fs.Create(fs.Join(home, gitconfigFile)) |
| 96 |
c.Assert(err, IsNil) |
| 97 |
_, err = f.Write([]byte("[core]\n")) |
| 98 |
c.Assert(err, IsNil) |
| 99 |
err = f.Close() |
| 100 |
c.Assert(err, IsNil) |
| 101 |
|
| 102 |
gitAttributesGlobal(fs, fs.Join(home, ".gitattributes_global")) |
| 103 |
|
| 104 |
s.MEFS = fs |
| 105 |
|
| 106 |
// setup root that contains user home, but missing .gitattributes |
| 107 |
fs = memfs.New() |
| 108 |
err = fs.MkdirAll(home, os.ModePerm) |
| 109 |
c.Assert(err, IsNil) |
| 110 |
|
| 111 |
f, err = fs.Create(fs.Join(home, gitconfigFile)) |
| 112 |
c.Assert(err, IsNil) |
| 113 |
_, err = f.Write([]byte("[core]\n")) |
| 114 |
c.Assert(err, IsNil) |
| 115 |
_, err = f.Write([]byte(" attributesfile = " + strconv.Quote(fs.Join(home, ".gitattributes_global")) + "\n")) |
| 116 |
c.Assert(err, IsNil) |
| 117 |
err = f.Close() |
| 118 |
c.Assert(err, IsNil) |
| 119 |
|
| 120 |
s.MIFS = fs |
| 121 |
|
| 122 |
// setup root that contains user home |
| 123 |
fs = memfs.New() |
| 124 |
err = fs.MkdirAll("etc", os.ModePerm) |
| 125 |
c.Assert(err, IsNil) |
| 126 |
|
| 127 |
f, err = fs.Create(systemFile) |
| 128 |
c.Assert(err, IsNil) |
| 129 |
_, err = f.Write([]byte("[core]\n")) |
| 130 |
c.Assert(err, IsNil) |
| 131 |
_, err = f.Write([]byte(" attributesfile = /etc/gitattributes_global\n")) |
| 132 |
c.Assert(err, IsNil) |
| 133 |
err = f.Close() |
| 134 |
c.Assert(err, IsNil) |
| 135 |
|
| 136 |
gitAttributesGlobal(fs, "/etc/gitattributes_global") |
| 137 |
|
| 138 |
s.SFS = fs |
| 139 |
} |
| 140 |
|
| 141 |
func (s *MatcherSuite) TestDir_ReadPatterns(c *C) { |
| 142 |
ps, err := ReadPatterns(s.GFS, nil) |
| 143 |
c.Assert(err, IsNil) |
| 144 |
c.Assert(ps, HasLen, 2) |
| 145 |
|
| 146 |
m := NewMatcher(ps) |
| 147 |
results, _ := m.Match([]string{"vendor", "gopkg.in", "file"}, nil) |
| 148 |
c.Assert(results["foo"].Value(), Equals, "bar") |
| 149 |
|
| 150 |
results, _ = m.Match([]string{"vendor", "github.com", "file"}, nil) |
| 151 |
c.Assert(results["foo"].IsUnset(), Equals, false) |
| 152 |
} |
| 153 |
|
| 154 |
func (s *MatcherSuite) TestDir_LoadGlobalPatterns(c *C) { |
| 155 |
ps, err := LoadGlobalPatterns(s.RFS) |
| 156 |
c.Assert(err, IsNil) |
| 157 |
c.Assert(ps, HasLen, 2) |
| 158 |
|
| 159 |
m := NewMatcher(ps) |
| 160 |
|
| 161 |
results, _ := m.Match([]string{"go-git.v4.iml"}, nil) |
| 162 |
c.Assert(results["text"].IsUnset(), Equals, true) |
| 163 |
|
| 164 |
results, _ = m.Match([]string{".idea", "file"}, nil) |
| 165 |
c.Assert(results["text"].IsSet(), Equals, true) |
| 166 |
} |
| 167 |
|
| 168 |
func (s *MatcherSuite) TestDir_LoadGlobalPatternsMissingGitconfig(c *C) { |
| 169 |
ps, err := LoadGlobalPatterns(s.MCFS) |
| 170 |
c.Assert(err, IsNil) |
| 171 |
c.Assert(ps, HasLen, 0) |
| 172 |
} |
| 173 |
|
| 174 |
func (s *MatcherSuite) TestDir_LoadGlobalPatternsMissingAttributesfile(c *C) { |
| 175 |
ps, err := LoadGlobalPatterns(s.MEFS) |
| 176 |
c.Assert(err, IsNil) |
| 177 |
c.Assert(ps, HasLen, 0) |
| 178 |
} |
| 179 |
|
| 180 |
func (s *MatcherSuite) TestDir_LoadGlobalPatternsMissingGitattributes(c *C) { |
| 181 |
ps, err := LoadGlobalPatterns(s.MIFS) |
| 182 |
c.Assert(err, IsNil) |
| 183 |
c.Assert(ps, HasLen, 0) |
| 184 |
} |
| 185 |
|
| 186 |
func (s *MatcherSuite) TestDir_LoadSystemPatterns(c *C) { |
| 187 |
ps, err := LoadSystemPatterns(s.SFS) |
| 188 |
c.Assert(err, IsNil) |
| 189 |
c.Assert(ps, HasLen, 2) |
| 190 |
|
| 191 |
m := NewMatcher(ps) |
| 192 |
results, _ := m.Match([]string{"go-git.v4.iml"}, nil) |
| 193 |
c.Assert(results["text"].IsUnset(), Equals, true) |
| 194 |
|
| 195 |
results, _ = m.Match([]string{".idea", "file"}, nil) |
| 196 |
c.Assert(results["text"].IsSet(), Equals, true) |
| 197 |
} |