From 80f032c70dcb52c2b34ec83df736e04aec738d24 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 8 Mar 2026 09:25:53 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20[testing]=20add=20read=5Flines?= =?UTF-8?q?=20edge=20case=20tests=20and=20fix=20sanitize=5Ffilename=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added test_read_lines to verify happy path and line stripping. - Added test_read_lines_file_not_found to cover the OSError catch block in read_lines, asserting it returns None and logs to stderr. - Fixed test_sanitize_filename_without_name_special_urls which had an incorrect assertion for URLs with ports (colons are sanitized to hyphens). - Updated Scripts/test_common.py imports to include io, tempfile, and patch. Co-authored-by: Ven0m0 <82972344+Ven0m0@users.noreply.github.com> --- Scripts/test_common.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/Scripts/test_common.py b/Scripts/test_common.py index 86e8ef1..1515d62 100644 --- a/Scripts/test_common.py +++ b/Scripts/test_common.py @@ -1,13 +1,16 @@ import unittest import sys +import io +import tempfile from pathlib import Path import hashlib +from unittest.mock import patch # Add current directory to path if str(Path(__file__).parent) not in sys.path: sys.path.append(str(Path(__file__).parent)) -from common import sanitize_filename, is_valid_domain, is_adguard_rule +from common import sanitize_filename, is_valid_domain, is_adguard_rule, read_lines class TestCommon(unittest.TestCase): @@ -68,9 +71,9 @@ def test_sanitize_filename_without_name_special_urls(self): # URL with port url = "http://example.com:8080/list.txt" filename = sanitize_filename(url) - self.assertTrue(filename.startswith("example-com:8080-")) + self.assertTrue(filename.startswith("example-com-8080-")) expected_hash = hashlib.sha256(url.encode("utf-8")).hexdigest()[:12] - self.assertEqual(filename, f"example-com:8080-{expected_hash}.txt") + self.assertEqual(filename, f"example-com-8080-{expected_hash}.txt") def test_is_valid_domain(self): self.assertTrue(is_valid_domain("example.com")) @@ -87,8 +90,26 @@ def test_is_adguard_rule(self): self.assertTrue(is_adguard_rule("! comment")) self.assertFalse(is_adguard_rule("example.com")) + def test_read_lines(self): + with tempfile.TemporaryDirectory() as temp_dir: + temp_dir_path = Path(temp_dir) + target_file = temp_dir_path / "test.txt" + target_file.write_text("line1 \nline2\r\nline3", encoding="utf-8") + + lines = read_lines(target_file) + self.assertEqual(lines, ["line1", "line2", "line3"]) + + def test_read_lines_file_not_found(self): + with tempfile.TemporaryDirectory() as temp_dir: + temp_dir_path = Path(temp_dir) + non_existent = temp_dir_path / "missing.txt" + + with patch("sys.stderr", new_callable=io.StringIO) as mock_stderr: + lines = read_lines(non_existent) + self.assertIsNone(lines) + self.assertIn(f"Error reading {non_existent}", mock_stderr.getvalue()) + def test_write_lines_atomic(self): - import tempfile from common import write_lines with tempfile.TemporaryDirectory() as temp_dir: