python-escpos/test/test_cli.py

110 lines
2.8 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
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
import escpos
2016-03-28 21:23:18 +00:00
2021-10-30 16:15:22 +00:00
TEST_DIR = os.path.abspath("test/test-cli-output")
2016-03-28 21:23:18 +00:00
2021-10-30 16:15:22 +00:00
DEVFILE_NAME = "testfile"
2016-03-28 21:23:18 +00:00
DEVFILE = os.path.join(TEST_DIR, DEVFILE_NAME)
2021-10-30 16:15:22 +00:00
CONFIGFILE = "testconfig.yaml"
CONFIG_YAML = """
2016-03-28 21:23:18 +00:00
---
printer:
type: file
devfile: {testfile}
2021-10-30 16:15:22 +00:00
""".format(
2016-03-28 21:23:18 +00:00
testfile=DEVFILE,
)
2016-04-02 13:29:51 +00:00
2017-01-29 23:10:14 +00:00
class TestCLI:
2021-10-30 16:15:22 +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):
2021-10-30 16:15:22 +00:00
"""Create a config file to read from"""
with open(CONFIGFILE, "w") as config:
2016-03-28 21:23:18 +00:00
config.write(CONFIG_YAML)
2016-08-30 15:47:09 +00:00
@classmethod
def teardown_class(cls):
2021-10-30 16:15:22 +00:00
"""Remove config file"""
2016-03-28 21:23:18 +00:00
os.remove(CONFIGFILE)
def setup(self):
2021-10-30 16:15:22 +00:00
"""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 = (
2021-10-30 16:15:22 +00:00
"python-escpos",
"-c",
2016-03-28 21:23:18 +00:00
CONFIGFILE,
)
2021-10-30 16:15:22 +00:00
fhandle = open(DEVFILE, "a")
2016-03-28 21:23:18 +00:00
try:
os.utime(DEVFILE, None)
finally:
fhandle.close()
def teardown(self):
2021-10-30 16:15:22 +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):
2021-10-30 16:15:22 +00:00
"""Test getting help from cli"""
result = self.env.run("python-escpos", "-h")
2016-03-28 21:23:18 +00:00
assert not result.stderr
2021-10-30 16:15:22 +00:00
assert "usage" in result.stdout
2016-03-28 21:23:18 +00:00
def test_cli_version(self):
2021-10-30 16:15:22 +00:00
"""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())
@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):
2021-10-30 16:15:22 +00:00
"""Make sure text returns what we sent it"""
test_text = "this is some text"
2016-03-28 21:23:18 +00:00
result = self.env.run(
2021-10-30 16:15:22 +00:00
*(
self.default_args
+ (
"text",
"--txt",
test_text,
)
)
2016-03-28 21:23:18 +00:00
)
assert not result.stderr
assert DEVFILE_NAME in result.files_updated.keys()
2021-10-30 16:15:22 +00:00
assert_equals(result.files_updated[DEVFILE_NAME].bytes, test_text + "\n")
2016-03-28 21:23:18 +00:00
def test_cli_text_inavlid_args(self):
2021-10-30 16:15:22 +00:00
"""Test a failure to send valid arguments"""
2016-03-28 21:23:18 +00:00
result = self.env.run(
2021-10-30 16:15:22 +00:00
*(self.default_args + ("text", "--invalid-param", "some data")),
2016-03-28 21:23:18 +00:00
expect_error=True,
expect_stderr=True
)
2020-11-08 20:29:18 +00:00
assert_equal(result.returncode, 2)
2021-10-30 16:15:22 +00:00
assert "error:" in result.stderr
2016-03-28 21:23:18 +00:00
assert not result.files_updated