Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

tests: tests for edge case file names (#6400)

Myles Scolnick5d50c275ebe1
diff --git a/pyproject.toml b/pyproject.toml
index 48cf216227..891aad4bd0 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -559,6 +559,9 @@ manager = "uv"
[tool.marimo.keymap]
vimrc = "configs/.vimrc"
+[tool.basedpyright]
+reportUnusedCallResult = false
+
[tool.pixi.workspace]
channels = ["conda-forge"]
platforms = ["osx-arm64", "linux-64"]
diff --git a/tests/_cli/test_file_path.py b/tests/_cli/test_file_path.py
index 30f35d1f97..59a2057651 100644
--- a/tests/_cli/test_file_path.py
+++ b/tests/_cli/test_file_path.py
@@ -9,6 +9,7 @@ from unittest.mock import patch
import click
import pytest
+from docs.blocks import uri_encode_component
from marimo._cli.file_path import (
FileContentReader,
GenericURLReader,
@@ -20,6 +21,7 @@ from marimo._cli.file_path import (
validate_name,
)
from marimo._utils.requests import Response
+from tests.mocks import EDGE_CASE_FILENAMES
temp_dir = tempfile.TemporaryDirectory()
@@ -383,3 +385,77 @@ def test_validate_name_with_relative_and_absolute_paths():
finally:
Path.unlink(rel_file)
Path.unlink(abs_file)
+
+
+ "filename",
+ EDGE_CASE_FILENAMES,
+)
+def test_validate_name_with_edge_case_filenames(tmp_path: Path, filename: str):
+ """Test file path validation with unicode, spaces, and special characters."""
+ file_path = tmp_path / filename
+ file_path.write_text("# test content")
+
+ # Should validate existing files correctly
+ result_path, temp_dir = validate_name(
+ str(file_path), allow_new_file=False, allow_directory=False
+ )
+ assert result_path == str(file_path)
+ assert temp_dir is None # Local file, no temp dir needed
+
+ # Should allow new files with problematic names
+ new_filename = f"new_{filename}"
+ new_path = tmp_path / new_filename
+ result_path, temp_dir = validate_name(
+ str(new_path), allow_new_file=True, allow_directory=False
+ )
+ assert result_path == str(new_path)
+ assert temp_dir is None
+
+
+ "filename",
+ EDGE_CASE_FILENAMES,
+)
+def test_local_file_reader_with_edge_case_filenames(
+ tmp_path: Path, filename: str
+):
+ """Test LocalFileReader with unicode and spaces in filenames."""
+ reader = LocalFileReader()
+ file_path = tmp_path / filename
+ test_content = f"# Test content for {filename}\nprint('hello')"
+ file_path.write_text(test_content, encoding="utf-8")
+
+ assert reader.can_read(str(file_path)) is True
+
+ content, read_filename = reader.read(str(file_path))
+ assert content == test_content
+ assert read_filename == filename
+
+
+ ("url", "expected_filename"),
+ [
+ (
+ f"https://example.com/{uri_encode_component(name)}",
+ uri_encode_component(name),
+ )
+ for name in EDGE_CASE_FILENAMES
+ ],
+)
+def test_generic_url_reader_with_edge_case_filenames(
+ url: str, expected_filename: str
+):
+ """Test GenericURLReader with unicode and spaces in URLs."""
+ reader = GenericURLReader()
+ assert reader.can_read(url) is True
+
+ with patch("marimo._utils.requests.get") as mock_get:
+ mock_get.return_value = Response(
+ 200,
+ b"print('Hello, world!')",
+ {},
+ )
+ content, filename = reader.read(url)
+ assert content == "print('Hello, world!')"
+ assert filename == expected_filename
diff --git a/tests/_server/api/endpoints/test_files.py b/tests/_server/api/endpoints/test_files.py
index b7d75b3a97..7233356907 100644
--- a/tests/_server/api/endpoints/test_files.py
+++ b/tests/_server/api/endpoints/test_files.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import os
import random
+import tempfile
from pathlib import Path
from typing import TYPE_CHECKING
from unittest.mock import Mock, patch
@@ -10,12 +11,14 @@ from unittest.mock import Mock, patch
import msgspec
import pytest
+from marimo._utils.platform import is_windows
from tests._server.conftest import get_session_manager
from tests._server.mocks import (
token_header,
with_session,
with_websocket_session,
)
+from tests.mocks import EDGE_CASE_FILENAMES
from tests.utils import try_assert_n_times
if TYPE_CHECKING:
@@ -463,3 +466,35 @@ def test_endpoints_without_authentication(client: TestClient) -> None:
)
# Should require authentication
assert response.status_code in [401, 403, 422]
+
+
+ is_windows(), reason="Windows doesn't support these edge case filenames"
+)
+@with_session(SESSION_ID)
+def test_rename_with_edge_case_filenames(client: TestClient) -> None:
+ """Test rename endpoint with unicode and spaces in filenames."""
+ with tempfile.TemporaryDirectory() as tmpdir:
+ for filename in EDGE_CASE_FILENAMES:
+ current_filename = get_session_manager(
+ client
+ ).file_router.get_unique_file_key()
+ assert current_filename
+
+ new_path = Path(tmpdir) / filename
+ response = client.post(
+ "/api/kernel/rename",
+ headers=HEADERS,
+ json={
+ "filename": str(new_path),
+ },
+ )
+ assert response.json() == {"success": True}
+
+ def _new_path_exists():
+ assert new_path.exists() # noqa: B023
+ # Ensure content is preserved and readable
+ content = new_path.read_text(encoding="utf-8") # noqa: B023
+ assert "import marimo" in content
+
+ try_assert_n_times(5, _new_path_exists)
diff --git a/tests/_server/test_file_manager_filename.py b/tests/_server/test_file_manager_filename.py
new file mode 100644
index 0000000000..8b483583ec
--- /dev/null
+++ b/tests/_server/test_file_manager_filename.py
@@ -0,0 +1,61 @@
+# Copyright 2024 Marimo. All rights reserved.
+from __future__ import annotations
+
+from pathlib import Path
+
+import pytest
+
+from marimo._server.api.status import HTTPException
+from marimo._server.file_manager import AppFileManager
+from tests.mocks import EDGE_CASE_FILENAMES
+
+
+class TestAppFileManagerFilenames:
+ """Test AppFileManager filename handling with unicode, spaces, and special characters."""
+
+ @pytest.mark.parametrize("filename", EDGE_CASE_FILENAMES)
+ def test_app_file_manager_operations_with_edge_case_filenames(
+ self, filename: str, tmp_path: Path
+ ) -> None:
+ """Test AppFileManager core operations with problematic filenames."""
+ file_path = tmp_path / filename
+ content = "import marimo as mo\n\napp = mo.App()\n\[email protected]\ndef __():\n return\n"
+
+ # Create and test initialization
+ Path(file_path).write_text(content, encoding="utf-8")
+ file_manager = AppFileManager(file_path)
+
+ assert file_manager.filename == str(file_path)
+ assert file_manager.path == str(file_path)
+ assert file_manager.is_notebook_named
+
+ # Test reading
+ read_content = file_manager.read_file()
+ assert read_content == content
+
+ # Test rename to new problematic filename
+ new_filename = f"new_{filename}"
+ new_path = tmp_path / new_filename
+
+ file_manager.rename(str(new_path))
+ assert file_manager.filename == str(new_path)
+ assert Path(new_path).exists()
+ assert not Path(file_path).exists()
+
+ def test_app_file_manager_rename_collision_raises_error(
+ self, tmp_path: Path
+ ) -> None:
+ """Test that renaming to existing filename raises HTTPException."""
+ original_path = tmp_path / "original.py"
+ target_path = tmp_path / "café notebook.py"
+
+ # Create both files
+ Path(original_path).write_text("# Original", encoding="utf-8")
+ Path(target_path).write_text("# Target", encoding="utf-8")
+
+ file_manager = AppFileManager(original_path)
+
+ with pytest.raises(HTTPException) as exc_info:
+ file_manager.rename(str(target_path))
+
+ assert "already exists" in str(exc_info.value.detail)
diff --git a/tests/_server/test_templates_filename.py b/tests/_server/test_templates_filename.py
new file mode 100644
index 0000000000..a13f15a494
--- /dev/null
+++ b/tests/_server/test_templates_filename.py
@@ -0,0 +1,87 @@
+# Copyright 2024 Marimo. All rights reserved.
+from __future__ import annotations
+
+import json
+
+import pytest
+
+from marimo._ast.app_config import _AppConfig
+from marimo._config.config import MarimoConfig, PartialMarimoConfig
+from marimo._server.templates.templates import (
+ _get_mount_config,
+ _html_escape,
+)
+from marimo._server.tokens import SkewProtectionToken
+from tests.mocks import EDGE_CASE_FILENAMES
+
+
+class TestTemplateFilenameHandling:
+ """Test template filename handling with unicode, spaces, and special characters."""
+
+ @pytest.mark.parametrize(
+ ("filename", "expected_contains"),
+ [
+ # Basic cases
+ ("test.py", "test.py"),
+ # HTML special characters that need escaping
+ ("<script>alert('xss')</script>.py", "&lt;script&gt;"),
+ ("test&example.py", "&amp;"),
+ ('test"quotes".py', "&quot;"),
+ ("test'quotes'.py", "&#x27;"),
+ # Unicode characters (should be preserved)
+ ("tést.py", "tést.py"),
+ ("café.py", "café.py"),
+ ("测试.py", "测试.py"),
+ ("🚀notebook.py", "🚀notebook.py"),
+ # Spaces (should be preserved)
+ ("test file.py", "test file.py"),
+ ("my notebook.py", "my notebook.py"),
+ # Mixed unicode and spaces
+ ("café notebook.py", "café notebook.py"),
+ ("测试 file.py", "测试 file.py"),
+ # Complex injection attempts
+ ("test<>&'\"file.py", "&lt;&gt;&amp;&#x27;&quot;"),
+ ],
+ )
+ def test_html_escape_function(
+ self, filename: str, expected_contains: str
+ ) -> None:
+ """Test _html_escape function properly escapes HTML while preserving unicode."""
+ result = _html_escape(filename)
+ assert expected_contains in result
+
+ # Should not contain unescaped HTML
+ assert "<script>" not in result
+ assert "onerror=" not in result
+
+ @pytest.mark.parametrize(
+ "filename",
+ [*EDGE_CASE_FILENAMES, None],
+ )
+ def test_get_mount_config_filename_handling(
+ self, filename: str | None
+ ) -> None:
+ """Test _get_mount_config function with problematic filenames."""
+ server_token = SkewProtectionToken("test-token")
+ user_config = MarimoConfig()
+ config_overrides = PartialMarimoConfig()
+ app_config = _AppConfig()
+
+ result = _get_mount_config(
+ filename=filename,
+ mode="edit",
+ server_token=server_token,
+ user_config=user_config,
+ config_overrides=config_overrides,
+ app_config=app_config,
+ )
+ # Remove the last ','
+ last_comma_index = result.rfind(",")
+ result = result[:last_comma_index] + result[last_comma_index + 1 :]
+
+ # Should be valid JSON
+ config_data = json.loads(result)
+
+ # Filename should be properly handled
+ expected_filename = filename or ""
+ assert config_data["filename"] == expected_filename
diff --git a/tests/_server/test_utils_filename.py b/tests/_server/test_utils_filename.py
new file mode 100644
index 0000000000..fd65884720
--- /dev/null
+++ b/tests/_server/test_utils_filename.py
@@ -0,0 +1,66 @@
+# Copyright 2024 Marimo. All rights reserved.
+from __future__ import annotations
+
+import os
+
+import pytest
+
+from marimo._server.utils import canonicalize_filename
+
+
+class TestCanonicalizeFilename:
+ """Test filename canonicalization with unicode, spaces, and special characters."""
+
+ @pytest.mark.parametrize(
+ ("input_filename", "should_add_py"),
+ [
+ # Basic cases
+ ("test", True),
+ ("test.py", False),
+ ("test.md", False),
+ ("test.qmd", False),
+ # Unicode characters
+ ("tést", True),
+ ("café.py", False),
+ ("测试", True),
+ ("🚀notebook.py", False),
+ # Spaces
+ ("test file", True),
+ ("my notebook.py", False),
+ # Mixed unicode and spaces
+ ("café notebook", True),
+ ("测试 file.py", False),
+ # Special characters
+ ("test-file", True),
+ ("test_file.py", False),
+ # Edge cases
+ ("", True),
+ (".", True),
+ # User paths
+ ("~/test", True),
+ ("~/test.py", False),
+ ("~/café notebook", True),
+ ("~/测试 file.py", False),
+ ],
+ )
+ def test_canonicalize_filename(
+ self, input_filename: str, should_add_py: bool
+ ) -> None:
+ """Test that canonicalize_filename handles problematic filenames correctly."""
+ result = canonicalize_filename(input_filename)
+
+ # Should expand user path
+ if should_add_py:
+ expected = os.path.expanduser(input_filename + ".py")
+ else:
+ expected = os.path.expanduser(input_filename)
+
+ assert result == expected
+
+ # Should not contain ~ in the result if it was there
+ if "~" in input_filename:
+ assert "~" not in result
+
+ # Result should be a valid string
+ assert isinstance(result, str)
+ assert len(result) > 0
diff --git a/tests/_utils/test_marimo_path.py b/tests/_utils/test_marimo_path.py
index fedd9002ae..6f768ae18d 100644
--- a/tests/_utils/test_marimo_path.py
+++ b/tests/_utils/test_marimo_path.py
@@ -6,6 +6,7 @@ from pathlib import Path
import pytest
from marimo._utils.marimo_path import MarimoPath
+from tests.mocks import EDGE_CASE_FILENAMES
def test_init():
@@ -92,3 +93,66 @@ def test_properties():
# Test last_modified
assert mp.last_modified == os.path.getmtime(__file__)
+
+
+ "filename",
+ [
+ *EDGE_CASE_FILENAMES,
+ "café.qmd",
+ "🚀 my notebook.md",
+ ],
+)
+def test_marimo_path_with_edge_case_filenames(tmp_path: Path, filename: str):
+ """Test MarimoPath with unicode, spaces, and special characters."""
+ file_path = tmp_path / filename
+ file_path.write_text("# test content", encoding="utf-8")
+
+ # Should be able to create MarimoPath
+ mp = MarimoPath(file_path)
+ assert mp.path == file_path
+ assert mp.is_valid()
+
+ # Should handle file operations correctly
+ content = mp.read_text()
+ assert content == "# test content"
+
+ # Should handle properties correctly
+ assert mp.short_name == filename
+ assert filename in mp.absolute_name
+
+ # Should handle rename correctly
+ new_filename = f"new_{filename}"
+ new_path = tmp_path / new_filename
+ mp.rename(new_path)
+ assert not file_path.exists()
+ assert new_path.exists()
+
+
+ "filename",
+ [
+ # Valid files
+ "café.md",
+ "测试.qmd",
+ "café notebook.markdown",
+ *EDGE_CASE_FILENAMES,
+ ],
+)
+def test_is_valid_path_with_edge_case_filenames(filename: str):
+ """Test MarimoPath.is_valid_path with unicode and spaces."""
+ assert MarimoPath.is_valid_path(filename)
+
+
+ "filename",
+ [
+ "tést.txt",
+ "café.doc",
+ "测试.xyz",
+ "test file.pdf",
+ "test.py.txt",
+ ],
+)
+def test_is_valid_path_with_invalid_filenames(filename: str):
+ assert not MarimoPath.is_valid_path(filename)
diff --git a/tests/mocks.py b/tests/mocks.py
index dcecd89526..d785fcd0a6 100644
--- a/tests/mocks.py
+++ b/tests/mocks.py
@@ -11,6 +11,7 @@ import pytest
from marimo import __version__
from marimo._utils.paths import maybe_make_dirs
+from marimo._utils.platform import is_windows
class ToText(HTMLParser):
@@ -127,3 +128,25 @@ def _sanitize_version(output: str) -> str:
return output.replace(f"{__version__} (editable)", "0.0.0").replace(
f"{__version__}", "0.0.0"
)
+
+
+NON_WINDOWS_EDGE_CASE_FILENAMES = [
+ "test<script>.py",
+ 'test"quotes".py',
+]
+
+
+EDGE_CASE_FILENAMES = [
+ # Unicode characters
+ "tést.py",
+ "café.py",
+ "测试.py",
+ "🚀notebook.py",
+ # Spaces
+ "test file with spaces.py",
+ # Mixed unicode and spaces
+ "café notebook.py",
+ "测试 file.py",
+ # Injection attempts
+ *(NON_WINDOWS_EDGE_CASE_FILENAMES if not is_windows() else []),
+]