github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/lua/parse_rockspec_test.go (about)

     1  package lua
     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 TestParseRockspec(t *testing.T) {
    15  	ctx := context.TODO()
    16  	tests := []struct {
    17  		Fixture     string
    18  		ExpectedPkg pkg.Package
    19  	}{
    20  		{
    21  			Fixture: "test-fixtures/rockspec/kong-3.7.0-0.rockspec",
    22  			ExpectedPkg: pkg.Package{
    23  				Name:     "kong",
    24  				Version:  "3.7.0-0",
    25  				PURL:     "pkg:luarocks/kong@3.7.0-0",
    26  				Type:     pkg.LuaRocksPkg,
    27  				Language: pkg.Lua,
    28  				Licenses: pkg.NewLicenseSet(
    29  					pkg.NewLicenseFromLocationsWithContext(ctx, "Apache-2.0", file.NewLocation("test-fixtures/rockspec/kong-3.7.0-0.rockspec")),
    30  				),
    31  				Metadata: pkg.LuaRocksPackage{
    32  					Name:        "kong",
    33  					Version:     "3.7.0-0",
    34  					License:     "Apache-2.0",
    35  					Homepage:    "https://konghq.com",
    36  					Description: "Kong is a scalable and customizable API Management Layer built on top of Nginx.",
    37  					URL:         "git+https://github.com/Kong/kong.git",
    38  					Dependencies: map[string]string{
    39  						"inspect":               "== 3.1.3",
    40  						"luasec":                "== 1.3.2",
    41  						"luasocket":             "== 3.0-rc1",
    42  						"penlight":              "== 1.13.1",
    43  						"lua-resty-http":        "== 0.17.1",
    44  						"lua-resty-jit-uuid":    "== 0.0.7",
    45  						"lua-ffi-zlib":          "== 0.6",
    46  						"multipart":             "== 0.5.9",
    47  						"version":               "== 1.0.1",
    48  						"kong-lapis":            "== 1.16.0.1",
    49  						"kong-pgmoon":           "== 1.16.2",
    50  						"luatz":                 "== 0.4",
    51  						"lua_system_constants":  "== 0.1.4",
    52  						"lyaml":                 "== 6.2.8",
    53  						"luasyslog":             "== 2.0.1",
    54  						"lua_pack":              "== 2.0.0",
    55  						"binaryheap":            ">= 0.4",
    56  						"luaxxhash":             ">= 1.0",
    57  						"lua-protobuf":          "== 0.5.0",
    58  						"lua-resty-healthcheck": "== 3.0.1",
    59  						"lua-messagepack":       "== 0.5.4",
    60  						"lua-resty-aws":         "== 1.3.6",
    61  						"lua-resty-openssl":     "== 1.2.0",
    62  						"lua-resty-counter":     "== 0.2.1",
    63  						"lua-resty-ipmatcher":   "== 0.6.1",
    64  						"lua-resty-acme":        "== 0.12.0",
    65  						"lua-resty-session":     "== 4.0.5",
    66  						"lua-resty-timer-ng":    "== 0.2.6",
    67  						"lpeg":                  "== 1.1.0",
    68  						"lua-resty-ljsonschema": "== 1.1.6-2",
    69  					},
    70  				},
    71  			},
    72  		},
    73  		{
    74  			Fixture: "test-fixtures/rockspec/lpeg-1.0.2-1.rockspec",
    75  			ExpectedPkg: pkg.Package{
    76  				Name:     "LPeg",
    77  				Version:  "1.0.2-1",
    78  				PURL:     "pkg:luarocks/LPeg@1.0.2-1",
    79  				Type:     pkg.LuaRocksPkg,
    80  				Language: pkg.Lua,
    81  				Licenses: pkg.NewLicenseSet(
    82  					pkg.NewLicenseFromLocationsWithContext(ctx, "MIT/X11", file.NewLocation("test-fixtures/rockspec/lpeg-1.0.2-1.rockspec")),
    83  				),
    84  				Metadata: pkg.LuaRocksPackage{
    85  					Name:        "LPeg",
    86  					Version:     "1.0.2-1",
    87  					License:     "MIT/X11",
    88  					Homepage:    "http://www.inf.puc-rio.br/~roberto/lpeg.html",
    89  					Description: "Parsing Expression Grammars For Lua",
    90  					URL:         "http://www.inf.puc-rio.br/~roberto/lpeg/lpeg-1.0.2.tar.gz",
    91  					Dependencies: map[string]string{
    92  						"lua": ">= 5.1",
    93  					},
    94  				},
    95  			},
    96  		},
    97  		{
    98  			Fixture: "test-fixtures/rockspec/kong-pgmoon-1.16.2-1.rockspec",
    99  			ExpectedPkg: pkg.Package{
   100  				Name:     "kong-pgmoon",
   101  				Version:  "1.16.2-1",
   102  				PURL:     "pkg:luarocks/kong-pgmoon@1.16.2-1",
   103  				Type:     pkg.LuaRocksPkg,
   104  				Language: pkg.Lua,
   105  				Licenses: pkg.NewLicenseSet(
   106  					pkg.NewLicenseFromLocationsWithContext(ctx, "MIT", file.NewLocation("test-fixtures/rockspec/kong-pgmoon-1.16.2-1.rockspec")),
   107  				),
   108  				Metadata: pkg.LuaRocksPackage{
   109  					Name:        "kong-pgmoon",
   110  					Version:     "1.16.2-1",
   111  					License:     "MIT",
   112  					Homepage:    "https://github.com/Kong/pgmoon",
   113  					Description: "Postgres driver for OpenResty and Lua",
   114  					URL:         "git+https://github.com/kong/pgmoon.git",
   115  					Dependencies: map[string]string{
   116  						"lua":  ">= 5.1",
   117  						"lpeg": "",
   118  					},
   119  				},
   120  			},
   121  		},
   122  		{
   123  			Fixture: "test-fixtures/rockspec/luasyslog-2.0.1-1.rockspec",
   124  			ExpectedPkg: pkg.Package{
   125  				Name:     "luasyslog",
   126  				Version:  "2.0.1-1",
   127  				PURL:     "pkg:luarocks/luasyslog@2.0.1-1",
   128  				Type:     pkg.LuaRocksPkg,
   129  				Language: pkg.Lua,
   130  				Licenses: pkg.NewLicenseSet(
   131  					pkg.NewLicenseFromLocationsWithContext(ctx, "MIT/X11", file.NewLocation("test-fixtures/rockspec/luasyslog-2.0.1-1.rockspec")),
   132  				),
   133  				Metadata: pkg.LuaRocksPackage{
   134  					Name:        "luasyslog",
   135  					Version:     "2.0.1-1",
   136  					License:     "MIT/X11",
   137  					Homepage:    "https://github.com/lunarmodules/luasyslog",
   138  					Description: "Syslog logging for Lua",
   139  					URL:         "git://github.com/lunarmodules/luasyslog.git",
   140  					Dependencies: map[string]string{
   141  						"lua":        ">= 5.1",
   142  						"lualogging": ">= 1.4.0, < 2.0.0",
   143  					},
   144  				},
   145  			},
   146  		},
   147  	}
   148  
   149  	for _, test := range tests {
   150  		t.Run(test.Fixture, func(t *testing.T) {
   151  			test.ExpectedPkg.Locations.Add(file.NewLocation(test.Fixture))
   152  			pkgtest.TestFileParser(t, test.Fixture, parseRockspec, []pkg.Package{test.ExpectedPkg}, nil)
   153  		})
   154  	}
   155  }
   156  
   157  func Test_corruptRockspec(t *testing.T) {
   158  	pkgtest.NewCatalogTester().
   159  		FromFile(t, "test-fixtures/corrupt/bad-1.23.0-0.rockspec").
   160  		WithError().
   161  		TestParser(t, parseRockspec)
   162  }
   163  
   164  func Test_parseDependency(t *testing.T) {
   165  	tests := []struct {
   166  		name            string
   167  		input           string
   168  		expectedName    string
   169  		expectedVersion string
   170  	}{
   171  		{
   172  			name:            "dependency with >= constraint",
   173  			input:           "lua >= 5.1",
   174  			expectedName:    "lua",
   175  			expectedVersion: ">= 5.1",
   176  		},
   177  		{
   178  			name:            "dependency with == constraint",
   179  			input:           "inspect == 3.1.3",
   180  			expectedName:    "inspect",
   181  			expectedVersion: "== 3.1.3",
   182  		},
   183  		{
   184  			name:            "dependency without constraint",
   185  			input:           "lpeg",
   186  			expectedName:    "lpeg",
   187  			expectedVersion: "",
   188  		},
   189  		{
   190  			name:            "dependency with complex constraint",
   191  			input:           "lualogging >= 1.4.0, < 2.0.0",
   192  			expectedName:    "lualogging",
   193  			expectedVersion: ">= 1.4.0, < 2.0.0",
   194  		},
   195  		{
   196  			name:            "dependency with version including dash",
   197  			input:           "luasocket == 3.0-rc1",
   198  			expectedName:    "luasocket",
   199  			expectedVersion: "== 3.0-rc1",
   200  		},
   201  		{
   202  			name:            "dependency with extra whitespace",
   203  			input:           "  kong-pgmoon   ==   1.16.2  ",
   204  			expectedName:    "kong-pgmoon",
   205  			expectedVersion: "==   1.16.2",
   206  		},
   207  		{
   208  			name:            "empty string",
   209  			input:           "",
   210  			expectedName:    "",
   211  			expectedVersion: "",
   212  		},
   213  		{
   214  			name:            "whitespace only",
   215  			input:           "   ",
   216  			expectedName:    "",
   217  			expectedVersion: "",
   218  		},
   219  	}
   220  
   221  	for _, test := range tests {
   222  		t.Run(test.name, func(t *testing.T) {
   223  			actualName, actualVersion := parseDependency(test.input)
   224  			assert.Equal(t, test.expectedName, actualName)
   225  			assert.Equal(t, test.expectedVersion, actualVersion)
   226  		})
   227  	}
   228  }