-
Notifications
You must be signed in to change notification settings - Fork 311
Expand file tree
/
Copy pathtest_cli.py
More file actions
116 lines (94 loc) · 3.03 KB
/
test_cli.py
File metadata and controls
116 lines (94 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""Test for the CLI
"""
import os
import shutil
import tempfile
from scripttest import TestFileEnvironment as TFE
import escpos
TEST_DIR = tempfile.mkdtemp() + "/cli-test"
DEVFILE_NAME = "testfile"
DEVFILE = os.path.join(TEST_DIR, DEVFILE_NAME)
CONFIGFILE = "testconfig.yaml"
CONFIG_YAML = f"""
---
printer:
type: file
devfile: {DEVFILE}
"""
class TestCLI:
"""Contains setups, teardowns, and tests for CLI"""
@classmethod
def setup_class(cls) -> None:
"""Create a config file to read from"""
with open(CONFIGFILE, "w") as config:
config.write(CONFIG_YAML)
@classmethod
def teardown_class(cls) -> None:
"""Remove config file"""
os.remove(CONFIGFILE)
shutil.rmtree(TEST_DIR)
def setup_method(self) -> None:
"""Create a file to print to and set up env"""
self.env = TFE(
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_method(self) -> None:
"""Destroy printer file and env"""
os.remove(DEVFILE)
self.env.clear()
def test_cli_help(self) -> None:
"""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) -> None:
"""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) -> None:
"""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
def test_cli_text(self) -> None:
"""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 (
result.files_updated[DEVFILE_NAME].bytes == "\x1bt\x00" + test_text + "\n"
)
def test_cli_text_invalid_args(self) -> None:
"""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 result.returncode == 2
assert "error:" in result.stderr
assert not result.files_updated