Skip to content
Closed
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
29 changes: 25 additions & 4 deletions Scripts/test_common.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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"))
Expand All @@ -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())
Comment on lines +102 to +110
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

The read_lines function catches both OSError and UnicodeError. While this test covers the OSError case (via FileNotFoundError), the UnicodeError path remains untested. To ensure complete coverage of the error handling, please consider adding a test case for a file with invalid UTF-8 encoding. You could add a new method like this:

    def test_read_lines_unicode_error(self):
        """Test that read_lines returns None on a UnicodeDecodeError."""
        with tempfile.TemporaryDirectory() as temp_dir:
            target_file = Path(temp_dir) / "bad_encoding.txt"
            with open(target_file, "wb") as f:
                f.write(b"this is not valid utf-8 \xff")

            with patch("sys.stderr", new_callable=io.StringIO) as mock_stderr:
                lines = read_lines(target_file)
                self.assertIsNone(lines)
                self.assertIn(f"Error reading {target_file}", mock_stderr.getvalue())


def test_write_lines_atomic(self):
import tempfile
from common import write_lines

with tempfile.TemporaryDirectory() as temp_dir:
Expand Down
Loading