2015-11-09 01:46:38 +03:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
2015-11-09 01:05:32 +03:00
|
|
|
import pytest
|
2019-06-05 21:00:43 +03:00
|
|
|
from cookiecutter.exceptions import FailedHookException
|
2019-04-01 10:47:46 +03:00
|
|
|
import sh
|
|
|
|
import yaml
|
2015-11-09 01:46:38 +03:00
|
|
|
from binaryornot.check import is_binary
|
|
|
|
|
2019-05-27 18:54:08 +03:00
|
|
|
PATTERN = r"{{(\s?cookiecutter)[.](.*?)}}"
|
2015-11-09 01:46:38 +03:00
|
|
|
RE_OBJ = re.compile(PATTERN)
|
|
|
|
|
2015-11-09 01:05:32 +03:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def context():
|
|
|
|
return {
|
2018-04-09 01:03:29 +03:00
|
|
|
"project_name": "My Test Project",
|
|
|
|
"project_slug": "my_test_project",
|
|
|
|
"author_name": "Test Author",
|
|
|
|
"email": "test@example.com",
|
|
|
|
"description": "A short description of the project.",
|
|
|
|
"domain_name": "example.com",
|
|
|
|
"version": "0.1.0",
|
|
|
|
"timezone": "UTC",
|
2015-11-09 01:05:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-29 20:15:35 +03:00
|
|
|
SUPPORTED_COMBINATIONS = [
|
|
|
|
{"open_source_license": "MIT"},
|
|
|
|
{"open_source_license": "BSD"},
|
|
|
|
{"open_source_license": "GPLv3"},
|
|
|
|
{"open_source_license": "Apache Software License 2.0"},
|
|
|
|
{"open_source_license": "Not open source"},
|
|
|
|
{"windows": "y"},
|
|
|
|
{"windows": "n"},
|
|
|
|
{"use_pycharm": "y"},
|
|
|
|
{"use_pycharm": "n"},
|
|
|
|
{"use_docker": "y"},
|
|
|
|
{"use_docker": "n"},
|
|
|
|
{"postgresql_version": "11.3"},
|
|
|
|
{"postgresql_version": "10.8"},
|
|
|
|
{"postgresql_version": "9.6"},
|
|
|
|
{"postgresql_version": "9.5"},
|
|
|
|
{"postgresql_version": "9.4"},
|
|
|
|
{"cloud_provider": "AWS", "use_whitenoise": "y"},
|
|
|
|
{"cloud_provider": "AWS", "use_whitenoise": "n"},
|
|
|
|
{"cloud_provider": "GCP", "use_whitenoise": "y"},
|
|
|
|
{"cloud_provider": "GCP", "use_whitenoise": "n"},
|
|
|
|
{"cloud_provider": "None", "use_whitenoise": "y"},
|
|
|
|
# Note: cloud_provider=None AND use_whitenoise=n is not supported
|
|
|
|
{"use_drf": "y"},
|
|
|
|
{"use_drf": "n"},
|
|
|
|
{"js_task_runner": "None"},
|
|
|
|
{"js_task_runner": "Gulp"},
|
|
|
|
{"custom_bootstrap_compilation": "y"},
|
|
|
|
{"custom_bootstrap_compilation": "n"},
|
|
|
|
{"use_compressor": "y"},
|
|
|
|
{"use_compressor": "n"},
|
|
|
|
{"use_celery": "y"},
|
|
|
|
{"use_celery": "n"},
|
|
|
|
{"use_mailhog": "y"},
|
|
|
|
{"use_mailhog": "n"},
|
|
|
|
{"use_sentry": "y"},
|
|
|
|
{"use_sentry": "n"},
|
|
|
|
{"use_whitenoise": "y"},
|
|
|
|
{"use_whitenoise": "n"},
|
|
|
|
{"use_heroku": "y"},
|
|
|
|
{"use_heroku": "n"},
|
|
|
|
{"ci_tool": "None"},
|
|
|
|
{"ci_tool": "Travis"},
|
|
|
|
{"ci_tool": "Gitlab"},
|
|
|
|
{"keep_local_envs_in_vcs": "y"},
|
|
|
|
{"keep_local_envs_in_vcs": "n"},
|
|
|
|
{"debug": "y"},
|
|
|
|
{"debug": "n"},
|
|
|
|
]
|
|
|
|
|
|
|
|
UNSUPPORTED_COMBINATIONS = [{"cloud_provider": "None", "use_whitenoise": "n"}]
|
|
|
|
|
|
|
|
|
|
|
|
def _fixture_id(ctx):
|
|
|
|
"""Helper to get a user friendly test name from the parametrized context."""
|
|
|
|
return "-".join(f"{key}:{value}" for key, value in ctx.items())
|
2019-04-01 10:47:46 +03:00
|
|
|
|
|
|
|
|
2015-11-09 01:46:38 +03:00
|
|
|
def build_files_list(root_dir):
|
|
|
|
"""Build a list containing absolute paths to the generated files."""
|
|
|
|
return [
|
|
|
|
os.path.join(dirpath, file_path)
|
|
|
|
for dirpath, subdirs, files in os.walk(root_dir)
|
|
|
|
for file_path in files
|
|
|
|
]
|
|
|
|
|
2015-11-09 02:02:07 +03:00
|
|
|
|
2015-11-09 01:46:38 +03:00
|
|
|
def check_paths(paths):
|
2020-02-29 20:15:35 +03:00
|
|
|
"""Method to check all paths have correct substitutions."""
|
2015-11-09 01:46:38 +03:00
|
|
|
# Assert that no match is found in any of the files
|
|
|
|
for path in paths:
|
|
|
|
if is_binary(path):
|
|
|
|
continue
|
2018-04-09 01:03:29 +03:00
|
|
|
|
|
|
|
for line in open(path, "r"):
|
2015-11-09 01:46:38 +03:00
|
|
|
match = RE_OBJ.search(line)
|
2018-04-09 01:03:29 +03:00
|
|
|
msg = "cookiecutter variable not replaced in {}"
|
2015-11-09 01:46:38 +03:00
|
|
|
assert match is None, msg.format(path)
|
|
|
|
|
|
|
|
|
2020-02-29 20:15:35 +03:00
|
|
|
@pytest.mark.parametrize("context_override", SUPPORTED_COMBINATIONS, ids=_fixture_id)
|
|
|
|
def test_project_generation(cookies, context, context_override):
|
|
|
|
"""Test that project is generated and fully rendered."""
|
|
|
|
result = cookies.bake(extra_context={**context, **context_override})
|
2015-11-09 01:05:32 +03:00
|
|
|
assert result.exit_code == 0
|
2015-11-09 01:46:38 +03:00
|
|
|
assert result.exception is None
|
2018-04-09 01:03:29 +03:00
|
|
|
assert result.project.basename == context["project_slug"]
|
2015-11-09 01:46:38 +03:00
|
|
|
assert result.project.isdir()
|
|
|
|
|
|
|
|
paths = build_files_list(str(result.project))
|
|
|
|
assert paths
|
|
|
|
check_paths(paths)
|
2015-11-09 01:59:10 +03:00
|
|
|
|
|
|
|
|
2020-02-29 20:15:35 +03:00
|
|
|
@pytest.mark.parametrize("context_override", SUPPORTED_COMBINATIONS, ids=_fixture_id)
|
|
|
|
def test_flake8_passes(cookies, context_override):
|
|
|
|
"""Generated project should pass flake8."""
|
|
|
|
result = cookies.bake(extra_context=context_override)
|
2015-11-09 02:02:07 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
sh.flake8(str(result.project))
|
|
|
|
except sh.ErrorReturnCode as e:
|
2020-03-03 22:37:27 +03:00
|
|
|
pytest.fail(e.stdout.decode())
|
Add missing `script` key to Travis CI config (#1950)
* Add failing test for travis.yml
I see three options to test travis.yml :
1. Testing that the YAML contains relevant value. Least useful and least
reliable, but simplest to implement.
2. Testing that the YAML is valid TravisCI YAML. Unfortunately this is
difficult / impossible. Doing 'travis lint' would succeed, this command
does not check for 'script' key presence and wouldn't be useful for us.
We could use 'travis-build' to verify that the YAML can be converted to
a worker config, but as of now 'travis-build' doesn't work out of the
box.
There is a new tool for validating travis YAML files 'travis-yml', but
as of now it's a ruby-only library and it's still a work in progress.
3. Running Travis CI task based on the generated YAML. This seems the
best approach, however since cookiecutter-django itself uses Travis CI,
that would require running Travis CI from within Travis CI.
Scheduling Travis CI job without a github push still requires a public
github repo, which is something that we can't generate on demand.
Given that I'm opting to use approach 1.
* Adds missing config to generated .travis.yml
The keys added are as follows:
1. 'script'
Required by Travis, cookiecutter-django used to provide it until it has
been removed together with hitch.
I'm assuming hitch has been replaced with pytest, I'm setting pytest as
the new value for the 'script' key.
2. 'install'
Not required by Travis, but necessary in our case; installs test
libraries, mostly pytest.
As of now this points to 'local.txt' requirements file. There used to be
a separate 'test.txt' requirements file but it has been decided to merge
it with 'local.txt', see discussion in
https://github.com/pydanny/cookiecutter-django/pull/1557 .
* Update CONTRIBUTORS.rst
2019-03-06 04:10:45 +03:00
|
|
|
|
2019-05-15 14:37:17 +03:00
|
|
|
|
2020-02-29 20:15:35 +03:00
|
|
|
@pytest.mark.parametrize("context_override", SUPPORTED_COMBINATIONS, ids=_fixture_id)
|
|
|
|
def test_black_passes(cookies, context_override):
|
|
|
|
"""Generated project should pass black."""
|
|
|
|
result = cookies.bake(extra_context=context_override)
|
2019-05-15 14:37:17 +03:00
|
|
|
|
2019-04-01 10:47:46 +03:00
|
|
|
try:
|
|
|
|
sh.black("--check", "--diff", "--exclude", "migrations", f"{result.project}/")
|
|
|
|
except sh.ErrorReturnCode as e:
|
2020-03-03 22:37:27 +03:00
|
|
|
pytest.fail(e.stdout.decode())
|
2019-04-01 10:47:46 +03:00
|
|
|
|
Add missing `script` key to Travis CI config (#1950)
* Add failing test for travis.yml
I see three options to test travis.yml :
1. Testing that the YAML contains relevant value. Least useful and least
reliable, but simplest to implement.
2. Testing that the YAML is valid TravisCI YAML. Unfortunately this is
difficult / impossible. Doing 'travis lint' would succeed, this command
does not check for 'script' key presence and wouldn't be useful for us.
We could use 'travis-build' to verify that the YAML can be converted to
a worker config, but as of now 'travis-build' doesn't work out of the
box.
There is a new tool for validating travis YAML files 'travis-yml', but
as of now it's a ruby-only library and it's still a work in progress.
3. Running Travis CI task based on the generated YAML. This seems the
best approach, however since cookiecutter-django itself uses Travis CI,
that would require running Travis CI from within Travis CI.
Scheduling Travis CI job without a github push still requires a public
github repo, which is something that we can't generate on demand.
Given that I'm opting to use approach 1.
* Adds missing config to generated .travis.yml
The keys added are as follows:
1. 'script'
Required by Travis, cookiecutter-django used to provide it until it has
been removed together with hitch.
I'm assuming hitch has been replaced with pytest, I'm setting pytest as
the new value for the 'script' key.
2. 'install'
Not required by Travis, but necessary in our case; installs test
libraries, mostly pytest.
As of now this points to 'local.txt' requirements file. There used to be
a separate 'test.txt' requirements file but it has been decided to merge
it with 'local.txt', see discussion in
https://github.com/pydanny/cookiecutter-django/pull/1557 .
* Update CONTRIBUTORS.rst
2019-03-06 04:10:45 +03:00
|
|
|
|
|
|
|
def test_travis_invokes_pytest(cookies, context):
|
2019-12-06 10:55:00 +03:00
|
|
|
context.update({"ci_tool": "Travis"})
|
Add missing `script` key to Travis CI config (#1950)
* Add failing test for travis.yml
I see three options to test travis.yml :
1. Testing that the YAML contains relevant value. Least useful and least
reliable, but simplest to implement.
2. Testing that the YAML is valid TravisCI YAML. Unfortunately this is
difficult / impossible. Doing 'travis lint' would succeed, this command
does not check for 'script' key presence and wouldn't be useful for us.
We could use 'travis-build' to verify that the YAML can be converted to
a worker config, but as of now 'travis-build' doesn't work out of the
box.
There is a new tool for validating travis YAML files 'travis-yml', but
as of now it's a ruby-only library and it's still a work in progress.
3. Running Travis CI task based on the generated YAML. This seems the
best approach, however since cookiecutter-django itself uses Travis CI,
that would require running Travis CI from within Travis CI.
Scheduling Travis CI job without a github push still requires a public
github repo, which is something that we can't generate on demand.
Given that I'm opting to use approach 1.
* Adds missing config to generated .travis.yml
The keys added are as follows:
1. 'script'
Required by Travis, cookiecutter-django used to provide it until it has
been removed together with hitch.
I'm assuming hitch has been replaced with pytest, I'm setting pytest as
the new value for the 'script' key.
2. 'install'
Not required by Travis, but necessary in our case; installs test
libraries, mostly pytest.
As of now this points to 'local.txt' requirements file. There used to be
a separate 'test.txt' requirements file but it has been decided to merge
it with 'local.txt', see discussion in
https://github.com/pydanny/cookiecutter-django/pull/1557 .
* Update CONTRIBUTORS.rst
2019-03-06 04:10:45 +03:00
|
|
|
result = cookies.bake(extra_context=context)
|
|
|
|
|
|
|
|
assert result.exit_code == 0
|
|
|
|
assert result.exception is None
|
|
|
|
assert result.project.basename == context["project_slug"]
|
|
|
|
assert result.project.isdir()
|
|
|
|
|
2019-03-18 20:49:43 +03:00
|
|
|
with open(f"{result.project}/.travis.yml", "r") as travis_yml:
|
Add missing `script` key to Travis CI config (#1950)
* Add failing test for travis.yml
I see three options to test travis.yml :
1. Testing that the YAML contains relevant value. Least useful and least
reliable, but simplest to implement.
2. Testing that the YAML is valid TravisCI YAML. Unfortunately this is
difficult / impossible. Doing 'travis lint' would succeed, this command
does not check for 'script' key presence and wouldn't be useful for us.
We could use 'travis-build' to verify that the YAML can be converted to
a worker config, but as of now 'travis-build' doesn't work out of the
box.
There is a new tool for validating travis YAML files 'travis-yml', but
as of now it's a ruby-only library and it's still a work in progress.
3. Running Travis CI task based on the generated YAML. This seems the
best approach, however since cookiecutter-django itself uses Travis CI,
that would require running Travis CI from within Travis CI.
Scheduling Travis CI job without a github push still requires a public
github repo, which is something that we can't generate on demand.
Given that I'm opting to use approach 1.
* Adds missing config to generated .travis.yml
The keys added are as follows:
1. 'script'
Required by Travis, cookiecutter-django used to provide it until it has
been removed together with hitch.
I'm assuming hitch has been replaced with pytest, I'm setting pytest as
the new value for the 'script' key.
2. 'install'
Not required by Travis, but necessary in our case; installs test
libraries, mostly pytest.
As of now this points to 'local.txt' requirements file. There used to be
a separate 'test.txt' requirements file but it has been decided to merge
it with 'local.txt', see discussion in
https://github.com/pydanny/cookiecutter-django/pull/1557 .
* Update CONTRIBUTORS.rst
2019-03-06 04:10:45 +03:00
|
|
|
try:
|
2019-03-18 20:49:43 +03:00
|
|
|
assert yaml.load(travis_yml)["script"] == ["pytest"]
|
Add missing `script` key to Travis CI config (#1950)
* Add failing test for travis.yml
I see three options to test travis.yml :
1. Testing that the YAML contains relevant value. Least useful and least
reliable, but simplest to implement.
2. Testing that the YAML is valid TravisCI YAML. Unfortunately this is
difficult / impossible. Doing 'travis lint' would succeed, this command
does not check for 'script' key presence and wouldn't be useful for us.
We could use 'travis-build' to verify that the YAML can be converted to
a worker config, but as of now 'travis-build' doesn't work out of the
box.
There is a new tool for validating travis YAML files 'travis-yml', but
as of now it's a ruby-only library and it's still a work in progress.
3. Running Travis CI task based on the generated YAML. This seems the
best approach, however since cookiecutter-django itself uses Travis CI,
that would require running Travis CI from within Travis CI.
Scheduling Travis CI job without a github push still requires a public
github repo, which is something that we can't generate on demand.
Given that I'm opting to use approach 1.
* Adds missing config to generated .travis.yml
The keys added are as follows:
1. 'script'
Required by Travis, cookiecutter-django used to provide it until it has
been removed together with hitch.
I'm assuming hitch has been replaced with pytest, I'm setting pytest as
the new value for the 'script' key.
2. 'install'
Not required by Travis, but necessary in our case; installs test
libraries, mostly pytest.
As of now this points to 'local.txt' requirements file. There used to be
a separate 'test.txt' requirements file but it has been decided to merge
it with 'local.txt', see discussion in
https://github.com/pydanny/cookiecutter-django/pull/1557 .
* Update CONTRIBUTORS.rst
2019-03-06 04:10:45 +03:00
|
|
|
except yaml.YAMLError as e:
|
|
|
|
pytest.fail(e)
|
2019-06-05 21:00:43 +03:00
|
|
|
|
|
|
|
|
2019-12-06 10:55:00 +03:00
|
|
|
def test_gitlab_invokes_flake8_and_pytest(cookies, context):
|
|
|
|
context.update({"ci_tool": "Gitlab"})
|
|
|
|
result = cookies.bake(extra_context=context)
|
|
|
|
|
|
|
|
assert result.exit_code == 0
|
|
|
|
assert result.exception is None
|
|
|
|
assert result.project.basename == context["project_slug"]
|
|
|
|
assert result.project.isdir()
|
|
|
|
|
|
|
|
with open(f"{result.project}/.gitlab-ci.yml", "r") as gitlab_yml:
|
|
|
|
try:
|
|
|
|
gitlab_config = yaml.load(gitlab_yml)
|
|
|
|
assert gitlab_config["flake8"]["script"] == ["flake8"]
|
|
|
|
assert gitlab_config["pytest"]["script"] == ["pytest"]
|
|
|
|
except yaml.YAMLError as e:
|
|
|
|
pytest.fail(e)
|
|
|
|
|
|
|
|
|
2019-06-05 21:00:43 +03:00
|
|
|
@pytest.mark.parametrize("slug", ["project slug", "Project_Slug"])
|
|
|
|
def test_invalid_slug(cookies, context, slug):
|
|
|
|
"""Invalid slug should failed pre-generation hook."""
|
|
|
|
context.update({"project_slug": slug})
|
|
|
|
|
|
|
|
result = cookies.bake(extra_context=context)
|
|
|
|
|
|
|
|
assert result.exit_code != 0
|
|
|
|
assert isinstance(result.exception, FailedHookException)
|
2019-10-02 18:03:33 +03:00
|
|
|
|
|
|
|
|
2020-02-29 20:15:35 +03:00
|
|
|
@pytest.mark.parametrize("invalid_context", UNSUPPORTED_COMBINATIONS)
|
|
|
|
def test_error_if_incompatible(cookies, context, invalid_context):
|
|
|
|
"""It should not generate project an incompatible combination is selected."""
|
|
|
|
context.update(invalid_context)
|
2019-10-02 18:03:33 +03:00
|
|
|
result = cookies.bake(extra_context=context)
|
|
|
|
|
2019-10-03 17:54:15 +03:00
|
|
|
assert result.exit_code != 0
|
|
|
|
assert isinstance(result.exception, FailedHookException)
|