コード例 #1
0
    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)
コード例 #2
0
 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)])
コード例 #3
0
 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))
コード例 #4
0
 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))
コード例 #5
0
 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()}"
コード例 #6
0
 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())
コード例 #7
0
 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())
コード例 #8
0
    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)
コード例 #9
0
 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"
     ])
コード例 #10
0
    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)
コード例 #11
0
    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()
コード例 #12
0
    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()
コード例 #13
0
 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'
     ])
コード例 #14
0
 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'),
     ])
コード例 #15
0
 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"),
     ])
コード例 #16
0
    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()
コード例 #17
0
    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()
コード例 #18
0
 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"
     ])
コード例 #19
0
    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()
コード例 #20
0
    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()
コード例 #21
0
    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)
コード例 #22
0
    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
コード例 #23
0
    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
コード例 #24
0
    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)
コード例 #25
0
    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
コード例 #26
0
    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)
        ])
コード例 #27
0
    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
コード例 #28
0
    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
コード例 #29
0
    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
コード例 #30
0
    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