@@ -15,13 +15,30 @@ import (
1515
1616 "github.com/chainguard-dev/clog"
1717 "github.com/chainguard-dev/malcontent/pkg/archive"
18+ "github.com/chainguard-dev/malcontent/pkg/file"
1819 "github.com/chainguard-dev/malcontent/pkg/malcontent"
1920 "github.com/chainguard-dev/malcontent/pkg/render"
2021 "github.com/chainguard-dev/malcontent/rules"
2122 thirdparty "github.com/chainguard-dev/malcontent/third_party"
2223 "github.com/google/go-cmp/cmp"
2324)
2425
26+ // readTestFile reads a file using file.GetContents for consistency with production code.
27+ func readTestFile (t * testing.T , path string ) []byte {
28+ t .Helper ()
29+ f , err := os .Open (path )
30+ if err != nil {
31+ t .Fatalf ("failed to open test file %s: %v" , path , err )
32+ }
33+ defer f .Close ()
34+ buf := make ([]byte , file .ExtractBuffer )
35+ data , err := file .GetContents (f , buf )
36+ if err != nil {
37+ t .Fatalf ("failed to read test file %s: %v" , path , err )
38+ }
39+ return data
40+ }
41+
2542func TestExtractionMethod (t * testing.T ) {
2643 tests := []struct {
2744 name string
@@ -256,10 +273,7 @@ func TestScanArchive(t *testing.T) {
256273
257274 got := out .String ()
258275
259- td , err := os .ReadFile ("testdata/scan_archive" )
260- if err != nil {
261- t .Fatalf ("testdata read failed: %v" , err )
262- }
276+ td := readTestFile (t , "testdata/scan_archive" )
263277 want := string (td )
264278
265279 if diff := cmp .Diff (want , got ); diff != "" {
@@ -303,10 +317,7 @@ func TestScanDeb(t *testing.T) {
303317
304318 got := out .String ()
305319
306- td , err := os .ReadFile ("testdata/scan_deb" )
307- if err != nil {
308- t .Fatalf ("testdata read failed: %v" , err )
309- }
320+ td := readTestFile (t , "testdata/scan_deb" )
310321 want := string (td )
311322
312323 if diff := cmp .Diff (want , got ); diff != "" {
@@ -350,10 +361,7 @@ func TestScanRPM(t *testing.T) {
350361
351362 got := out .String ()
352363
353- td , err := os .ReadFile ("testdata/scan_rpm" )
354- if err != nil {
355- t .Fatalf ("testdata read failed: %v" , err )
356- }
364+ td := readTestFile (t , "testdata/scan_rpm" )
357365 want := string (td )
358366
359367 if diff := cmp .Diff (want , got ); diff != "" {
@@ -397,10 +405,7 @@ func TestScanZlib(t *testing.T) {
397405
398406 got := out .String ()
399407
400- td , err := os .ReadFile ("testdata/scan_zlib" )
401- if err != nil {
402- t .Fatalf ("testdata read failed: %v" , err )
403- }
408+ td := readTestFile (t , "testdata/scan_zlib" )
404409 want := string (td )
405410
406411 if diff := cmp .Diff (want , got ); diff != "" {
@@ -444,10 +449,7 @@ func TestScanZstd(t *testing.T) {
444449
445450 got := out .String ()
446451
447- td , err := os .ReadFile ("testdata/scan_zstd" )
448- if err != nil {
449- t .Fatalf ("testdata read failed: %v" , err )
450- }
452+ td := readTestFile (t , "testdata/scan_zstd" )
451453 want := string (td )
452454
453455 if diff := cmp .Diff (want , got ); diff != "" {
@@ -582,10 +584,7 @@ func TestScanConflictingArchiveFiles(t *testing.T) {
582584 }
583585
584586 got := out .String ()
585- td , err := os .ReadFile ("testdata/scan_conflict" )
586- if err != nil {
587- t .Fatalf ("testdata read failed: %v" , err )
588- }
587+ td := readTestFile (t , "testdata/scan_conflict" )
589588 want := string (td )
590589
591590 if diff := cmp .Diff (want , got ); diff != "" {
0 commit comments