From f7f457e6c239861f952918de403b04d5bfc85700 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 20 Dec 2018 18:59:25 +0000 Subject: [PATCH] WIP project run command --- catfish/__main__.py | 4 ++++ catfish/project/cli.py | 26 ++++++++++++++++++++++++++ tests/__init__.py | 11 +++++++++++ tests/test_project/test_cli.py | 16 ++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 catfish/project/cli.py create mode 100644 tests/test_project/test_cli.py diff --git a/catfish/__main__.py b/catfish/__main__.py index 355bcc9..650c6f1 100644 --- a/catfish/__main__.py +++ b/catfish/__main__.py @@ -4,6 +4,7 @@ import click import daemonize from catfish import __version__, worker +from catfish.project.cli import project as project_cli from catfish.utils.sockets import create_base_socket_dir, delete_base_socket_dir @@ -48,5 +49,8 @@ def stop(ctx): delete_base_socket_dir() +cli.add_command(project_cli) + + if __name__ == "__main__": cli() diff --git a/catfish/project/cli.py b/catfish/project/cli.py new file mode 100644 index 0000000..6ee46dc --- /dev/null +++ b/catfish/project/cli.py @@ -0,0 +1,26 @@ +import os +import shlex +import subprocess + +import click + +from . import Project + + +@click.group() +def project(): + pass + + +@project.command() +@click.argument("command", nargs=-1, type=str) +@click.pass_context +def run(ctx, command): + command = " ".join(command) + project = Project(os.getcwd()) + proc = subprocess.run( + shlex.split(command), + env={**os.environ, **project.get_environment()}, + shell=True + ) + return ctx.exit(proc.returncode) diff --git a/tests/__init__.py b/tests/__init__.py index 9680ee1..bfbe8df 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -3,6 +3,7 @@ import os import subprocess import time from pathlib import Path +from contextlib import contextmanager from unittest import TestCase import psutil @@ -31,6 +32,7 @@ class BaseTestCase(TestCase): self.cli_runner = CliRunner() self.cli = cli self.run_cli = functools.partial(self.cli_runner.invoke, self.cli) + self.in_example_dir = functools.partial(self.change_dir, self.EXAMPLE_DIR) def terminate_dummy_processes(self): dummy_processes = [] @@ -47,6 +49,15 @@ class BaseTestCase(TestCase): self.terminate_dummy_processes() delete_base_socket_dir() + @contextmanager + def change_dir(self, newdir): + prevdir = os.getcwd() + os.chdir(os.path.expanduser(newdir)) + try: + yield + finally: + os.chdir(prevdir) + class BaseWorkerTestCase(BaseTestCase): def setUp(self): diff --git a/tests/test_project/test_cli.py b/tests/test_project/test_cli.py new file mode 100644 index 0000000..874e729 --- /dev/null +++ b/tests/test_project/test_cli.py @@ -0,0 +1,16 @@ +from tests import BaseTestCase +from catfish.project import Project +from dotenv import dotenv_values + + +class ProjectRunCLITestCase(BaseTestCase): + def setUp(self): + super().setUp() + self.project = Project(self.EXAMPLE_DIR) + + def test_sets_environment(self): + with self.in_example_dir(): + result = self.run_cli(["project", "run", "env"]) + print(result) + self.assertEqual(result.exit_code, 0) + environment = dotenv_values(result.stdout)