Skip to content

🧪 [testing improvement] Add error path tests for detect_encoding in combine.py#272

Open
Ven0m0 wants to merge 1 commit into
mainfrom
jules-test-combine-encoding-16703043991518026794
Open

🧪 [testing improvement] Add error path tests for detect_encoding in combine.py#272
Ven0m0 wants to merge 1 commit into
mainfrom
jules-test-combine-encoding-16703043991518026794

Conversation

@Ven0m0

@Ven0m0 Ven0m0 commented Jun 28, 2026

Copy link
Copy Markdown
Owner

This PR addresses a testing gap in RaspberryPi/Scripts/combine.py by implementing unit tests for the detect_encoding function.

🎯 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:

  • Environment where chardet is not available.
  • chardet returns None for encoding.
  • chardet successfully 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

…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>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +3 to +12
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Simplify import mechanism ∴ sys.path.insert is cleaner & avoids importlib boilerplate.

Suggested change
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

Comment on lines +14 to +32
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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Use patch.object ∴ cleaner & more robust than string-based patch lookup on sys.modules.

Suggested change
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")

@kilo-code-bot

kilo-code-bot Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (1 files)
  • RaspberryPi/Scripts/test_combine.py

Reviewed by step-3.7-flash-20260528 · Input: 155.8K · Output: 8.3K · Cached: 135.7K

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant