python-escpos/test/test_cli.py

118 lines
2.9 KiB
Python
Raw Normal View History

2016-03-28 21:23:18 +00:00
"""Test for the CLI
"""
2016-03-30 20:31:21 +00:00
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
2016-03-28 21:23:18 +00:00
import os
import sys
from scripttest import TestFileEnvironment
from nose.tools import assert_equals
import escpos
2016-03-28 21:23:18 +00:00
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,
)
2016-04-02 13:29:51 +00:00
class TestCLI():
2016-03-28 22:01:40 +00:00
""" Contains setups, teardowns, and tests for CLI
"""
2016-03-28 21:23:18 +00:00
2016-08-30 15:47:09 +00:00
@classmethod
def setup_class(cls):
2016-03-28 21:23:18 +00:00
""" Create a config file to read from """
with open(CONFIGFILE, 'w') as config:
config.write(CONFIG_YAML)
2016-08-30 15:47:09 +00:00
@classmethod
def teardown_class(cls):
2016-03-28 21:23:18 +00:00
""" Remove config file """
os.remove(CONFIGFILE)
def setup(self):
""" Create a file to print to and set up env"""
2016-08-30 15:47:09 +00:00
self.env = None
self.default_args = None
2016-03-28 21:23:18 +00:00
self.env = TestFileEnvironment(
base_path=TEST_DIR,
cwd=os.getcwd(),
)
self.default_args = (
'python-escpos',
2016-03-28 21:23:18 +00:00
'-c',
CONFIGFILE,
)
fhandle = open(DEVFILE, 'a')
try:
os.utime(DEVFILE, None)
finally:
fhandle.close()
def teardown(self):
2016-03-28 21:57:06 +00:00
""" Destroy printer file and env """
2016-03-28 21:23:18 +00:00
os.remove(DEVFILE)
2016-03-28 21:57:06 +00:00
self.env.clear()
2016-03-28 21:23:18 +00:00
def test_cli_help(self):
""" Test getting help from cli """
result = self.env.run('python-escpos', '-h')
2016-03-28 21:23:18 +00:00
assert not result.stderr
assert 'usage' in result.stdout
def test_cli_version(self):
""" Test the version string """
result = self.env.run('python-escpos', 'version')
assert not result.stderr
assert_equals(escpos.__version__, result.stdout.strip())
2016-03-28 21:23:18 +00:00
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,
2016-03-28 22:21:16 +00:00
test_text + '\n'
2016-03-28 21:23:18 +00:00
)
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