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
|
import shutil
|
||||||
from subprocess import call
|
from subprocess import call
|
||||||
import sys
|
import sys
|
||||||
|
from runpy import run_module
|
||||||
from .app import Datasette, DEFAULT_CONFIG, CONFIG_OPTIONS, pm
|
from .app import Datasette, DEFAULT_CONFIG, CONFIG_OPTIONS, pm
|
||||||
from .utils import (
|
from .utils import (
|
||||||
check_connection,
|
check_connection,
|
||||||
|
|
@ -235,12 +236,8 @@ def package(
|
||||||
@click.argument("packages", nargs=-1, required=True)
|
@click.argument("packages", nargs=-1, required=True)
|
||||||
def install(packages):
|
def install(packages):
|
||||||
"Install Python packages - e.g. Datasette plugins - into the same environment as Datasette"
|
"Install Python packages - e.g. Datasette plugins - into the same environment as Datasette"
|
||||||
from pip._internal.cli.main import main
|
sys.argv = ["pip", "install"] + list(packages)
|
||||||
|
run_module("pip", run_name="__main__")
|
||||||
try:
|
|
||||||
main(["install"] + list(packages))
|
|
||||||
except SystemExit as e:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
|
|
@ -248,12 +245,8 @@ def install(packages):
|
||||||
@click.option("-y", "--yes", is_flag=True, help="Don't ask for confirmation")
|
@click.option("-y", "--yes", is_flag=True, help="Don't ask for confirmation")
|
||||||
def uninstall(packages, yes):
|
def uninstall(packages, yes):
|
||||||
"Uninstall Python packages (e.g. plugins) from the Datasette environment"
|
"Uninstall Python packages (e.g. plugins) from the Datasette environment"
|
||||||
from pip._internal.cli.main import main
|
sys.argv = ["pip", "uninstall"] + list(packages) + (["-y"] if yes else [])
|
||||||
|
run_module("pip", run_name="__main__")
|
||||||
try:
|
|
||||||
main(["uninstall"] + list(packages) + (["-y"] if yes else []))
|
|
||||||
except SystemExit as e:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import io
|
||||||
import json
|
import json
|
||||||
import pathlib
|
import pathlib
|
||||||
import pytest
|
import pytest
|
||||||
|
import sys
|
||||||
import textwrap
|
import textwrap
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
|
|
@ -110,17 +111,22 @@ def test_metadata_yaml():
|
||||||
assert {"title": "Hello from YAML"} == response.json
|
assert {"title": "Hello from YAML"} == response.json
|
||||||
|
|
||||||
|
|
||||||
@mock.patch("pip._internal.cli.main.main")
|
@mock.patch("datasette.cli.run_module")
|
||||||
def test_install(main):
|
def test_install(run_module):
|
||||||
runner = CliRunner()
|
runner = CliRunner()
|
||||||
runner.invoke(cli, ["install", "datasette-mock-plugin", "datasette-mock-plugin2"])
|
runner.invoke(cli, ["install", "datasette-mock-plugin", "datasette-mock-plugin2"])
|
||||||
main.assert_called_once_with(
|
run_module.assert_called_once_with("pip", run_name="__main__")
|
||||||
["install", "datasette-mock-plugin", "datasette-mock-plugin2"]
|
assert sys.argv == [
|
||||||
)
|
"pip",
|
||||||
|
"install",
|
||||||
|
"datasette-mock-plugin",
|
||||||
|
"datasette-mock-plugin2",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@mock.patch("pip._internal.cli.main.main")
|
@mock.patch("datasette.cli.run_module")
|
||||||
def test_uninstall(main):
|
def test_uninstall(run_module):
|
||||||
runner = CliRunner()
|
runner = CliRunner()
|
||||||
runner.invoke(cli, ["uninstall", "datasette-mock-plugin", "-y"])
|
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