tests: verify requirements.txt captures versions

Signed-off-by: Alfredo Deza <adeza@anchore.com>
This commit is contained in:
Alfredo Deza 2020-07-21 14:26:34 -04:00
parent 83056a4b6f
commit f8a2b7a626
3 changed files with 54 additions and 5 deletions

View File

@ -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)
}

View File

@ -9,11 +9,11 @@ import (
func assertPkgsEqual(t *testing.T, actual []pkg.Package, expected map[string]pkg.Package) { func assertPkgsEqual(t *testing.T, actual []pkg.Package, expected map[string]pkg.Package) {
t.Helper() t.Helper()
if len(actual) != 1 { if len(actual) != len(expected) {
for _, a := range actual { for _, a := range actual {
t.Log(" ", a) 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 { 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) 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) 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)
}
} }
} }

View File

@ -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