Skip to content

Commit

Permalink
fix: makes uninstall work when using the long name displayed in ape p…
Browse files Browse the repository at this point in the history
…m list (#2287)

Co-authored-by: slush <sam@apeworx.io>
Co-authored-by: antazoey <jules@apeworx.io>
  • Loading branch information
3 people authored Sep 17, 2024
1 parent c50bee9 commit 385e77c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
6 changes: 5 additions & 1 deletion docs/userguides/dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,16 @@ project.dependencies.install(

### uninstall

Remove previously installed packages using the `uninstall` command:
Remove previously installed packages using the `uninstall` command, providing it either the dependency's name or package_id:

```shell
ape pm uninstall OpenZeppelin
```

```shell
ape pm uninstall OpenZeppelin/openzeppelin-contracts
```

If there is a single version installed, the command will remove the single version.
If multiple versions are installed, pass additional arguments specifying the version(s) to be removed:

Expand Down
11 changes: 7 additions & 4 deletions src/ape_pm/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ def uninstall(cli_ctx, name, versions, yes):
versions to remove. You can also choose to remove all versions of the package.
Examples:\n
- Remove specific versions: ape pm remove <PackageName> "1.0.0" "2.0.0"\n
- Prompt to choose versions: ape pm remove <PackageName>\n
- Remove all versions: ape pm remove <PackageName> -y
- Remove specific versions: ape pm uninstall <PackageName> "1.0.0" "2.0.0"\n
- Prompt to choose versions: ape pm uninstall <PackageName>\n
- Remove all versions: ape pm uninstall <PackageName> -y
"""

pm = cli_ctx.local_project
Expand Down Expand Up @@ -233,7 +233,10 @@ def uninstall(cli_ctx, name, versions, yes):

else:
deps_to_remove = {
d for d in installed if d.name == name and (d.version in versions if versions else True)
d
for d in installed
if (d.name == name or d.package_id == name)
and (d.version in versions if versions else True)
}
for dependency in deps_to_remove:
did_find = True
Expand Down
13 changes: 8 additions & 5 deletions tests/functional/test_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ def test_decode_dependency_with_config_override(project):
def test_uri_map(project_with_dependency_config):
actual = project_with_dependency_config.dependencies.uri_map
here = Path(__file__).parent
expected = f"file://{here}/data/projects/LongContractsFolder"
# Wrap in Path to handle Windows.
expected = Path(f"file://{here}/data/projects/LongContractsFolder")
assert "testdependency" in actual
assert str(actual["testdependency"]) == expected
assert Path(str(actual["testdependency"])) == expected


def test_get_dependency_by_package_id(project_with_downloaded_dependencies):
Expand Down Expand Up @@ -254,13 +255,15 @@ def test_install_dependencies_of_dependencies(project, with_dependencies_project
# deps_of_deps = [x for x in actual.project.dependencies.specified]


def test_uninstall(project_with_downloaded_dependencies):
name = "openzeppelin"
@pytest.mark.parametrize("name", ("openzeppelin", "OpenZeppelin/openzeppelin-contracts"))
def test_uninstall(name, project_with_downloaded_dependencies):
version = "4.4.2"
dm = project_with_downloaded_dependencies.dependencies
dependency = dm.get_dependency(name, version)
dependency.uninstall()
assert not any(d.name == name and d.version == version for d in dm.installed)
assert not any(
(d.name == name or d.package_id == name) and d.version == version for d in dm.installed
)


def test_unpack(project_with_downloaded_dependencies):
Expand Down
14 changes: 14 additions & 0 deletions tests/integration/cli/test_pm.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,20 @@ def test_uninstall(pm_runner, integ_project):
assert expected_message in result.output or result._completed_process.stderr


@skip_projects_except("only-dependencies")
def test_uninstall_by_long_name(pm_runner, integ_project):
pm_runner.project = integ_project
package_name = "dependency-in-project-only"
package_long_name = integ_project.dependencies.get_dependency(package_name, "local").package_id

# Install packages
pm_runner.invoke("install", ".", "--force")
result = pm_runner.invoke("uninstall", package_long_name, "--yes")
expected_message = f"Uninstalled '{package_name}=local'."
assert result.exit_code == 0, result.output or result._completed_process.stderr
assert expected_message in result.output or result._completed_process.stderr


@skip_projects_except("only-dependencies")
def test_uninstall_not_exists(pm_runner, integ_project):
pm_runner.project = integ_project
Expand Down

0 comments on commit 385e77c

Please sign in to comment.