Skip to content

Commit

Permalink
added basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Schoch authored and Tobias Schoch committed May 4, 2018
1 parent d25f5bf commit 61f174d
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 84 deletions.
108 changes: 107 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,107 @@
.idea
.idea
### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/

76 changes: 0 additions & 76 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,76 +0,0 @@
# -*- coding: utf-8 -*-
import os
import re
import shutil


def fix_template_expansion(content, replacements):
"""
fix template expansion after hook copy
:param content: the duplicated hook content
:param replacements: array of dictionaries
cookiecutter_key => template key expanded
"""
for replacement in replacements:
for key, to_be_replaced in replacement.items():
replacement = chr(123) + chr(123) + \
'cookiecutter.' + key + chr(125) + chr(125)
content = content.replace(to_be_replaced, replacement)
return content


def get_file_content(file):
"""
get the content of a given file
:param file: the file to get the content of
"""
return open(file, 'r').read()


def set_file_content(file, content):
"""
write content to file
:param file: the file to rewrite its content
:param content: content to write to the given file
"""
return open(file, 'w').write(content)

# format title of the generated readme file
readme = os.getcwd() + '/README.md'
if (os.path.exists(readme)):
title_underliner = ''.center(len('{{cookiecutter.project_name}}'), '=')
set_file_content(
readme,
re.sub(
r'^=+$', title_underliner, get_file_content(readme), 1, flags=re.M
)
)
# end format title of the generated readme file

# issue #3 - copy post hook to project directory
if (re.match(r'YES', '{{cookiecutter.copy_hooks}}', re.I)):
if (re.match(r'^cookiecutter\-', '{{cookiecutter.project_slug}}')):
hooksdir = os.getcwd() + '/hooks'
posthook = hooksdir + '/post_gen_project.py'
source = os.path.realpath(__file__)
replacements = [
{'project_slug': '{{cookiecutter.project_slug}}'},
{'copy_hooks': '{{cookiecutter.copy_hooks}}'},

# project_name must be set after project_slug to prevent side
# effects
{'project_name': '{{cookiecutter.project_name}}'}
]

if (not os.path.exists(hooksdir)):
os.makedirs(hooksdir)
shutil.copyfile(source, posthook)

set_file_content(
posthook,
fix_template_expansion(
get_file_content(posthook), replacements
) + "\n"
)
# end issue #3

1 change: 0 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
cookiecutter==1.6.0
flake8==3.5.0
pytest==3.3.2
pytest-cookies==0.3.0
55 changes: 49 additions & 6 deletions tests/test_bake_project.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
from contextlib import contextmanager

import subprocess
import os

CCDS_ROOT = os.path.abspath(
os.path.join(
__file__,
os.pardir,
os.pardir
)
)


def read(filename):
with open(filename, 'r') as fp:
s = fp.read()
return s

@contextmanager
def inside_dir(dirpath):
Expand All @@ -18,13 +31,43 @@ def inside_dir(dirpath):


def test_project_tree(cookies):
result = cookies.bake(extra_context={'project_slug': 'test_project'})
result = cookies.bake(template=CCDS_ROOT, extra_context={'project_slug': 'test_project'})
assert result.exit_code == 0
assert result.exception is None
assert result.project.basename == 'test_project'
assert read(result.project.join('README.md'))=="""Python Package
===============================
author: Your name
Overview
--------
A python package that can be installed with pip.
Change-Log
----------
##### 0.0.1
* initial version
Installation / Usage
--------------------
clone the repo:
```
git clone <git-url>
```
build the docker image
```
docker build . -t test_project
```
Example
-------
def test_run_flake8(cookies):
result = cookies.bake(extra_context={'project_slug': 'flake8_compat'})
with inside_dir(str(result.project)):
assert os.subprocess.check_call(['flake8']) == 0
run a container of the image
```
docker run -v ... -p ... test_project
```"""
assert os.path.exists(result.project.join("Dockerfile"))

0 comments on commit 61f174d

Please sign in to comment.