python-escpos/test/test_cli.py
Patrick Kanzler 9f5eed0020
add version-strings into the module
The version string is in the module as __version__ available.
In the doc the version will be automatically parsed. The version comes
from the installed module if on read the docs or directly from
setuptools_scm if you are working locally.
The CLI will issue the version string if you call it with the option
'version'. The CLI does not accept commands like '--version', since this
would not be conform with the rest of the interface (and argparse).

The configuration for loading the version-string is adapted from
pimutils/vdirsyncer. It autogenerates a version string setuptools_scm at
install-time and then adds it to the __version__ member in __init__.py

I adapted the GitHub-template with a fitting comment and bumped the
changelog.

closes #141
2016-07-17 19:39:42 +02:00

123 lines
3.0 KiB
Python

"""Test for the CLI
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import sys
from scripttest import TestFileEnvironment
from nose.tools import assert_equals
import escpos
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():
""" Contains setups, teardowns, and tests for CLI
"""
def __init__(self):
""" Initalize the tests.
Just define some vars here since most of them get set during
setup_class and teardown_class
"""
self.env = None
self.default_args = None
@staticmethod
def setup_class():
""" Create a config file to read from """
with open(CONFIGFILE, 'w') as config:
config.write(CONFIG_YAML)
@staticmethod
def teardown_class():
""" 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 = (
'python-escpos',
'-c',
CONFIGFILE,
)
fhandle = open(DEVFILE, 'a')
try:
os.utime(DEVFILE, None)
finally:
fhandle.close()
def teardown(self):
""" Destroy printer file and env """
os.remove(DEVFILE)
self.env.clear()
def test_cli_help(self):
""" Test getting help from cli """
result = self.env.run('python-escpos', '-h')
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())
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 + '\n'
)
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