Skip to content

Commit 66935df

Browse files
authored
Merge pull request #2 from clagraff/python-unittests
This fails CircleCI because of linting issues. Going to accept and merge, and address linting issues at a later time.
2 parents 155b3a1 + 0b29d15 commit 66935df

File tree

8 files changed

+219
-6
lines changed

8 files changed

+219
-6
lines changed

.circleci/config.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Python CircleCI 2.0 configuration file
2+
#
3+
# Check https://circleci.com/docs/2.0/language-python/ for more details
4+
#
5+
version: 2
6+
jobs:
7+
build:
8+
docker:
9+
# specify the version you desire here
10+
# use `-browsers` prefix for selenium tests, e.g. `3.6.1-browsers`
11+
- image: circleci/python:3.6.1
12+
13+
# Specify service dependencies here if necessary
14+
# CircleCI maintains a library of pre-built images
15+
# documented at https://circleci.com/docs/2.0/circleci-images/
16+
# - image: circleci/postgres:9.4
17+
18+
working_directory: ~/repo
19+
20+
steps:
21+
- checkout
22+
23+
# Download and cache dependencies
24+
- restore_cache:
25+
keys:
26+
- v1-dependencies-{{ checksum "requirements.txt" }}
27+
# fallback to using the latest cache if no exact match is found
28+
- v1-dependencies-
29+
30+
- run:
31+
name: install dependencies
32+
command: |
33+
python3 -m venv venv
34+
. venv/bin/activate
35+
pip install -r requirements.txt
36+
37+
- save_cache:
38+
paths:
39+
- ./venv
40+
key: v1-dependencies-{{ checksum "requirements.txt" }}
41+
42+
# run tests!
43+
- run:
44+
name: run tests
45+
command: |
46+
. venv/bin/activate
47+
python3 setup.py test
48+
python3 -m pep8 beaver/
49+
50+
- store_artifacts:
51+
path: test-reports
52+
destination: test-reports
53+

beaver/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ def run(cmd, stdin):
5959

6060
def path_context(path):
6161
ctx = {}
62-
6362
name = os.path.basename(path)
63+
split = name.split(".")
6464

6565
ctx["__file__"] = name
66-
ctx["__name__"] = name.split(".")[0]
67-
ctx["__ext__"] = name.split(".")[-1]
66+
ctx["__name__"] = ".".join(split[0:-1] ) if len(split) > 1 else name
67+
ctx["__ext__"] = split[-1] if len(split) > 1 else ""
6868
ctx["__dir__"] = os.path.dirname(path)
6969
ctx["__path__"] = path
7070

beaver/tests/__init__.py

Whitespace-only changes.

beaver/tests/drivers/test__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import unittest
2+
import unittest.mock as mock
3+
4+
import beaver.drivers as drivers
5+
6+
7+
class TestGetExt(unittest.TestCase):
8+
def test_empty_path(self):
9+
with self.assertRaises(Exception):
10+
drivers.get_ext("")

beaver/tests/test__init__.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import unittest
2+
import unittest.mock as mock
3+
4+
import beaver
5+
6+
7+
class TestRun(unittest.TestCase):
8+
@mock.patch("beaver.subprocess.Popen")
9+
def test_retcode_exception(self, popen_mock):
10+
"""test_retcode_exception ensures that `beaver.run` will raise
11+
an exception when it executes a subprocess which returns a non-zero
12+
execution result."""
13+
m = mock.MagicMock()
14+
popen_mock.return_value = m
15+
16+
m.returncode = 1 # non-zero return value
17+
18+
with self.assertRaises(Exception):
19+
beaver.run("echo", "some text goes here")
20+
21+
@mock.patch("beaver.subprocess.Popen")
22+
def test_retcode_no_exception(self, popen_mock):
23+
"""test_retcode_exception ensures that `beaver.run` will not raise
24+
an exception when it executes a subprocess which returns a zero
25+
execution result."""
26+
m = mock.MagicMock()
27+
popen_mock.return_value = m
28+
29+
m.returncode = 0
30+
beaver.run("echo", "some text goes here")
31+
32+
33+
class TestPathContext(unittest.TestCase):
34+
def test_path_context(self):
35+
# argument: expected
36+
table = {
37+
# empty state
38+
"": {
39+
"__file__": "",
40+
"__name__": "",
41+
"__ext__": "",
42+
"__dir__": "",
43+
"__path__": "",
44+
},
45+
"/": {
46+
"__file__": "",
47+
"__name__": "",
48+
"__ext__": "",
49+
"__dir__": "/",
50+
"__path__": "/",
51+
},
52+
"/dir/file": {
53+
"__file__": "file",
54+
"__name__": "file",
55+
"__ext__": "",
56+
"__dir__": "/dir",
57+
"__path__": "/dir/file",
58+
},
59+
"/dir1/dir2/": {
60+
"__file__": "",
61+
"__name__": "",
62+
"__ext__": "",
63+
"__dir__": "/dir1/dir2",
64+
"__path__": "/dir1/dir2/",
65+
},
66+
"/dir/file.ext": {
67+
"__file__": "file.ext",
68+
"__name__": "file",
69+
"__ext__": "ext",
70+
"__dir__": "/dir",
71+
"__path__": "/dir/file.ext",
72+
},
73+
"/dir/file.ext1.ext2": {
74+
"__file__": "file.ext1.ext2",
75+
"__name__": "file.ext1",
76+
"__ext__": "ext2",
77+
"__dir__": "/dir",
78+
"__path__": "/dir/file.ext1.ext2",
79+
},
80+
}
81+
82+
for arg in table:
83+
result = beaver.path_context(arg)
84+
expected = table[arg]
85+
86+
self.assertEqual(result, expected)
87+
88+
89+
class TestDoOne(unittest.TestCase):
90+
@mock.patch("beaver.os.path.isfile")
91+
def test_file_exceptions(self, mock_isfile):
92+
mock_isfile.return_value = True
93+
def side_effect(path):
94+
return bool(path)
95+
mock_isfile.side_effect = side_effect
96+
97+
m = mock.Mock()
98+
m.template = ""
99+
100+
with self.assertRaises(Exception):
101+
beaver.do_one(m)
102+
103+
m.template = "/some/path"
104+
m.input = ""
105+
106+
with self.assertRaises(Exception):
107+
beaver.do_one(m)
108+
109+
@mock.patch("beaver.open")
110+
@mock.patch("beaver.write_output")
111+
@mock.patch("beaver.run")
112+
@mock.patch("beaver.load_template")
113+
@mock.patch("beaver.drivers.parse")
114+
@mock.patch("beaver.os.path.isfile")
115+
def test_ensure_posts(
116+
self, mock_isfile, mock_parse, mock_load_template,
117+
mock_run, mock_write_output, mock_open
118+
):
119+
m = mock.Mock()
120+
m.post = [
121+
"first",
122+
"second",
123+
"third"
124+
]
125+
126+
mock_tpl = mock.Mock()
127+
mock_tpl.return_value = None
128+
129+
mock_load_template.return_value = mock_tpl
130+
131+
beaver.do_one(m)
132+
133+
assert mock_run.call_count == len(m.post)
134+
135+
136+
if __name__ == '__main__':
137+
unittest.main()

beaver/tests/test_version.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import unittest
2+
import unittest.mock as mock
3+
4+
import beaver.version as version
5+
6+
7+
class TestVersion(unittest.TestCase):
8+
def test_ensure_existance(self):
9+
assert version.__version__

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
Jinja2==2.9.6
22
MarkupSafe==1.0
3+
nose==1.3.7
4+
pep8==1.7.0
5+
pudb==2017.1.4
6+
Pygments==2.2.0
37
PyYAML==3.12
8+
urwid==1.3.1
49
xmldict==0.4.1

setup.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
exec(compile(open('beaver/version.py').read(),
6262
'beaver/version.py', 'exec'))
6363

64-
DESCRIPTION = 'Tool for templated code generation'
64+
DESCRIPTION = 'A code generation tool made with Python'
6565
LONG_DESCRIPTION = 'Command-line program to generate source code from a Jinja2 template and 1 or more input structures'
6666

6767
py2exe_console = [{
@@ -132,9 +132,8 @@ def find_repository():
132132
author='Curtis La Graff',
133133
author_email='[email protected]',
134134
classifiers=[
135-
'Development Status :: 2 - Pre-Alpha',
136135
'Environment :: Console',
137-
'License :: Public Domain',
136+
'License :: MIT LICENSE',
138137
'Programming Language :: Python :: 3.2',
139138
'Programming Language :: Python :: 3.3',
140139
'Programming Language :: Python :: 3.4',

0 commit comments

Comments
 (0)