WIP project run command

This commit is contained in:
Jake Howard 2018-12-20 18:59:25 +00:00
parent 5869befecb
commit f7f457e6c2
Signed by: jake
GPG key ID: 57AFB45680EDD477
4 changed files with 57 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import click
import daemonize import daemonize
from catfish import __version__, worker 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 from catfish.utils.sockets import create_base_socket_dir, delete_base_socket_dir
@ -48,5 +49,8 @@ def stop(ctx):
delete_base_socket_dir() delete_base_socket_dir()
cli.add_command(project_cli)
if __name__ == "__main__": if __name__ == "__main__":
cli() cli()

26
catfish/project/cli.py Normal file
View file

@ -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)

View file

@ -3,6 +3,7 @@ import os
import subprocess import subprocess
import time import time
from pathlib import Path from pathlib import Path
from contextlib import contextmanager
from unittest import TestCase from unittest import TestCase
import psutil import psutil
@ -31,6 +32,7 @@ class BaseTestCase(TestCase):
self.cli_runner = CliRunner() self.cli_runner = CliRunner()
self.cli = cli self.cli = cli
self.run_cli = functools.partial(self.cli_runner.invoke, self.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): def terminate_dummy_processes(self):
dummy_processes = [] dummy_processes = []
@ -47,6 +49,15 @@ class BaseTestCase(TestCase):
self.terminate_dummy_processes() self.terminate_dummy_processes()
delete_base_socket_dir() 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): class BaseWorkerTestCase(BaseTestCase):
def setUp(self): def setUp(self):

View file

@ -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)