|  | 
| 1 | 1 | import os | 
|  | 2 | +import re | 
| 2 | 3 | from pathlib import Path | 
| 3 | 4 | 
 | 
| 4 | 5 | import pytest | 
| @@ -230,66 +231,86 @@ def test_copy_examples_location(input, expected_path, example_cases): | 
| 230 | 231 | @pytest.mark.parametrize( | 
| 231 | 232 |     "bad_inputs,expected,path", | 
| 232 | 233 |     [ | 
| 233 |  | -        (  # input not found (example or pack) | 
|  | 234 | +        ( | 
|  | 235 | +            # 1) Input not found (example or pack). | 
|  | 236 | +            # Expected: Raise an error with the message. | 
| 234 | 237 |             ["bad_example"], | 
| 235 |  | -            ValueError, | 
|  | 238 | +            "No examples or packs found for input: 'bad_example'", | 
| 236 | 239 |             None, | 
| 237 | 240 |         ), | 
| 238 |  | -        (  # mixed good ex and bad inputs | 
|  | 241 | +        ( | 
|  | 242 | +            # 2) Mixed good example and bad input. | 
|  | 243 | +            # Expected: Raise an error with the message. | 
| 239 | 244 |             ["ex1", "bad_example"], | 
| 240 |  | -            ValueError, | 
|  | 245 | +            "No examples or packs found for input: 'bad_example'", | 
| 241 | 246 |             None, | 
| 242 | 247 |         ), | 
| 243 |  | -        (  # mixed good pack and bad inputs | 
|  | 248 | +        ( | 
|  | 249 | +            # 3) Mixed good pack and bad input. | 
|  | 250 | +            # Expected: Raise an error with the message. | 
| 244 | 251 |             ["packA", "bad_example"], | 
| 245 |  | -            ValueError, | 
|  | 252 | +            "No examples or packs found for input: 'bad_example'", | 
| 246 | 253 |             None, | 
| 247 | 254 |         ), | 
| 248 |  | -        (  # path to dir already exists | 
|  | 255 | +        ( | 
|  | 256 | +            # 4) Path to directory already exists. | 
|  | 257 | +            # Expected: Raise a warning with the message. | 
| 249 | 258 |             ["ex1"], | 
| 250 |  | -            FileExistsError, | 
|  | 259 | +            "Target directory already exists. To overwrite use --force.", | 
| 251 | 260 |             Path("docs/examples/"), | 
| 252 | 261 |         ), | 
| 253 |  | -        (  # No input provided | 
|  | 262 | +        ( | 
|  | 263 | +            # 5) No input provided. | 
|  | 264 | +            # Expected: Raise an error with the message. | 
| 254 | 265 |             [], | 
| 255 |  | -            ValueError, | 
|  | 266 | +            ( | 
|  | 267 | +                "No input specified. " | 
|  | 268 | +                "Provide at least one example or pack to copy." | 
|  | 269 | +            ), | 
| 256 | 270 |             None, | 
| 257 | 271 |         ), | 
| 258 | 272 |     ], | 
| 259 | 273 | ) | 
| 260 | 274 | def test_copy_examples_bad(bad_inputs, expected, path, example_cases): | 
| 261 | 275 |     examples_dir = example_cases / "case3" | 
| 262 | 276 |     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) | 
| 268 | 280 | 
 | 
| 269 | 281 | 
 | 
| 270 | 282 | # Test bad target_dir to copy_examples on case3 | 
| 271 | 283 | # 1) target doesn't exist | 
| 272 | 284 | # 2) target is a file | 
| 273 | 285 | # 3) target is nested in a file | 
| 274 | 286 | @pytest.mark.parametrize( | 
| 275 |  | -    "bad_inputs,expected", | 
|  | 287 | +    "bad_target,expected", | 
| 276 | 288 |     [ | 
| 277 |  | -        (Path("nonexistent/path/target"), FileNotFoundError),  # doesn't exist | 
| 278 | 289 |         ( | 
|  | 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. | 
| 279 | 298 |             Path("docs/examples/packA/ex1/script4.py"), | 
| 280 |  | -            NotADirectoryError, | 
| 281 |  | -        ),  # target is a file | 
|  | 299 | +            "Target path is not a directory", | 
|  | 300 | +        ), | 
| 282 | 301 |         ( | 
|  | 302 | +            # 3) Target is nested in a file. | 
|  | 303 | +            # Expected: Raise an error that the target path is not a directory. | 
| 283 | 304 |             Path("docs/examples/packA/ex1/script4.py/nested"), | 
| 284 |  | -            NotADirectoryError, | 
| 285 |  | -        ),  # nested in file | 
|  | 305 | +            "Target path is not a directory", | 
|  | 306 | +        ), | 
| 286 | 307 |     ], | 
| 287 | 308 | ) | 
| 288 |  | -def test_copy_examples_bad_target(bad_inputs, expected, example_cases): | 
|  | 309 | +def test_copy_examples_bad_target(bad_target, expected, example_cases): | 
| 289 | 310 |     examples_dir = example_cases / "case3" | 
| 290 | 311 |     pm = PacksManager(root_path=examples_dir) | 
| 291 |  | -    with pytest.raises(expected): | 
|  | 312 | +    with pytest.raises(Exception, match=re.escape(expected)): | 
| 292 | 313 |         pm.copy_examples( | 
| 293 | 314 |             ["packA"], | 
| 294 |  | -            target_dir=bad_inputs, | 
|  | 315 | +            target_dir=bad_target, | 
| 295 | 316 |         ) | 
0 commit comments