Skip to content

Commit

Permalink
chore: fix type for tests
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Phillips <[email protected]>
  • Loading branch information
spiffcs committed Jan 30, 2025
1 parent 47a1c46 commit 13ef836
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 37 deletions.
8 changes: 4 additions & 4 deletions syft/format/common/cyclonedxhelpers/to_format_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package cyclonedxhelpers

import (
"fmt"
"os"
"slices"
"strings"
"time"

"github.com/CycloneDX/cyclonedx-go"
"github.com/google/uuid"

stfile "github.com/anchore/stereoscope/pkg/file"
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/cpe"
Expand Down Expand Up @@ -52,9 +52,9 @@ func ToFormatModel(s sbom.SBOM) *cyclonedx.BOM {
if !exists {
continue
}
if fileMetadata.IsDir() ||
fileMetadata.Mode() == os.ModeSymlink ||
fileMetadata.Mode() == os.ModeSocket {
if fileMetadata.Type == stfile.TypeDirectory ||
fileMetadata.Type == stfile.TypeSocket ||
fileMetadata.Type == stfile.TypeSymLink {
// skip dir, symlinks and sockets for the final bom
continue
}
Expand Down
42 changes: 9 additions & 33 deletions syft/format/common/cyclonedxhelpers/to_format_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package cyclonedxhelpers

import (
"fmt"
"os"
"testing"
"time"

"github.com/CycloneDX/cyclonedx-go"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

stfile "github.com/anchore/stereoscope/pkg/file"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/format/internal/cyclonedxutil/helpers"
Expand Down Expand Up @@ -161,7 +160,7 @@ func Test_FileComponents(t *testing.T) {
Artifacts: sbom.Artifacts{
Packages: pkg.NewCollection(p1),
FileMetadata: map[file.Coordinates]file.Metadata{
{RealPath: "/test"}: {Path: "/test", FileInfo: newMockFileInfo(false, false)}, // Embed the mock that always returns IsDir() = true
{RealPath: "/test"}: {Path: "/test", Type: stfile.TypeRegular},
},
FileDigests: map[file.Coordinates][]file.Digest{
{RealPath: "/test"}: {
Expand Down Expand Up @@ -194,7 +193,7 @@ func Test_FileComponents(t *testing.T) {
sbom: sbom.SBOM{
Artifacts: sbom.Artifacts{
FileMetadata: map[file.Coordinates]file.Metadata{
{RealPath: "/test"}: {Path: "/test", FileInfo: newMockFileInfo(false, false)},
{RealPath: "/test"}: {Path: "/test", Type: stfile.TypeRegular},
},
FileDigests: map[file.Coordinates][]file.Digest{
{RealPath: "/test"}: {
Expand Down Expand Up @@ -228,7 +227,7 @@ func Test_FileComponents(t *testing.T) {
sbom: sbom.SBOM{
Artifacts: sbom.Artifacts{
FileMetadata: map[file.Coordinates]file.Metadata{
{RealPath: "/test"}: {Path: "/test", FileInfo: newMockFileInfo(false, false)},
{RealPath: "/test"}: {Path: "/test", Type: stfile.TypeRegular},
},
FileDigests: map[file.Coordinates][]file.Digest{
{RealPath: "/test"}: {
Expand All @@ -248,14 +247,14 @@ func Test_FileComponents(t *testing.T) {
Artifacts: sbom.Artifacts{
FileMetadata: map[file.Coordinates]file.Metadata{
{RealPath: "/testdir"}: {
Path: "/testdir",
FileInfo: newMockFileInfo(true, false),
Path: "/testdir",
Type: stfile.TypeDirectory,
},
{RealPath: "/testsym"}: {
Path: "/testsym",
FileInfo: newMockFileInfo(false, true),
Path: "/testsym",
Type: stfile.TypeSymLink,
},
{RealPath: "/test"}: {Path: "/test", FileInfo: newMockFileInfo(false, false)},
{RealPath: "/test"}: {Path: "/test", Type: stfile.TypeRegular},
},
FileDigests: map[file.Coordinates][]file.Digest{
{RealPath: "/test"}: {
Expand Down Expand Up @@ -311,29 +310,6 @@ type mockFileInfo struct {
isSymlink bool
}

func newMockFileInfo(isDir, isSym bool) mockFileInfo {
return mockFileInfo{
isDir,
isSym,
}
}

// Implement os.FileInfo interface methods
func (m mockFileInfo) Name() string { return "mockDir" }
func (m mockFileInfo) Size() int64 { return 0 }
func (m mockFileInfo) Mode() os.FileMode {
if m.isSymlink {
return os.ModeSymlink
}
if m.isDir {
return os.ModeDir
}
return os.ModeType
} // Mark as directory
func (m mockFileInfo) ModTime() time.Time { return time.Now() }
func (m mockFileInfo) IsDir() bool { return m.isDir }
func (m mockFileInfo) Sys() any { return nil }

func Test_toBomDescriptor(t *testing.T) {
type args struct {
name string
Expand Down

0 comments on commit 13ef836

Please sign in to comment.