From f500435c769407bc823305b9efbaa527a72ae39e Mon Sep 17 00:00:00 2001 From: bartek Date: Wed, 6 Mar 2019 00:12:19 +0100 Subject: [PATCH] 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. --- requirements.txt | 1 + tests/test_cookiecutter_generation.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/requirements.txt b/requirements.txt index dabb5dd91..37a96913c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,3 +11,4 @@ flake8==3.7.6 tox==3.6.1 pytest==4.3.0 pytest-cookies==0.3.0 +pyyaml==3.13 diff --git a/tests/test_cookiecutter_generation.py b/tests/test_cookiecutter_generation.py index cf173576b..b2c235a82 100755 --- a/tests/test_cookiecutter_generation.py +++ b/tests/test_cookiecutter_generation.py @@ -1,6 +1,7 @@ import os import re import sh +import yaml import pytest from binaryornot.check import is_binary @@ -85,3 +86,19 @@ def test_flake8_compliance(cookies): sh.flake8(str(result.project)) except sh.ErrorReturnCode as e: pytest.fail(e) + + +def test_travis_invokes_pytest(cookies, context): + context.update({"use_travisci": "y"}) + 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}/.travis.yml', 'r') as travis_yml: + try: + assert yaml.load(travis_yml)['script'] == ['pytest'] + except yaml.YAMLError as e: + pytest.fail(e)