aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Haarman <inglorion@chromium.org>2023-08-11 23:52:00 +0000
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-08-14 16:28:55 +0000
commit0eb892bf0e54f2f18174973ad31ec579cb5fa73f (patch)
tree343a564394cbcef780739bbe27431ab98517b7e8
parent6131a0adbb3c0ed059bff054cd72bc4ef006a545 (diff)
downloadtoolchain-utils-0eb892bf0e54f2f18174973ad31ec579cb5fa73f.tar.gz
rust_uprev: use Path.{read,write}_text instead of open().{read,write}
This replaces open(...).read(...) with Path(...).read_text() and open(...).write(...) with Path(...).write_text(...). This avoids cros lint complaining about wanting to wrap the code in a with statement. BUG=None TEST=cros lint; run rust_uprev_test.py Change-Id: Ic3763c09e5f84470aeb36377d0aed92480279a4d Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/4775699 Reviewed-by: George Burgess <gbiv@chromium.org> Tested-by: Bob Haarman <inglorion@chromium.org> Commit-Queue: Bob Haarman <inglorion@chromium.org>
-rwxr-xr-xrust_tools/rust_uprev.py7
-rwxr-xr-xrust_tools/rust_uprev_test.py41
2 files changed, 33 insertions, 15 deletions
diff --git a/rust_tools/rust_uprev.py b/rust_tools/rust_uprev.py
index d7a27bc9..6d6ca5a4 100755
--- a/rust_tools/rust_uprev.py
+++ b/rust_tools/rust_uprev.py
@@ -408,8 +408,8 @@ def update_bootstrap_ebuild(new_bootstrap_version: RustVersion) -> None:
def update_bootstrap_version(
path: PathOrStr, new_bootstrap_version: RustVersion
) -> None:
- # pylint: disable=consider-using-with
- contents = open(path, encoding="utf-8").read()
+ path = Path(path)
+ contents = path.read_text(encoding="utf-8")
contents, subs = re.subn(
r"^BOOTSTRAP_VERSION=.*$",
'BOOTSTRAP_VERSION="%s"' % (new_bootstrap_version,),
@@ -418,8 +418,7 @@ def update_bootstrap_version(
)
if not subs:
raise RuntimeError(f"BOOTSTRAP_VERSION not found in {path}")
- # pylint: disable=consider-using-with
- open(path, "w", encoding="utf-8").write(contents)
+ path.write_text(contents, encoding="utf-8")
logging.info("Rust BOOTSTRAP_VERSION updated to %s", new_bootstrap_version)
diff --git a/rust_tools/rust_uprev_test.py b/rust_tools/rust_uprev_test.py
index 33dbc111..271557e3 100755
--- a/rust_tools/rust_uprev_test.py
+++ b/rust_tools/rust_uprev_test.py
@@ -24,6 +24,23 @@ def _fail_command(cmd, *_args, **_kwargs):
raise err
+def start_mock(obj, *args, **kwargs):
+ """Creates a patcher, starts it, and registers a cleanup to stop it.
+
+ Args:
+ obj:
+ the object to attach the cleanup to
+ *args:
+ passed to mock.patch()
+ **kwargs:
+ passsed to mock.patch()
+ """
+ patcher = mock.patch(*args, **kwargs)
+ val = patcher.start()
+ obj.addCleanup(patcher.stop)
+ return val
+
+
class FetchDistfileTest(unittest.TestCase):
"""Tests rust_uprev.fetch_distfile_from_mirror()"""
@@ -338,26 +355,28 @@ BOOTSTRAP_VERSION="1.2.0"
BOOTSTRAP_VERSION="1.3.6"
"""
+ def setUp(self):
+ self.mock_read_text = start_mock(self, "pathlib.Path.read_text")
+
def test_success(self):
- mock_open = mock.mock_open(read_data=self.ebuild_file_before)
+ self.mock_read_text.return_value = self.ebuild_file_before
# ebuild_file and new bootstrap version are deliberately different
ebuild_file = "/path/to/rust/cros-rustc.eclass"
- with mock.patch("builtins.open", mock_open):
+ with mock.patch("pathlib.Path.write_text") as mock_write_text:
rust_uprev.update_bootstrap_version(
ebuild_file, rust_uprev.RustVersion.parse("1.3.6")
)
- mock_open.return_value.__enter__().write.assert_called_once_with(
- self.ebuild_file_after
- )
+ mock_write_text.assert_called_once_with(
+ self.ebuild_file_after, encoding="utf-8"
+ )
def test_fail_when_ebuild_misses_a_variable(self):
- mock_open = mock.mock_open(read_data="")
+ self.mock_read_text.return_value = ""
ebuild_file = "/path/to/rust/rust-1.3.5.ebuild"
- with mock.patch("builtins.open", mock_open):
- with self.assertRaises(RuntimeError) as context:
- rust_uprev.update_bootstrap_version(
- ebuild_file, rust_uprev.RustVersion.parse("1.2.0")
- )
+ with self.assertRaises(RuntimeError) as context:
+ rust_uprev.update_bootstrap_version(
+ ebuild_file, rust_uprev.RustVersion.parse("1.2.0")
+ )
self.assertEqual(
"BOOTSTRAP_VERSION not found in /path/to/rust/rust-1.3.5.ebuild",
str(context.exception),