github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/file/cataloger/internal/all_regular_files_test.go (about) 1 package internal 2 3 import ( 4 "testing" 5 6 "github.com/google/go-cmp/cmp" 7 "github.com/scylladb/go-set/strset" 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 11 "github.com/anchore/stereoscope/pkg/imagetest" 12 "github.com/anchore/syft/syft/file" 13 "github.com/anchore/syft/syft/source" 14 ) 15 16 func Test_allRegularFiles(t *testing.T) { 17 tests := []struct { 18 name string 19 setup func() file.Resolver 20 wantRealPaths *strset.Set 21 wantVirtualPaths *strset.Set 22 }{ 23 { 24 name: "image", 25 setup: func() file.Resolver { 26 testImage := "image-file-type-mix" 27 28 img := imagetest.GetFixtureImage(t, "docker-archive", testImage) 29 30 s, err := source.NewFromStereoscopeImageObject(img, testImage, nil) 31 require.NoError(t, err) 32 33 r, err := s.FileResolver(source.SquashedScope) 34 require.NoError(t, err) 35 36 return r 37 }, 38 wantRealPaths: strset.New("/file-1.txt"), 39 wantVirtualPaths: strset.New("/file-1.txt", "/symlink-1", "/hardlink-1"), 40 }, 41 { 42 name: "directory", 43 setup: func() file.Resolver { 44 s, err := source.NewFromDirectoryPath("test-fixtures/symlinked-root/nested/link-root") 45 require.NoError(t, err) 46 r, err := s.FileResolver(source.SquashedScope) 47 require.NoError(t, err) 48 return r 49 }, 50 wantRealPaths: strset.New("file1.txt", "nested/file2.txt"), 51 wantVirtualPaths: strset.New("nested/linked-file1.txt"), 52 }, 53 } 54 for _, tt := range tests { 55 t.Run(tt.name, func(t *testing.T) { 56 resolver := tt.setup() 57 locations := AllRegularFiles(resolver) 58 realLocations := strset.New() 59 virtualLocations := strset.New() 60 for _, l := range locations { 61 realLocations.Add(l.RealPath) 62 if l.VirtualPath != "" { 63 virtualLocations.Add(l.VirtualPath) 64 } 65 } 66 67 // this is difficult to reproduce in a cross-platform way 68 realLocations.Remove("/hardlink-1") 69 virtualLocations.Remove("/hardlink-1") 70 tt.wantRealPaths.Remove("/hardlink-1") 71 tt.wantVirtualPaths.Remove("/hardlink-1") 72 73 assert.ElementsMatch(t, tt.wantRealPaths.List(), realLocations.List(), "real paths differ: "+cmp.Diff(tt.wantRealPaths.List(), realLocations.List())) 74 assert.ElementsMatch(t, tt.wantVirtualPaths.List(), virtualLocations.List(), "virtual paths differ: "+cmp.Diff(tt.wantVirtualPaths.List(), virtualLocations.List())) 75 }) 76 } 77 }