diff --git a/cookiecutter.json b/cookiecutter.json index d6d217ca4..6d030be0c 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -40,7 +40,11 @@ "use_sentry": "n", "use_whitenoise": "n", "use_heroku": "n", - "use_travisci": "n", + "ci_tool": [ + "None", + "Travis", + "Gitlab" + ], "keep_local_envs_in_vcs": "y", "debug": "n" diff --git a/docs/project-generation-options.rst b/docs/project-generation-options.rst index afa9b8aff..ae47b0972 100644 --- a/docs/project-generation-options.rst +++ b/docs/project-generation-options.rst @@ -94,8 +94,12 @@ use_heroku: Indicates whether the project should be configured so as to be deployable to Heroku_. -use_travisci: - Indicates whether the project should be configured to use `Travis CI`_. +ci_tool: + Select a CI tool for running tests. The choices are: + + 1. None + 2. Travis_ + 3. Gitlab_ keep_local_envs_in_vcs: Indicates whether the project's ``.envs/.local/`` should be kept in VCS @@ -138,3 +142,6 @@ debug: .. _Heroku: https://github.com/heroku/heroku-buildpack-python .. _Travis CI: https://travis-ci.org/ + +.. _GitLab CI: https://docs.gitlab.com/ee/ci/ + diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 0544f14bc..79c32f9b4 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -70,7 +70,7 @@ def remove_heroku_files(): for file_name in file_names: if ( file_name == "requirements.txt" - and "{{ cookiecutter.use_travisci }}".lower() == "y" + and "{{ cookiecutter.ci_tool }}".lower() == "travis" ): # don't remove the file if we are using travisci but not using heroku continue @@ -105,6 +105,10 @@ def remove_dottravisyml_file(): os.remove(".travis.yml") +def remove_dotgitlabciyml_file(): + os.remove(".gitlab-ci.yml") + + def append_to_project_gitignore(path): gitignore_file_path = ".gitignore" with open(gitignore_file_path, "a") as gitignore_file: @@ -349,9 +353,12 @@ def main(): if "{{ cookiecutter.use_docker }}".lower() == "y": remove_celery_compose_dirs() - if "{{ cookiecutter.use_travisci }}".lower() == "n": + if "{{ cookiecutter.ci_tool }}".lower() != "travis": remove_dottravisyml_file() + if "{{ cookiecutter.ci_tool }}".lower() != "gitlab": + remove_dotgitlabciyml_file() + print(SUCCESS + "Project initialized, keep up the good work!" + TERMINATOR) diff --git a/tests/test_cookiecutter_generation.py b/tests/test_cookiecutter_generation.py index f931bce06..8c2f71fe8 100755 --- a/tests/test_cookiecutter_generation.py +++ b/tests/test_cookiecutter_generation.py @@ -140,7 +140,7 @@ def test_black_passes(cookies, context_combination): def test_travis_invokes_pytest(cookies, context): - context.update({"use_travisci": "y"}) + context.update({"ci_tool": "Travis"}) result = cookies.bake(extra_context=context) assert result.exit_code == 0 @@ -155,6 +155,24 @@ def test_travis_invokes_pytest(cookies, context): pytest.fail(e) +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) + + @pytest.mark.parametrize("slug", ["project slug", "Project_Slug"]) def test_invalid_slug(cookies, context, slug): """Invalid slug should failed pre-generation hook.""" diff --git a/{{cookiecutter.project_slug}}/.gitlab-ci.yml b/{{cookiecutter.project_slug}}/.gitlab-ci.yml new file mode 100644 index 000000000..15ff73b10 --- /dev/null +++ b/{{cookiecutter.project_slug}}/.gitlab-ci.yml @@ -0,0 +1,33 @@ +stages: + - lint + - test + +variables: + POSTGRES_USER: '{{ cookiecutter.project_slug }}' + POSTGRES_PASSWORD: '' + POSTGRES_DB: 'test_{{ cookiecutter.project_slug }}' + +flake8: + stage: lint + image: python:3.7-alpine + before_script: + - pip install -q flake8 + script: + - flake8 + +pytest: + stage: test + image: python:3.7 + tags: + - docker + services: + - postgres:11 + variables: + DATABASE_URL: pgsql://$POSTGRES_USER:$POSTGRES_PASSWORD@postgres/$POSTGRES_DB + + before_script: + - pip install -r requirements/local.txt + + script: + - pytest +