github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/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  			actualPkgs, _, err := parseTarWrappedJavaArchive(nil, nil, file.LocationReadCloser{
    44  				Location:   file.NewLocation(test.fixture),
    45  				ReadCloser: fixture,
    46  			})
    47  			require.NoError(t, err)
    48  
    49  			var actualNames []string
    50  			for _, p := range actualPkgs {
    51  				actualNames = append(actualNames, p.Name)
    52  			}
    53  
    54  			assert.ElementsMatch(t, test.expected, actualNames)
    55  		})
    56  	}
    57  }