diff --git a/escpos/cli.py b/escpos/cli.py index c84a4b8..0f2f55a 100755 --- a/escpos/cli.py +++ b/escpos/cli.py @@ -141,8 +141,8 @@ ESCPOS_COMMANDS = [ }, 'arguments': [ { - 'option_strings': ('--text',), - 'help': 'Text to print as a qr code', + 'option_strings': ('--txt',), + 'help': 'Plain text to print', 'required': True, } ], diff --git a/setup.py b/setup.py index d68d869..2eddb08 100755 --- a/setup.py +++ b/setup.py @@ -78,6 +78,6 @@ setup( setup_requires=[ 'setuptools_scm', ], - tests_require=['tox', 'nose'], + tests_require=['tox', 'nose', 'scripttest'], cmdclass={'test': Tox}, ) diff --git a/test/test_cli.py b/test/test_cli.py new file mode 100644 index 0000000..11018b6 --- /dev/null +++ b/test/test_cli.py @@ -0,0 +1,99 @@ +"""Test for the CLI + +""" + +import os +import sys +from scripttest import TestFileEnvironment +from nose.tools import assert_equals + +TEST_DIR = os.path.abspath('test/test-cli-output') + +DEVFILE_NAME = 'testfile' + +DEVFILE = os.path.join(TEST_DIR, DEVFILE_NAME) +CONFIGFILE = 'testconfig.yaml' +CONFIG_YAML = ''' +--- + +printer: + type: file + devfile: {testfile} +'''.format( + testfile=DEVFILE, +) + +class TestCLI: + + @classmethod + def setup_class(cls): + """ Create a config file to read from """ + with open(CONFIGFILE, 'w') as config: + config.write(CONFIG_YAML) + + @classmethod + def teardown_class(cls): + """ Remove config file """ + os.remove(CONFIGFILE) + + def setup(self): + """ Create a file to print to and set up env""" + self.env = TestFileEnvironment( + base_path=TEST_DIR, + cwd=os.getcwd(), + ) + + self.default_args = ( + sys.executable, + '-mescpos.cli', + '-c', + CONFIGFILE, + ) + + fhandle = open(DEVFILE, 'a') + try: + os.utime(DEVFILE, None) + finally: + fhandle.close() + + def teardown(self): + """ Destroy printer file """ + os.remove(DEVFILE) + + def test_cli_help(self): + """ Test getting help from cli """ + result = self.env.run(sys.executable, '-mescpos.cli', '-h') + assert not result.stderr + assert 'usage' in result.stdout + + def test_cli_text(self): + """ Make sure text returns what we sent it """ + test_text = 'this is some text' + result = self.env.run( + *(self.default_args + ( + 'text', + '--txt', + test_text, + )) + ) + assert not result.stderr + assert DEVFILE_NAME in result.files_updated.keys() + assert_equals( + result.files_updated[DEVFILE_NAME].bytes, + test_text + ) + + def test_cli_text_inavlid_args(self): + """ Test a failure to send valid arguments """ + result = self.env.run( + *(self.default_args + ( + 'text', + '--invalid-param', + 'some data' + )), + expect_error=True, + expect_stderr=True + ) + assert_equals(result.returncode, 2) + assert 'error:' in result.stderr + assert not result.files_updated diff --git a/tox.ini b/tox.ini index 0cee432..6c1b7bc 100644 --- a/tox.ini +++ b/tox.ini @@ -4,4 +4,5 @@ envlist = py27, py34, py35 [testenv] deps = nose coverage + scripttest commands = nosetests --with-coverage --cover-erase --cover-branches