diff options
author | Bob Haarman <inglorion@chromium.org> | 2021-09-23 22:18:10 +0000 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2021-09-25 01:10:16 +0000 |
commit | e98100768cf8ef916c2b0701a708156a7850cb3e (patch) | |
tree | ae7bc6d41e95fe290a53748b108963145d22d588 | |
parent | 391b1b039c028cb1d37cca6b77d4bfde63f95520 (diff) | |
download | toolchain-utils-e98100768cf8ef916c2b0701a708156a7850cb3e.tar.gz |
rust_uprev_test: mock equery w commands
rust_uprev uses `equery w` to find the ebuild file for a package.
This depends on filesystem state and so should not affect the test.
This change mocks these calls so that they return specified values
instead of actually running commands and accessing the filesystem.
BUG=None
TEST=Ran the tests outside the chroot, where equery does not exist
Change-Id: Id48a0e063f9bf112888d378d549420f008fdcb4f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/3182629
Tested-by: Bob Haarman <inglorion@chromium.org>
Reviewed-by: George Burgess <gbiv@chromium.org>
Reviewed-by: Michael Benfield <mbenfield@google.com>
Commit-Queue: Bob Haarman <inglorion@chromium.org>
-rwxr-xr-x | rust_tools/rust_uprev.py | 18 | ||||
-rwxr-xr-x | rust_tools/rust_uprev_test.py | 14 |
2 files changed, 24 insertions, 8 deletions
diff --git a/rust_tools/rust_uprev.py b/rust_tools/rust_uprev.py index 16fb0da8..492729ea 100755 --- a/rust_tools/rust_uprev.py +++ b/rust_tools/rust_uprev.py @@ -46,6 +46,9 @@ from pathlib import Path from typing import Any, Callable, Dict, List, NamedTuple, Optional, T, Tuple from llvm_tools import chroot, git + +EQUERY = 'equery' +GSUTIL = 'gsutil.py' RUST_PATH = Path( '/mnt/host/source/src/third_party/chromiumos-overlay/dev-lang/rust') @@ -90,6 +93,11 @@ class RustVersion(NamedTuple): int(m.group('patch'))) +def find_ebuild_for_package(name: str) -> os.PathLike: + """Returns the path to the ebuild for the named package.""" + return get_command_output([EQUERY, 'w', name]) + + def find_ebuild_path(directory: Path, name: str, version: Optional[RustVersion] = None) -> Path: @@ -252,7 +260,7 @@ def parse_commandline_args() -> argparse.Namespace: def prepare_uprev(rust_version: RustVersion, template: Optional[RustVersion] ) -> Optional[Tuple[RustVersion, str, RustVersion]]: if template is None: - ebuild_path = get_command_output(['equery', 'w', 'rust']) + ebuild_path = find_ebuild_for_package('rust') ebuild_name = os.path.basename(ebuild_path) template_version = RustVersion.parse_from_ebuild(ebuild_name) else: @@ -354,7 +362,7 @@ def flip_mirror_in_ebuild(ebuild_file: Path, add: bool) -> None: def ebuild_actions(package: str, actions: List[str], sudo: bool = False) -> None: - ebuild_path_inchroot = get_command_output(['equery', 'w', package]) + ebuild_path_inchroot = find_ebuild_for_package(package) cmd = ['ebuild', ebuild_path_inchroot] + actions if sudo: cmd = ['sudo'] + cmd @@ -507,7 +515,7 @@ def remove_rust_bootstrap_version(version: RustVersion, prefix = f'rust-bootstrap-{version}' run_step('remove old bootstrap ebuild', lambda: remove_files( f'{prefix}*.ebuild', rust_bootstrap_path())) - ebuild_file = get_command_output(['equery', 'w', 'rust-bootstrap']) + ebuild_file = find_ebuild_for_package('rust-bootstrap') run_step('update bootstrap manifest to delete old version', lambda: update_manifest(ebuild_file)) @@ -532,7 +540,7 @@ def remove_rust_uprev(rust_version: Optional[RustVersion], 'remove patches', lambda: remove_files( f'files/rust-{delete_version}-*.patch', RUST_PATH)) run_step('remove ebuild', lambda: remove_files(delete_ebuild, RUST_PATH)) - ebuild_file = get_command_output(['equery', 'w', 'rust']) + ebuild_file = find_ebuild_for_package('rust') run_step('update manifest to delete old version', lambda: update_manifest( ebuild_file)) run_step('remove version from rust packages', lambda: update_rust_packages( @@ -561,7 +569,7 @@ def create_new_repo(rust_version: RustVersion) -> None: def build_cross_compiler() -> None: # Get target triples in ebuild - rust_ebuild = get_command_output(['equery', 'w', 'rust']) + rust_ebuild = find_ebuild_for_package('rust') with open(rust_ebuild, encoding='utf-8') as f: contents = f.read() diff --git a/rust_tools/rust_uprev_test.py b/rust_tools/rust_uprev_test.py index 7b3c9e66..18e8d9e8 100755 --- a/rust_tools/rust_uprev_test.py +++ b/rust_tools/rust_uprev_test.py @@ -17,6 +17,7 @@ from unittest import mock from llvm_tools import git import rust_uprev +from rust_uprev import RustVersion class FindEbuildPathTest(unittest.TestCase): @@ -103,9 +104,12 @@ class PrepareUprevTest(unittest.TestCase): @mock.patch.object(rust_uprev, 'find_ebuild_for_rust_version', return_value='/path/to/ebuild') + @mock.patch.object(rust_uprev, + 'get_rust_bootstrap_version', + return_value=RustVersion(0, 41, 12)) @mock.patch.object(rust_uprev, 'get_command_output') def test_return_none_with_template_larger_than_input(self, mock_command, - _mock_find_ebuild): + *_args): ret = rust_uprev.prepare_uprev(rust_version=self.version_old, template=self.version_new) self.assertIsNone(ret) @@ -129,10 +133,13 @@ class PrepareUprevTest(unittest.TestCase): mock_command.assert_called_once_with(['equery', 'w', 'rust']) mock_exists.assert_not_called() + @mock.patch.object(rust_uprev, + 'get_rust_bootstrap_version', + return_value=RustVersion(0, 41, 12)) @mock.patch.object(os.path, 'exists') @mock.patch.object(rust_uprev, 'get_command_output') def test_return_none_with_ebuild_larger_than_input(self, mock_command, - mock_exists): + mock_exists, *_args): mock_command.return_value = f'/path/to/rust/rust-{self.version_new}.ebuild' ret = rust_uprev.prepare_uprev(rust_version=self.version_old, template=None) @@ -350,8 +357,9 @@ class RustUprevOtherStagesTests(unittest.TestCase): ['git', 'add', f'rust-{self.new_version}.ebuild'], cwd=rust_uprev.RUST_PATH) + @mock.patch.object(rust_uprev, 'find_ebuild_for_package') @mock.patch.object(subprocess, 'check_call') - def test_remove_rust_bootstrap_version(self, mock_call): + def test_remove_rust_bootstrap_version(self, mock_call, *_args): bootstrap_path = os.path.join(rust_uprev.RUST_PATH, '..', 'rust-bootstrap') rust_uprev.remove_rust_bootstrap_version(self.old_version, lambda *x: ()) mock_call.has_calls([ |