Skip to content

Commit 0b68825

Browse files
committed
Adapt the django coverage fix to work with older django versions.
1 parent e9fb2bf commit 0b68825

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
if __name__ == "__main__":
5+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
6+
try:
7+
from django.core.management import execute_from_command_line
8+
except ImportError:
9+
# The above import may fail for some other reason. Ensure that the
10+
# issue is really that Django is missing to avoid masking other
11+
# exceptions on Python 2.
12+
try:
13+
import django
14+
except ImportError:
15+
raise ImportError(
16+
"Couldn't import Django. Are you sure it's installed and "
17+
"available on your PYTHONPATH environment variable? Did you "
18+
"forget to activate a virtual environment?"
19+
)
20+
raise
21+
execute_from_command_line(sys.argv)

python_files/tests/unittestadapter/test_coverage.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ def test_basic_coverage():
5252
assert set(focal_function_coverage.get("lines_covered")) == {4, 5, 7, 9, 10, 11, 12, 13, 14}
5353
assert set(focal_function_coverage.get("lines_missed")) == {6}
5454

55-
55+
@pytest.mark.parametrize("manage_py_file", ["manage.py", "old_manage.py"])
5656
@pytest.mark.timeout(30)
57-
def test_basic_django_coverage():
57+
def test_basic_django_coverage(manage_py_file):
5858
"""This test validates that the coverage is correctly calculated for a Django project."""
5959
data_path: pathlib.Path = TEST_DATA_PATH / "simple_django"
60-
manage_py_path: str = os.fsdecode(data_path / "manage.py")
60+
manage_py_path: str = os.fsdecode(data_path / manage_py_file)
6161
execution_script: pathlib.Path = python_files_path / "unittestadapter" / "execution.py"
6262

6363
test_ids = [
@@ -82,7 +82,7 @@ def test_basic_django_coverage():
8282
assert coverage
8383
results = coverage["result"]
8484
assert results
85-
assert len(results) == 15
85+
assert len(results) == 16
8686
polls_views_coverage = results.get(str(data_path / "polls" / "views.py"))
8787
assert polls_views_coverage
8888
assert polls_views_coverage.get("lines_covered") is not None

python_files/unittestadapter/django_handler.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ def django_discovery_runner(manage_py_path: str, args: List[str]) -> None:
7575

7676

7777
def django_execution_runner(manage_py_path: str, test_ids: List[str], args: List[str]) -> None:
78+
manage_path: pathlib.Path = pathlib.Path(manage_py_path)
7879
# Attempt a small amount of validation on the manage.py path.
79-
if not pathlib.Path(manage_py_path).exists():
80+
if not manage_path.exists():
8081
raise VSCodeUnittestError("Error running Django, manage.py path does not exist.")
8182

8283
try:
@@ -89,28 +90,28 @@ def django_execution_runner(manage_py_path: str, test_ids: List[str], args: List
8990
else:
9091
env["PYTHONPATH"] = os.fspath(custom_test_runner_dir)
9192

92-
django_project_dir: pathlib.Path = pathlib.Path(manage_py_path).parent
93+
django_project_dir: pathlib.Path = manage_path.parent
9394
sys.path.insert(0, os.fspath(django_project_dir))
9495
print(f"Django project directory: {django_project_dir}")
9596

96-
manage_spec: ModuleSpec | None = importlib.util.spec_from_file_location(
97-
"manage", manage_py_path
98-
)
99-
if manage_spec is None or manage_spec.loader is None:
100-
raise VSCodeUnittestError("Error importing manage.py when running Django testing.")
101-
manage_module = importlib.util.module_from_spec(manage_spec)
102-
manage_spec.loader.exec_module(manage_module)
103-
10497
manage_argv: List[str] = [
105-
manage_py_path,
98+
str(manage_path),
10699
"test",
107100
"--testrunner=django_test_runner.CustomExecutionTestRunner",
108101
*args,
109102
*test_ids,
110103
]
111104
print(f"Django manage.py arguments: {manage_argv}")
112105

113-
with override_argv(manage_argv), suppress(SystemExit):
114-
manage_module.main()
106+
try:
107+
with (
108+
override_argv(manage_argv),
109+
suppress(SystemExit),
110+
manage_path.open() as manage_file,
111+
):
112+
manage_code = manage_file.read()
113+
exec(manage_code, {"__name__": "__main__"})
114+
except OSError as e:
115+
raise VSCodeUnittestError("Error running Django, unable to read manage.py") from e
115116
except Exception as e:
116117
print(f"Error during Django test execution: {e}", file=sys.stderr)

0 commit comments

Comments
 (0)