Skip to content

Commit 7348c5d

Browse files
Make File.is_installed binary-safe
File.is_installed compared the installed copy with the source using read_text(), which raises UnicodeDecodeError for files that are not valid UTF-8. install() copies bytes with shutil.copyfile, so compare bytes as well. Also report a content mismatch as such. Until now the message said that the file does not exist even when it existed with different contents. Fixes #782 Signed-off-by: Max Freedom Pollard <[email protected]>
1 parent ea9db35 commit 7348c5d

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

‎src/cloudai/_core/installables/file.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ def uninstall(self, installer: "BaseInstaller") -> InstallStatusResult:
6262

6363
def is_installed(self, installer: "BaseInstaller") -> InstallStatusResult:
6464
installed_path = installer.system.install_path / self.src.name
65-
if installed_path.exists() and installed_path.read_text() == self.src.read_text():
66-
self.installed_path = installed_path
67-
return InstallStatusResult(True)
68-
return InstallStatusResult(False, f"File {installed_path} does not exist")
65+
if not installed_path.exists():
66+
return InstallStatusResult(False, f"File {installed_path} does not exist")
67+
if installed_path.read_bytes() != self.src.read_bytes():
68+
return InstallStatusResult(False, f"File {installed_path} differs from {self.src}")
69+
self.installed_path = installed_path
70+
return InstallStatusResult(True)
6971

7072
def mark_as_installed(self, installer: "BaseInstaller") -> InstallStatusResult:
7173
self.installed_path = installer.system.install_path / self.src.name

‎tests/core/installables/test_file.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,21 @@ def test_is_installed_checks_content(installer: BaseInstaller, f: File):
5858

5959
res = f.is_installed(installer)
6060
assert not res.success
61+
assert res.message == f"File {f.installed_path} differs from {f.src}"
62+
63+
64+
def test_is_installed_missing_file(installer: BaseInstaller, f: File):
65+
res = f.is_installed(installer)
66+
assert not res.success
67+
assert res.message == f"File {installer.system.install_path / f.src.name} does not exist"
68+
69+
70+
def test_is_installed_binary_file(installer: BaseInstaller, tmp_path: Path):
71+
src = tmp_path / "file.bin"
72+
src.write_bytes(b"\xff\xfe\x00 not valid UTF-8")
73+
f = File(src)
74+
f.install(installer)
75+
76+
res = f.is_installed(installer)
77+
assert res.success
78+
assert f.installed_path == installer.system.install_path / src.name

0 commit comments

Comments
 (0)