Skip to content

Commit 6d42b24

Browse files
committed
patterns: add path existence validation for RootPath, improve test coverage
1 parent 0a1d559 commit 6d42b24

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

src/borg/patterns.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import unicodedata
66
from collections import namedtuple
7+
import os
78
from enum import Enum
89

910
from .helpers import clean_lines, shellpattern
@@ -376,8 +377,18 @@ def parse_inclexcl_command(cmd_line_str, fallback=ShellPattern):
376377
raise ArgumentTypeError("A pattern/command must have a value part.")
377378

378379
if cmd is IECommand.RootPath:
379-
if not remainder_str.startswith("/"):
380-
logger.warning("Root path %r does not look like an absolute path", remainder_str)
380+
# Check if path is absolute
381+
is_absolute = remainder_str.startswith("/")
382+
# Check if path exists
383+
path_exists = os.path.exists(remainder_str)
384+
385+
# Warn about relative paths
386+
if not is_absolute:
387+
logger.warning("Root path %r is relative, recommended to use absolute path", remainder_str)
388+
# Warn about non-existent paths
389+
if not path_exists:
390+
logger.warning("Root path %r does not exist", remainder_str)
391+
381392
val = remainder_str
382393
elif cmd is IECommand.PatternStyle:
383394
# then remainder_str is something like 're' or 'sh'

src/borg/testsuite/patterns_test.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -632,16 +632,44 @@ def test_command_recurses_dir():
632632

633633

634634
def test_root_path_validation(caplog):
635+
import tempfile
636+
import os as test_os
637+
635638
caplog.set_level(logging.WARNING, logger="borg.patterns")
636639

637-
# absolute path should not warn
638-
parse_inclexcl_command("R /absolute/path")
639-
assert "does not look like an absolute path" not in caplog.text
640+
# Test 1: Unix-style absolute path that doesn't exist
641+
# On Unix systems, /absolute/paths don't trigger "relative" warning
642+
# On Windows, such paths may not exist but still validate correctly
643+
caplog.clear()
644+
parse_inclexcl_command("R /absolute/unix/path")
645+
# Should not have "relative" warning (starts with /)
646+
assert "relative" not in caplog.text
647+
648+
# Test 2: absolute path that doesn't exist should warn about existence
649+
caplog.clear()
650+
parse_inclexcl_command("R /nonexistent/absolute/path/12345")
651+
assert "does not exist" in caplog.text
640652

641-
# relative path should warn
653+
# Test 3: relative path that exists should warn about relative
654+
caplog.clear()
655+
with tempfile.TemporaryDirectory() as tmpdir:
656+
# Create temp subdir to be relative
657+
old_cwd = test_os.getcwd()
658+
try:
659+
test_os.chdir(tmpdir)
660+
# Create a subdir
661+
test_os.makedirs("test_root_dir", exist_ok=True)
662+
parse_inclexcl_command("R test_root_dir")
663+
assert "relative" in caplog.text
664+
assert "does not exist" not in caplog.text
665+
finally:
666+
test_os.chdir(old_cwd)
667+
668+
# Test 4: relative path that doesn't exist should warn about both
642669
caplog.clear()
643-
parse_inclexcl_command("R relative/path")
644-
assert "does not look like an absolute path" in caplog.text
670+
parse_inclexcl_command("R relative/nonexistent/path")
671+
assert "relative" in caplog.text
672+
assert "does not exist" in caplog.text
645673

646674

647675
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)