Skip to content

Commit d40ffb4

Browse files
dmitrypoloisms
authored andcommitted
Expand Setup Tests (drivendataorg#131)
* change tests to use class scope and test user inputs * name was backwards * name was backwards * compat for mac and linux
1 parent a2798e8 commit d40ffb4

File tree

3 files changed

+141
-82
lines changed

3 files changed

+141
-82
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ docs/site/
66
# test cache
77
.cache/*
88
tests/__pycache__/*
9+
*.pytest_cache/

tests/conftest.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sys
2+
import pytest
3+
import shutil
4+
from pathlib import Path
5+
from cookiecutter import main
6+
7+
CCDS_ROOT = Path(__file__).parents[1].resolve()
8+
9+
args = {
10+
'project_name': 'DrivenData',
11+
'author_name': 'DrivenData',
12+
'open_source_license': 'BSD-3-Clause',
13+
'python_interpreter': 'python'
14+
}
15+
16+
17+
def system_check(basename):
18+
platform = sys.platform
19+
if 'linux' in platform:
20+
basename = basename.lower()
21+
return basename
22+
23+
24+
@pytest.fixture(scope='class', params=[{}, args])
25+
def default_baked_project(tmpdir_factory, request):
26+
temp = tmpdir_factory.mktemp('data-project')
27+
out_dir = Path(temp).resolve()
28+
29+
pytest.param = request.param
30+
main.cookiecutter(
31+
str(CCDS_ROOT),
32+
no_input=True,
33+
extra_context=pytest.param,
34+
output_dir=out_dir
35+
)
36+
37+
pn = pytest.param.get('project_name') or 'project_name'
38+
39+
# project name gets converted to lower case on Linux but not Mac
40+
pn = system_check(pn)
41+
42+
proj = out_dir / pn
43+
request.cls.path = proj
44+
yield
45+
46+
# cleanup after
47+
shutil.rmtree(out_dir)

tests/test_creation.py

Lines changed: 93 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,7 @@
11
import os
22
import pytest
3-
import shutil
4-
from pathlib import Path
5-
6-
from cookiecutter import main
7-
8-
CCDS_ROOT = Path(__file__).parents[1].resolve()
9-
10-
11-
@pytest.fixture(scope='function')
12-
def default_baked_project(tmpdir):
13-
temp = tmpdir.mkdir('data-project')
14-
out_dir = Path(temp).resolve()
15-
16-
main.cookiecutter(
17-
str(CCDS_ROOT),
18-
no_input=True,
19-
extra_context={},
20-
output_dir=out_dir
21-
)
22-
23-
# default project name is project_name
24-
yield out_dir / 'project_name'
25-
26-
# cleanup after
27-
shutil.rmtree(out_dir)
28-
29-
30-
def test_readme(default_baked_project):
31-
readme_path = default_baked_project / 'README.md'
32-
33-
assert readme_path.exists()
34-
assert no_curlies(readme_path)
35-
36-
37-
def test_license(default_baked_project):
38-
license_path = default_baked_project / 'LICENSE'
39-
40-
assert license_path.exists()
41-
assert no_curlies(license_path)
42-
43-
44-
def test_requirements(default_baked_project):
45-
reqs_path = default_baked_project / 'requirements.txt'
46-
47-
assert reqs_path.exists()
48-
assert no_curlies(reqs_path)
49-
50-
51-
def test_makefile(default_baked_project):
52-
makefile_path = default_baked_project / 'Makefile'
53-
54-
assert makefile_path.exists()
55-
assert no_curlies(makefile_path)
56-
57-
58-
def test_folders(default_baked_project):
59-
expected_dirs = [
60-
'data',
61-
'data/external',
62-
'data/interim',
63-
'data/processed',
64-
'data/raw',
65-
'docs',
66-
'models',
67-
'notebooks',
68-
'references',
69-
'reports',
70-
'reports/figures',
71-
'src',
72-
'src/data',
73-
'src/features',
74-
'src/models',
75-
'src/visualization',
76-
]
77-
78-
ignored_dirs = [
79-
str(default_baked_project)
80-
]
81-
82-
abs_expected_dirs = [str(default_baked_project / d) for d in expected_dirs]
83-
abs_dirs, _, _ = list(zip(*os.walk(default_baked_project)))
84-
assert len(set(abs_expected_dirs + ignored_dirs) - set(abs_dirs)) == 0
3+
from subprocess import check_output
4+
from conftest import system_check
855

866

877
def no_curlies(filepath):
@@ -100,3 +20,94 @@ def no_curlies(filepath):
10020

10121
template_strings_in_file = [s in data for s in template_strings]
10222
return not any(template_strings_in_file)
23+
24+
25+
@pytest.mark.usefixtures("default_baked_project")
26+
class TestCookieSetup(object):
27+
def test_project_name(self):
28+
project = self.path
29+
if pytest.param.get('project_name'):
30+
name = system_check('DrivenData')
31+
assert project.name == name
32+
else:
33+
assert project.name == 'project_name'
34+
35+
def test_author(self):
36+
setup_ = self.path / 'setup.py'
37+
args = ['python', setup_, '--author']
38+
p = check_output(args).decode('ascii').strip()
39+
if pytest.param.get('author_name'):
40+
assert p == 'DrivenData'
41+
else:
42+
assert p == 'Your name (or your organization/company/team)'
43+
44+
def test_readme(self):
45+
readme_path = self.path / 'README.md'
46+
assert readme_path.exists()
47+
assert no_curlies(readme_path)
48+
if pytest.param.get('project_name'):
49+
with open(readme_path) as fin:
50+
assert 'DrivenData' == next(fin).strip()
51+
52+
def test_setup(self):
53+
setup_ = self.path / 'setup.py'
54+
args = ['python', setup_, '--version']
55+
p = check_output(args).decode('ascii').strip()
56+
assert p == '0.1.0'
57+
58+
def test_license(self):
59+
license_path = self.path / 'LICENSE'
60+
assert license_path.exists()
61+
assert no_curlies(license_path)
62+
63+
def test_license_type(self):
64+
setup_ = self.path / 'setup.py'
65+
args = ['python', setup_, '--license']
66+
p = check_output(args).decode('ascii').strip()
67+
if pytest.param.get('open_source_license'):
68+
assert p == 'BSD-3'
69+
else:
70+
assert p == 'MIT'
71+
72+
def test_requirements(self):
73+
reqs_path = self.path / 'requirements.txt'
74+
assert reqs_path.exists()
75+
assert no_curlies(reqs_path)
76+
if pytest.param.get('python_interpreter'):
77+
with open(reqs_path) as fin:
78+
lines = list(map(lambda x: x.strip(), fin.readlines()))
79+
assert 'pathlib2' in lines
80+
81+
def test_makefile(self):
82+
makefile_path = self.path / 'Makefile'
83+
assert makefile_path.exists()
84+
assert no_curlies(makefile_path)
85+
86+
def test_folders(self):
87+
expected_dirs = [
88+
'data',
89+
'data/external',
90+
'data/interim',
91+
'data/processed',
92+
'data/raw',
93+
'docs',
94+
'models',
95+
'notebooks',
96+
'references',
97+
'reports',
98+
'reports/figures',
99+
'src',
100+
'src/data',
101+
'src/features',
102+
'src/models',
103+
'src/visualization',
104+
]
105+
106+
ignored_dirs = [
107+
str(self.path)
108+
]
109+
110+
abs_expected_dirs = [str(self.path / d) for d in expected_dirs]
111+
abs_dirs, _, _ = list(zip(*os.walk(self.path)))
112+
assert len(set(abs_expected_dirs + ignored_dirs) - set(abs_dirs)) == 0
113+

0 commit comments

Comments
 (0)