4
4
import tempfile
5
5
from textwrap import dedent
6
6
import pytest
7
+ import shutil
8
+ import subprocess
7
9
8
10
import pythonanywhere .django_project
9
11
from pythonanywhere .django_project import DjangoProject
@@ -205,14 +207,76 @@ def test_nuke_option_deletes_directory_first(self, mock_subprocess, fake_home):
205
207
assert not old_file .exists ()
206
208
207
209
210
+ @pytest .fixture
211
+ def non_nested_submodule ():
212
+ subprocess .check_call (['git' , 'submodule' , 'update' , '--init' , '--recursive' ])
213
+ submodule_path = Path (__file__ ).parents [1 ] / 'submodules' / 'example-django-project'
214
+ subprocess .check_call (
215
+ ['git' , 'checkout' , 'master' ],
216
+ cwd = submodule_path
217
+ )
218
+ yield submodule_path
219
+ subprocess .check_call (
220
+ ['git' , 'checkout' , 'master' ],
221
+ cwd = submodule_path
222
+ )
223
+
224
+
225
+
226
+
227
+ class TestFindDjangoFiles :
228
+
229
+ def test_non_nested (self , fake_home , non_nested_submodule ):
230
+ project = DjangoProject ('mydomain.com' )
231
+ shutil .copytree (non_nested_submodule , project .project_path )
232
+ expected_settings_path = project .project_path / 'myproject/settings.py'
233
+ assert expected_settings_path .exists ()
234
+ expected_manage_py = project .project_path / 'manage.py'
235
+ assert expected_manage_py .exists ()
236
+
237
+ project .find_django_files ()
238
+
239
+ assert project .settings_path == expected_settings_path
240
+ assert project .manage_py_path == expected_manage_py
241
+
242
+
243
+ def test_raises_if_empty_project_folder (self , fake_home ):
244
+ project = DjangoProject ('mydomain.com' )
245
+ with pytest .raises (SanityException ) as e :
246
+ project .find_django_files ()
247
+
248
+ assert 'Could not find your settings.py' in str (e .value )
249
+
250
+
251
+ def test_raises_if_no_settings_in_any_subfolders (self , fake_home ):
252
+ project = DjangoProject ('mydomain.com' )
253
+ not_this_folder = project .project_path / 'not_this_folder'
254
+ not_this_folder .mkdir (parents = True )
255
+ with pytest .raises (SanityException ) as e :
256
+ project .find_django_files ()
257
+
258
+ assert 'Could not find your settings.py' in str (e .value )
259
+
260
+
261
+ def test_raises_if_manage_py_not_found (self , fake_home , non_nested_submodule ):
262
+ project = DjangoProject ('mydomain.com' )
263
+ shutil .copytree (non_nested_submodule , project .project_path )
264
+ expected_manage_py = project .project_path / 'manage.py'
265
+ assert expected_manage_py .exists ()
266
+ expected_manage_py .unlink ()
267
+ with pytest .raises (SanityException ) as e :
268
+ project .find_django_files ()
269
+
270
+ assert 'Could not find your manage.py' in str (e .value )
271
+
208
272
209
273
class TestUpdateSettingsFile :
210
274
211
275
def test_adds_STATIC_and_MEDIA_config_to_settings (self ):
212
276
project = DjangoProject ('mydomain.com' )
213
- project .project_path = Path (tempfile .mkdtemp () )
214
- ( project . project_path / 'mysite' ). mkdir ( parents = True )
215
- with open (project .project_path / 'mysite/settings.py' , 'w' ) as f :
277
+ project .settings_path = Path (tempfile .NamedTemporaryFile (). name )
278
+
279
+ with open (project .settings_path , 'w' ) as f :
216
280
f .write (dedent (
217
281
"""
218
282
# settings file
@@ -223,10 +287,9 @@ def test_adds_STATIC_and_MEDIA_config_to_settings(self):
223
287
224
288
project .update_settings_file ()
225
289
226
- with open (project .project_path / 'mysite/settings.py' ) as f :
227
- contents = f .read ()
290
+ with open (project .settings_path ) as f :
291
+ lines = f .read (). split ( ' \n ' )
228
292
229
- lines = contents .split ('\n ' )
230
293
assert "STATIC_URL = '/static/'" in lines
231
294
assert "MEDIA_URL = '/media/'" in lines
232
295
assert "STATIC_ROOT = os.path.join(BASE_DIR, 'static')" in lines
@@ -235,9 +298,9 @@ def test_adds_STATIC_and_MEDIA_config_to_settings(self):
235
298
236
299
def test_adds_domain_to_ALLOWED_HOSTS (self ):
237
300
project = DjangoProject ('mydomain.com' )
238
- project .project_path = Path (tempfile .mkdtemp () )
239
- ( project . project_path / 'mysite' ). mkdir ( parents = True )
240
- with open (project .project_path / 'mysite/settings.py' , 'w' ) as f :
301
+ project .settings_path = Path (tempfile .NamedTemporaryFile (). name )
302
+
303
+ with open (project .settings_path , 'w' ) as f :
241
304
f .write (dedent (
242
305
"""
243
306
# settings file
@@ -248,10 +311,8 @@ def test_adds_domain_to_ALLOWED_HOSTS(self):
248
311
249
312
project .update_settings_file ()
250
313
251
- with open (project .project_path / 'mysite/settings.py' ) as f :
252
- contents = f .read ()
253
-
254
- lines = contents .split ('\n ' )
314
+ with open (project .settings_path ) as f :
315
+ lines = f .read ().split ('\n ' )
255
316
256
317
assert "ALLOWED_HOSTS = ['mydomain.com']" in lines
257
318
@@ -261,10 +322,14 @@ class TestRunCollectStatic:
261
322
262
323
def test_runs_manage_py_in_correct_virtualenv (self , mock_subprocess , fake_home ):
263
324
project = DjangoProject ('mydomain.com' )
264
- project .virtualenv_path = '/path/to/virtualenv'
325
+ project .virtualenv_path = Path ('/path/to/virtualenv' )
326
+ project .manage_py_path = Path ('/path/to/manage.py' )
265
327
project .run_collectstatic ()
266
328
assert mock_subprocess .check_call .call_args == call ([
267
- Path (project .virtualenv_path ) / 'bin/python' , project .project_path / 'manage.py' , 'collectstatic' , '--noinput'
329
+ project .virtualenv_path / 'bin/python' ,
330
+ project .manage_py_path ,
331
+ 'collectstatic' ,
332
+ '--noinput'
268
333
])
269
334
270
335
0 commit comments