Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/longpath/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def _policy_lines(pal: Palette) -> List[str]:


def _clean_base(base: Optional[str]) -> Optional[str]:
# `--base "C:\dest\"` in PowerShell/cmd turns the trailing \" into a
# `--base "C:\\dest\\"` in PowerShell/cmd turns the trailing \\" into a
# literal quote; a path cannot legally end in one, so strip it.
if base is None:
return None
Expand All @@ -247,6 +247,9 @@ def _run_scan(args: argparse.Namespace) -> int:
if args.limit < 16:
_err("longpath: --limit must be at least 16")
return EXIT_ERROR
if args.top < 0:
_err("longpath: --top must be >= 0")
return EXIT_ERROR

base = _clean_base(args.base)
result = scan_tree(args.dir, limit=args.limit, base=base, exclude=args.exclude)
Expand Down Expand Up @@ -472,7 +475,7 @@ def main(argv: Optional[List[str]] = None) -> int:
except (AttributeError, OSError):
pass

# sugar: `longpath D:\dir` == `longpath scan D:\dir`; bare `longpath` scans .
# sugar: `longpath D:\\dir` == `longpath scan D:\\dir`; bare `longpath` scans .
if not argv:
argv = ["scan"]
elif argv[0] not in _SUBCOMMANDS and not argv[0].startswith("-"):
Expand Down
7 changes: 7 additions & 0 deletions src/longpath/validate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Small CLI argument guards."""


def require_nonneg_top(n: int) -> int:
if int(n) < 0:
raise ValueError("--top must be >= 0")
return int(n)
18 changes: 18 additions & 0 deletions tests/test_negative_top.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest

from longpath.validate import require_nonneg_top

from conftest import run_cli


def test_require_nonneg_top():
assert require_nonneg_top(0) == 0
assert require_nonneg_top(20) == 20
with pytest.raises(ValueError, match="--top"):
require_nonneg_top(-1)


def test_scan_negative_top_exit_2(tree):
p = run_cli("scan", str(tree), "--top", "-1")
assert p.returncode == 2
assert "--top must be >= 0" in p.stderr
Loading