mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Use runpy in install/uninstall, refs #928
This commit is contained in:
parent
adfe304281
commit
afdeda8216
2 changed files with 19 additions and 20 deletions
|
|
@ -9,6 +9,7 @@ import pathlib
|
|||
import shutil
|
||||
from subprocess import call
|
||||
import sys
|
||||
from runpy import run_module
|
||||
from .app import Datasette, DEFAULT_CONFIG, CONFIG_OPTIONS, pm
|
||||
from .utils import (
|
||||
check_connection,
|
||||
|
|
@ -235,12 +236,8 @@ def package(
|
|||
@click.argument("packages", nargs=-1, required=True)
|
||||
def install(packages):
|
||||
"Install Python packages - e.g. Datasette plugins - into the same environment as Datasette"
|
||||
from pip._internal.cli.main import main
|
||||
|
||||
try:
|
||||
main(["install"] + list(packages))
|
||||
except SystemExit as e:
|
||||
pass
|
||||
sys.argv = ["pip", "install"] + list(packages)
|
||||
run_module("pip", run_name="__main__")
|
||||
|
||||
|
||||
@cli.command()
|
||||
|
|
@ -248,12 +245,8 @@ def install(packages):
|
|||
@click.option("-y", "--yes", is_flag=True, help="Don't ask for confirmation")
|
||||
def uninstall(packages, yes):
|
||||
"Uninstall Python packages (e.g. plugins) from the Datasette environment"
|
||||
from pip._internal.cli.main import main
|
||||
|
||||
try:
|
||||
main(["uninstall"] + list(packages) + (["-y"] if yes else []))
|
||||
except SystemExit as e:
|
||||
pass
|
||||
sys.argv = ["pip", "uninstall"] + list(packages) + (["-y"] if yes else [])
|
||||
run_module("pip", run_name="__main__")
|
||||
|
||||
|
||||
@cli.command()
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import io
|
|||
import json
|
||||
import pathlib
|
||||
import pytest
|
||||
import sys
|
||||
import textwrap
|
||||
from unittest import mock
|
||||
|
||||
|
|
@ -110,17 +111,22 @@ def test_metadata_yaml():
|
|||
assert {"title": "Hello from YAML"} == response.json
|
||||
|
||||
|
||||
@mock.patch("pip._internal.cli.main.main")
|
||||
def test_install(main):
|
||||
@mock.patch("datasette.cli.run_module")
|
||||
def test_install(run_module):
|
||||
runner = CliRunner()
|
||||
runner.invoke(cli, ["install", "datasette-mock-plugin", "datasette-mock-plugin2"])
|
||||
main.assert_called_once_with(
|
||||
["install", "datasette-mock-plugin", "datasette-mock-plugin2"]
|
||||
)
|
||||
run_module.assert_called_once_with("pip", run_name="__main__")
|
||||
assert sys.argv == [
|
||||
"pip",
|
||||
"install",
|
||||
"datasette-mock-plugin",
|
||||
"datasette-mock-plugin2",
|
||||
]
|
||||
|
||||
|
||||
@mock.patch("pip._internal.cli.main.main")
|
||||
def test_uninstall(main):
|
||||
@mock.patch("datasette.cli.run_module")
|
||||
def test_uninstall(run_module):
|
||||
runner = CliRunner()
|
||||
runner.invoke(cli, ["uninstall", "datasette-mock-plugin", "-y"])
|
||||
main.assert_called_once_with(["uninstall", "datasette-mock-plugin", "-y"])
|
||||
run_module.assert_called_once_with("pip", run_name="__main__")
|
||||
assert sys.argv == ["pip", "uninstall", "datasette-mock-plugin", "-y"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue