Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/notebooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ on:
pull_request:
paths:
- "**.ipynb"
- "tools/**.py"
- ".github/workflows/notebooks.yaml"
# Allow manual runs
workflow_dispatch:

Expand All @@ -26,6 +28,8 @@ jobs:
- uses: actions/setup-python@7f4fc3e22c37d6ff65e88745f38bd3157c663f7c # v4
- name: Fetch main branch
run: git fetch -u origin main:main
- name: Run formatter unit tests
run: python3 -m unittest tools.test_nbfmt
- name: Check notebook formatting
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
Expand Down
4 changes: 2 additions & 2 deletions tools/nbfmt/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ def format_bytes(self, data: Dict[str, Any], file_path: pathlib.Path) -> bytes:
ensure_ascii=False
)

# Clean trailing whitespace on each line
lines = [line.rstrip() for line in formatted_json.splitlines()]
# Split only on JSON formatting newlines, preserving Unicode separators in strings.
lines = [line.rstrip() for line in formatted_json.split("\n")]
clean_json_str = "\n".join(lines) + "\n"

# Match standard HTML entity escape conventions
Expand Down
82 changes: 82 additions & 0 deletions tools/test_nbfmt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Regression tests for the notebook formatter."""

import json
import pathlib
import tempfile
import unittest

from tools.nbfmt.formatter import NotebookFormatter


class TestNotebookFormatter(unittest.TestCase):
"""Checks that formatting preserves notebook content and valid JSON."""

def test_unicode_separators_round_trip(self):
for separator in ("\u0085", "\u2028", "\u2029"):
for source_is_string in (False, True):
with self.subTest(separator=repr(separator), source_is_string=source_is_string):
text = f"before{separator}after"
data = {
"cells": [{
"cell_type": "markdown",
"metadata": {},
"source": text if source_is_string else [text],
}],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 0,
}
formatter = NotebookFormatter()
path = pathlib.Path("example.ipynb")

formatted = formatter.format_bytes(data, path)
parsed = json.loads(formatted)

self.assertEqual("".join(parsed["cells"][0]["source"]), text)
self.assertEqual(formatter.format_bytes(parsed, path), formatted)

def test_unicode_separators_in_outputs_and_metadata(self):
with tempfile.TemporaryDirectory() as temp_dir:
path = pathlib.Path(temp_dir) / "example.ipynb"
text = "before\u0085middle\u2028more\u2029after"
outputs = [{"output_type": "stream", "name": "stdout", "text": [text]}]
data = {
"cells": [{
"cell_type": "code",
"metadata": {},
"source": ["print('example')"],
"execution_count": 1,
"outputs": outputs,
}],
"metadata": {"language_info": {"name": "python", "description": text}},
"nbformat": 4,
"nbformat_minor": 0,
}
path.write_text(json.dumps(data), encoding="utf-8")
formatter = NotebookFormatter()

result = formatter.format_file(path)
self.assertTrue(result.is_formatted)
parsed = json.loads(path.read_bytes())

self.assertEqual(parsed["cells"][0]["outputs"], outputs)
self.assertEqual(parsed["metadata"]["language_info"]["description"], text)
self.assertTrue(formatter.format_file(path, test_only=True).is_formatted)


if __name__ == "__main__":
unittest.main()
Loading