Skip to content

Commit d52d3dc

Browse files
committed
get tests to only shell out to python versions that exist in the current environment
1 parent 51ec9c6 commit d52d3dc

File tree

2 files changed

+60
-54
lines changed

2 files changed

+60
-54
lines changed

tests/test_django_project.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import subprocess
44
import tempfile
55
from pathlib import Path
6+
from platform import python_version
67
from textwrap import dedent
78
from unittest.mock import Mock, call
89

@@ -282,7 +283,7 @@ def test_updates_wsgi_file_from_template(self):
282283
def test_actually_produces_wsgi_file_that_can_import_project_non_nested(
283284
self, fake_home, non_nested_submodule, virtualenvs_folder
284285
):
285-
project = DjangoProject("mydomain.com", "3.6")
286+
project = DjangoProject("mydomain.com", ".".join(python_version().split(".")[:2]))
286287
shutil.copytree(str(non_nested_submodule), str(project.project_path))
287288
print(subprocess.check_call(["tree", str(project.project_path)]))
288289
project.create_virtualenv()
@@ -298,7 +299,7 @@ def test_actually_produces_wsgi_file_that_can_import_project_non_nested(
298299
def test_actually_produces_wsgi_file_that_can_import_nested_project(
299300
self, fake_home, more_nested_submodule, virtualenvs_folder
300301
):
301-
project = DjangoProject("mydomain.com", "3.6")
302+
project = DjangoProject("mydomain.com", ".".join(python_version().split(".")[:2]))
302303
shutil.copytree(str(more_nested_submodule), str(project.project_path))
303304
project.create_virtualenv()
304305
project.find_django_files()
Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,90 @@
1-
from unittest.mock import call, patch
21
import getpass
32
import os
4-
import pytest
53
import subprocess
4+
from platform import python_version
5+
from unittest.mock import call, patch
66

7+
import pytest
78
from scripts.pa_start_django_webapp_with_virtualenv import main
89

910

1011
class TestMain:
11-
1212
def test_calls_all_stuff_in_right_order(self):
13-
with patch('scripts.pa_start_django_webapp_with_virtualenv.DjangoProject') as mock_DjangoProject:
14-
main('www.domain.com', 'django.version', 'python.version', nuke='nuke option')
15-
assert mock_DjangoProject.call_args == call('www.domain.com', 'python.version')
13+
with patch("scripts.pa_start_django_webapp_with_virtualenv.DjangoProject") as mock_DjangoProject:
14+
main("www.domain.com", "django.version", "python.version", nuke="nuke option")
15+
assert mock_DjangoProject.call_args == call("www.domain.com", "python.version")
1616
assert mock_DjangoProject.return_value.method_calls == [
17-
call.sanity_checks(nuke='nuke option'),
18-
call.create_virtualenv('django.version', nuke='nuke option'),
19-
call.run_startproject(nuke='nuke option'),
17+
call.sanity_checks(nuke="nuke option"),
18+
call.create_virtualenv("django.version", nuke="nuke option"),
19+
call.run_startproject(nuke="nuke option"),
2020
call.find_django_files(),
2121
call.update_settings_file(),
2222
call.run_collectstatic(),
23-
call.create_webapp(nuke='nuke option'),
23+
call.create_webapp(nuke="nuke option"),
2424
call.add_static_file_mappings(),
2525
call.update_wsgi_file(),
2626
call.webapp.reload(),
2727
]
2828

29-
3029
def test_domain_defaults_to_using_current_username_and_domain_from_env(self, monkeypatch):
3130
username = getpass.getuser()
32-
monkeypatch.setenv('PYTHONANYWHERE_DOMAIN', 'pythonanywhere.domain')
33-
with patch('scripts.pa_start_django_webapp_with_virtualenv.DjangoProject') as mock_DjangoProject:
34-
main('your-username.pythonanywhere.com', 'django.version', 'python.version', nuke=False)
35-
assert mock_DjangoProject.call_args == call(username + '.pythonanywhere.domain', 'python.version')
36-
31+
monkeypatch.setenv("PYTHONANYWHERE_DOMAIN", "pythonanywhere.domain")
32+
with patch("scripts.pa_start_django_webapp_with_virtualenv.DjangoProject") as mock_DjangoProject:
33+
main("your-username.pythonanywhere.com", "django.version", "python.version", nuke=False)
34+
assert mock_DjangoProject.call_args == call(username + ".pythonanywhere.domain", "python.version")
3735

3836
def test_lowercases_username(self):
39-
with patch('scripts.pa_start_django_webapp_with_virtualenv.getpass') as mock_getpass:
40-
mock_getpass.getuser.return_value = 'UserName1'
41-
with patch('scripts.pa_start_django_webapp_with_virtualenv.DjangoProject') as mock_DjangoProject:
42-
main('your-username.pythonanywhere.com', 'django.version', 'python.version', 'nukey')
43-
assert mock_DjangoProject.call_args == call('username1.pythonanywhere.com', 'python.version')
44-
37+
with patch("scripts.pa_start_django_webapp_with_virtualenv.getpass") as mock_getpass:
38+
mock_getpass.getuser.return_value = "UserName1"
39+
with patch("scripts.pa_start_django_webapp_with_virtualenv.DjangoProject") as mock_DjangoProject:
40+
main("your-username.pythonanywhere.com", "django.version", "python.version", "nukey")
41+
assert mock_DjangoProject.call_args == call("username1.pythonanywhere.com", "python.version")
4542

4643
@pytest.mark.slowtest
4744
def test_actually_creates_django_project_in_virtualenv_with_hacked_settings_and_static_files(
4845
self, fake_home, virtualenvs_folder, api_token
4946
):
5047

51-
with patch('scripts.pa_start_django_webapp_with_virtualenv.DjangoProject.update_wsgi_file'):
52-
with patch('pythonanywhere.api.call_api'):
53-
main('mydomain.com', '1.9.2', '2.7', nuke=False)
54-
55-
django_version = subprocess.check_output([
56-
str(virtualenvs_folder / 'mydomain.com/bin/python'),
57-
'-c'
58-
'import django; print(django.get_version())'
59-
]).decode().strip()
60-
assert django_version == '1.9.2'
61-
62-
with (fake_home / 'mydomain.com/mysite/settings.py').open() as f:
63-
lines = f.read().split('\n')
48+
with patch("scripts.pa_start_django_webapp_with_virtualenv.DjangoProject.update_wsgi_file"):
49+
with patch("pythonanywhere.api.call_api"):
50+
main("mydomain.com", "1.9.2", "2.7", nuke=False)
51+
52+
django_version = (
53+
subprocess.check_output(
54+
[
55+
str(virtualenvs_folder / "mydomain.com/bin/python"),
56+
"-c" "import django; print(django.get_version())",
57+
]
58+
)
59+
.decode()
60+
.strip()
61+
)
62+
assert django_version == "1.9.2"
63+
64+
with (fake_home / "mydomain.com/mysite/settings.py").open() as f:
65+
lines = f.read().split("\n")
6466
assert "MEDIA_ROOT = os.path.join(BASE_DIR, 'media')" in lines
6567
assert "ALLOWED_HOSTS = ['mydomain.com']" in lines
6668

67-
assert 'base.css' in os.listdir(str(fake_home / 'mydomain.com/static/admin/css'))
68-
69+
assert "base.css" in os.listdir(str(fake_home / "mydomain.com/static/admin/css"))
6970

7071
@pytest.mark.slowtest
71-
def test_nuke_option_lets_you_run_twice(
72-
self, fake_home, virtualenvs_folder, api_token
73-
):
74-
75-
with patch('scripts.pa_start_django_webapp_with_virtualenv.DjangoProject.update_wsgi_file'):
76-
with patch('pythonanywhere.api.call_api'):
77-
main('mydomain.com', '1.9.2', '2.7', nuke=False)
78-
main('mydomain.com', '1.11.3', '3.6', nuke=True)
79-
80-
django_version = subprocess.check_output([
81-
str(virtualenvs_folder / 'mydomain.com/bin/python'),
82-
'-c'
83-
'import django; print(django.get_version())'
84-
]).decode().strip()
85-
assert django_version == '1.11.3'
72+
def test_nuke_option_lets_you_run_twice(self, fake_home, virtualenvs_folder, api_token):
73+
74+
with patch("scripts.pa_start_django_webapp_with_virtualenv.DjangoProject.update_wsgi_file"):
75+
with patch("pythonanywhere.api.call_api"):
76+
version = ".".join(python_version().split(".")[:2])
77+
main("mydomain.com", "1.9.2", version, nuke=False)
78+
main("mydomain.com", "1.11.3", version, nuke=True)
79+
80+
django_version = (
81+
subprocess.check_output(
82+
[
83+
str(virtualenvs_folder / "mydomain.com/bin/python"),
84+
"-c" "import django; print(django.get_version())",
85+
]
86+
)
87+
.decode()
88+
.strip()
89+
)
90+
assert django_version == "1.11.3"

0 commit comments

Comments
 (0)