@@ -2,6 +2,7 @@ package gitfs_test
22
33import (
44 "io"
5+ "io/fs"
56 "testing"
67 "testing/fstest"
78
@@ -171,3 +172,70 @@ func TestSubdirSymlinkFS(t *testing.T) {
171172 }
172173 })
173174}
175+
176+ func TestSubmoduleFS (t * testing.T ) {
177+ // TODO instead of cloning a remote repository, synthesize a very simple Git repository right in the test here (benefit of the remote repository is that it's much larger, so fstest.TestFS has a lot more data to test against)
178+ // Init + CreateRemoteAnonymous + Fetch because Clone doesn't support fetch-by-commit
179+ repo , err := git .Init (memory .NewStorage (), nil )
180+ if err != nil {
181+ t .Fatal (err )
182+ }
183+ remote , err := repo .CreateRemoteAnonymous (& goGitConfig.RemoteConfig {
184+ Name : "anonymous" ,
185+ URLs : []string {"https://github.com/debuerreotype/debuerreotype.git" }, // just a repository with a known submodule (`./validate/`)
186+ })
187+ if err != nil {
188+ t .Fatal (err )
189+ }
190+ commit := "d12af8e5556e39f82082b44628288e2eb27d4c34"
191+ err = remote .Fetch (& git.FetchOptions {
192+ RefSpecs : []goGitConfig.RefSpec {goGitConfig .RefSpec (commit + ":FETCH_HEAD" )},
193+ Tags : git .NoTags ,
194+ })
195+ if err != nil {
196+ t .Fatal (err )
197+ }
198+ f , err := gitfs .CommitHash (repo , commit )
199+ if err != nil {
200+ t .Fatal (err )
201+ }
202+
203+ t .Run ("Stat" , func (t * testing.T ) {
204+ fi , err := fs .Stat (f , "validate" )
205+ if err != nil {
206+ t .Fatal (err )
207+ }
208+ if fi .Mode ().IsRegular () {
209+ t .Fatal ("validate should not be a regular file" )
210+ }
211+ if ! fi .IsDir () {
212+ t .Fatal ("validate should be a directory but isn't" )
213+ }
214+ })
215+ t .Run ("ReadDir" , func (t * testing.T ) {
216+ entries , err := fs .ReadDir (f , "validate" )
217+ if err != nil {
218+ t .Fatal (err )
219+ }
220+ if len (entries ) != 0 {
221+ t .Fatalf ("validate should have 0 entries, not %d\n \n %#v" , len (entries ), entries )
222+ }
223+ })
224+
225+ // might as well run fstest again, now that we have a new filesystem tree 😅
226+ t .Run ("fstest.TestFS" , func (t * testing.T ) {
227+ if err := fstest .TestFS (f , "Dockerfile" ); err != nil {
228+ t .Fatal (err )
229+ }
230+ })
231+ t .Run ("Sub+fstest.TestFS" , func (t * testing.T ) {
232+ sub , err := fs .Sub (f , "validate" )
233+ if err != nil {
234+ t .Fatal (err )
235+ }
236+ // "As a special case, if no expected files are listed, fsys must be empty."
237+ if err := fstest .TestFS (sub ); err != nil {
238+ t .Fatal (err )
239+ }
240+ })
241+ }
0 commit comments