2016-03-28 21:23:18 +00:00
|
|
|
"""Test for the CLI
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2016-03-30 20:31:21 +00:00
|
|
|
|
2016-03-28 21:23:18 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from scripttest import TestFileEnvironment
|
2020-11-08 20:29:18 +00:00
|
|
|
from nose.tools import assert_equal, nottest
|
2016-07-17 09:17:43 +00:00
|
|
|
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
|
|
|
|
2017-01-29 23:10:14 +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 = (
|
2016-07-17 09:17:43 +00:00
|
|
|
'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 """
|
2016-07-17 09:17:43 +00:00
|
|
|
result = self.env.run('python-escpos', '-h')
|
2016-03-28 21:23:18 +00:00
|
|
|
assert not result.stderr
|
|
|
|
assert 'usage' in result.stdout
|
|
|
|
|
2016-07-17 09:17:43 +00:00
|
|
|
def test_cli_version(self):
|
|
|
|
""" Test the version string """
|
|
|
|
result = self.env.run('python-escpos', 'version')
|
|
|
|
assert not result.stderr
|
2020-11-08 20:29:18 +00:00
|
|
|
assert_equal(escpos.__version__, result.stdout.strip())
|
2016-07-17 09:17:43 +00:00
|
|
|
|
2016-07-23 20:16:11 +00:00
|
|
|
@nottest # disable this test as it is not that easy anymore to predict the outcome of this call
|
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
|
|
|
|
)
|
2020-11-08 20:29:18 +00:00
|
|
|
assert_equal(result.returncode, 2)
|
2016-03-28 21:23:18 +00:00
|
|
|
assert 'error:' in result.stderr
|
|
|
|
assert not result.files_updated
|