Skip to content

Commit c4df09e

Browse files
authored
Merge pull request #169 from redpwn/test/match-warnings
fix(test): use match kwarg to check warning messages
2 parents 0495ff2 + ebfa7c4 commit c4df09e

2 files changed

Lines changed: 5 additions & 15 deletions

File tree

tests/challenge/test_config.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,11 @@ def test_nonexistent_flag_file(configloader, test_datadir) -> None:
110110

111111

112112
def test_warn_multiline_flag(configloader, test_datadir) -> None:
113-
with pytest.warns(RuntimeWarning) as record:
113+
with pytest.warns(
114+
RuntimeWarning, match=r"^Flag contains multiple lines; is this intended\?$"
115+
):
114116
cfg, errors = configloader.check_config(test_datadir / "challenge.yml")
115117
assert errors is None
116-
assert len(record) == 1
117-
assert isinstance(record[0].message, Warning)
118-
assert (
119-
str(record[0].message.args[0])
120-
== "Flag contains multiple lines; is this intended?"
121-
)
122118

123119

124120
def test_default_category(configloader, test_datadir) -> None:

tests/project/test_assets.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,23 +303,17 @@ def test_extra_file(self, datadir: Path, am_fn: assets.AssetManager) -> None:
303303
file1 = ctx._root / "files" / "file1"
304304
with file1.open("w") as fd:
305305
fd.write("abcd")
306-
with pytest.warns(RuntimeWarning) as record:
306+
with pytest.warns(RuntimeWarning, match=r"^Unexpected item found in cache: "):
307307
ctx.sync(check=True)
308-
assert len(record) == 1
309-
assert isinstance(record[0].message, Warning)
310-
assert "Unexpected item found in cache: " in str(record[0].message.args[0])
311308
assert not file1.exists()
312309

313310
def test_extra_dir(self, datadir: Path, am_fn: assets.AssetManager) -> None:
314311
asset_manager = am_fn
315312
ctx = asset_manager.create_context("challenge")
316313
dir1 = ctx._root / "files" / "dir1"
317314
dir1.mkdir()
318-
with pytest.warns(RuntimeWarning) as record:
315+
with pytest.warns(RuntimeWarning, match=r"^Unexpected item found in cache: "):
319316
ctx.sync(check=True)
320-
assert len(record) == 1
321-
assert isinstance(record[0].message, Warning)
322-
assert "Unexpected item found in cache: " in str(record[0].message.args[0])
323317
assert not dir1.exists()
324318

325319
def test_broken_link(self, datadir: Path, am_fn: assets.AssetManager) -> None:

0 commit comments

Comments
 (0)