python-escpos/test/test_cli.py

121 lines
3.0 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 shutil
import tempfile
import pytest
from scripttest import TestFileEnvironment as TFE
import escpos
2016-03-28 21:23:18 +00:00
TEST_DIR = tempfile.mkdtemp() + "/cli-test"
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 = f"""
2016-03-28 21:23:18 +00:00
---
printer:
type: file
devfile: {DEVFILE}
"""
2016-03-28 21:23:18 +00:00
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)
shutil.rmtree(TEST_DIR)
2016-03-28 21:23:18 +00:00
def setup_method(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
self.env = TFE(
2016-03-28 21:23:18 +00:00
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_method(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
assert escpos.__version__ == result.stdout.strip()
def test_cli_version_extended(self):
"""Test the extended version information"""
result = self.env.run("python-escpos", "version_extended")
assert not result.stderr
assert escpos.__version__ in result.stdout
# test that additional information on e.g. Serial is printed
assert "Serial" in result.stdout
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()
assert (
result.files_updated[DEVFILE_NAME].bytes == "\x1bt\x00" + test_text + "\n"
)
2016-03-28 21:23:18 +00:00
def test_cli_text_invalid_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,
2016-03-28 21:23:18 +00:00
)
assert 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