Skip to content

Commit 8b752b1

Browse files
committed
Update tests
Use of regex to avoid having to update the cli tests each time the operations and facts used in them are modified
1 parent 3cedecb commit 8b752b1

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

tests/test_cli/test_cli_exceptions.py

+25-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import sys
23
from os import path
34
from unittest import TestCase
@@ -21,7 +22,24 @@ def setUpClass(cls):
2122
def assert_cli_exception(self, args, message):
2223
result = self.runner.invoke(cli, args, standalone_mode=False)
2324
self.assertIsInstance(result.exception, CliError)
24-
assert getattr(result.exception, "message") == message
25+
26+
if isinstance(message, str):
27+
message = [message]
28+
29+
for part in message:
30+
31+
# Test if the string is a regex
32+
is_regex = False
33+
try:
34+
re.compile(part)
35+
is_regex = True
36+
except re.error:
37+
pass
38+
39+
if is_regex:
40+
assert re.search(part, result.exception.message, re.MULTILINE)
41+
else:
42+
assert part == result.exception.message
2543

2644
def test_bad_deploy_file(self):
2745
self.assert_cli_exception(
@@ -44,13 +62,12 @@ def test_no_fact_module(self):
4462
def test_no_fact_cls(self):
4563
self.assert_cli_exception(
4664
["my-server.net", "fact", "server.NotAFact"],
47-
(
48-
"No such attribute in module server: NotAFact\n"
49-
"Available facts in module are: User, Home, Path, TmpDir, Hostname, Kernel, "
50-
"KernelVersion, Os, OsVersion, Arch, Command, Which, Date, MacosVersion, Mounts, "
51-
"KernelModules, LsbRelease, OsRelease, Sysctl, Groups, Users, LinuxDistribution, "
52-
"Selinux, LinuxGui, Locales, SecurityLimits"
53-
),
65+
[
66+
r"^No such attribute in module server: NotAFact.*",
67+
r".*Available facts are: .*",
68+
r".* User,.*",
69+
r".* Os,",
70+
],
5471
)
5572

5673

tests/test_cli/test_cli_util.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
import sys
34
from datetime import datetime
45
from io import StringIO
@@ -36,12 +37,13 @@ def test_setup_not_exists_op(self):
3637
with self.assertRaises(CliError) as context:
3738
get_func_and_args(("server.not_exists",))
3839

39-
assert context.exception.message == (
40-
"No such attribute in module server: not_exists\n"
41-
"Available operations are: reboot, wait, shell, script, script_template, "
42-
"modprobe, mount, hostname, sysctl, service, packages, crontab, group, "
43-
"user_authorized_keys, user, locale, security_limit"
44-
)
40+
for part in [
41+
r"^No such attribute in module server: not_exists$",
42+
r"Available operations are: .*",
43+
r".* modprobe, .*",
44+
r".* mount, .*",
45+
]:
46+
assert re.search(part, context.exception.message, re.MULTILINE)
4547

4648
def test_setup_op_and_args(self):
4749
commands = ("pyinfra.operations.server.user", "one", "two", "hello=world")

0 commit comments

Comments
 (0)