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