def test_raises_if_empty_project_folder(self, fake_home, virtualenvs_folder): project = DjangoProject("mydomain.com", "python.version") with pytest.raises(SanityException) as e: project.find_django_files() assert "Could not find your settings.py" in str(e.value)
def test_calls_git_subprocess(self, mock_subprocess, fake_home, virtualenvs_folder): project = DjangoProject("www.a.domain.com", "py.version") project.download_repo("repo", nuke=False) assert mock_subprocess.check_call.call_args == call( ["git", "clone", "repo", str(project.project_path)])
def test_actually_downloads_repo(self, fake_home, virtualenvs_folder): repo = "https://gist.github.com/hjwp/4173bcface139beb7632ec93726f91ea" project = DjangoProject("www.a.domain.com", "py.version") project.download_repo(repo, nuke=False) assert project.project_path.is_dir() assert "file1.py" in os.listdir(str(project.project_path)) assert "file2.py" in os.listdir(str(project.project_path))
def test_actually_downloads_repo(self, fake_home): repo = 'https://gist.github.com/hjwp/4173bcface139beb7632ec93726f91ea' project = DjangoProject('www.a.domain.com', 'py.version') project.download_repo(repo, nuke=False) assert project.project_path.is_dir() assert 'file1.py' in os.listdir(str(project.project_path)) assert 'file2.py' in os.listdir(str(project.project_path))
def test_if_requirements_txt_exists(self, fake_home, virtualenvs_folder): project = DjangoProject("mydomain.com", "python.version") project.project_path.mkdir() requirements_txt = project.project_path / "requirements.txt" requirements_txt.touch() assert project.detect_requirements( ) == f"-r {requirements_txt.resolve()}"
def test_if_requirements_txt_exists(self, fake_home): project = DjangoProject("mydomain.com", "python.version") project.project_path.mkdir() requirements_txt = project.project_path / "requirements.txt" requirements_txt.touch() assert project.detect_requirements() == "-r {requirements_txt}".format( requirements_txt=requirements_txt.resolve())
def test_if_requirements_txt_exists(self, fake_home): project = DjangoProject('mydomain.com', 'python.version') project.project_path.mkdir() requirements_txt = project.project_path / 'requirements.txt' requirements_txt.touch() assert project.detect_requirements() == '-r {requirements_txt}'.format( requirements_txt=requirements_txt.resolve())
def test_raises_if_no_settings_in_any_subfolders(self, fake_home): project = DjangoProject('mydomain.com', 'python.version') not_this_folder = project.project_path / 'not_this_folder' not_this_folder.mkdir(parents=True) with pytest.raises(SanityException) as e: project.find_django_files() assert 'Could not find your settings.py' in str(e.value)
def test_runs_manage_py_in_correct_virtualenv(self, mock_subprocess, fake_home): project = DjangoProject("mydomain.com", "python.version") project.manage_py_path = Path("/path/to/manage.py") project.run_migrate() assert mock_subprocess.check_call.call_args == call([ str(project.virtualenv.path / "bin/python"), str(project.manage_py_path), "migrate" ])
def test_raises_if_no_settings_in_any_subfolders(self, fake_home, virtualenvs_folder): project = DjangoProject("mydomain.com", "python.version") not_this_folder = project.project_path / "not_this_folder" not_this_folder.mkdir(parents=True) with pytest.raises(SanityException) as e: project.find_django_files() assert "Could not find your settings.py" in str(e.value)
def test_nuke_option_ignores_directory_doesnt_exist( self, mock_subprocess, fake_home, virtualenvs_folder): project = DjangoProject("www.a.domain.com", "py.version") mock_subprocess.check_call.side_effect = lambda *_, **__: Path( project.project_path).mkdir() project.download_repo("repo", nuke=True) # should not raise assert project.project_path.is_dir()
def test_nuke_option_ignores_directory_doesnt_exist( self, mock_subprocess, fake_home): project = DjangoProject('www.a.domain.com', 'py.version') mock_subprocess.check_call.side_effect = lambda *_, **__: Path( project.project_path).mkdir() project.download_repo('repo', nuke=True) # should not raise assert project.project_path.is_dir()
def test_runs_manage_py_in_correct_virtualenv(self, mock_subprocess, fake_home): project = DjangoProject('mydomain.com', 'python.version') project.manage_py_path = Path('/path/to/manage.py') project.run_collectstatic() assert mock_subprocess.check_call.call_args == call([ str(project.virtualenv.path / 'bin/python'), str(project.manage_py_path), 'collectstatic', '--noinput' ])
def test_calls_startproject(self, mock_subprocess, fake_home): project = DjangoProject('mydomain.com', 'python.version') project.run_startproject(nuke=False) assert mock_subprocess.check_call.call_args == call([ str(Path(project.virtualenv.path / 'bin/django-admin.py')), 'startproject', 'mysite', str(fake_home / 'mydomain.com'), ])
def test_calls_startproject(self, mock_subprocess, fake_home): project = DjangoProject("mydomain.com", "python.version") project.run_startproject(nuke=False) assert mock_subprocess.check_call.call_args == call([ str(Path(project.virtualenv.path / "bin/django-admin.py")), "startproject", "mysite", str(fake_home / "mydomain.com"), ])
def test_nuke_option_deletes_directory_first(self, mock_subprocess, fake_home): project = DjangoProject('www.a.domain.com', 'py.version') project.project_path.mkdir() (project.project_path / 'old-thing.txt').touch() mock_subprocess.check_call.side_effect = lambda *_, **__: Path( project.project_path).mkdir() project.download_repo('repo', nuke=True) assert 'old-thing.txt' not in project.project_path.iterdir()
def test_nuke_option_deletes_directory_first(self, mock_subprocess, fake_home): project = DjangoProject('mydomain.com', 'python.version') (fake_home / project.domain).mkdir() old_file = fake_home / project.domain / 'old_file.py' old_file.write_text('old stuff') project.run_startproject(nuke=True) assert not old_file.exists()
def test_runs_manage_py_in_correct_virtualenv(self, mock_subprocess, fake_home, virtualenvs_folder): project = DjangoProject("mydomain.com", "python.version") project.manage_py_path = Path("/path/to/manage.py") project.run_collectstatic() assert mock_subprocess.check_call.call_args == call([ str(project.virtualenv.path / "bin/python"), str(project.manage_py_path), "collectstatic", "--noinput" ])
def test_nuke_option_deletes_directory_first(self, mock_subprocess, fake_home, virtualenvs_folder): project = DjangoProject("mydomain.com", "python.version") (fake_home / project.domain).mkdir() old_file = fake_home / project.domain / "old_file.py" old_file.write_text("old stuff") project.run_startproject(nuke=True) assert not old_file.exists()
def test_nuke_option_deletes_directory_first(self, mock_subprocess, fake_home, virtualenvs_folder): project = DjangoProject("www.a.domain.com", "py.version") project.project_path.mkdir() (project.project_path / "old-thing.txt").touch() mock_subprocess.check_call.side_effect = lambda *_, **__: Path( project.project_path).mkdir() project.download_repo("repo", nuke=True) assert "old-thing.txt" not in project.project_path.iterdir()
def test_raises_if_manage_py_not_found(self, fake_home, non_nested_submodule): project = DjangoProject('mydomain.com', 'python.version') shutil.copytree(str(non_nested_submodule), str(project.project_path)) expected_manage_py = project.project_path / 'manage.py' assert expected_manage_py.exists() expected_manage_py.unlink() with pytest.raises(SanityException) as e: project.find_django_files() assert 'Could not find your manage.py' in str(e.value)
def test_non_nested(self, fake_home, non_nested_submodule): project = DjangoProject("mydomain.com", "python.version") shutil.copytree(str(non_nested_submodule), str(project.project_path)) expected_settings_path = project.project_path / "myproject/settings.py" assert expected_settings_path.exists() expected_manage_py = project.project_path / "manage.py" assert expected_manage_py.exists() project.find_django_files() assert project.settings_path == expected_settings_path assert project.manage_py_path == expected_manage_py
def test_nested(self, fake_home, more_nested_submodule): project = DjangoProject('mydomain.com', 'python.version') shutil.copytree(str(more_nested_submodule), str(project.project_path)) expected_settings_path = project.project_path / 'mysite/mysite/settings.py' assert expected_settings_path.exists() expected_manage_py = project.project_path / 'mysite/manage.py' assert expected_manage_py.exists() project.find_django_files() assert project.settings_path == expected_settings_path assert project.manage_py_path == expected_manage_py
def test_raises_if_manage_py_not_found(self, fake_home, non_nested_submodule, virtualenvs_folder): project = DjangoProject("mydomain.com", "python.version") shutil.copytree(str(non_nested_submodule), str(project.project_path)) expected_manage_py = project.project_path / "manage.py" assert expected_manage_py.exists() expected_manage_py.unlink() with pytest.raises(SanityException) as e: project.find_django_files() assert "Could not find your manage.py" in str(e.value)
def test_adds_domain_to_ALLOWED_HOSTS(self): project = DjangoProject('mydomain.com', 'python.version') project.settings_path = Path(tempfile.NamedTemporaryFile().name) with project.settings_path.open('w') as f: f.write( dedent(""" # settings file STATIC_URL = '/static/' ALLOWED_HOSTS = [] """)) project.update_settings_file() with project.settings_path.open() as f: lines = f.read().split('\n') assert "ALLOWED_HOSTS = ['mydomain.com']" in lines
def test_actually_produces_wsgi_file_that_can_import_nested_project( self, fake_home, more_nested_submodule, virtualenvs_folder): project = DjangoProject('mydomain.com', '3.6') shutil.copytree(str(more_nested_submodule), str(project.project_path)) project.create_virtualenv() project.find_django_files() project.wsgi_file_path = Path(tempfile.NamedTemporaryFile().name) project.update_wsgi_file() print(project.wsgi_file_path.open().read()) subprocess.check_output([ str(project.virtualenv.path / 'bin/python'), str(project.wsgi_file_path) ])
def test_adds_domain_to_ALLOWED_HOSTS(self, virtualenvs_folder): project = DjangoProject("mydomain.com", "python.version") project.settings_path = Path(tempfile.NamedTemporaryFile().name) project.virtualenv.get_version = Mock(return_value="1.0") with project.settings_path.open("w") as f: f.write( dedent(""" # settings file STATIC_URL = '/static/' ALLOWED_HOSTS = [] """)) project.update_settings_file() with project.settings_path.open() as f: lines = f.read().split("\n") assert "ALLOWED_HOSTS = ['mydomain.com']" in lines
def test_adds_STATIC_and_MEDIA_config_to_settings(self): project = DjangoProject('mydomain.com', 'python.version') project.settings_path = Path(tempfile.NamedTemporaryFile().name) with project.settings_path.open('w') as f: f.write( dedent(""" # settings file STATIC_URL = '/static/' ALLOWED_HOSTS = [] """)) project.update_settings_file() with project.settings_path.open() as f: lines = f.read().split('\n') assert "STATIC_URL = '/static/'" in lines assert "MEDIA_URL = '/media/'" in lines assert "STATIC_ROOT = os.path.join(BASE_DIR, 'static')" in lines assert "MEDIA_ROOT = os.path.join(BASE_DIR, 'media')" in lines
def test_only_adds_MEDIA_ROOT_if_its_not_already_there( self, virtualenvs_folder): project = DjangoProject("mydomain.com", "python.version") project.settings_path = Path(tempfile.NamedTemporaryFile().name) project.virtualenv.get_version = Mock(return_value="1.0") with project.settings_path.open("w") as f: f.write( dedent(""" # settings file STATIC_URL = '/static/' ALLOWED_HOSTS = [] MEDIA_ROOT = media_root """)) project.update_settings_file() with project.settings_path.open() as f: lines = f.read().split("\n") assert "MEDIA_ROOT = media_root" in lines assert "MEDIA_ROOT = os.path.join(BASE_DIR, 'media')" not in lines
def test_adds_STATIC_and_MEDIA_config_to_settings_with_new_django( self, virtualenvs_folder): project = DjangoProject("mydomain.com", "python.version") project.settings_path = Path(tempfile.NamedTemporaryFile().name) project.virtualenv.get_version = Mock(return_value="3.1.1") with project.settings_path.open("w") as f: f.write( dedent(""" # settings file STATIC_URL = '/static/' ALLOWED_HOSTS = [] """)) project.update_settings_file() with project.settings_path.open() as f: lines = f.read().split("\n") assert "STATIC_URL = '/static/'" in lines assert "MEDIA_URL = '/media/'" in lines assert "STATIC_ROOT = Path(BASE_DIR / 'static')" in lines assert "MEDIA_ROOT = Path(BASE_DIR / 'media')" in lines