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