Skip to content

Commit a61c828

Browse files
authored
fix: support requirements with options in requirements.txt (#196)
1 parent f43297a commit a61c828

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
boto3==1.26.121 --hash=sha256:f87d694c351eba1dfd19b5bef5892a1047e7adb09c57c2c00049de209a8ab55d
2+
foo == 1.0.0
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
boto3==1.26.121 --hash=sha256:f87d694c351eba1dfd19b5bef5892a1047e7adb09c57c2c00049de209a8ab55d
2+
foo == 1.0.0
3+
4+
# from https://pip.pypa.io/en/stable/topics/secure-installs/#hash-checking-mode
5+
6+
FooProject == 1.2 \
7+
--hash=sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 \
8+
--hash=sha256:486ea46224d1bb4fb680f34f7c9ad96a8f24ec88be73ea8e5a6c65260e9cb8a7
9+
10+
# from https://pip.pypa.io/en/stable/reference/requirements-file-format/#influencing-the-build-system
11+
12+
BarProject >= 1.2 --global-option="--no-user-cfg"

pkg/lockfile/parse-requirements-txt.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212
const PipEcosystem Ecosystem = "PyPI"
1313

1414
// todo: expand this to support more things, e.g.
15-
// https://pip.pypa.io/en/stable/reference/requirements-file-format/#example
15+
//
16+
// https://pip.pypa.io/en/stable/reference/requirements-file-format/#example
1617
func parseLine(line string) PackageDetails {
1718
var constraint string
1819
name := line
@@ -41,7 +42,7 @@ func parseLine(line string) PackageDetails {
4142
name = strings.TrimSpace(splitted[0])
4243

4344
if constraint != "!=" {
44-
version = strings.TrimSpace(splitted[1])
45+
version = strings.Split(strings.TrimSpace(splitted[1]), " ")[0]
4546
}
4647
}
4748

pkg/lockfile/parse-requirements-txt_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,43 @@ func TestParseRequirementsTxt_CyclicRComplex(t *testing.T) {
526526
})
527527
}
528528

529+
func TestParseRequirementsTxt_WithPerRequirementOptions(t *testing.T) {
530+
t.Parallel()
531+
532+
packages, err := lockfile.ParseRequirementsTxt("fixtures/pip/with-per-requirement-options.txt")
533+
534+
if err != nil {
535+
t.Errorf("Got unexpected error: %v", err)
536+
}
537+
538+
expectPackages(t, packages, []lockfile.PackageDetails{
539+
{
540+
Name: "boto3",
541+
Version: "1.26.121",
542+
Ecosystem: lockfile.PipEcosystem,
543+
CompareAs: lockfile.PipEcosystem,
544+
},
545+
{
546+
Name: "foo",
547+
Version: "1.0.0",
548+
Ecosystem: lockfile.PipEcosystem,
549+
CompareAs: lockfile.PipEcosystem,
550+
},
551+
{
552+
Name: "fooproject",
553+
Version: "1.2",
554+
Ecosystem: lockfile.PipEcosystem,
555+
CompareAs: lockfile.PipEcosystem,
556+
},
557+
{
558+
Name: "barproject",
559+
Version: "1.2",
560+
Ecosystem: lockfile.PipEcosystem,
561+
CompareAs: lockfile.PipEcosystem,
562+
},
563+
})
564+
}
565+
529566
func TestParseRequirementsTxt_LineContinuation(t *testing.T) {
530567
t.Parallel()
531568

0 commit comments

Comments
 (0)