mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
feat: Add support for pip Package Manager (#33)
* feat/init-pip-cmd * feat: Add PyPi resolver * test: Add tests for pypi and pypi_resolver * refactor: unify package dependency resolution and improve PyPI version handling using registry adapter * chore: remove unused file Signed-off-by: Sahil Bansal <bansalsahil315@gmail.com> * feat: add Python dependency parsing with extras support * refactor(deps): Improve PyPI dependency resolution and add custom resolver support * chore: remove extra file * fix: improve dependency resolution and package deduplication * chore: remove extra print statement * chore: typo fix * feat: support PyPi package extras * test: add test for pypi dependency parse function * fix: remove overwritten of parsedCmd * chore: remove extra print statement * chore: typo fix * feat: support PyPi package extras * test: add test for pypi dependency parse function * fix: remove overwritten of parsedCmd * refactor: enhance code readability & remove extra code * chore: remove extra code * Update cmd/npm/npm.go Co-authored-by: Omkar Phansopkar <omkarphansopkar@gmail.com> Signed-off-by: Sahil Bansal <bansalsahil315@gmail.com> * Update cmd/npm/pnpm.go Co-authored-by: Omkar Phansopkar <omkarphansopkar@gmail.com> Signed-off-by: Sahil Bansal <bansalsahil315@gmail.com> * Update cmd/pypi/pip.go Co-authored-by: Omkar Phansopkar <omkarphansopkar@gmail.com> Signed-off-by: Sahil Bansal <bansalsahil315@gmail.com> --------- Signed-off-by: Sahil Bansal <bansalsahil315@gmail.com> Co-authored-by: Omkar Phansopkar <omkarphansopkar@gmail.com>
This commit is contained in:
co-authored by
Omkar Phansopkar
parent
4031219375
commit
53783c6604
@@ -0,0 +1,158 @@
|
||||
package packagemanager
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPipParsePackageInfo(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
input string
|
||||
pkgName string
|
||||
version string
|
||||
extras []string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "simple package name",
|
||||
input: "fastapi",
|
||||
pkgName: "fastapi",
|
||||
version: "",
|
||||
extras: nil,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "package with exact version & extra",
|
||||
input: "fastapi[all]==0.115.7",
|
||||
pkgName: "fastapi",
|
||||
version: "==0.115.7",
|
||||
extras: []string{"all"},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "package with version range",
|
||||
input: "requests>=2.0,<3.0",
|
||||
pkgName: "requests",
|
||||
version: ">=2.0,<3.0",
|
||||
extras: nil,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "package with exclusion",
|
||||
input: "pydantic!=1.8,!=1.8.1",
|
||||
pkgName: "pydantic",
|
||||
version: "!=1.8,!=1.8.1",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "package with compatible release",
|
||||
input: "django~=3.1.0",
|
||||
pkgName: "django",
|
||||
version: "~=3.1.0",
|
||||
extras: nil,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "package with greater than with empty extra",
|
||||
input: "numpy[]>1.20.0",
|
||||
pkgName: "numpy",
|
||||
version: ">1.20.0",
|
||||
extras: nil,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "package with less than",
|
||||
input: "pandas<2.0.0",
|
||||
pkgName: "pandas",
|
||||
version: "<2.0.0",
|
||||
extras: nil,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "empty input",
|
||||
input: "",
|
||||
pkgName: "",
|
||||
version: "",
|
||||
extras: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "only version specifier",
|
||||
input: "==1.0.0",
|
||||
pkgName: "",
|
||||
version: "",
|
||||
extras: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "package with whitespace",
|
||||
input: " requests == 2.0.0 ",
|
||||
pkgName: "requests",
|
||||
version: "== 2.0.0",
|
||||
extras: nil,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
pkgName, version, extras, err := pipParsePackageInfo(tc.input)
|
||||
if tc.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tc.pkgName, pkgName)
|
||||
assert.Equal(t, tc.version, version)
|
||||
assert.Equal(t, tc.extras, extras)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPipConvertCompatibleRelease(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "standard version",
|
||||
input: "~=3.1.0",
|
||||
expected: ">=3.1.0,<3.2.0",
|
||||
},
|
||||
{
|
||||
name: "single digit minor",
|
||||
input: "~=2.1.5",
|
||||
expected: ">=2.1.5,<2.2.0",
|
||||
},
|
||||
{
|
||||
name: "double digit minor",
|
||||
input: "~=1.10.0",
|
||||
expected: ">=1.10.0,<1.11.0",
|
||||
},
|
||||
{
|
||||
name: "invalid format",
|
||||
input: "~=1",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
name: "missing prefix",
|
||||
input: "3.1.0",
|
||||
expected: "3.1.0",
|
||||
},
|
||||
{
|
||||
name: "extra segments",
|
||||
input: "~=2.1.5.2",
|
||||
expected: ">=2.1.5.2,<2.1.6",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := pipConvertCompatibleRelease(tc.input)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user