Expand Setup Tests (#131)

* change tests to use class scope and test user inputs

* name was backwards

* name was backwards

* compat for mac and linux
This commit is contained in:
dmitrypolo 2018-07-28 10:31:14 -04:00 committed by Isaac Slavitt
parent a2798e84da
commit d40ffb462d
3 changed files with 141 additions and 82 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@ docs/site/
# test cache
.cache/*
tests/__pycache__/*
*.pytest_cache/

47
tests/conftest.py Normal file
View File

@ -0,0 +1,47 @@
import sys
import pytest
import shutil
from pathlib import Path
from cookiecutter import main
CCDS_ROOT = Path(__file__).parents[1].resolve()
args = {
'project_name': 'DrivenData',
'author_name': 'DrivenData',
'open_source_license': 'BSD-3-Clause',
'python_interpreter': 'python'
}
def system_check(basename):
platform = sys.platform
if 'linux' in platform:
basename = basename.lower()
return basename
@pytest.fixture(scope='class', params=[{}, args])
def default_baked_project(tmpdir_factory, request):
temp = tmpdir_factory.mktemp('data-project')
out_dir = Path(temp).resolve()
pytest.param = request.param
main.cookiecutter(
str(CCDS_ROOT),
no_input=True,
extra_context=pytest.param,
output_dir=out_dir
)
pn = pytest.param.get('project_name') or 'project_name'
# project name gets converted to lower case on Linux but not Mac
pn = system_check(pn)
proj = out_dir / pn
request.cls.path = proj
yield
# cleanup after
shutil.rmtree(out_dir)

View File

@ -1,61 +1,89 @@
import os
import pytest
import shutil
from pathlib import Path
from cookiecutter import main
CCDS_ROOT = Path(__file__).parents[1].resolve()
from subprocess import check_output
from conftest import system_check
@pytest.fixture(scope='function')
def default_baked_project(tmpdir):
temp = tmpdir.mkdir('data-project')
out_dir = Path(temp).resolve()
def no_curlies(filepath):
""" Utility to make sure no curly braces appear in a file.
That is, was jinja able to render everthing?
"""
with open(filepath, 'r') as f:
data = f.read()
main.cookiecutter(
str(CCDS_ROOT),
no_input=True,
extra_context={},
output_dir=out_dir
)
template_strings = [
'{{',
'}}',
'{%',
'%}'
]
# default project name is project_name
yield out_dir / 'project_name'
# cleanup after
shutil.rmtree(out_dir)
template_strings_in_file = [s in data for s in template_strings]
return not any(template_strings_in_file)
def test_readme(default_baked_project):
readme_path = default_baked_project / 'README.md'
@pytest.mark.usefixtures("default_baked_project")
class TestCookieSetup(object):
def test_project_name(self):
project = self.path
if pytest.param.get('project_name'):
name = system_check('DrivenData')
assert project.name == name
else:
assert project.name == 'project_name'
def test_author(self):
setup_ = self.path / 'setup.py'
args = ['python', setup_, '--author']
p = check_output(args).decode('ascii').strip()
if pytest.param.get('author_name'):
assert p == 'DrivenData'
else:
assert p == 'Your name (or your organization/company/team)'
def test_readme(self):
readme_path = self.path / 'README.md'
assert readme_path.exists()
assert no_curlies(readme_path)
if pytest.param.get('project_name'):
with open(readme_path) as fin:
assert 'DrivenData' == next(fin).strip()
def test_setup(self):
setup_ = self.path / 'setup.py'
args = ['python', setup_, '--version']
p = check_output(args).decode('ascii').strip()
assert p == '0.1.0'
def test_license(default_baked_project):
license_path = default_baked_project / 'LICENSE'
def test_license(self):
license_path = self.path / 'LICENSE'
assert license_path.exists()
assert no_curlies(license_path)
def test_license_type(self):
setup_ = self.path / 'setup.py'
args = ['python', setup_, '--license']
p = check_output(args).decode('ascii').strip()
if pytest.param.get('open_source_license'):
assert p == 'BSD-3'
else:
assert p == 'MIT'
def test_requirements(default_baked_project):
reqs_path = default_baked_project / 'requirements.txt'
def test_requirements(self):
reqs_path = self.path / 'requirements.txt'
assert reqs_path.exists()
assert no_curlies(reqs_path)
if pytest.param.get('python_interpreter'):
with open(reqs_path) as fin:
lines = list(map(lambda x: x.strip(), fin.readlines()))
assert 'pathlib2' in lines
def test_makefile(default_baked_project):
makefile_path = default_baked_project / 'Makefile'
def test_makefile(self):
makefile_path = self.path / 'Makefile'
assert makefile_path.exists()
assert no_curlies(makefile_path)
def test_folders(default_baked_project):
def test_folders(self):
expected_dirs = [
'data',
'data/external',
@ -76,27 +104,10 @@ def test_folders(default_baked_project):
]
ignored_dirs = [
str(default_baked_project)
str(self.path)
]
abs_expected_dirs = [str(default_baked_project / d) for d in expected_dirs]
abs_dirs, _, _ = list(zip(*os.walk(default_baked_project)))
abs_expected_dirs = [str(self.path / d) for d in expected_dirs]
abs_dirs, _, _ = list(zip(*os.walk(self.path)))
assert len(set(abs_expected_dirs + ignored_dirs) - set(abs_dirs)) == 0
def no_curlies(filepath):
""" Utility to make sure no curly braces appear in a file.
That is, was jinja able to render everthing?
"""
with open(filepath, 'r') as f:
data = f.read()
template_strings = [
'{{',
'}}',
'{%',
'%}'
]
template_strings_in_file = [s in data for s in template_strings]
return not any(template_strings_in_file)