diff options
author | Treehugger Robot <android-test-infra-autosubmit@system.gserviceaccount.com> | 2023-12-05 00:46:59 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2023-12-05 00:46:59 +0000 |
commit | 34ab548c04cab6671c71a9f4bf4d23ef2e5a4d04 (patch) | |
tree | 883fde269b4e3c5142d0dc8fbb4eb1dd1826f124 | |
parent | 4f99ffe271d7944ed078ce36849f9c551842bb64 (diff) | |
parent | 482fc0e564ba551ef71446e09ee51930f365ab81 (diff) | |
download | external_updater-34ab548c04cab6671c71a9f4bf4d23ef2e5a4d04.tar.gz |
Merge "Remove mangling of command line paths." into main am: 482fc0e564
Original change: https://android-review.googlesource.com/c/platform/tools/external_updater/+/2855445
Change-Id: Ia85366a9683a4a19fc20fecc0177fa2c400bec0e
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r-- | fileutils.py | 30 | ||||
-rw-r--r-- | test_fileutils.py | 50 |
2 files changed, 44 insertions, 36 deletions
diff --git a/fileutils.py b/fileutils.py index 8bbd42c..8e56cc2 100644 --- a/fileutils.py +++ b/fileutils.py @@ -107,26 +107,20 @@ def get_absolute_project_path(proj_path: Path) -> Path: def resolve_command_line_paths(paths: list[str]) -> list[Path]: - """Expand paths via globs. + """Resolves project paths provided by the command line. - Paths are resolved in one of two ways: external-relative, or absolute. - - Absolute paths are not modified. - - External-relative paths are converted to absolute paths where the input path is - relative to the //external directory of the tree defined by either - $ANDROID_BUILD_TOP or, if not set, the CWD. - - Both forms of paths will glob expand after being resolved to an absolute path. + Both relative and absolute paths are resolved to fully qualified paths and returned. + If any path does not exist relative to the CWD, a message will be printed and that + path will be pruned from the list. """ - # We want to use glob to get all the paths, so we first convert to absolute. - abs_paths = [get_absolute_project_path(Path(path)) - for path in paths] - result = [path for abs_path in abs_paths - for path in sorted(glob.glob(str(abs_path)))] - if paths and not result: - print(f'Could not find any valid paths in {str(paths)}') - return [Path(p) for p in result] + resolved: list[Path] = [] + for path_str in paths: + path = Path(path_str) + if not path.exists(): + print(f"Provided path {path} ({path.resolve()}) does not exist. Skipping.") + else: + resolved.append(path.resolve()) + return resolved def get_metadata_path(proj_path: Path) -> Path: diff --git a/test_fileutils.py b/test_fileutils.py index 9667470..6056a48 100644 --- a/test_fileutils.py +++ b/test_fileutils.py @@ -16,7 +16,6 @@ """Unit tests for fileutils.""" import contextlib -import os import unittest from pathlib import Path from tempfile import TemporaryDirectory @@ -33,32 +32,47 @@ class ResolveCommandLinePathsTest(unittest.TestCase): def test_absolute_paths(self) -> None: """Tests that absolute paths are resolved correctly.""" - # The current implementation will remove paths which do not exist from the - # output, so the test inputs need to be paths that exist on the system running - # the test. - self.assertListEqual( - [Path("/usr/lib"), Path("/bin")], - fileutils.resolve_command_line_paths(["/usr/lib", "/bin"]), - ) - - def test_external_relative_paths(self) -> None: - """Tests that paths relative to //external are resolved correctly.""" with TemporaryDirectory() as temp_dir_str: temp_dir = Path(temp_dir_str) + a = temp_dir / "a" + b = temp_dir / "external" / "b" + a.mkdir() + b.mkdir(parents=True) + self.assertListEqual( + [a, b], + fileutils.resolve_command_line_paths( + [str(a), str(b), "/does/not/exist"] + ), + ) + + def test_relative_paths(self) -> None: + """Tests that relative paths are resolved correctly.""" + with TemporaryDirectory() as temp_dir_str: + # Make this absolute so the CWD change later doesn't break it. + temp_dir = Path(temp_dir_str).resolve() external = temp_dir / "external" external.mkdir() a = external / "a" - b = external / "b" a.mkdir() + + working_dir = temp_dir / "cwd" + working_dir.mkdir() + b = working_dir / "b" b.mkdir() - old_env = dict(os.environ) - try: - os.environ["ANDROID_BUILD_TOP"] = str(temp_dir) + with contextlib.chdir(working_dir): self.assertListEqual( - [a, b], fileutils.resolve_command_line_paths(["a", "b"]) + [a, working_dir, b], + fileutils.resolve_command_line_paths( + [ + # These will all be resolved as absolute paths and returned. + "../external/a", + ".", + "b", + # This one doesn't exist. It will be pruned from the result. + "c", + ] + ), ) - finally: - os.environ = old_env # type: ignore class FindTreeContainingTest(unittest.TestCase): |