Add test for run_commands

This commit is contained in:
Paul O'Leary McCann 2022-09-12 15:22:43 +09:00
parent e13e11e1d8
commit 0989ccdc8a
2 changed files with 31 additions and 5 deletions

View File

@ -25,6 +25,7 @@ def project_update_dvc_cli(
project_dir: Path = Arg(Path.cwd(), help="Location of project directory. Defaults to current working directory.", exists=True, file_okay=False), project_dir: Path = Arg(Path.cwd(), help="Location of project directory. Defaults to current working directory.", exists=True, file_okay=False),
workflow: Optional[str] = Arg(None, help=f"Name of workflow defined in {PROJECT_FILE}. Defaults to first workflow if not set."), workflow: Optional[str] = Arg(None, help=f"Name of workflow defined in {PROJECT_FILE}. Defaults to first workflow if not set."),
verbose: bool = Opt(False, "--verbose", "-V", help="Print more info"), verbose: bool = Opt(False, "--verbose", "-V", help="Print more info"),
quiet: bool = Opt(False, "--quiet", "-q", help="Print less info"),
force: bool = Opt(False, "--force", "-F", help="Force update DVC config"), force: bool = Opt(False, "--force", "-F", help="Force update DVC config"),
# fmt: on # fmt: on
): ):
@ -36,7 +37,7 @@ def project_update_dvc_cli(
DOCS: https://spacy.io/api/cli#project-dvc DOCS: https://spacy.io/api/cli#project-dvc
""" """
project_update_dvc(project_dir, workflow, verbose=verbose, force=force) project_update_dvc(project_dir, workflow, verbose=verbose, quiet=quiet, force=force)
def project_update_dvc( def project_update_dvc(
@ -44,6 +45,7 @@ def project_update_dvc(
workflow: Optional[str] = None, workflow: Optional[str] = None,
*, *,
verbose: bool = False, verbose: bool = False,
quiet: bool = False,
force: bool = False, force: bool = False,
) -> None: ) -> None:
"""Update the auto-generated Data Version Control (DVC) config file. A DVC """Update the auto-generated Data Version Control (DVC) config file. A DVC
@ -54,11 +56,12 @@ def project_update_dvc(
workflow (Optional[str]): Optional name of workflow defined in project.yml. workflow (Optional[str]): Optional name of workflow defined in project.yml.
If not set, the first workflow will be used. If not set, the first workflow will be used.
verbose (bool): Print more info. verbose (bool): Print more info.
quiet (bool): Print less info.
force (bool): Force update DVC config. force (bool): Force update DVC config.
""" """
config = load_project_config(project_dir) config = load_project_config(project_dir)
updated = update_dvc_config( updated = update_dvc_config(
project_dir, config, workflow, verbose=verbose, force=force project_dir, config, workflow, verbose=verbose, quiet=quiet, force=force
) )
help_msg = "To execute the workflow with DVC, run: dvc repro" help_msg = "To execute the workflow with DVC, run: dvc repro"
if updated: if updated:
@ -72,7 +75,7 @@ def update_dvc_config(
config: Dict[str, Any], config: Dict[str, Any],
workflow: Optional[str] = None, workflow: Optional[str] = None,
verbose: bool = False, verbose: bool = False,
silent: bool = False, quiet: bool = False,
force: bool = False, force: bool = False,
) -> bool: ) -> bool:
"""Re-run the DVC commands in dry mode and update dvc.yaml file in the """Re-run the DVC commands in dry mode and update dvc.yaml file in the
@ -83,7 +86,7 @@ def update_dvc_config(
path (Path): The path to the project directory. path (Path): The path to the project directory.
config (Dict[str, Any]): The loaded project.yml. config (Dict[str, Any]): The loaded project.yml.
verbose (bool): Whether to print additional info (via DVC). verbose (bool): Whether to print additional info (via DVC).
silent (bool): Don't output anything (via DVC). quiet (bool): Don't output anything (via DVC).
force (bool): Force update, even if hashes match. force (bool): Force update, even if hashes match.
RETURNS (bool): Whether the DVC config file was updated. RETURNS (bool): Whether the DVC config file was updated.
""" """
@ -107,10 +110,13 @@ def update_dvc_config(
config_commands = {cmd["name"]: cmd for cmd in config.get("commands", [])} config_commands = {cmd["name"]: cmd for cmd in config.get("commands", [])}
# some flags that apply to every command # some flags that apply to every command
if verbose and quiet:
# don't allow contradictions
msg.fail("Can't set both --verbose and --quiet", exits=1)
flags = [] flags = []
if verbose: if verbose:
flags.append("--verbose") flags.append("--verbose")
if silent: if quiet:
flags.append("--quiet") flags.append("--quiet")
for name in workflows[workflow]: for name in workflows[workflow]:

View File

@ -25,6 +25,7 @@ from spacy.cli.download import get_compatibility, get_version
from spacy.cli.init_config import RECOMMENDATIONS, init_config, fill_config from spacy.cli.init_config import RECOMMENDATIONS, init_config, fill_config
from spacy.cli.package import get_third_party_dependencies from spacy.cli.package import get_third_party_dependencies
from spacy.cli.package import _is_permitted_package_name from spacy.cli.package import _is_permitted_package_name
from spacy.cli.project.run import run_commands
from spacy.cli.validate import get_model_pkgs from spacy.cli.validate import get_model_pkgs
from spacy.compat import is_windows from spacy.compat import is_windows
from spacy.lang.en import English from spacy.lang.en import English
@ -880,6 +881,8 @@ def test_shell_quoting(tmp_path):
ls_cmd = "dir" if is_windows else "ls" ls_cmd = "dir" if is_windows else "ls"
ret = run_command(f'{ls_cmd} "a b/c"') ret = run_command(f'{ls_cmd} "a b/c"')
assert ret.returncode == 0 assert ret.returncode == 0
# check outside the commands
assert os.path.isdir(tmp_path / "a b" / "c")
# since this is a temp dir, we don't have to delete it explicitly # since this is a temp dir, we don't have to delete it explicitly
except: except:
# we failed, so make sure the test fails. # we failed, so make sure the test fails.
@ -887,3 +890,20 @@ def test_shell_quoting(tmp_path):
finally: finally:
# restore the original cwd so other tests are unaffected # restore the original cwd so other tests are unaffected
os.chdir(cwd) os.chdir(cwd)
def test_run_commands(tmp_path):
# minimal test
cwd = os.getcwd()
os.chdir(tmp_path)
try:
ls_cmd = "dir" if is_windows else "ls"
run_commands(["mkdir x", f"{ls_cmd} x"])
# check outside the commands
assert os.path.isdir(tmp_path / "x")
except:
# we failed, so make sure the test fails.
raise
finally:
# restore the original cwd so other tests are unaffected
os.chdir(cwd)