@@ -2,19 +2,28 @@ package gitignore
22
33import (
44 "os"
5+ "os/user"
6+ "strconv"
57
68 . "gopkg.in/check.v1"
79 "gopkg.in/src-d/go-billy.v4"
810 "gopkg.in/src-d/go-billy.v4/memfs"
911)
1012
1113type MatcherSuite struct {
12- FS billy.Filesystem
14+ GFS billy.Filesystem // git repository root
15+ RFS billy.Filesystem // root that contains user home
16+ MCFS billy.Filesystem // root that contains user home, but missing ~/.gitconfig
17+ MEFS billy.Filesystem // root that contains user home, but missing excludesfile entry
18+ MIFS billy.Filesystem // root that contains user home, but missing .gitnignore
19+
20+ SFS billy.Filesystem // root that contains /etc/gitconfig
1321}
1422
1523var _ = Suite (& MatcherSuite {})
1624
1725func (s * MatcherSuite ) SetUpTest (c * C ) {
26+ // setup generic git repository root
1827 fs := memfs .New ()
1928 f , err := fs .Create (".gitignore" )
2029 c .Assert (err , IsNil )
@@ -36,15 +45,169 @@ func (s *MatcherSuite) SetUpTest(c *C) {
3645 fs .MkdirAll ("vendor/github.com" , os .ModePerm )
3746 fs .MkdirAll ("vendor/gopkg.in" , os .ModePerm )
3847
39- s .FS = fs
48+ s .GFS = fs
49+
50+ // setup root that contains user home
51+ usr , err := user .Current ()
52+ c .Assert (err , IsNil )
53+
54+ fs = memfs .New ()
55+ err = fs .MkdirAll (usr .HomeDir , os .ModePerm )
56+ c .Assert (err , IsNil )
57+
58+ f , err = fs .Create (fs .Join (usr .HomeDir , gitconfigFile ))
59+ c .Assert (err , IsNil )
60+ _ , err = f .Write ([]byte ("[core]\n " ))
61+ c .Assert (err , IsNil )
62+ _ , err = f .Write ([]byte (" excludesfile = " + strconv .Quote (fs .Join (usr .HomeDir , ".gitignore_global" )) + "\n " ))
63+ c .Assert (err , IsNil )
64+ err = f .Close ()
65+ c .Assert (err , IsNil )
66+
67+ f , err = fs .Create (fs .Join (usr .HomeDir , ".gitignore_global" ))
68+ c .Assert (err , IsNil )
69+ _ , err = f .Write ([]byte ("# IntelliJ\n " ))
70+ c .Assert (err , IsNil )
71+ _ , err = f .Write ([]byte (".idea/\n " ))
72+ c .Assert (err , IsNil )
73+ _ , err = f .Write ([]byte ("*.iml\n " ))
74+ c .Assert (err , IsNil )
75+ err = f .Close ()
76+ c .Assert (err , IsNil )
77+
78+ s .RFS = fs
79+
80+ // root that contains user home, but missing ~/.gitconfig
81+ fs = memfs .New ()
82+ err = fs .MkdirAll (usr .HomeDir , os .ModePerm )
83+ c .Assert (err , IsNil )
84+
85+ f , err = fs .Create (fs .Join (usr .HomeDir , ".gitignore_global" ))
86+ c .Assert (err , IsNil )
87+ _ , err = f .Write ([]byte ("# IntelliJ\n " ))
88+ c .Assert (err , IsNil )
89+ _ , err = f .Write ([]byte (".idea/\n " ))
90+ c .Assert (err , IsNil )
91+ _ , err = f .Write ([]byte ("*.iml\n " ))
92+ c .Assert (err , IsNil )
93+ err = f .Close ()
94+ c .Assert (err , IsNil )
95+
96+ s .MCFS = fs
97+
98+ // setup root that contains user home, but missing excludesfile entry
99+ fs = memfs .New ()
100+ err = fs .MkdirAll (usr .HomeDir , os .ModePerm )
101+ c .Assert (err , IsNil )
102+
103+ f , err = fs .Create (fs .Join (usr .HomeDir , gitconfigFile ))
104+ c .Assert (err , IsNil )
105+ _ , err = f .Write ([]byte ("[core]\n " ))
106+ c .Assert (err , IsNil )
107+ err = f .Close ()
108+ c .Assert (err , IsNil )
109+
110+ f , err = fs .Create (fs .Join (usr .HomeDir , ".gitignore_global" ))
111+ c .Assert (err , IsNil )
112+ _ , err = f .Write ([]byte ("# IntelliJ\n " ))
113+ c .Assert (err , IsNil )
114+ _ , err = f .Write ([]byte (".idea/\n " ))
115+ c .Assert (err , IsNil )
116+ _ , err = f .Write ([]byte ("*.iml\n " ))
117+ c .Assert (err , IsNil )
118+ err = f .Close ()
119+ c .Assert (err , IsNil )
120+
121+ s .MEFS = fs
122+
123+ // setup root that contains user home, but missing .gitnignore
124+ fs = memfs .New ()
125+ err = fs .MkdirAll (usr .HomeDir , os .ModePerm )
126+ c .Assert (err , IsNil )
127+
128+ f , err = fs .Create (fs .Join (usr .HomeDir , gitconfigFile ))
129+ c .Assert (err , IsNil )
130+ _ , err = f .Write ([]byte ("[core]\n " ))
131+ c .Assert (err , IsNil )
132+ _ , err = f .Write ([]byte (" excludesfile = " + strconv .Quote (fs .Join (usr .HomeDir , ".gitignore_global" )) + "\n " ))
133+ c .Assert (err , IsNil )
134+ err = f .Close ()
135+ c .Assert (err , IsNil )
136+
137+ s .MIFS = fs
138+
139+ // setup root that contains user home
140+ fs = memfs .New ()
141+ err = fs .MkdirAll ("etc" , os .ModePerm )
142+ c .Assert (err , IsNil )
143+
144+ f , err = fs .Create (systemFile )
145+ c .Assert (err , IsNil )
146+ _ , err = f .Write ([]byte ("[core]\n " ))
147+ c .Assert (err , IsNil )
148+ _ , err = f .Write ([]byte (" excludesfile = /etc/gitignore_global\n " ))
149+ c .Assert (err , IsNil )
150+ err = f .Close ()
151+ c .Assert (err , IsNil )
152+
153+ f , err = fs .Create ("/etc/gitignore_global" )
154+ c .Assert (err , IsNil )
155+ _ , err = f .Write ([]byte ("# IntelliJ\n " ))
156+ c .Assert (err , IsNil )
157+ _ , err = f .Write ([]byte (".idea/\n " ))
158+ c .Assert (err , IsNil )
159+ _ , err = f .Write ([]byte ("*.iml\n " ))
160+ c .Assert (err , IsNil )
161+ err = f .Close ()
162+ c .Assert (err , IsNil )
163+
164+ s .SFS = fs
40165}
41166
42167func (s * MatcherSuite ) TestDir_ReadPatterns (c * C ) {
43- ps , err := ReadPatterns (s .FS , nil )
168+ ps , err := ReadPatterns (s .GFS , nil )
44169 c .Assert (err , IsNil )
45170 c .Assert (ps , HasLen , 2 )
46171
47172 m := NewMatcher (ps )
48173 c .Assert (m .Match ([]string {"vendor" , "gopkg.in" }, true ), Equals , true )
49174 c .Assert (m .Match ([]string {"vendor" , "github.com" }, true ), Equals , false )
50175}
176+
177+ func (s * MatcherSuite ) TestDir_LoadGlobalPatterns (c * C ) {
178+ ps , err := LoadGlobalPatterns (s .RFS )
179+ c .Assert (err , IsNil )
180+ c .Assert (ps , HasLen , 2 )
181+
182+ m := NewMatcher (ps )
183+ c .Assert (m .Match ([]string {"go-git.v4.iml" }, true ), Equals , true )
184+ c .Assert (m .Match ([]string {".idea" }, true ), Equals , true )
185+ }
186+
187+ func (s * MatcherSuite ) TestDir_LoadGlobalPatternsMissingGitconfig (c * C ) {
188+ ps , err := LoadGlobalPatterns (s .MCFS )
189+ c .Assert (err , IsNil )
190+ c .Assert (ps , HasLen , 0 )
191+ }
192+
193+ func (s * MatcherSuite ) TestDir_LoadGlobalPatternsMissingExcludesfile (c * C ) {
194+ ps , err := LoadGlobalPatterns (s .MEFS )
195+ c .Assert (err , IsNil )
196+ c .Assert (ps , HasLen , 0 )
197+ }
198+
199+ func (s * MatcherSuite ) TestDir_LoadGlobalPatternsMissingGitignore (c * C ) {
200+ ps , err := LoadGlobalPatterns (s .MIFS )
201+ c .Assert (err , IsNil )
202+ c .Assert (ps , HasLen , 0 )
203+ }
204+
205+ func (s * MatcherSuite ) TestDir_LoadSystemPatterns (c * C ) {
206+ ps , err := LoadSystemPatterns (s .SFS )
207+ c .Assert (err , IsNil )
208+ c .Assert (ps , HasLen , 2 )
209+
210+ m := NewMatcher (ps )
211+ c .Assert (m .Match ([]string {"go-git.v4.iml" }, true ), Equals , true )
212+ c .Assert (m .Match ([]string {".idea" }, true ), Equals , true )
213+ }
0 commit comments