|
1 | 1 | import io |
2 | 2 | import os.path |
3 | 3 | import sys |
| 4 | +import warnings |
4 | 5 |
|
5 | 6 | import pytest |
6 | 7 |
|
7 | 8 | from ..helpers.argparsing import ArgumentTypeError |
8 | 9 | from ..patterns import PathFullPattern, PathPrefixPattern, FnmatchPattern, ShellPattern, RegexPattern |
9 | | -from ..patterns import load_exclude_file, load_pattern_file |
10 | | -from ..patterns import parse_pattern, PatternMatcher |
| 10 | +from ..patterns import IECommand, load_exclude_file, load_pattern_file |
| 11 | +from ..patterns import command_recurses_dir, parse_inclexcl_command, parse_pattern, PatternMatcher |
11 | 12 | from ..patterns import get_regex_from_pattern |
12 | 13 |
|
13 | 14 |
|
@@ -605,25 +606,53 @@ def test_pattern_matcher(): |
605 | 606 | for i in ["", "foo", "bar"]: |
606 | 607 | assert pm.match(i) is None |
607 | 608 |
|
608 | | - # add extra entries to aid in testing |
609 | | - for target in ["A", "B", "Empty", "FileNotFound"]: |
610 | | - pm.is_include_cmd[target] = target |
| 609 | + pm.add([RegexPattern("^a")], IECommand.Include) |
| 610 | + pm.add([RegexPattern("^b"), RegexPattern("^z")], IECommand.Exclude) |
| 611 | + pm.add([RegexPattern("^$")], IECommand.ExcludeNoRecurse) |
| 612 | + pm.fallback = False |
611 | 613 |
|
612 | | - pm.add([RegexPattern("^a")], "A") |
613 | | - pm.add([RegexPattern("^b"), RegexPattern("^z")], "B") |
614 | | - pm.add([RegexPattern("^$")], "Empty") |
615 | | - pm.fallback = "FileNotFound" |
616 | | - |
617 | | - assert pm.match("") == "Empty" |
618 | | - assert pm.match("aaa") == "A" |
619 | | - assert pm.match("bbb") == "B" |
620 | | - assert pm.match("ccc") == "FileNotFound" |
621 | | - assert pm.match("xyz") == "FileNotFound" |
622 | | - assert pm.match("z") == "B" |
| 614 | + assert pm.match("") is False # ExcludeNoRecurse -> not include |
| 615 | + assert pm.match("aaa") is True # Include |
| 616 | + assert pm.match("bbb") is False # Exclude |
| 617 | + assert pm.match("ccc") is False # fallback |
| 618 | + assert pm.match("xyz") is False # fallback |
| 619 | + assert pm.match("z") is False # Exclude (matches ^z) |
623 | 620 |
|
624 | 621 | assert PatternMatcher(fallback="hey!").fallback == "hey!" |
625 | 622 |
|
626 | 623 |
|
| 624 | +def test_command_recurses_dir(): |
| 625 | + assert command_recurses_dir(IECommand.Include) is True |
| 626 | + assert command_recurses_dir(IECommand.Exclude) is True |
| 627 | + assert command_recurses_dir(IECommand.ExcludeNoRecurse) is False |
| 628 | + with pytest.raises(ValueError, match="unexpected command"): |
| 629 | + command_recurses_dir(IECommand.RootPath) |
| 630 | + with pytest.raises(ValueError, match="unexpected command"): |
| 631 | + command_recurses_dir(IECommand.PatternStyle) |
| 632 | + |
| 633 | + |
| 634 | +def test_root_path_validation(tmp_path): |
| 635 | + # absolute path that exists: no warnings |
| 636 | + with warnings.catch_warnings(): |
| 637 | + warnings.simplefilter("error") |
| 638 | + parse_inclexcl_command(f"R {tmp_path}") |
| 639 | + |
| 640 | + # absolute path that doesn't exist: only "does not exist" warning |
| 641 | + nonexistent = str(tmp_path / "nonexistent_subdir_12345") |
| 642 | + with pytest.warns(UserWarning) as warning_list: |
| 643 | + parse_inclexcl_command(f"R {nonexistent}") |
| 644 | + messages = [str(w.message) for w in warning_list] |
| 645 | + assert any("does not exist" in m for m in messages) |
| 646 | + assert not any("absolute" in m for m in messages) |
| 647 | + |
| 648 | + # relative path that doesn't exist: warns about both |
| 649 | + with pytest.warns(UserWarning) as warning_list: |
| 650 | + parse_inclexcl_command("R relative/nonexistent/path/xyz123") |
| 651 | + messages = [str(w.message) for w in warning_list] |
| 652 | + assert any("absolute" in m for m in messages) |
| 653 | + assert any("does not exist" in m for m in messages) |
| 654 | + |
| 655 | + |
627 | 656 | @pytest.mark.parametrize( |
628 | 657 | "pattern, regex", |
629 | 658 | [ |
|
0 commit comments