11import copy
22import pathlib
33import shutil
4+ from pathlib import Path
45from textwrap import dedent
5- from typing import Optional
6+ from typing import Optional , cast
67
78import pytest
89from django .conf import settings
910
11+ from .helpers import DjangoPytester
12+
1013
1114pytest_plugins = "pytester"
1215
1316REPOSITORY_ROOT = pathlib .Path (__file__ ).parent
1417
1518
16- def pytest_configure (config ) -> None :
19+ def pytest_configure (config : pytest . Config ) -> None :
1720 config .addinivalue_line (
18- "markers" , "django_project: options for the django_testdir fixture"
21+ "markers" , "django_project: options for the django_pytester fixture"
1922 )
2023
2124
@@ -32,13 +35,17 @@ def _marker_apifun(
3235
3336
3437@pytest .fixture
35- def testdir ( testdir , monkeypatch ) :
38+ def pytester ( pytester : pytest . Pytester , monkeypatch : pytest . MonkeyPatch ) -> pytest . Pytester :
3639 monkeypatch .delenv ("PYTEST_ADDOPTS" , raising = False )
37- return testdir
40+ return pytester
3841
3942
4043@pytest .fixture (scope = "function" )
41- def django_testdir (request , testdir , monkeypatch ):
44+ def django_pytester (
45+ request : pytest .FixtureRequest ,
46+ pytester : pytest .Pytester ,
47+ monkeypatch : pytest .MonkeyPatch ,
48+ ) -> DjangoPytester :
4249 from pytest_django_test .db_helpers import (
4350 DB_NAME , SECOND_DB_NAME , SECOND_TEST_DB_NAME , TEST_DB_NAME ,
4451 )
@@ -106,56 +113,60 @@ def django_testdir(request, testdir, monkeypatch):
106113 )
107114
108115 if options ["project_root" ]:
109- project_root = testdir .mkdir (options ["project_root" ])
116+ project_root = pytester .mkdir (options ["project_root" ])
110117 else :
111- project_root = testdir . tmpdir
118+ project_root = pytester . path
112119
113- tpkg_path = project_root .mkdir ("tpkg" )
120+ tpkg_path = project_root / "tpkg"
121+ tpkg_path .mkdir ()
114122
115123 if options ["create_manage_py" ]:
116- project_root .ensure ("manage.py" )
124+ project_root .joinpath ("manage.py" ). touch ( )
117125
118- tpkg_path .ensure ("__init__.py" )
126+ tpkg_path .joinpath ("__init__.py" ). touch ( )
119127
120128 app_source = REPOSITORY_ROOT / "../pytest_django_test/app"
121- test_app_path = tpkg_path . join ( "app" )
129+ test_app_path = tpkg_path / "app"
122130
123131 # Copy the test app to make it available in the new test run
124132 shutil .copytree (str (app_source ), str (test_app_path ))
125- tpkg_path .join ("the_settings.py" ).write (test_settings )
133+ tpkg_path .joinpath ("the_settings.py" ).write_text (test_settings )
126134
127135 monkeypatch .setenv ("DJANGO_SETTINGS_MODULE" , "tpkg.the_settings" )
128136
129- def create_test_module (test_code : str , filename : str = "test_the_test.py" ):
130- r = tpkg_path .join (filename )
131- r .write (dedent (test_code ), ensure = True )
137+ def create_test_module (test_code : str , filename : str = "test_the_test.py" ) -> Path :
138+ r = tpkg_path .joinpath (filename )
139+ r .parent .mkdir (parents = True , exist_ok = True )
140+ r .write_text (dedent (test_code ))
132141 return r
133142
134- def create_app_file (code : str , filename : str ):
135- r = test_app_path .join (filename )
136- r .write (dedent (code ), ensure = True )
143+ def create_app_file (code : str , filename : str ) -> Path :
144+ r = test_app_path .joinpath (filename )
145+ r .parent .mkdir (parents = True , exist_ok = True )
146+ r .write_text (dedent (code ))
137147 return r
138148
139- testdir .create_test_module = create_test_module
140- testdir .create_app_file = create_app_file
141- testdir .project_root = project_root
142-
143- testdir .makeini (
149+ pytester .makeini (
144150 """
145151 [pytest]
146152 addopts = --strict-markers
147153 console_output_style=classic
148154 """
149155 )
150156
151- return testdir
157+ django_pytester_ = cast (DjangoPytester , pytester )
158+ django_pytester_ .create_test_module = create_test_module # type: ignore[method-assign]
159+ django_pytester_ .create_app_file = create_app_file # type: ignore[method-assign]
160+ django_pytester_ .project_root = project_root
161+
162+ return django_pytester_
152163
153164
154165@pytest .fixture
155- def django_testdir_initial ( django_testdir ) :
156- """A django_testdir fixture which provides initial_data."""
157- django_testdir . project_root .join ("tpkg/app/migrations" ). remove ( )
158- django_testdir .makefile (
166+ def django_pytester_initial ( django_pytester : DjangoPytester ) -> pytest . Pytester :
167+ """A django_pytester fixture which provides initial_data."""
168+ shutil . rmtree ( django_pytester . project_root .joinpath ("tpkg/app/migrations" ))
169+ django_pytester .makefile (
159170 ".json" ,
160171 initial_data = """
161172 [{
@@ -165,4 +176,4 @@ def django_testdir_initial(django_testdir):
165176 }]""" ,
166177 )
167178
168- return django_testdir
179+ return django_pytester
0 commit comments