10000 move download_repo into class · pythonanywhere/helper_scripts@fc7bf21 · GitHub
[go: up one dir, main page]

Skip to content

Commit fc7bf21

Browse files
committed
move download_repo into class
1 parent 4fb03c6 commit fc7bf21

File tree

4 files changed

+61
-82
lines changed

4 files changed

+61
-82
lines changed

pythonanywhere/django_project.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ def sanity_checks(self, nuke):
3030

3131

3232

33+
def download_repo(self, repo, nuke):
34+
if nuke:
35+
shutil.rmtree(self.project_path)
36+
subprocess.check_call(['git', 'clone', repo, self.project_path])
37+
38+
3339
def create_virtualenv(self, python_version, django_version=None, nuke=False):
3440
if django_version is None:
3541
packages = self.detect_django_version()

scripts/pa_autoconfigure_django.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,19 @@
1818

1919
from docopt import docopt
2020
import getpass
21-
from pathlib import Path
22-
import subprocess
23-
import shutil
2421

2522
from pythonanywhere.django_project import DjangoProject
2623
from pythonanywhere.snakesay import snakesay
2724

2825

29-
def download_repo(repo, domain, nuke):
30-
target = Path('~').expanduser() / domain
31-
if nuke:
32-
shutil.rmtree(target)
33-
subprocess.check_call(['git', 'clone', repo, target])
34-
return target
35-
36-
3726
def main(repo_url, domain, python_version, nuke):
3827
if domain == 'your-username.pythonanywhere.com':
3928
username = getpass.getuser().lower()
4029
domain = f'{username}.pythonanywhere.com'
4130

42-
download_repo(repo_url, domain, nuke=nuke)
43-
4431
project = DjangoProject(domain)
4532
project.sanity_checks(nuke=nuke)
33+
project.download_repo(repo_url, nuke=nuke),
4634
project.create_virtualenv(python_version, nuke=nuke)
4735
project.update_settings_file()
4836
project.run_collectstatic()

tests/test_django_project.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from unittest.mock import call, patch, Mock
22
from pathlib import Path
3+
import os
34
import tempfile
45
from textwrap import dedent
56
import pytest
@@ -34,6 +35,7 @@ def test_virtualenv_path(self, fake_home):
3435
assert project.virtualenv_path == virtualenv_path('mydomain.com')
3536

3637

38+
3739
class TestSanityChecks:
3840

3941
def test_calls_webapp_sanity_checks(self, fake_home):
@@ -78,6 +80,37 @@ def test_nuke_option_overrides_directory_checks(self, fake_home, virtualenvs_fol
7880

7981

8082

83+
class TestDownloadRepo:
84+
85+
@pytest.mark.slowtest
86+
def test_actually_downloads_repo(self, fake_home):
87+
repo = 'https://gist.github.com/hjwp/4173bcface139beb7632ec93726f91ea'
88+
project = DjangoProject('www.a.domain.com')
89+
project.download_repo(repo, nuke=False)
90+
assert project.project_path.is_dir()
91+
assert 'file1.py' in os.listdir(project.project_path)
92+
assert 'file2.py' in os.listdir(project.project_path)
93+
94+
95+
def test_calls_git_subprocess(self, mock_subprocess, fake_home):
96+
project = DjangoProject('www.a.domain.com')
97+
project.download_repo('repo', nuke=False)
98+
assert mock_subprocess.check_call.call_args == call(
99+
['git', 'clone', 'repo', project.project_path]
100+
)
101+
102+
103+
def test_nuke_option(self, mock_subprocess, fake_home):
104+
project = DjangoProject('www.a.domain.com')
105+
project.project_path.mkdir()
106+
(project.project_path / 'old-thing.txt').touch()
107+
mock_subprocess.check_call.side_effect = lambda *_, **__: Path(project.project_path).mkdir()
108+
109+
project.download_repo('repo', nuke=True)
110+
assert 'old-thing.txt' not in project.project_path.iterdir()
111+
112+
113+
81114
class TestDetectDjangoVersion:
82115

83116
def test_is_django_by_default(self, fake_home):

tests/test_pa_autoconfigure_django.py

Lines changed: 21 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,24 @@
1-
from unittest.mock import call, patch, Mock
1+
from unittest.mock import call, patch
22
import getpass
3-
import os
4-
from pathlib import Path
5-
import pytest
63

7-
from scripts.pa_autoconfigure_django import main, download_repo
8-
9-
10-
@pytest.fixture
11-
def mock_main_functions():
12-
mocks = Mock()
13-
patchers = []
14-
functions = [
15-
'download_repo',
16-
'DjangoProject',
17-
]
18-
for function in functions:
19-
mock = getattr(mocks, function)
20-
patcher = patch(
21-
'scripts.pa_autoconfigure_django.{}'.format(function),
22-
mock
23-
)
24-
patchers.append(patcher)
25-
patcher.start()
26-
27-
yield mocks
28-
29-
for patcher in patchers:
30-
patcher.stop()
4+
from scripts.pa_autoconfigure_django import main
315

326

337

348
class TestMain:
359

36-
def test_calls_all_stuff_in_right_order(self, mock_main_functions):
37-
main('https://github.com/pythonanywhere.com/example-django-project.git', 'www.domain.com', 'python.version', nuke='nuke option')
38-
mock_django_project = mock_main_functions.DjangoProject.return_value
39-
assert mock_main_functions.method_calls == [
40-
call.download_repo('https://github.com/pythonanywhere.com/example-django-project.git', 'www.domain.com', nuke='nuke option'),
41-
call.DjangoProject('www.domain.com'),
42-
]
43-
assert mock_django_project.method_calls == [
10+
def test_calls_all_stuff_in_right_order(self):
11+
with patch('scripts.pa_autoconfigure_django.DjangoProject') as mock_DjangoProject:
12+
main(
13+
'https://github.com/pythonanywhere.com/example-django-project.git',
14+
'www.domain.com',
15+
'python.version',
16+
nuke='nuke option'
17+
)
18+
assert mock_DjangoProject.call_args == call('www.domain.com')
19+
assert mock_DjangoProject.return_value.method_calls == [
4420
call.sanity_checks(nuke='nuke option'),
21+
call.download_repo('https://github.com/pythonanywhere.com/example-django-project.git', nuke='nuke option'),
4522
call.create_virtualenv('python.version', nuke='nuke option'),
4623
call.update_settings_file(),
4724
call.run_collectstatic(),
@@ -52,52 +29,27 @@ def test_calls_all_stuff_in_right_order(self, mock_main_functions):
5229
]
5330

5431

55-
def test_domain_defaults_to_using_current_username(self, mock_main_functions):
32+
def test_domain_defaults_to_using_current_username(self):
5633
username = getpass.getuser()
57-
main('a-repo', 'your-username.pythonanywhere.com', 'python.version', nuke=False)
58-
assert mock_main_functions.DjangoProject.call_args == call(
34+
with patch('scripts.pa_autoconfigure_django.DjangoProject') as mock_DjangoProject:
35+
main('a-repo', 'your-username.pythonanywhere.com', 'python.version', nuke=False)
36+
assert mock_DjangoProject.call_args == call(
5937
username + '.pythonanywhere.com'
6038
)
6139

6240

63-
def test_lowercases_username(self, mock_main_functions):
41+
def test_lowercases_username(self):
6442
with patch('scripts.pa_autoconfigure_django.getpass') as mock_getpass:
6543
mock_getpass.getuser.return_value = 'UserName1'
66-
main('a-url', 'your-username.pythonanywhere.com', 'python.version', 'nukey')
67-
assert mock_main_functions.DjangoProject.call_args == call(
44+
with patch('scripts.pa_autoconfigure_django.DjangoProject') as mock_DjangoProject:
45+
main('a-url', 'your-username.pythonanywhere.com', 'python.version', 'nukey')
46+
assert mock_DjangoProject.call_args == call(
6847
'username1.pythonanywhere.com',
6948
)
7049

7150

72-
class TestDownloadRepo:
73-
74-
@pytest.mark.slowtest
75-
def test_actually_downloads_repo(self, fake_home):
76-
new_folder = download_repo('https://gist.github.com/hjwp/4173bcface139beb7632ec93726f91ea', 'a-domain.com', nuke=False)
77-
print(os.listdir(fake_home))
78-
assert new_folder.is_dir()
79-
assert 'file1.py' in os.listdir(new_folder)
80-
assert 'file2.py' in os.listdir(new_folder)
81-
82-
83-
def test_calls_git_subprocess(self, mock_subprocess, fake_home):
84-
new_folder = download_repo('repo', 'a-domain.com', nuke=False)
85-
assert new_folder == Path(fake_home) / 'a-domain.com'
86-
assert mock_subprocess.check_call.call_args == call(
87-
['git', 'clone', 'repo', new_folder]
88-
)
89-
90-
91-
def test_nuke_option(self, mock_subprocess, fake_home):
92-
mock_subprocess.check_call.side_effect = lambda *_, **__: Path(fake_home / 'a-domain.com').mkdir()
93-
Path(fake_home / 'a-domain.com').mkdir()
94-
Path(fake_home / 'a-domain.com' / 'old-thing.txt').touch()
95-
new_folder = download_repo('repo', 'a-domain.com', nuke=True)
96-
assert 'old-thing.txt' not in new_folder.iterdir()
97-
9851

9952
def test_todos():
100-
assert not 'move download_repo onto djangoproject'
10153
assert not 'existing-project sanity checks eg settings.py not found, requirements empty'
10254
assert not 'SECRET_KEY'
10355
assert not 'database stuff?'

0 commit comments

Comments
 (0)
0