github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/syft/pkg/cataloger/java/tar_wrapped_archive_parser_test.go (about)

     1  package java
     2  
     3  import (
     4  	"os"
     5  	"path"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/anchore/syft/syft/file"
    12  )
    13  
    14  func Test_parseTarWrappedJavaArchive(t *testing.T) {
    15  	tests := []struct {
    16  		fixture  string
    17  		expected []string
    18  	}{
    19  		{
    20  			fixture: "test-fixtures/java-builds/packages/example-java-app-maven-0.1.0.tar",
    21  			expected: []string{
    22  				"example-java-app-maven",
    23  				"joda-time",
    24  			},
    25  		},
    26  		{
    27  			fixture: "test-fixtures/java-builds/packages/example-java-app-maven-0.1.0.tar.gz",
    28  			expected: []string{
    29  				"example-java-app-maven",
    30  				"joda-time",
    31  			},
    32  		},
    33  	}
    34  	for _, test := range tests {
    35  		t.Run(path.Base(test.fixture), func(t *testing.T) {
    36  			generateJavaBuildFixture(t, test.fixture)
    37  
    38  			fixture, err := os.Open(test.fixture)
    39  			if err != nil {
    40  				t.Fatalf("failed to open fixture: %+v", err)
    41  			}
    42  
    43  			gtp := newGenericTarWrappedJavaArchiveParser(ArchiveCatalogerConfig{})
    44  			actualPkgs, _, err := gtp.parseTarWrappedJavaArchive(nil, nil, file.LocationReadCloser{
    45  				Location:   file.NewLocation(test.fixture),
    46  				ReadCloser: fixture,
    47  			})
    48  			require.NoError(t, err)
    49  
    50  			var actualNames []string
    51  			for _, p := range actualPkgs {
    52  				actualNames = append(actualNames, p.Name)
    53  			}
    54  
    55  			assert.ElementsMatch(t, test.expected, actualNames)
    56  		})
    57  	}
    58  }