github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/formats/internal/testutils/directory_input.go (about)

     1  package testutils
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/anchore/syft/syft/cpe"
    11  	"github.com/anchore/syft/syft/file"
    12  	"github.com/anchore/syft/syft/linux"
    13  	"github.com/anchore/syft/syft/pkg"
    14  	"github.com/anchore/syft/syft/sbom"
    15  	"github.com/anchore/syft/syft/source"
    16  )
    17  
    18  func DirectoryInput(t testing.TB, dir string) sbom.SBOM {
    19  	catalog := newDirectoryCatalog()
    20  
    21  	path := filepath.Join(dir, "some", "path")
    22  
    23  	require.NoError(t, os.MkdirAll(path, 0755))
    24  
    25  	src, err := source.NewFromDirectory(
    26  		source.DirectoryConfig{
    27  			Path: path,
    28  			Base: dir,
    29  		},
    30  	)
    31  	require.NoError(t, err)
    32  
    33  	return sbom.SBOM{
    34  		Artifacts: sbom.Artifacts{
    35  			Packages: catalog,
    36  			LinuxDistribution: &linux.Release{
    37  				PrettyName: "debian",
    38  				Name:       "debian",
    39  				ID:         "debian",
    40  				IDLike:     []string{"like!"},
    41  				Version:    "1.2.3",
    42  				VersionID:  "1.2.3",
    43  			},
    44  		},
    45  		Source: src.Describe(),
    46  		Descriptor: sbom.Descriptor{
    47  			Name:    "syft",
    48  			Version: "v0.42.0-bogus",
    49  			// the application configuration should be persisted here, however, we do not want to import
    50  			// the application configuration in this package (it's reserved only for ingestion by the cmd package)
    51  			Configuration: map[string]string{
    52  				"config-key": "config-value",
    53  			},
    54  		},
    55  	}
    56  }
    57  
    58  func DirectoryInputWithAuthorField(t testing.TB) sbom.SBOM {
    59  	catalog := newDirectoryCatalogWithAuthorField()
    60  
    61  	dir := t.TempDir()
    62  	path := filepath.Join(dir, "some", "path")
    63  
    64  	require.NoError(t, os.MkdirAll(path, 0755))
    65  
    66  	src, err := source.NewFromDirectory(
    67  		source.DirectoryConfig{
    68  			Path: path,
    69  			Base: dir,
    70  		},
    71  	)
    72  	require.NoError(t, err)
    73  
    74  	return sbom.SBOM{
    75  		Artifacts: sbom.Artifacts{
    76  			Packages: catalog,
    77  			LinuxDistribution: &linux.Release{
    78  				PrettyName: "debian",
    79  				Name:       "debian",
    80  				ID:         "debian",
    81  				IDLike:     []string{"like!"},
    82  				Version:    "1.2.3",
    83  				VersionID:  "1.2.3",
    84  			},
    85  		},
    86  		Source: src.Describe(),
    87  		Descriptor: sbom.Descriptor{
    88  			Name:    "syft",
    89  			Version: "v0.42.0-bogus",
    90  			// the application configuration should be persisted here, however, we do not want to import
    91  			// the application configuration in this package (it's reserved only for ingestion by the cmd package)
    92  			Configuration: map[string]string{
    93  				"config-key": "config-value",
    94  			},
    95  		},
    96  	}
    97  }
    98  
    99  func newDirectoryCatalog() *pkg.Collection {
   100  	catalog := pkg.NewCollection()
   101  
   102  	// populate catalog with test data
   103  	catalog.Add(pkg.Package{
   104  		Name:    "package-1",
   105  		Version: "1.0.1",
   106  		Type:    pkg.PythonPkg,
   107  		FoundBy: "the-cataloger-1",
   108  		Locations: file.NewLocationSet(
   109  			file.NewLocation("/some/path/pkg1"),
   110  		),
   111  		Language:     pkg.Python,
   112  		MetadataType: pkg.PythonPackageMetadataType,
   113  		Licenses: pkg.NewLicenseSet(
   114  			pkg.NewLicense("MIT"),
   115  		),
   116  		Metadata: pkg.PythonPackageMetadata{
   117  			Name:    "package-1",
   118  			Version: "1.0.1",
   119  			Files: []pkg.PythonFileRecord{
   120  				{
   121  					Path: "/some/path/pkg1/dependencies/foo",
   122  				},
   123  			},
   124  		},
   125  		PURL: "a-purl-2", // intentionally a bad pURL for test fixtures
   126  		CPEs: []cpe.CPE{
   127  			cpe.Must("cpe:2.3:*:some:package:2:*:*:*:*:*:*:*"),
   128  		},
   129  	})
   130  	catalog.Add(pkg.Package{
   131  		Name:    "package-2",
   132  		Version: "2.0.1",
   133  		Type:    pkg.DebPkg,
   134  		FoundBy: "the-cataloger-2",
   135  		Locations: file.NewLocationSet(
   136  			file.NewLocation("/some/path/pkg1"),
   137  		),
   138  		MetadataType: pkg.DpkgMetadataType,
   139  		Metadata: pkg.DpkgMetadata{
   140  			Package: "package-2",
   141  			Version: "2.0.1",
   142  		},
   143  		PURL: "pkg:deb/debian/package-2@2.0.1",
   144  		CPEs: []cpe.CPE{
   145  			cpe.Must("cpe:2.3:*:some:package:2:*:*:*:*:*:*:*"),
   146  		},
   147  	})
   148  
   149  	return catalog
   150  }
   151  
   152  func newDirectoryCatalogWithAuthorField() *pkg.Collection {
   153  	catalog := pkg.NewCollection()
   154  
   155  	// populate catalog with test data
   156  	catalog.Add(pkg.Package{
   157  		Name:    "package-1",
   158  		Version: "1.0.1",
   159  		Type:    pkg.PythonPkg,
   160  		FoundBy: "the-cataloger-1",
   161  		Locations: file.NewLocationSet(
   162  			file.NewLocation("/some/path/pkg1"),
   163  		),
   164  		Language:     pkg.Python,
   165  		MetadataType: pkg.PythonPackageMetadataType,
   166  		Licenses: pkg.NewLicenseSet(
   167  			pkg.NewLicense("MIT"),
   168  		),
   169  		Metadata: pkg.PythonPackageMetadata{
   170  			Name:    "package-1",
   171  			Version: "1.0.1",
   172  			Author:  "test-author",
   173  			Files: []pkg.PythonFileRecord{
   174  				{
   175  					Path: "/some/path/pkg1/dependencies/foo",
   176  				},
   177  			},
   178  		},
   179  		PURL: "a-purl-2", // intentionally a bad pURL for test fixtures
   180  		CPEs: []cpe.CPE{
   181  			cpe.Must("cpe:2.3:*:some:package:2:*:*:*:*:*:*:*"),
   182  		},
   183  	})
   184  	catalog.Add(pkg.Package{
   185  		Name:    "package-2",
   186  		Version: "2.0.1",
   187  		Type:    pkg.DebPkg,
   188  		FoundBy: "the-cataloger-2",
   189  		Locations: file.NewLocationSet(
   190  			file.NewLocation("/some/path/pkg1"),
   191  		),
   192  		MetadataType: pkg.DpkgMetadataType,
   193  		Metadata: pkg.DpkgMetadata{
   194  			Package: "package-2",
   195  			Version: "2.0.1",
   196  		},
   197  		PURL: "pkg:deb/debian/package-2@2.0.1",
   198  		CPEs: []cpe.CPE{
   199  			cpe.Must("cpe:2.3:*:some:package:2:*:*:*:*:*:*:*"),
   200  		},
   201  	})
   202  
   203  	return catalog
   204  }