Skip to content

Commit 5cfff41

Browse files
committed
add run error tests for coverage
1 parent 6bd909b commit 5cfff41

File tree

8 files changed

+141
-61
lines changed

8 files changed

+141
-61
lines changed

django_routines/management/commands/routine.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
width = 80
3636
use_rich = find_spec("rich") is not None
3737
if use_rich:
38-
from rich.console import Console
38+
from rich.console import Console # pyright: ignore[reportMissingImports]
3939

4040
width = Console().width
4141

tests/base_settings.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import os
2+
from pathlib import Path
3+
from django.utils.translation import gettext_lazy as _
4+
5+
USE_TZ = True
6+
7+
8+
# Set default terminal width for help outputs - necessary for
9+
# testing help output in CI environments.
10+
os.environ["TERMINAL_WIDTH"] = "80"
11+
12+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
13+
BASE_DIR = Path(__file__).resolve().parent.parent
14+
15+
# Application definition
16+
17+
INSTALLED_APPS = [
18+
"render_static",
19+
"tests.django_routines_tests",
20+
"django_routines",
21+
"django_typer",
22+
"django.contrib.auth",
23+
"django.contrib.contenttypes",
24+
"django.contrib.sessions",
25+
"django.contrib.messages",
26+
"django.contrib.staticfiles",
27+
]
28+
29+
30+
DATABASES = {
31+
"default": {
32+
"ENGINE": "django.db.backends.sqlite3",
33+
"NAME": BASE_DIR / "db.sqlite3",
34+
"TEST": {"NAME": BASE_DIR / "db.sqlite3"},
35+
}
36+
}
37+
38+
MIDDLEWARE = [
39+
"django.middleware.security.SecurityMiddleware",
40+
"django.contrib.sessions.middleware.SessionMiddleware",
41+
"django.middleware.common.CommonMiddleware",
42+
"django.middleware.csrf.CsrfViewMiddleware",
43+
"django.contrib.auth.middleware.AuthenticationMiddleware",
44+
"django.contrib.messages.middleware.MessageMiddleware",
45+
"django.middleware.clickjacking.XFrameOptionsMiddleware",
46+
]
47+
48+
DJANGO_ROUTINES = None
49+
50+
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
51+
52+
STATIC_URL = "static/"
53+
54+
SECRET_KEY = "fake"

tests/settings.py

+1-44
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
)
1313
from tests import track_file, system_cmd
1414
from django.utils.translation import gettext_lazy as _
15+
from tests.base_settings import *
1516

1617
track_file = str(track_file.relative_to(Path(os.getcwd())))
1718
system_cmd = ("python", str(system_cmd.relative_to(Path(os.getcwd()))))
@@ -23,50 +24,6 @@
2324
# testing help output in CI environments.
2425
os.environ["TERMINAL_WIDTH"] = "80"
2526

26-
# Build paths inside the project like this: BASE_DIR / 'subdir'.
27-
BASE_DIR = Path(__file__).resolve().parent.parent
28-
29-
# Application definition
30-
31-
INSTALLED_APPS = [
32-
"render_static",
33-
"tests.django_routines_tests",
34-
"django_routines",
35-
"django_typer",
36-
"django.contrib.auth",
37-
"django.contrib.contenttypes",
38-
"django.contrib.sessions",
39-
"django.contrib.messages",
40-
"django.contrib.staticfiles",
41-
]
42-
43-
44-
DATABASES = {
45-
"default": {
46-
"ENGINE": "django.db.backends.sqlite3",
47-
"NAME": BASE_DIR / "db.sqlite3",
48-
"TEST": {"NAME": BASE_DIR / "db.sqlite3"},
49-
}
50-
}
51-
52-
MIDDLEWARE = [
53-
"django.middleware.security.SecurityMiddleware",
54-
"django.contrib.sessions.middleware.SessionMiddleware",
55-
"django.middleware.common.CommonMiddleware",
56-
"django.middleware.csrf.CsrfViewMiddleware",
57-
"django.contrib.auth.middleware.AuthenticationMiddleware",
58-
"django.contrib.messages.middleware.MessageMiddleware",
59-
"django.middleware.clickjacking.XFrameOptionsMiddleware",
60-
]
61-
62-
DJANGO_ROUTINES = None
63-
64-
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
65-
66-
STATIC_URL = "static/"
67-
68-
SECRET_KEY = "fake"
69-
7027
routine(
7128
"deploy",
7229
_("Deploy the site application into production."),

tests/settings_subproc_error.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from .base_settings import *
2+
from django_routines import routine, SystemCommand
3+
4+
5+
routine(
6+
"subproc_error",
7+
"Test what happens when a subprocess throws an error.",
8+
SystemCommand(("python", "tests/throw_error.py")),
9+
)

tests/settings_subproc_opt_error.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from .base_settings import *
2+
from django_routines import routine, RoutineCommand
3+
4+
5+
routine(
6+
"subproc_opt_error",
7+
"Test what happens when you can't convert an option to subprocess arg string.",
8+
RoutineCommand(("collectstatic"), options={"not-an-option": False}),
9+
)

tests/test_deploy.py

-16
This file was deleted.

tests/test_runs.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
Miscellaneous tests - mostly for coverage
3+
"""
4+
5+
from io import StringIO
6+
import sys
7+
import subprocess
8+
from django.core.management import call_command
9+
from django.test import TestCase
10+
11+
12+
class TestDeploy(TestCase):
13+
def test_deploy_routine(self):
14+
out = StringIO()
15+
err = StringIO()
16+
call_command("routine", "deploy", "--prepare", stdout=out, stderr=err)
17+
self.assertTrue(out.getvalue())
18+
self.assertFalse(err.getvalue().strip())
19+
20+
21+
def test_subprocess_error():
22+
result = subprocess.run(
23+
[
24+
sys.executable,
25+
"./manage.py",
26+
"routine",
27+
"--settings",
28+
"tests.settings_subproc_error",
29+
"subproc-error",
30+
"--subprocess",
31+
],
32+
text=True,
33+
capture_output=True,
34+
)
35+
36+
assert "panic!" in result.stderr
37+
assert "Subprocess command failed" in result.stderr
38+
39+
40+
def test_subprocess_opt_error():
41+
result = subprocess.run(
42+
[
43+
sys.executable,
44+
"./manage.py",
45+
"routine",
46+
"--settings",
47+
"tests.settings_subproc_opt_error",
48+
"subproc-opt-error",
49+
"--subprocess",
50+
],
51+
text=True,
52+
capture_output=True,
53+
)
54+
55+
assert (
56+
"CommandError: Failed to convert collectstatic options to CLI format: {'not-an-option': False}"
57+
in result.stderr
58+
)

tests/throw_error.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python
2+
import sys
3+
from pathlib import Path
4+
5+
sys.path.append(str(Path(__file__).parent.parent))
6+
7+
8+
if __name__ == "__main__":
9+
raise Exception("panic!")

0 commit comments

Comments
 (0)