Skip to content

Commit

Permalink
Introduce logged_call_command
Browse files Browse the repository at this point in the history
  • Loading branch information
hansegucker committed Nov 27, 2023
1 parent 26b9bba commit aa83856
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 29 deletions.
6 changes: 4 additions & 2 deletions evap/development/management/commands/dump_testdata.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os

from django.conf import settings
from django.core.management import call_command
from django.core.management.base import BaseCommand

from evap.evaluation.management.commands.tools import logged_call_command


class Command(BaseCommand):
args = ""
Expand All @@ -12,7 +13,8 @@ class Command(BaseCommand):

def handle(self, *args, **options):
outfile_name = os.path.join(settings.BASE_DIR, "development", "fixtures", "test_data.json")
call_command(
logged_call_command(
self.stdout,
"dumpdata",
"auth.group",
"evaluation",
Expand Down
21 changes: 7 additions & 14 deletions evap/development/management/commands/reload_testdata.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.core.management import call_command
from django.core.management.base import BaseCommand

from evap.evaluation.management.commands.tools import confirm_harmful_operation
from evap.evaluation.management.commands.tools import confirm_harmful_operation, logged_call_command


class Command(BaseCommand):
Expand All @@ -14,24 +13,18 @@ def handle(self, *args, **options):
if not confirm_harmful_operation(self.stdout):
return

self.stdout.write('Executing "python manage.py reset_db"')
call_command("reset_db", interactive=False)
logged_call_command(self.stdout, "reset_db", interactive=False)

self.stdout.write('Executing "python manage.py migrate"')
call_command("migrate")
logged_call_command(self.stdout, "migrate")

# clear any data the migrations created.
# their pks might differ from the ones in the dump, which results in errors on loaddata
self.stdout.write('Executing "python manage.py flush"')
call_command("flush", interactive=False)
logged_call_command(self.stdout, "flush", interactive=False)

self.stdout.write('Executing "python manage.py loaddata test_data"')
call_command("loaddata", "test_data")
logged_call_command(self.stdout, "loaddata", "test_data")

self.stdout.write('Executing "python manage.py clear_cache --all -v=1"')
call_command("clear_cache", "--all", "-v=1")
logged_call_command(self.stdout, "clear_cache", "--all", "-v=1")

self.stdout.write('Executing "python manage.py refresh_results_cache"')
call_command("refresh_results_cache")
logged_call_command(self.stdout, "refresh_results_cache")

self.stdout.write("Done.")
13 changes: 5 additions & 8 deletions evap/development/management/commands/translate.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
from django.core.management import call_command
from django.core.management.base import BaseCommand

from evap.evaluation.management.commands.tools import logged_call_command

Check warning on line 3 in evap/development/management/commands/translate.py

View check run for this annotation

Codecov / codecov/patch

evap/development/management/commands/translate.py#L3

Added line #L3 was not covered by tests


class Command(BaseCommand):
args = ""
help = 'Execute "makemessages --locale=de --ignore=node_modules/*"'

def handle(self, *args, **options):
self.stdout.write('Executing "manage.py makemessages --locale=de --ignore=node_modules/*"')
call_command("makemessages", "--locale=de", "--ignore=node_modules/*")
self.stdout.write(
'Executing ""makemessages --domain=djangojs --extension=js,ts "'
'"--locale=de --ignore=node_modules/* --ignore=evap/static/js/*.min.js",'
)
call_command(
logged_call_command(self.stdout, "makemessages", "--locale=de", "--ignore=node_modules/*")
logged_call_command(

Check warning on line 12 in evap/development/management/commands/translate.py

View check run for this annotation

Codecov / codecov/patch

evap/development/management/commands/translate.py#L11-L12

Added lines #L11 - L12 were not covered by tests
self.stdout,
"makemessages",
"--domain=djangojs",
"--extension=js,ts",
Expand Down
6 changes: 3 additions & 3 deletions evap/development/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class TestDumpTestDataCommand(TestCase):
@staticmethod
def test_dumpdata_called():
with patch("evap.development.management.commands.dump_testdata.call_command") as mock:
with patch("evap.evaluation.management.commands.tools.call_command") as mock:
management.call_command("dump_testdata")

outfile_name = os.path.join(settings.BASE_DIR, "development", "fixtures", "test_data.json")
Expand All @@ -31,7 +31,7 @@ def test_dumpdata_called():

class TestReloadTestdataCommand(TestCase):
@patch("builtins.input")
@patch("evap.development.management.commands.reload_testdata.call_command")
@patch("evap.evaluation.management.commands.tools.call_command")
def test_aborts(self, mock_call_command, mock_input):
mock_input.return_value = "not yes"

Expand All @@ -40,7 +40,7 @@ def test_aborts(self, mock_call_command, mock_input):
self.assertEqual(mock_call_command.call_count, 0)

@patch("builtins.input")
@patch("evap.development.management.commands.reload_testdata.call_command")
@patch("evap.evaluation.management.commands.tools.call_command")
def test_executes_key_commands(self, mock_call_command, mock_input):
mock_input.return_value = "yes"

Expand Down
7 changes: 7 additions & 0 deletions evap/evaluation/management/commands/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys

from django.conf import settings
from django.core.management import call_command

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -40,3 +41,9 @@ def handle(self, *args, **options):
raise

return NewClass


def logged_call_command(stdout, *args, **kwargs):
"""Log execution of management command with all args."""
stdout.write("Executing python manage.py " + " ".join(list(args) + [f"{a}={b}" for a, b in kwargs.items()]))
call_command(*args, **kwargs)
2 changes: 0 additions & 2 deletions evap/static/js/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# ignore the typescript output, but still track the libraries
*.js
!*.min.js
!sortable_form.js
!moment_de.js

0 comments on commit aa83856

Please sign in to comment.