Skip to content

Commit 20731a2

Browse files
committed
Consider .. in src_paths
1 parent 052da00 commit 20731a2

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

onlinejudge_verify/languages/rust.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def from_dep_kind(cls, kind: str):
133133
depended_target = next(filter(_is_lib_or_proc_macro, depended_package['targets']), None)
134134
if depended_target:
135135
related_source_files = _related_source_files(basedir, _cargo_metadata_by_manifest_path(pathlib.Path(depended_package["manifest_path"])))
136-
ret |= _source_files_in_same_targets(pathlib.Path(depended_target['src_path']), related_source_files)
136+
ret |= _source_files_in_same_targets(pathlib.Path(depended_target['src_path']).resolve(strict=True), related_source_files)
137137
return sorted(ret)
138138

139139

@@ -186,15 +186,15 @@ def _related_source_files(basedir: pathlib.Path, metadata: Dict[str, Any]) -> Di
186186
s = s.rstrip('\\')
187187
s += ' '
188188
s += next(it)
189-
path = pathlib.Path(metadata['workspace_root'], s)
189+
path = pathlib.Path(metadata['workspace_root'], s).resolve(strict=True)
190190
# Ignores paths that don't start with the `basedir`. (e.g. `/dev/null`, `/usr/local/share/foo/bar`)
191191
try:
192192
# `PurePath.is_relative_to` is since Python 3.9.
193193
_ = path.relative_to(basedir)
194194
paths.append(path)
195195
except ValueError:
196196
pass
197-
if paths[:1] == [pathlib.Path(target['src_path'])]:
197+
if paths[:1] == [pathlib.Path(target['src_path']).resolve(strict=True)]:
198198
ret[paths[0]] = frozenset(paths[1:])
199199
break
200200
else:
@@ -289,7 +289,7 @@ def is_verification_file(self, path: pathlib.Path, *, basedir: pathlib.Path) ->
289289
if not package_and_target:
290290
return False
291291
_, target = package_and_target
292-
return _is_bin_or_example_bin(target) and 'PROBLEM' in special_comments.list_special_comments(pathlib.Path(target['src_path']))
292+
return _is_bin_or_example_bin(target) and 'PROBLEM' in special_comments.list_special_comments(path)
293293

294294
def list_environments(self, path: pathlib.Path, *, basedir: pathlib.Path) -> Sequence[RustLanguageEnvironment]:
295295
return [RustLanguageEnvironment()]
@@ -298,10 +298,10 @@ def list_environments(self, path: pathlib.Path, *, basedir: pathlib.Path) -> Seq
298298
def _cargo_metadata(cwd: pathlib.Path) -> Dict[str, Any]:
299299
"""Runs `cargo metadata` for a Cargo.toml file in `cwd` or its parent directories.
300300
301-
:raises ValueError: if `cwd` is not absolute
301+
:raises ValueError: if `cwd` is not absolute or contains `..`
302302
"""
303-
if not cwd.is_absolute():
304-
raise ValueError(f'the `cwd` parameter must be absolute: {cwd}')
303+
if not cwd.is_absolute() or '..' in cwd.parts:
304+
raise ValueError(f'the `cwd` parameter must be absolute and must not contain `..`: {cwd}')
305305

306306
def find_root_manifest_for_wd() -> pathlib.Path:
307307
# https://docs.rs/cargo/0.48.0/cargo/util/important_paths/fn.find_root_manifest_for_wd.html
@@ -341,7 +341,8 @@ def _find_target(
341341
) -> Optional[Tuple[Dict[str, Any], Dict[str, Any]]]:
342342
for package in metadata['packages']:
343343
for target in package['targets']:
344-
if pathlib.Path(target['src_path']) == src_path:
344+
# A `src_path` may contain `..`
345+
if pathlib.Path(target['src_path']).resolve(strict=True) == src_path:
345346
return package, target
346347
return None
347348

tests/test_rust.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,15 @@ def test_gathered_source_files(self) -> None:
222222
}
223223

224224
with tests.utils.load_files_pathlib(files) as tempdir:
225-
expected = [*[tempdir / 'crates' / 'manifests' / name / '..' / '..' / 'sourcefiles' / f'{name}.rs' for name in ['a', 'b', 'c']], tempdir / 'src' / 'lib.rs']
225+
expected = [*[tempdir / 'crates' / 'sourcefiles' / f'{name}.rs' for name in ['a', 'b', 'c']], tempdir / 'src' / 'lib.rs']
226226
actual = sorted(RustLanguage(config=None).list_dependencies(tempdir / 'src' / 'lib.rs', basedir=tempdir))
227227
self.assertEqual(actual, expected)
228228

229-
# FIXME: should pass. something is wrong
230-
# expected = [tempdir / 'crates' / 'manifests' / name / '..' / '..' / 'sourcefiles' / f'{name}.rs' for name in ['a', 'b', 'c']]
231-
# actual = sorted(RustLanguage(config=None).list_dependencies(tempdir / 'crates' / 'sourcefiles' / 'a.rs', basedir=tempdir))
232-
# self.assertEqual(actual, expected)
229+
expected = [tempdir / 'crates' / 'sourcefiles' / f'{name}.rs' for name in ['a', 'b', 'c']]
230+
actual = sorted(RustLanguage(config=None).list_dependencies(tempdir / 'crates' / 'sourcefiles' / 'a.rs', basedir=tempdir))
231+
self.assertEqual(actual, expected)
233232

234-
expected = [tempdir / 'crates' / 'manifests' / 'a' / '..' / '..' / 'sourcefiles' / 'a.rs', tempdir / 'verification' / 'src' / 'bin' / 'aizu-online-judge-itp1-1-a.rs']
233+
expected = [tempdir / 'crates' / 'sourcefiles' / 'a.rs', tempdir / 'verification' / 'src' / 'bin' / 'aizu-online-judge-itp1-1-a.rs']
235234
actual = sorted(RustLanguage(config=None).list_dependencies(tempdir / 'verification' / 'src' / 'bin' / 'aizu-online-judge-itp1-1-a.rs', basedir=tempdir))
236235
self.assertEqual(actual, expected)
237236

0 commit comments

Comments
 (0)