From f8a2b7a6261ba3c9e08010a46508c14025377f97 Mon Sep 17 00:00:00 2001 From: Alfredo Deza Date: Tue, 21 Jul 2020 14:26:34 -0400 Subject: [PATCH] tests: verify requirements.txt captures versions Signed-off-by: Alfredo Deza --- .../python/parse_requirements_test.go | 39 +++++++++++++++++++ .../cataloger/python/parse_wheel_egg_test.go | 13 ++++--- .../test-fixtures/requires/requirements.txt | 7 ++++ 3 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 imgbom/cataloger/python/parse_requirements_test.go create mode 100644 imgbom/cataloger/python/test-fixtures/requires/requirements.txt diff --git a/imgbom/cataloger/python/parse_requirements_test.go b/imgbom/cataloger/python/parse_requirements_test.go new file mode 100644 index 000000000..2b980751d --- /dev/null +++ b/imgbom/cataloger/python/parse_requirements_test.go @@ -0,0 +1,39 @@ +package python + +import ( + "os" + "testing" + + "github.com/anchore/imgbom/imgbom/pkg" +) + +func TestParseRequirementsTxt(t *testing.T) { + expected := map[string]pkg.Package{ + "foo": { + Name: "foo", + Version: "1.0.0", + Language: pkg.Python, + Type: pkg.PythonRequirementsPkg, + Licenses: []string{}, + }, + "flask": { + Name: "flask", + Version: "4.0.0", + Language: pkg.Python, + Type: pkg.PythonRequirementsPkg, + Licenses: []string{}, + }, + } + fixture, err := os.Open("test-fixtures/requires/requirements.txt") + if err != nil { + t.Fatalf("failed to open fixture: %+v", err) + } + + actual, err := parseRequirementsTxt(fixture.Name(), fixture) + if err != nil { + t.Fatalf("failed to parse requirements: %+v", err) + } + + assertPkgsEqual(t, actual, expected) + +} diff --git a/imgbom/cataloger/python/parse_wheel_egg_test.go b/imgbom/cataloger/python/parse_wheel_egg_test.go index c58b27bf6..bd268c636 100644 --- a/imgbom/cataloger/python/parse_wheel_egg_test.go +++ b/imgbom/cataloger/python/parse_wheel_egg_test.go @@ -9,11 +9,11 @@ import ( func assertPkgsEqual(t *testing.T, actual []pkg.Package, expected map[string]pkg.Package) { t.Helper() - if len(actual) != 1 { + if len(actual) != len(expected) { for _, a := range actual { t.Log(" ", a) } - t.Fatalf("unexpected package count: %d!=%d", len(actual), 1) + t.Fatalf("unexpected package count: %d!=%d", len(actual), len(expected)) } for _, a := range actual { @@ -34,10 +34,13 @@ func assertPkgsEqual(t *testing.T, actual []pkg.Package, expected map[string]pkg t.Errorf("bad package type: %+v", a.Type) } - if len(a.Licenses) < 1 { + if len(a.Licenses) < len(expectedPkg.Licenses) { t.Errorf("bad package licenses count: '%+v'", a.Licenses) - } else if a.Licenses[0] != expectedPkg.Licenses[0] { - t.Errorf("bad package licenses: '%+v'", a.Licenses) + } + if len(a.Licenses) > 0 { + if a.Licenses[0] != expectedPkg.Licenses[0] { + t.Errorf("bad package licenses: '%+v'", a.Licenses) + } } } diff --git a/imgbom/cataloger/python/test-fixtures/requires/requirements.txt b/imgbom/cataloger/python/test-fixtures/requires/requirements.txt new file mode 100644 index 000000000..8b115d3af --- /dev/null +++ b/imgbom/cataloger/python/test-fixtures/requires/requirements.txt @@ -0,0 +1,7 @@ + flask == 4.0.0 +# a line that is ignored +sqlalchemy >= 1.0.0 + foo == 1.0.0 # a comment that needs to be ignored +-e https://github.com/pecan/pecan.git +-r other-requirements.txt +--requirements super-secretrequirements.txt \ No newline at end of file