From 099647263edc3d2f77a6a2e0dd762d08cbeb6b93 Mon Sep 17 00:00:00 2001 From: Phil Bell Date: Thu, 18 Aug 2022 14:56:10 +0100 Subject: [PATCH 01/10] updating to use server container instead of frontend and backend --- legl_dev/main.py | 35 ++++++++++++++++------------------- setup.py | 2 +- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/legl_dev/main.py b/legl_dev/main.py index 07b1123..83f24d9 100755 --- a/legl_dev/main.py +++ b/legl_dev/main.py @@ -4,7 +4,6 @@ import pkg_resources import typer - from legl_dev.command import Command, Steps app = typer.Typer(invoke_without_command=True) @@ -40,17 +39,15 @@ def build( steps.add( [ Command(command=f"docker compose up -d"), - Command(command=(f"docker compose exec backend python manage.py migrate")), + Command(command=(f"docker compose exec server python manage.py migrate")), Command( - command=( - f"docker compose exec backend python manage.py flush --noinput" - ) + command=(f"docker compose exec server python manage.py flush --noinput") ), Command( - command=(f"docker compose exec backend python manage.py run_factories") + command=(f"docker compose exec server python manage.py run_factories") ), Command( - command=(f"docker compose exec backend python manage.py seed_emails") + command=(f"docker compose exec server python manage.py seed_emails") ), Command(command=f"docker compose stop"), ] @@ -176,7 +173,7 @@ def migrate( steps.add( Command( command=( - f"docker compose exec backend python manage.py makemigrations --merge" + f"docker compose exec server python manage.py makemigrations --merge" ) ), ) @@ -185,14 +182,14 @@ def migrate( ( Command( command=( - f"docker compose exec backend python manage.py makemigrations" + f"docker compose exec server python manage.py makemigrations" ) ) ), ) if run: steps.add( - Command(command=(f"docker compose exec backend python manage.py migrate")), + Command(command=(f"docker compose exec server python manage.py migrate")), ) steps.run() @@ -205,14 +202,14 @@ def factories( steps = Steps( steps=[ Command( - command=(f"docker compose exec backend python manage.py run_factories") + command=(f"docker compose exec server python manage.py run_factories") ), ], ) if emails: steps.add( Command( - command=(f"docker compose exec backend python manage.py seed_emails") + command=(f"docker compose exec server python manage.py seed_emails") ), ) steps.run() @@ -266,12 +263,12 @@ def install( [ Command( command=( - f"docker exec backend pip install {'--upgrade' if upgrade else ''} {package}" + f"docker exec server pip install {'--upgrade' if upgrade else ''} {package}" ) ), Command( command=( - f"docker exec backend pip freeze | grep {package} >> requirements.txt" + f"docker exec server pip freeze | grep {package} >> requirements.txt" ), shell=True, ), @@ -282,7 +279,7 @@ def install( steps.add( Command( command=( - f"docker exec frontend yarn {'up' if upgrade else 'add'} {package}" + f"docker exec server yarn {'up' if upgrade else 'add'} {package}" ) ) ) @@ -304,14 +301,14 @@ def install( steps.run() + @app.command(help="Remote into a container") -def shell(environment: Optional[str] = typer.Argument(default="backend", help="Container to remote into")): +def shell(): steps = Steps() - steps.add( - Command(command=f"docker compose exec {environment} bash") - ) + steps.add(Command(command=f"docker compose exec server bash")) steps.run() + @app.callback() def main(version: bool = False): if version: diff --git a/setup.py b/setup.py index 8971adb..96a1960 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from setuptools import setup -version = "1.2.3" +version = "1.2.4" packages = ["legl_dev"] From fa4dd7886ae786b0f26e3e8c42181063c756e9c8 Mon Sep 17 00:00:00 2001 From: Phil Bell Date: Thu, 18 Aug 2022 14:59:59 +0100 Subject: [PATCH 02/10] updating tests --- legl_dev/tests/test_main.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/legl_dev/tests/test_main.py b/legl_dev/tests/test_main.py index 845c7a1..e962c67 100644 --- a/legl_dev/tests/test_main.py +++ b/legl_dev/tests/test_main.py @@ -29,13 +29,13 @@ def test_install_pip(run): ) calls = [ mock.call( - ["docker", "exec", "backend", "pip", "install", "example"], + ["docker", "exec", "server", "pip", "install", "example"], universal_newlines=True, shell=False, check=True, ), mock.call( - "docker exec backend pip freeze | grep example >> requirements.txt", + "docker exec server pip freeze | grep example >> requirements.txt", universal_newlines=True, shell=True, check=True, @@ -56,13 +56,13 @@ def test_install_pip_upgrade(run): ) calls = [ mock.call( - ["docker", "exec", "backend", "pip", "install", "--upgrade", "example"], + ["docker", "exec", "server", "pip", "install", "--upgrade", "example"], universal_newlines=True, shell=False, check=True, ), mock.call( - "docker exec backend pip freeze | grep example >> requirements.txt", + "docker exec server pip freeze | grep example >> requirements.txt", universal_newlines=True, shell=True, check=True, @@ -83,7 +83,7 @@ def test_install_yarn(run): ) calls = [ mock.call( - ["docker", "exec", "frontend", "yarn", "add", "example"], + ["docker", "exec", "server", "yarn", "add", "example"], universal_newlines=True, shell=False, check=True, @@ -104,7 +104,7 @@ def test_install_yarn_upgrade(run): ) calls = [ mock.call( - ["docker", "exec", "frontend", "yarn", "up", "example"], + ["docker", "exec", "server", "yarn", "up", "example"], universal_newlines=True, shell=False, check=True, @@ -160,12 +160,13 @@ def test_install_self_upgrade(run): ] run.assert_has_calls(calls) + @mock.patch("legl_dev.command.run") -def test_remote_frontend_commands(run): - main.shell("frontend") +def test_remote_server_commands(run): + main.shell("server") calls = [ mock.call( - ["docker", "compose", "exec", "frontend", "bash"], + ["docker", "compose", "exec", "server", "bash"], universal_newlines=True, shell=False, check=True, @@ -173,12 +174,13 @@ def test_remote_frontend_commands(run): ] run.assert_has_calls(calls) + @mock.patch("legl_dev.command.run") -def test_remote_frontend_commands(run): - main.shell("frontend") +def test_remote_server_commands(run): + main.shell("server") calls = [ mock.call( - ["docker", "compose", "exec", "frontend", "bash"], + ["docker", "compose", "exec", "server", "bash"], universal_newlines=True, shell=False, check=True, From 7652f311836510b5b0018faeb40932b5e89452d5 Mon Sep 17 00:00:00 2001 From: Phil Bell Date: Thu, 18 Aug 2022 15:01:43 +0100 Subject: [PATCH 03/10] finxing shell test --- legl_dev/tests/test_main.py | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/legl_dev/tests/test_main.py b/legl_dev/tests/test_main.py index e962c67..5b2d7b2 100644 --- a/legl_dev/tests/test_main.py +++ b/legl_dev/tests/test_main.py @@ -163,21 +163,7 @@ def test_install_self_upgrade(run): @mock.patch("legl_dev.command.run") def test_remote_server_commands(run): - main.shell("server") - calls = [ - mock.call( - ["docker", "compose", "exec", "server", "bash"], - universal_newlines=True, - shell=False, - check=True, - ), - ] - run.assert_has_calls(calls) - - -@mock.patch("legl_dev.command.run") -def test_remote_server_commands(run): - main.shell("server") + main.shell() calls = [ mock.call( ["docker", "compose", "exec", "server", "bash"], From 39821b34287be68d3036ffb481d943aad9fb7bca Mon Sep 17 00:00:00 2001 From: Phil Bell Date: Thu, 18 Aug 2022 15:13:32 +0100 Subject: [PATCH 04/10] updating commands to use variables --- legl_dev/main.py | 70 +++++++++++++++++------------------------------- 1 file changed, 25 insertions(+), 45 deletions(-) diff --git a/legl_dev/main.py b/legl_dev/main.py index 83f24d9..9ac159d 100755 --- a/legl_dev/main.py +++ b/legl_dev/main.py @@ -7,7 +7,9 @@ from legl_dev.command import Command, Steps app = typer.Typer(invoke_without_command=True) - +docker_cmd = "docker compose" +exec_cmd = f"{docker_cmd} exec server" +django_cmd = f"{exec_cmd} python manage.py" os.environ["COMPOSE_DOCKER_CLI_BUILD"] = "1" os.environ["DOCKER_BUILDKIT"] = "1" @@ -32,24 +34,20 @@ def build( steps = Steps( steps=[ Command( - command=f"docker compose build {extra_args}", + command=f"{docker_cmd} build {extra_args}", ), ], ) steps.add( [ - Command(command=f"docker compose up -d"), - Command(command=(f"docker compose exec server python manage.py migrate")), - Command( - command=(f"docker compose exec server python manage.py flush --noinput") - ), - Command( - command=(f"docker compose exec server python manage.py run_factories") - ), - Command( - command=(f"docker compose exec server python manage.py seed_emails") - ), - Command(command=f"docker compose stop"), + Command(command=f"{docker_cmd} up -d"), + Command(command=f"{django_cmd} migrate"), + Command(command=f"{docker_cmd} stop database"), + Command(command=f"{docker_cmd} rm database"), + Command(command=f"{docker_cmd} up database -d"), + Command(command=f"{django_cmd} run_factories"), + Command(command=f"{django_cmd} seed_emails"), + Command(command=f"{docker_cmd} stop"), ] ) steps.run() @@ -109,7 +107,7 @@ def pytest( steps=[ Command( command=( - f"docker compose " + f"{docker_cmd} " "run --rm backend pytest " "--html=unit_test_results.html " f"{extra_args} /code/{path}" @@ -127,9 +125,9 @@ def format( steps = Steps( steps=[ - Command(command=(f"isort . --profile black")), - Command(command=(f"black .")), - Command(command=("yarn format:prettier")), + Command(command=f"isort . --profile black"), + Command(command=f"black ."), + Command(command="yarn format:prettier"), ] ) if push: @@ -171,25 +169,15 @@ def migrate( steps = Steps() if merge: steps.add( - Command( - command=( - f"docker compose exec server python manage.py makemigrations --merge" - ) - ), + Command(command=f"{django_cmd} makemigrations --merge"), ) if make: steps.add( - ( - Command( - command=( - f"docker compose exec server python manage.py makemigrations" - ) - ) - ), + (Command(command=f"{django_cmd} makemigrations")), ) if run: steps.add( - Command(command=(f"docker compose exec server python manage.py migrate")), + Command(command=f"{django_cmd} migrate"), ) steps.run() @@ -201,16 +189,12 @@ def factories( steps = Steps( steps=[ - Command( - command=(f"docker compose exec server python manage.py run_factories") - ), + Command(command=f"{django_cmd} run_factories"), ], ) if emails: steps.add( - Command( - command=(f"docker compose exec server python manage.py seed_emails") - ), + Command(command=f"{django_cmd} seed_emails"), ) steps.run() @@ -263,12 +247,12 @@ def install( [ Command( command=( - f"docker exec server pip install {'--upgrade' if upgrade else ''} {package}" + f"{exec_cmd}pip install {'--upgrade' if upgrade else ''} {package}" ) ), Command( command=( - f"docker exec server pip freeze | grep {package} >> requirements.txt" + f"{exec_cmd} pip freeze | grep {package} >> requirements.txt" ), shell=True, ), @@ -277,11 +261,7 @@ def install( if yarn: steps.add( - Command( - command=( - f"docker exec server yarn {'up' if upgrade else 'add'} {package}" - ) - ) + Command(command=(f"{exec_cmd} yarn {'up' if upgrade else 'add'} {package}")) ) if self: @@ -305,7 +285,7 @@ def install( @app.command(help="Remote into a container") def shell(): steps = Steps() - steps.add(Command(command=f"docker compose exec server bash")) + steps.add(Command(command=f"{exec_cmd} bash")) steps.run() From 909cb2eb8eb85088916390288f11bf8fd18c245e Mon Sep 17 00:00:00 2001 From: Phil Bell Date: Thu, 18 Aug 2022 15:17:19 +0100 Subject: [PATCH 05/10] updating tests --- legl_dev/tests/test_main.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/legl_dev/tests/test_main.py b/legl_dev/tests/test_main.py index 5b2d7b2..6c9de80 100644 --- a/legl_dev/tests/test_main.py +++ b/legl_dev/tests/test_main.py @@ -29,7 +29,7 @@ def test_install_pip(run): ) calls = [ mock.call( - ["docker", "exec", "server", "pip", "install", "example"], + ["docker", "compose", "exec", "server", "pip", "install", "example"], universal_newlines=True, shell=False, check=True, @@ -56,7 +56,16 @@ def test_install_pip_upgrade(run): ) calls = [ mock.call( - ["docker", "exec", "server", "pip", "install", "--upgrade", "example"], + [ + "docker", + "compose", + "exec", + "server", + "pip", + "install", + "--upgrade", + "example", + ], universal_newlines=True, shell=False, check=True, @@ -83,7 +92,7 @@ def test_install_yarn(run): ) calls = [ mock.call( - ["docker", "exec", "server", "yarn", "add", "example"], + ["docker", "compose", "exec", "server", "yarn", "add", "example"], universal_newlines=True, shell=False, check=True, @@ -104,7 +113,7 @@ def test_install_yarn_upgrade(run): ) calls = [ mock.call( - ["docker", "exec", "server", "yarn", "up", "example"], + ["docker", "compose", "exec", "server", "yarn", "up", "example"], universal_newlines=True, shell=False, check=True, From 8d8e9a481cf21f98c00b4c0b6b9f91065f2abd34 Mon Sep 17 00:00:00 2001 From: Phil Bell Date: Thu, 18 Aug 2022 15:45:21 +0100 Subject: [PATCH 06/10] fxing database rm and pip command --- legl_dev/main.py | 5 +++-- setup.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/legl_dev/main.py b/legl_dev/main.py index 9ac159d..ffc58cf 100755 --- a/legl_dev/main.py +++ b/legl_dev/main.py @@ -43,8 +43,9 @@ def build( Command(command=f"{docker_cmd} up -d"), Command(command=f"{django_cmd} migrate"), Command(command=f"{docker_cmd} stop database"), - Command(command=f"{docker_cmd} rm database"), + Command(command=f"{docker_cmd} rm -f database"), Command(command=f"{docker_cmd} up database -d"), + Command(command=f"{django_cmd} migrate"), Command(command=f"{django_cmd} run_factories"), Command(command=f"{django_cmd} seed_emails"), Command(command=f"{docker_cmd} stop"), @@ -247,7 +248,7 @@ def install( [ Command( command=( - f"{exec_cmd}pip install {'--upgrade' if upgrade else ''} {package}" + f"{exec_cmd} pip install {'--upgrade' if upgrade else ''} {package}" ) ), Command( diff --git a/setup.py b/setup.py index 96a1960..0fa162b 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ package_data = {"": ["*"]} -install_requires = ["black", "isort==5.9.3", "typer>=0.4.0"] +install_requires = ["black", "isort==5.9.3", "typer==0.6.1"] entry_points = {"console_scripts": ["legl-dev = legl_dev.main:app"]} From 7af310ba3765a4df021facfd3afd20cfdd855909 Mon Sep 17 00:00:00 2001 From: Phil Bell Date: Thu, 18 Aug 2022 15:53:16 +0100 Subject: [PATCH 07/10] fixing tests --- legl_dev/tests/test_main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/legl_dev/tests/test_main.py b/legl_dev/tests/test_main.py index 6c9de80..cd31113 100644 --- a/legl_dev/tests/test_main.py +++ b/legl_dev/tests/test_main.py @@ -35,7 +35,7 @@ def test_install_pip(run): check=True, ), mock.call( - "docker exec server pip freeze | grep example >> requirements.txt", + "docker compose exec server pip freeze | grep example >> requirements.txt", universal_newlines=True, shell=True, check=True, @@ -71,7 +71,7 @@ def test_install_pip_upgrade(run): check=True, ), mock.call( - "docker exec server pip freeze | grep example >> requirements.txt", + "docker compose exec server pip freeze | grep example >> requirements.txt", universal_newlines=True, shell=True, check=True, From f00284ba46b27de19dbe13e1b253fc23069acf37 Mon Sep 17 00:00:00 2001 From: Phil Bell Date: Thu, 18 Aug 2022 15:56:02 +0100 Subject: [PATCH 08/10] adding auto update --- legl_dev/command.py | 51 ++++++++++++++++++++++++++++++++----- legl_dev/main.py | 36 +++++++++++++++++++++++--- legl_dev/tests/test_main.py | 1 + setup.py | 4 +-- 4 files changed, 81 insertions(+), 11 deletions(-) diff --git a/legl_dev/command.py b/legl_dev/command.py index be9e6d4..55ea35e 100644 --- a/legl_dev/command.py +++ b/legl_dev/command.py @@ -1,8 +1,10 @@ #!/usr/bin/env python3 -import typer -from subprocess import run, CalledProcessError +import pathlib +import subprocess from typing import Union +import typer + class Command: def __init__( @@ -13,17 +15,54 @@ def __init__( self.command = command self.shell = shell + def _success_output(self): + typer.secho(f"✅ Command successful!", fg=typer.colors.GREEN) + + def _error_output(self, e): + typer.secho( + f"⚠️ Command exited with code {e.returncode}! Check the logs: logs/legl-dev.logs", + fg=typer.colors.YELLOW, + ) + + def _command_output(self): + typer.secho(f"Running: {self.command}", fg=typer.colors.MAGENTA) + + def write_logs(self, result): + pathlib.Path("logs").mkdir(parents=True, exist_ok=True) + with open("logs/legl-dev.logs", "a") as logs: + logs.write("\n===================== new command =====================\n") + logs.write(f"Command run: {self.command}\n") + logs.write(f"Logs:\n\n {result.stdout}\n{result.stderr}") + def run(self) -> None: + self._command_output() + try: + result = subprocess.run( + self.command if self.shell else self.command.split(), + universal_newlines=True, + shell=self.shell, + check=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + ) + self.write_logs(result) + self._success_output() + except subprocess.CalledProcessError as e: + self._error_output(e) + + def run_verbose(self): + self._command_output() try: - run( + subprocess.run( self.command if self.shell else self.command.split(), universal_newlines=True, shell=self.shell, check=True, ) - typer.secho(f"✅ Command successful!", fg=typer.colors.GREEN) - except CalledProcessError as e: - typer.secho(f"⚠️ Command exited with code {e.returncode}!", fg=typer.colors.YELLOW) + self._success_output() + except subprocess.CalledProcessError as e: + self._error_output(e) class Steps: diff --git a/legl_dev/main.py b/legl_dev/main.py index ffc58cf..acecb27 100755 --- a/legl_dev/main.py +++ b/legl_dev/main.py @@ -1,8 +1,10 @@ #!/usr/bin/env python3 +import json import os from typing import Optional import pkg_resources +import requests import typer from legl_dev.command import Command, Steps @@ -19,12 +21,23 @@ def start(): steps = Steps() steps.add( Command( - command="docker compose up", + command="docker compose up -d", ) ) steps.run() +@app.command(help="Start the dev environment") +def logs(): + steps = Steps() + steps.add( + Command( + command="docker compose logs -f", + ) + ) + steps.run_verbose() + + @app.command(help="Rebuild the local environment") def build( cache: bool = typer.Option(True, help="Drop the database and create a fresh one"), @@ -282,7 +295,6 @@ def install( steps.run() - @app.command(help="Remote into a container") def shell(): steps = Steps() @@ -292,8 +304,26 @@ def shell(): @app.callback() def main(version: bool = False): + response = requests.get( + url="https://api.github.com/repos/crowdjustice/legl-dev/releases", + headers={"Accept": "application/vnd.github.v3+json"}, + ) + releases = response.json() + latest_vesrion = pkg_resources.parse_version(releases[0]["tag_name"]) + current_version = pkg_resources.parse_version( + pkg_resources.require("legl_dev")[0].version + ) + + if latest_vesrion > current_version: + update = typer.confirm( + f"A newer version ({latest_vesrion}) of legl-dev is availible, would you like to update?" + ) + if update: + command = Command(command=f"pip install --upgrade legl-dev") + command.run() + if version: - typer.echo(f"v{pkg_resources.require('legl_dev')[0].version}") + typer.echo(f"v{current_version}") raise typer.Exit() diff --git a/legl_dev/tests/test_main.py b/legl_dev/tests/test_main.py index cd31113..1d62637 100644 --- a/legl_dev/tests/test_main.py +++ b/legl_dev/tests/test_main.py @@ -170,6 +170,7 @@ def test_install_self_upgrade(run): run.assert_has_calls(calls) + @mock.patch("legl_dev.command.run") def test_remote_server_commands(run): main.shell() diff --git a/setup.py b/setup.py index 0fa162b..35bea8d 100644 --- a/setup.py +++ b/setup.py @@ -1,13 +1,13 @@ # -*- coding: utf-8 -*- from setuptools import setup -version = "1.2.4" +version = "1.3.0" packages = ["legl_dev"] package_data = {"": ["*"]} -install_requires = ["black", "isort==5.9.3", "typer==0.6.1"] +install_requires = ["black", "isort==5.9.3", "typer==0.6.1", "requests"] entry_points = {"console_scripts": ["legl-dev = legl_dev.main:app"]} From b937880a7eb4f0c13b3e74f4e4887aa14c75012a Mon Sep 17 00:00:00 2001 From: Phil Bell Date: Thu, 18 Aug 2022 17:22:23 +0100 Subject: [PATCH 09/10] Adding auto update and better output --- legl_dev/command.py | 48 +++++++++++++--------------------- legl_dev/main.py | 11 ++++---- legl_dev/tests/test_command.py | 6 ++--- legl_dev/tests/test_main.py | 33 ++++++++++++++++------- 4 files changed, 50 insertions(+), 48 deletions(-) diff --git a/legl_dev/command.py b/legl_dev/command.py index 55ea35e..aa053a1 100644 --- a/legl_dev/command.py +++ b/legl_dev/command.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 import pathlib +import shutil import subprocess from typing import Union @@ -15,43 +16,30 @@ def __init__( self.command = command self.shell = shell + def _line_message(self, message): + width, _ = shutil.get_terminal_size() + line = "".join(["-" for _ in range((width - len(message) - 4) // 2)]) + return f"{line} {message} {line}" + def _success_output(self): - typer.secho(f"✅ Command successful!", fg=typer.colors.GREEN) + typer.secho( + self._line_message(" ✅ Command successful! ✅ "), + fg=typer.colors.GREEN, + ) def _error_output(self, e): typer.secho( - f"⚠️ Command exited with code {e.returncode}! Check the logs: logs/legl-dev.logs", - fg=typer.colors.YELLOW, + f"💥 Command exited with code {e.returncode}! Check the logs above for more informations. 💥", + fg=typer.colors.BRIGHT_RED, ) def _command_output(self): - typer.secho(f"Running: {self.command}", fg=typer.colors.MAGENTA) - - def write_logs(self, result): - pathlib.Path("logs").mkdir(parents=True, exist_ok=True) - with open("logs/legl-dev.logs", "a") as logs: - logs.write("\n===================== new command =====================\n") - logs.write(f"Command run: {self.command}\n") - logs.write(f"Logs:\n\n {result.stdout}\n{result.stderr}") - - def run(self) -> None: - self._command_output() - try: - result = subprocess.run( - self.command if self.shell else self.command.split(), - universal_newlines=True, - shell=self.shell, - check=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - text=True, - ) - self.write_logs(result) - self._success_output() - except subprocess.CalledProcessError as e: - self._error_output(e) + typer.secho( + self._line_message(f"🚧 Running: {self.command} 🚧"), + fg=typer.colors.YELLOW, + ) - def run_verbose(self): + def run(self): self._command_output() try: subprocess.run( @@ -75,6 +63,6 @@ def add(self, steps: Union[list, Command]) -> None: except TypeError: self.steps += [steps] - def run(self) -> None: + def run(self, verbose=False) -> None: for step in self.steps: step.run() diff --git a/legl_dev/main.py b/legl_dev/main.py index acecb27..e47cb44 100755 --- a/legl_dev/main.py +++ b/legl_dev/main.py @@ -17,11 +17,11 @@ @app.command(help="Start the dev environment") -def start(): +def start(verbose: bool = typer.Option(True, help="Run in verbose mode")): steps = Steps() steps.add( Command( - command="docker compose up -d", + command=f"{docker_cmd} up {'-d' if verbose else ''}", ) ) steps.run() @@ -32,10 +32,10 @@ def logs(): steps = Steps() steps.add( Command( - command="docker compose logs -f", + command=f"{docker_cmd} logs -f", ) ) - steps.run_verbose() + steps.run() @app.command(help="Rebuild the local environment") @@ -43,7 +43,7 @@ def build( cache: bool = typer.Option(True, help="Drop the database and create a fresh one"), ) -> None: - extra_args = f"{'' if cache else '--no-cache'} " + extra_args = f"{'' if cache else '--no-cache'}" steps = Steps( steps=[ Command( @@ -295,6 +295,7 @@ def install( steps.run() + @app.command(help="Remote into a container") def shell(): steps = Steps() diff --git a/legl_dev/tests/test_command.py b/legl_dev/tests/test_command.py index 4b7f11b..a870ab0 100644 --- a/legl_dev/tests/test_command.py +++ b/legl_dev/tests/test_command.py @@ -3,7 +3,7 @@ from legl_dev.command import Command, Steps -@mock.patch("legl_dev.command.run") +@mock.patch("legl_dev.command.subprocess.run") def test_command_run(run): command = Command(command="echo test") command.run() @@ -12,7 +12,7 @@ def test_command_run(run): ) -@mock.patch("legl_dev.command.run") +@mock.patch("legl_dev.command.subprocess.run") def test_command_run_with_shell(run): command = Command(command="echo test", shell=True) command.run() @@ -56,7 +56,7 @@ def test_steps_dont_overflow_instance(): assert steps_two.steps == [test_command_two] -@mock.patch("legl_dev.command.run") +@mock.patch("legl_dev.command.subprocess.run") def test_standard_start(run): test_command_one = Command(command="echo test one") test_command_two = Command(command="echo test two") diff --git a/legl_dev/tests/test_main.py b/legl_dev/tests/test_main.py index 1d62637..e52cf3e 100644 --- a/legl_dev/tests/test_main.py +++ b/legl_dev/tests/test_main.py @@ -3,12 +3,26 @@ from legl_dev import main -@mock.patch("legl_dev.command.run") +@mock.patch("legl_dev.command.subprocess.run") +def test_standard_start_not_verbose(run): + main.start(verbose=False) + calls = [ + mock.call( + ["docker", "compose", "up"], + universal_newlines=True, + shell=False, + check=True, + ), + ] + run.assert_has_calls(calls) + + +@mock.patch("legl_dev.command.subprocess.run") def test_standard_start(run): main.start() calls = [ mock.call( - ["docker", "compose", "up"], + ["docker", "compose", "up", "-d"], universal_newlines=True, shell=False, check=True, @@ -17,7 +31,7 @@ def test_standard_start(run): run.assert_has_calls(calls) -@mock.patch("legl_dev.command.run") +@mock.patch("legl_dev.command.subprocess.run") def test_install_pip(run): main.install( package="example", @@ -44,7 +58,7 @@ def test_install_pip(run): run.assert_has_calls(calls) -@mock.patch("legl_dev.command.run") +@mock.patch("legl_dev.command.subprocess.run") def test_install_pip_upgrade(run): main.install( package="example", @@ -80,7 +94,7 @@ def test_install_pip_upgrade(run): run.assert_has_calls(calls) -@mock.patch("legl_dev.command.run") +@mock.patch("legl_dev.command.subprocess.run") def test_install_yarn(run): main.install( package="example", @@ -101,7 +115,7 @@ def test_install_yarn(run): run.assert_has_calls(calls) -@mock.patch("legl_dev.command.run") +@mock.patch("legl_dev.command.subprocess.run") def test_install_yarn_upgrade(run): main.install( package="example", @@ -122,7 +136,7 @@ def test_install_yarn_upgrade(run): run.assert_has_calls(calls) -@mock.patch("legl_dev.command.run") +@mock.patch("legl_dev.command.subprocess.run") def test_install_self(run): main.install( package="example", @@ -149,7 +163,7 @@ def test_install_self(run): run.assert_has_calls(calls) -@mock.patch("legl_dev.command.run") +@mock.patch("legl_dev.command.subprocess.run") def test_install_self_upgrade(run): main.install( package="example", @@ -170,8 +184,7 @@ def test_install_self_upgrade(run): run.assert_has_calls(calls) - -@mock.patch("legl_dev.command.run") +@mock.patch("legl_dev.command.subprocess.run") def test_remote_server_commands(run): main.shell() calls = [ From 27e040ff040ff29a3ff6cddddb15602ad4a90dcd Mon Sep 17 00:00:00 2001 From: Phil Bell Date: Thu, 18 Aug 2022 17:25:47 +0100 Subject: [PATCH 10/10] updating verbose for start --- legl_dev/main.py | 2 +- legl_dev/tests/test_main.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/legl_dev/main.py b/legl_dev/main.py index e47cb44..f094ee4 100755 --- a/legl_dev/main.py +++ b/legl_dev/main.py @@ -21,7 +21,7 @@ def start(verbose: bool = typer.Option(True, help="Run in verbose mode")): steps = Steps() steps.add( Command( - command=f"{docker_cmd} up {'-d' if verbose else ''}", + command=f"{docker_cmd} up {'' if verbose else '-d'}", ) ) steps.run() diff --git a/legl_dev/tests/test_main.py b/legl_dev/tests/test_main.py index e52cf3e..42f4b16 100644 --- a/legl_dev/tests/test_main.py +++ b/legl_dev/tests/test_main.py @@ -8,7 +8,7 @@ def test_standard_start_not_verbose(run): main.start(verbose=False) calls = [ mock.call( - ["docker", "compose", "up"], + ["docker", "compose", "up", "-d"], universal_newlines=True, shell=False, check=True, @@ -22,7 +22,7 @@ def test_standard_start(run): main.start() calls = [ mock.call( - ["docker", "compose", "up", "-d"], + ["docker", "compose", "up"], universal_newlines=True, shell=False, check=True,