Skip to content

Commit 3405e17

Browse files
committed
check exception message rather than the exception type
1 parent 6c38885 commit 3405e17

File tree

1 file changed

+45
-24
lines changed

1 file changed

+45
-24
lines changed

tests/test_packsmanager.py

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
from pathlib import Path
34

45
import pytest
@@ -230,66 +231,86 @@ def test_copy_examples_location(input, expected_path, example_cases):
230231
@pytest.mark.parametrize(
231232
"bad_inputs,expected,path",
232233
[
233-
( # input not found (example or pack)
234+
(
235+
# 1) Input not found (example or pack).
236+
# Expected: Raise an error with the message.
234237
["bad_example"],
235-
ValueError,
238+
"No examples or packs found for input: 'bad_example'",
236239
None,
237240
),
238-
( # mixed good ex and bad inputs
241+
(
242+
# 2) Mixed good example and bad input.
243+
# Expected: Raise an error with the message.
239244
["ex1", "bad_example"],
240-
ValueError,
245+
"No examples or packs found for input: 'bad_example'",
241246
None,
242247
),
243-
( # mixed good pack and bad inputs
248+
(
249+
# 3) Mixed good pack and bad input.
250+
# Expected: Raise an error with the message.
244251
["packA", "bad_example"],
245-
ValueError,
252+
"No examples or packs found for input: 'bad_example'",
246253
None,
247254
),
248-
( # path to dir already exists
255+
(
256+
# 4) Path to directory already exists.
257+
# Expected: Raise a warning with the message.
249258
["ex1"],
250-
FileExistsError,
259+
"Target directory already exists. To overwrite use --force.",
251260
Path("docs/examples/"),
252261
),
253-
( # No input provided
262+
(
263+
# 5) No input provided.
264+
# Expected: Raise an error with the message.
254265
[],
255-
ValueError,
266+
(
267+
"No input specified. "
268+
"Provide at least one example or pack to copy."
269+
),
256270
None,
257271
),
258272
],
259273
)
260274
def test_copy_examples_bad(bad_inputs, expected, path, example_cases):
261275
examples_dir = example_cases / "case3"
262276
pm = PacksManager(root_path=examples_dir)
263-
with pytest.raises(expected):
264-
pm.copy_examples(
265-
bad_inputs,
266-
target_dir=examples_dir / path if path is not None else None,
267-
)
277+
target_dir = None if path is None else examples_dir / path
278+
with pytest.raises(Exception, match=re.escape(expected)):
279+
pm.copy_examples(bad_inputs, target_dir=target_dir)
268280

269281

270282
# Test bad target_dir to copy_examples on case3
271283
# 1) target doesn't exist
272284
# 2) target is a file
273285
# 3) target is nested in a file
274286
@pytest.mark.parametrize(
275-
"bad_inputs,expected",
287+
"bad_target,expected",
276288
[
277-
(Path("nonexistent/path/target"), FileNotFoundError), # doesn't exist
278289
(
290+
# 1) Target doesn't exist.
291+
# Expected: Raise an error that the target directory is missing.
292+
Path("nonexistent/path/target"),
293+
"Target directory does not exist",
294+
),
295+
(
296+
# 2) Target is a file.
297+
# Expected: Raise an error that the target path is not a directory.
279298
Path("docs/examples/packA/ex1/script4.py"),
280-
NotADirectoryError,
281-
), # target is a file
299+
"Target path is not a directory",
300+
),
282301
(
302+
# 3) Target is nested in a file.
303+
# Expected: Raise an error that the target path is not a directory.
283304
Path("docs/examples/packA/ex1/script4.py/nested"),
284-
NotADirectoryError,
285-
), # nested in file
305+
"Target path is not a directory",
306+
),
286307
],
287308
)
288-
def test_copy_examples_bad_target(bad_inputs, expected, example_cases):
309+
def test_copy_examples_bad_target(bad_target, expected, example_cases):
289310
examples_dir = example_cases / "case3"
290311
pm = PacksManager(root_path=examples_dir)
291-
with pytest.raises(expected):
312+
with pytest.raises(Exception, match=re.escape(expected)):
292313
pm.copy_examples(
293314
["packA"],
294-
target_dir=bad_inputs,
315+
target_dir=bad_target,
295316
)

0 commit comments

Comments
 (0)