Skip to content

Commit fa839c9

Browse files
author
Alistair Turnbull
committed
Fix: expand directory names but forbid $realpath therein
1 parent a15d1f3 commit fa839c9

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

nancy/__init__.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,8 @@ def process_path(self, obj: Path) -> None:
196196
if self.output == Path("-"):
197197
raise ValueError("cannot output multiple files to stdout ('-')")
198198
debug(f"Entering directory '{obj}'")
199-
os.makedirs(
200-
self.output, exist_ok=True
201-
) # FIXME: `Trees.output` vs `Expand.output_file()`
199+
output_dir = Expand(self, None, obj).output_file()
200+
os.makedirs(output_dir, exist_ok=True)
202201
for child in self.scandir(obj):
203202
if child[0] != "." or self.process_hidden:
204203
self.process_path(obj / child)
@@ -224,12 +223,12 @@ class Expand:
224223
225224
Fields:
226225
trees (Trees):
227-
root (Path): one of `trees.inputs`
226+
root (Optional[Path]): one of `trees.inputs`
228227
path (Path): the `root`-relative `Path`
229228
"""
230229

231230
trees: Trees
232-
root: Path
231+
root: Optional[Path]
233232
path: Path
234233

235234
# The output file relative to `trees.output`.
@@ -243,10 +242,11 @@ class Expand:
243242
def __init__(
244243
self,
245244
trees: Trees,
246-
root: Path,
245+
root: Optional[Path],
247246
path: Path,
248247
):
249-
assert root in trees.inputs, (root, trees)
248+
if root is not None:
249+
assert root in trees.inputs, (root, trees)
250250
self.trees = trees
251251
self.root = root
252252
self.path = path
@@ -265,6 +265,8 @@ def __init__(
265265

266266
def input_file(self):
267267
"""Returns the filesystem input `Path`."""
268+
if self.root is None:
269+
raise ValueError("$realpath is not available for directories")
268270
return self.root / self.path
269271

270272
def output_file(self):

tests/test-files/realpath-in-dirname-src/$realpath/dummy.in

Whitespace-only changes.

tests/test_nancy.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ def test_outputpath_in_filename() -> None:
132132
passing_test(["outputpath-in-filename-src"], "outputpath-in-filename-expected")
133133

134134

135+
def test_realpath_in_dirname_gives_an_error() -> None:
136+
with chdir(tests_dir):
137+
failing_test(
138+
['realpath-in-dirname-src'],
139+
"$realpath is not available for directories",
140+
)
141+
142+
135143
def test_paste_does_not_expand_macros() -> None:
136144
with chdir(tests_dir):
137145
passing_test(["paste-src"], "paste-expected")

0 commit comments

Comments
 (0)