Skip to content

Commit f1da1d8

Browse files
fix(web): incorrect encoding when viewing files containing non-ASCII characters (#4570)
Co-authored-by: Jo <46752250+georgesittas@users.noreply.github.com>
1 parent 3e36c71 commit f1da1d8

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

tests/web/test_main.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ def test_write_file(client: TestClient, project_tmp_path: Path) -> None:
106106
}
107107

108108

109+
def test_write_file_non_ascii(client: TestClient, project_tmp_path: Path) -> None:
110+
response = client.post("/api/files/foo.txt", json={"content": "何か良いこと"})
111+
file = _get_file_with_content(project_tmp_path / "foo.txt", "foo.txt")
112+
assert response.status_code == 204
113+
assert file.dict() == {
114+
"name": "foo.txt",
115+
"path": "foo.txt",
116+
"extension": ".txt",
117+
"content": "何か良いこと",
118+
}
119+
120+
109121
def test_update_file(client: TestClient, project_tmp_path: Path) -> None:
110122
txt_file = project_tmp_path / "foo.txt"
111123
txt_file.write_text("bar")
@@ -212,7 +224,12 @@ def test_create_directory(client: TestClient, project_tmp_path: Path) -> None:
212224
response = client.post("/api/directories/new_dir")
213225
assert response.status_code == 200
214226
assert (project_tmp_path / "new_dir").exists()
215-
assert response.json() == {"directories": [], "files": [], "name": "new_dir", "path": "new_dir"}
227+
assert response.json() == {
228+
"directories": [],
229+
"files": [],
230+
"name": "new_dir",
231+
"path": "new_dir",
232+
}
216233

217234

218235
def test_create_directory_already_exists(client: TestClient, project_tmp_path: Path) -> None:
@@ -494,7 +511,9 @@ def test_delete_environment_failure(
494511
client: TestClient, web_sushi_context: Context, mocker: MockerFixture
495512
):
496513
mocker.patch.object(
497-
web_sushi_context.state_sync, "invalidate_environment", side_effect=Exception("Some error")
514+
web_sushi_context.state_sync,
515+
"invalidate_environment",
516+
side_effect=Exception("Some error"),
498517
)
499518

500519
response = client.delete("/api/environments/test")

web/server/api/endpoints/files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def walk_path(
168168
def _get_file_with_content(file_path: Path, relative_path: str) -> models.File:
169169
"""Get a file, including its contents."""
170170
try:
171-
content = file_path.read_text()
171+
content = file_path.read_text(encoding="utf-8")
172172
except FileNotFoundError as e:
173173
raise e
174174
except Exception:

0 commit comments

Comments
 (0)