Skip to content

Commit

Permalink
Using cement for CLI; avoiding subprocess in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrkarr committed Aug 8, 2016
1 parent eb1436d commit 25c5045
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 48 deletions.
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ recursive-include lib *

# license
include LICENSE

# requirements
include requirements.txt
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@
# built documents.
#
# The short X.Y version.
version = u'0.0.6'
version = u'0.0.7'
# The full version, including alpha/beta/rc tags.
release = u'0.0.6'
release = u'0.0.7'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cement
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os

# version
version = '0.0.6'
version = '0.0.7'

# parse requirements.txt
install_requires = [line.rstrip() for line in open('requirements.txt')]
Expand Down Expand Up @@ -31,7 +31,7 @@
],
entry_points={
'console_scripts': [
'unitth = unitth.bin.run:main',
'unitth = unitth.__main__:main',
],
},
)
23 changes: 14 additions & 9 deletions tests/test_unitth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
from nose2unitth.core import Converter as nose2unitth
from junit2htmlreport.parser import Junit as JunitParser
from unitth.core import UnitTH
from unitth.__main__ import App as UnitThCli
import nose
import shutil
import subprocess
import os
import sys
import tempfile
Expand All @@ -29,8 +30,9 @@ def setUp(self):
os.mkdir(unitth_dir)
os.mkdir(html_dir)

subprocess.check_call(['nosetests', 'tests/test_unitth.py:TestDummy.test_dummy_test',
'--with-xunit', '--xunit-file', os.path.join(nose_dir, '1.xml')])
if not nose.run(argv=['nosetests', 'tests/test_unitth.py:TestDummy.test_dummy_test',
'--with-xunit', '--xunit-file', os.path.join(nose_dir, '1.xml')]):
sys.exit(1)
shutil.copyfile(os.path.join(nose_dir, '1.xml'), os.path.join(nose_dir, '2.xml'))

nose2unitth.run(os.path.join(nose_dir, '1.xml'), os.path.join(unitth_dir, '1'))
Expand All @@ -51,14 +53,17 @@ def tearDown(self):

def test_api(self):
UnitTH.run(os.path.join(self._unitth_dir, '*'), xml_report_filter='', html_report_dir=self._html_dir)
self.assertTrue(os.path.isfile(os.path.join(self._html_dir, 'index.html')))
self.assertTrue(os.path.isfile(os.path .join(self._html_dir, 'index.html')))

def test_cli(self):
subprocess.check_call(['python' + str(sys.version_info[0]), 'unitth/bin/run.py',
os.path.join(self._unitth_dir, '*'),
'--xml_report_filter', '',
'--html_report_dir', self._html_dir,
])
argv = [
os.path.join(self._unitth_dir, '*'),
'--xml-report-filter', '',
'--html-report-dir', self._html_dir,
]
with UnitThCli(argv=argv) as app:
app.run()

self.assertTrue(os.path.isfile(os.path.join(self._html_dir, 'index.html')))


Expand Down
57 changes: 57 additions & 0 deletions unitth/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

from cement.core.foundation import CementApp
from cement.core.controller import CementBaseController, expose
from unitth.core import UnitTH


class BaseController(CementBaseController):

class Meta:
label = 'base'
description = "Generate HTML unit test history report"
arguments = [
(['xml_report_dir'], dict(type=str,
help='Parent directory of XML reports of individual builds to generate a history report of')),
(['--xml-report-filter'], dict(type=str, default='TEST-', nargs='?',
help='Starts-with filter for individual reports with `xml-report-dir` that should be included in the history report. Set `xml-report-filter` to '' to include all files/subdirectories in the history report.')),
(['--html-report-path'], dict(type=str, default='.',
help='Directory of HTML reports of individual builds(relative to XML directories of individual builds)')),
(['--generate-exec-time-graphs'], dict(type=bool, default=True,
help='Whether execution time graphs shall be generated')),
(['--html-report-dir'], dict(type=str, default='report.th',
help='directory to store generated HTML history report')),
]

@expose(hide=True)
def default(self):
""" Archive a coverage report:
* Copy report to artifacts directory
* Upload report to Coveralls
"""
args = self.app.pargs
UnitTH.run(args.xml_report_dir,
xml_report_filter=args.xml_report_filter or '',
html_report_path=args.html_report_path,
generate_exec_time_graphs=args.generate_exec_time_graphs,
html_report_dir=args.html_report_dir
)


class App(CementApp):

class Meta:
label = 'unitth'
base_controller = 'base'
handlers = [BaseController]


def main():
with App() as app:
app.run()


if __name__ == "__main__":
main()
Empty file removed unitth/bin/__init__.py
Empty file.
35 changes: 0 additions & 35 deletions unitth/bin/run.py

This file was deleted.

0 comments on commit 25c5045

Please sign in to comment.