github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/wordpress/parse_plugin_test.go (about)

     1  package wordpress
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/anchore/syft/syft/file"
    10  	"github.com/anchore/syft/syft/pkg"
    11  	"github.com/anchore/syft/syft/pkg/cataloger/internal/pkgtest"
    12  )
    13  
    14  func TestParseWordpressPluginFiles(t *testing.T) {
    15  	fixture := "test-fixtures/glob-paths/wp-content/plugins/akismet/akismet.php"
    16  	locations := file.NewLocationSet(file.NewLocation(fixture))
    17  	ctx := context.TODO()
    18  	var expectedPkg = pkg.Package{
    19  		Name:      "Akismet Anti-spam: Spam Protection",
    20  		Version:   "5.3",
    21  		Locations: locations,
    22  		Type:      pkg.WordpressPluginPkg,
    23  		Licenses: pkg.NewLicenseSet(
    24  			pkg.NewLicenseFromLocationsWithContext(ctx, "GPLv2"),
    25  		),
    26  		Language: pkg.PHP,
    27  		Metadata: pkg.WordpressPluginEntry{
    28  			PluginInstallDirectory: "akismet",
    29  			Author:                 "Automattic - Anti-spam Team",
    30  			AuthorURI:              "https://automattic.com/wordpress-plugins/",
    31  		},
    32  	}
    33  
    34  	pkgtest.TestFileParser(t, fixture, parseWordpressPluginFiles, []pkg.Package{expectedPkg}, nil)
    35  }
    36  
    37  func Test_extractFields(t *testing.T) {
    38  	tests := []struct {
    39  		name string
    40  		in   string
    41  		want map[string]any
    42  	}{
    43  		{
    44  			name: "carriage returns are stripped",
    45  			in:   "Plugin Name: WP Migration\r\nVersion: 5.3\r\nLicense: GPLv3\r\nAuthor: MonsterInsights\r\nAuthor URI: https://servmask.com/\r\n",
    46  			want: map[string]any{
    47  				"name":       "WP Migration",
    48  				"version":    "5.3",
    49  				"license":    "GPLv3",
    50  				"author":     "MonsterInsights",
    51  				"author_uri": "https://servmask.com/",
    52  			},
    53  		},
    54  	}
    55  	for _, tt := range tests {
    56  		t.Run(tt.name, func(t *testing.T) {
    57  			assert.Equal(t, tt.want, extractFields(tt.in))
    58  		})
    59  	}
    60  }