From 43d33dc025d754e361a469fff022330e0c006c41 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Mon, 20 Apr 2015 17:49:12 +0530 Subject: [PATCH 1/4] Add DjangoCookieTestCase --- .gitignore | 1 + .travis.yml | 6 +-- Makefile | 2 +- dev-requirements.txt | 3 -- requirements.txt | 13 +++++++ setup.cfg | 3 ++ tests/base.py | 51 +++++++++++++++++++++++++ tests/test_cookiecutter_substitution.py | 32 ++++++---------- 8 files changed, 84 insertions(+), 27 deletions(-) delete mode 100644 dev-requirements.txt create mode 100644 requirements.txt create mode 100644 setup.cfg create mode 100644 tests/base.py diff --git a/.gitignore b/.gitignore index 0aae0c9c1..b9a2bb82f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ local_settings.py repo_name .idea +my_test_project/* diff --git a/.travis.yml b/.travis.yml index a0dc7921b..2c6d2348f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ before_install: - time pip install pep8 install: -- pip install -r dev-requirements.txt +- pip install -r requirements.txt script: - pep8 --ignore E201,E202 --max-line-length=120 --exclude='migrations' . @@ -12,5 +12,5 @@ script: notifications: email: - on_success: never - on_failure: never + on_success: change + on_failure: always diff --git a/Makefile b/Makefile index ddb3a8032..3b126041e 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ .PHONY: test test: - py.test -q tests/*.py + py.test tests/*.py diff --git a/dev-requirements.txt b/dev-requirements.txt deleted file mode 100644 index fbcc80e16..000000000 --- a/dev-requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -cookiecutter -pep8 -pytest diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000..b9e49a29e --- /dev/null +++ b/requirements.txt @@ -0,0 +1,13 @@ +cookiecutter==1.0.0 +flake8==2.4.0 +sh + +# Debugging +# ------------------------------------- +ipdb==0.8 +ipython==3.1.0 + +# Testing +# ------------------------------------- +pytest==2.7.0 +git+git://github.com/mverteuil/pytest-ipdb.git diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 000000000..1133f7a33 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,3 @@ +[pytest] +python_paths = . +norecursedirs = .tox .git */migrations/* */static/* docs venv */{{cookiecutter.repo_name}}/* diff --git a/tests/base.py b/tests/base.py new file mode 100644 index 000000000..f6ba90fb0 --- /dev/null +++ b/tests/base.py @@ -0,0 +1,51 @@ +import os +import shutil +import unittest +from os.path import exists, dirname, join + +import sh + +from cookiecutter.main import cookiecutter + + +class DjangoCookieTestCase(unittest.TestCase): + + root_dir = dirname(dirname(__file__)) + ctx = {} + destpath = None + + def generate_project(self, extra_context=None): + ctx = { + "project_name": "My Test Project", + "repo_name": "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", + "now": "2015/01/13", + "year": "2015" + } + if extra_context: + assert isinstance(extra_context, dict) + ctx.update(extra_context) + + self.ctx = ctx + self.destpath = join(self.root_dir, self.ctx['repo_name']) + + cookiecutter(template='./', checkout=None, no_input=True, extra_context=ctx) + + # Build a list containing absolute paths to the generated files + paths = [os.path.join(dirpath, file_path) + for dirpath, subdirs, files in os.walk(self.destpath) + for file_path in files] + return paths + + def clean(self): + if exists(self.destpath): + shutil.rmtree(self.destpath) + sh.cd(self.root_dir) + + def tearDown(self): + self.clean() diff --git a/tests/test_cookiecutter_substitution.py b/tests/test_cookiecutter_substitution.py index 4cef245c7..dd09daa80 100644 --- a/tests/test_cookiecutter_substitution.py +++ b/tests/test_cookiecutter_substitution.py @@ -1,28 +1,16 @@ -import os import re -import shutil -import unittest -from os.path import dirname, exists, join -from cookiecutter.main import cookiecutter +import sh + +from .base import DjangoCookieTestCase -class TestCookiecutterSubstitution(unittest.TestCase): +class TestCookiecutterSubstitution(DjangoCookieTestCase): """Test that all cookiecutter instances are substituted""" - cookiecutter(dirname(dirname(__file__)), no_input=True) - - destpath = join(dirname(dirname(__file__)), 'project_name') - - def tearDown(self): - if exists(self.destpath): - shutil.rmtree(self.destpath) - def test_all_cookiecutter_instances_are_substituted(self): # Build a list containing absolute paths to the generated files - paths = [os.path.join(dirpath, file_path) - for dirpath, subdirs, files in os.walk(self.destpath) - for file_path in files] + paths = self.generate_project() # Construct the cookiecutter search pattern pattern = "{{(\s?cookiecutter)[.](.*?)}}" @@ -36,6 +24,10 @@ class TestCookiecutterSubstitution(unittest.TestCase): match, "cookiecutter variable not replaced in {}".format(path)) - -if __name__ == '__main__': - unittest.main() + def test_flake8_complaince(self): + """generated project should pass flake8""" + self.generate_project() + try: + sh.flake8(self.destpath) + except sh.ErrorReturnCode as e: + raise AssertionError(e) From d5a76a85f0fb45d107db7316e8c448056db7d41d Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Mon, 20 Apr 2015 17:50:52 +0530 Subject: [PATCH 2/4] Use Docker based container on travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2c6d2348f..4ae3129b0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: python - +sudo: false before_install: - time pip install pep8 From 06475b932b4030fe76e844e4bce3a86714636e8f Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Mon, 20 Apr 2015 17:57:37 +0530 Subject: [PATCH 3/4] remove unncessary pep8 checking now we have it handled via pytest --- .travis.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4ae3129b0..41486605d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,14 +1,11 @@ language: python sudo: false -before_install: - - time pip install pep8 install: - pip install -r requirements.txt script: - - pep8 --ignore E201,E202 --max-line-length=120 --exclude='migrations' . - - make test + - py.test notifications: email: From ec8283dbe7ed90a6afa6f31288a4f74296580d37 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Mon, 20 Apr 2015 18:01:55 +0530 Subject: [PATCH 4/4] Make all the PEP8 failure pass --- .../{{cookiecutter.repo_name}}/config/local.py | 2 +- .../{{cookiecutter.repo_name}}/config/production.py | 2 +- .../{{cookiecutter.repo_name}}/users/models.py | 11 ++++------- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/local.py b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/local.py index 4059bc993..c0d4345d2 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/local.py +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/local.py @@ -7,7 +7,7 @@ Local settings - Use Django Debug Toolbar ''' -from .common import * +from .common import * # noqa # DEBUG # ------------------------------------------------------------------------------ diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/production.py b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/production.py index 3e03e7f21..f0d863d89 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/production.py +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/production.py @@ -12,7 +12,7 @@ from __future__ import absolute_import, unicode_literals from boto.s3.connection import OrdinaryCallingFormat -from .common import * +from .common import * # noqa # This ensures that Django will be able to detect a secure connection # properly on Heroku. diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/users/models.py b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/users/models.py index fab73f1de..8dc4e4862 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/users/models.py +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/users/models.py @@ -1,14 +1,11 @@ # -*- coding: utf-8 -*- -# Import the AbstractUser model +from __future__ import unicode_literals, absolute_import + from django.contrib.auth.models import AbstractUser - -# Import the basic Django ORM models library -from django.db import models - -from django.utils.translation import ugettext_lazy as _ +# from django.db import models +# from django.utils.translation import ugettext_lazy as _ -# Subclass AbstractUser class User(AbstractUser): def __unicode__(self):