aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Haarman <inglorion@chromium.org>2024-01-24 17:47:10 +0000
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2024-01-25 00:04:06 +0000
commit9f1cac16955bb62fec78664999f55c22eeed499e (patch)
treeb3a356dce0f928514a501b4bbd658dcdffe6e1da
parentda46b24088738464c772eb9b5c00ca80ac91bd1d (diff)
downloadtoolchain-utils-9f1cac16955bb62fec78664999f55c22eeed499e.tar.gz
rust_uprev: rewrite find_rust_versions() to use Path instead of os.*
This is a small refactor ahead of a change to the logic. The rewritten find_rust_versions() returns a list of tuples of (RustVersion, Path) instead of (RustVersion, str) as before, and updates the call sites and test to match. BUG=b:318424456 TEST=presubmit type checks and tests Change-Id: If5e26ee3c40265031c43fe68d921d43405a44e3a Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/5233609 Reviewed-by: George Burgess <gbiv@chromium.org> Commit-Queue: Bob Haarman <inglorion@chromium.org> Tested-by: Bob Haarman <inglorion@chromium.org>
-rwxr-xr-xrust_tools/rust_uprev.py14
-rwxr-xr-xrust_tools/rust_uprev_test.py24
2 files changed, 21 insertions, 17 deletions
diff --git a/rust_tools/rust_uprev.py b/rust_tools/rust_uprev.py
index c0017186..bc64761c 100755
--- a/rust_tools/rust_uprev.py
+++ b/rust_tools/rust_uprev.py
@@ -401,13 +401,13 @@ def prepare_uprev(
return None
logging.info(
- "Template Rust version is %s (ebuild: %r)",
+ "Template Rust version is %s (ebuild: %s)",
template,
ebuild_path,
)
logging.info("rust-bootstrap version is %s", bootstrap_version)
- return PreparedUprev(template, Path(ebuild_path), bootstrap_version)
+ return PreparedUprev(template, ebuild_path, bootstrap_version)
def create_ebuild(
@@ -933,11 +933,11 @@ def create_rust_uprev(
)
-def find_rust_versions() -> List[Tuple[RustVersion, str]]:
+def find_rust_versions() -> List[Tuple[RustVersion, Path]]:
return [
- (RustVersion.parse_from_ebuild(x), os.path.join(RUST_PATH, x))
- for x in os.listdir(RUST_PATH)
- if x.endswith(".ebuild")
+ (RustVersion.parse_from_ebuild(ebuild), ebuild)
+ for ebuild in RUST_PATH.iterdir()
+ if ebuild.suffix == ".ebuild"
]
@@ -948,7 +948,7 @@ def find_oldest_rust_version() -> RustVersion:
return min(rust_versions)[0]
-def find_ebuild_for_rust_version(version: RustVersion) -> str:
+def find_ebuild_for_rust_version(version: RustVersion) -> Path:
rust_ebuilds = [
ebuild for x, ebuild in find_rust_versions() if x == version
]
diff --git a/rust_tools/rust_uprev_test.py b/rust_tools/rust_uprev_test.py
index f93c18fd..1aca2839 100755
--- a/rust_tools/rust_uprev_test.py
+++ b/rust_tools/rust_uprev_test.py
@@ -467,7 +467,7 @@ class PrepareUprevTest(unittest.TestCase):
@mock.patch.object(
rust_uprev,
"find_ebuild_for_rust_version",
- return_value="/path/to/ebuild",
+ return_value=Path("/path/to/ebuild"),
)
@mock.patch.object(rust_uprev, "find_ebuild_path")
@mock.patch.object(rust_uprev, "get_command_output")
@@ -858,21 +858,25 @@ class RustUprevOtherStagesTests(unittest.TestCase):
ebuild_path.parent.joinpath(f"rust-{self.new_version}.ebuild"),
)
- @mock.patch.object(os, "listdir")
- def test_find_oldest_rust_version_pass(self, mock_ls):
+ @mock.patch("rust_uprev.find_rust_versions")
+ def test_find_oldest_rust_version_pass(self, rust_versions):
oldest_version_name = f"rust-{self.old_version}.ebuild"
- mock_ls.return_value = [
- oldest_version_name,
- f"rust-{self.current_version}.ebuild",
- f"rust-{self.new_version}.ebuild",
+ rust_versions.return_value = [
+ (self.old_version, oldest_version_name),
+ (self.current_version, f"rust-{self.current_version}.ebuild"),
+ (self.new_version, f"rust-{self.new_version}.ebuild"),
]
actual = rust_uprev.find_oldest_rust_version()
expected = self.old_version
self.assertEqual(expected, actual)
- @mock.patch.object(os, "listdir")
- def test_find_oldest_rust_version_fail_with_only_one_ebuild(self, mock_ls):
- mock_ls.return_value = [f"rust-{self.new_version}.ebuild"]
+ @mock.patch("rust_uprev.find_rust_versions")
+ def test_find_oldest_rust_version_fail_with_only_one_ebuild(
+ self, rust_versions
+ ):
+ rust_versions.return_value = [
+ (self.new_version, f"rust-{self.new_version}.ebuild"),
+ ]
with self.assertRaises(RuntimeError) as context:
rust_uprev.find_oldest_rust_version()
self.assertEqual(