🧪 [testing improvement] Add error path tests for detect_encoding in combine.py#272
🧪 [testing improvement] Add error path tests for detect_encoding in combine.py#272Ven0m0 wants to merge 1 commit into
Conversation
…ombine.py - Add `RaspberryPi/Scripts/test_combine.py` to cover `detect_encoding` functionality. - Test cases include: - `chardet` not installed (fallback to utf-8). - `chardet` returns no encoding (fallback to utf-8). - `chardet` returns a valid encoding. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
There was a problem hiding this comment.
Code Review
Added test_combine.py to test encoding detection in combine.py. Feedback recommends simplifying the import mechanism via sys.path.insert to avoid importlib boilerplate, and using patch.object instead of string-based patch lookups on sys.modules for cleaner and more robust mocking.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| import sys | ||
| from pathlib import Path | ||
| import importlib.util | ||
|
|
||
| # Import combine.py using importlib | ||
| path = Path(__file__).parent / "combine.py" | ||
| spec = importlib.util.spec_from_file_location("combine", path) | ||
| combine = importlib.util.module_from_spec(spec) | ||
| sys.modules["combine"] = combine | ||
| spec.loader.exec_module(combine) |
There was a problem hiding this comment.
Simplify import mechanism ∴ sys.path.insert is cleaner & avoids importlib boilerplate.
| import sys | |
| from pathlib import Path | |
| import importlib.util | |
| # Import combine.py using importlib | |
| path = Path(__file__).parent / "combine.py" | |
| spec = importlib.util.spec_from_file_location("combine", path) | |
| combine = importlib.util.module_from_spec(spec) | |
| sys.modules["combine"] = combine | |
| spec.loader.exec_module(combine) | |
| import sys | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).parent)) | |
| import combine |
| class TestCombine(unittest.TestCase): | ||
| def test_detect_encoding_no_chardet(self): | ||
| """Test fallback to utf-8 when chardet is not available.""" | ||
| with patch("combine.chardet", None): | ||
| self.assertEqual(combine.detect_encoding(b"test"), "utf-8") | ||
|
|
||
| def test_detect_encoding_with_chardet_success(self): | ||
| """Test that encoding from chardet is used when available.""" | ||
| mock_chardet = MagicMock() | ||
| mock_chardet.detect.return_value = {"encoding": "shift_jis"} | ||
| with patch("combine.chardet", mock_chardet): | ||
| self.assertEqual(combine.detect_encoding(b"test"), "shift_jis") | ||
|
|
||
| def test_detect_encoding_with_chardet_none_result(self): | ||
| """Test fallback to utf-8 when chardet returns None for encoding.""" | ||
| mock_chardet = MagicMock() | ||
| mock_chardet.detect.return_value = {"encoding": None} | ||
| with patch("combine.chardet", mock_chardet): | ||
| self.assertEqual(combine.detect_encoding(b"test"), "utf-8") |
There was a problem hiding this comment.
Use patch.object ∴ cleaner & more robust than string-based patch lookup on sys.modules.
| class TestCombine(unittest.TestCase): | |
| def test_detect_encoding_no_chardet(self): | |
| """Test fallback to utf-8 when chardet is not available.""" | |
| with patch("combine.chardet", None): | |
| self.assertEqual(combine.detect_encoding(b"test"), "utf-8") | |
| def test_detect_encoding_with_chardet_success(self): | |
| """Test that encoding from chardet is used when available.""" | |
| mock_chardet = MagicMock() | |
| mock_chardet.detect.return_value = {"encoding": "shift_jis"} | |
| with patch("combine.chardet", mock_chardet): | |
| self.assertEqual(combine.detect_encoding(b"test"), "shift_jis") | |
| def test_detect_encoding_with_chardet_none_result(self): | |
| """Test fallback to utf-8 when chardet returns None for encoding.""" | |
| mock_chardet = MagicMock() | |
| mock_chardet.detect.return_value = {"encoding": None} | |
| with patch("combine.chardet", mock_chardet): | |
| self.assertEqual(combine.detect_encoding(b"test"), "utf-8") | |
| class TestCombine(unittest.TestCase): | |
| def test_detect_encoding_no_chardet(self): | |
| """Test fallback to utf-8 when chardet is not available.""" | |
| with patch.object(combine, "chardet", None): | |
| self.assertEqual(combine.detect_encoding(b"test"), "utf-8") | |
| def test_detect_encoding_with_chardet_success(self): | |
| """Test that encoding from chardet is used when available.""" | |
| mock_chardet = MagicMock() | |
| mock_chardet.detect.return_value = {"encoding": "shift_jis"} | |
| with patch.object(combine, "chardet", mock_chardet): | |
| self.assertEqual(combine.detect_encoding(b"test"), "shift_jis") | |
| def test_detect_encoding_with_chardet_none_result(self): | |
| """Test fallback to utf-8 when chardet returns None for encoding.""" | |
| mock_chardet = MagicMock() | |
| mock_chardet.detect.return_value = {"encoding": None} | |
| with patch.object(combine, "chardet", mock_chardet): | |
| self.assertEqual(combine.detect_encoding(b"test"), "utf-8") |
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Files Reviewed (1 files)
Reviewed by step-3.7-flash-20260528 · Input: 155.8K · Output: 8.3K · Cached: 135.7K |
This PR addresses a testing gap in
RaspberryPi/Scripts/combine.pyby implementing unit tests for thedetect_encodingfunction.🎯 What: The testing gap addressed is the lack of coverage for the fallback logic and error paths in
detect_encoding.📊 Coverage: The following scenarios are now tested:
chardetis not available.chardetreturnsNonefor encoding.chardetsuccessfully detects an encoding.✨ Result: Improved reliability of encoding detection logic and ensured fallback to UTF-8 works as expected.
PR created automatically by Jules for task 16703043991518026794 started by @Ven0m0