from codesnap.utils.file_utils import FileUtils class TestFileUtils: def test_should_ignore_patterns(self) -> None: assert FileUtils.should_ignore("test.pyc", ["*.pyc"]) is True assert FileUtils.should_ignore("test.pyc", ["*.pyc", "*.pyo"]) is True assert FileUtils.should_ignore("test.py", ["*.pyc"]) is False def test_should_ignore_directory(self) -> None: assert FileUtils.should_ignore("src/__pycache__", ["__pycache__/*"]) is True assert FileUtils.should_ignore(".git/config", [".git/*"]) is True def test_is_text_file(self) -> None: assert FileUtils.is_text_file("test.py") is True assert FileUtils.is_text_file("test.js") is True assert FileUtils.is_text_file("test.tsx") is True assert FileUtils.is_text_file("Dockerfile") is True def test_is_not_binary_file(self) -> None: assert FileUtils.is_text_file("test.png") is False assert FileUtils.is_text_file("test.jpg") is False assert FileUtils.is_text_file("test.so") is False def test_read_file_content(self) -> None: import os import tempfile with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f: f.write("print('hello')") temp_path = f.name try: content = FileUtils.read_file_content(temp_path) assert content == "print('hello')" finally: os.unlink(temp_path) def test_read_file_content_not_found(self) -> None: content = FileUtils.read_file_content("/nonexistent/file.py") assert content is None def test_count_lines(self) -> None: content = "line1\nline2\nline3" assert FileUtils.count_lines(content) == 3 assert FileUtils.count_lines("") == 1 assert FileUtils.count_lines("single") == 1 def test_get_relative_path(self) -> None: import os import tempfile with tempfile.TemporaryDirectory() as tmpdir: subdir = os.path.join(tmpdir, "subdir") os.makedirs(subdir) filepath = os.path.join(subdir, "test.py") rel = FileUtils.get_relative_path(filepath, tmpdir) assert rel == os.path.join("subdir", "test.py") def test_walk_directory(self) -> None: import os import tempfile with tempfile.TemporaryDirectory() as tmpdir: os.makedirs(os.path.join(tmpdir, "src")) os.makedirs(os.path.join(tmpdir, "tests")) with open(os.path.join(tmpdir, "main.py"), "w") as f: f.write("print('hello')") with open(os.path.join(tmpdir, "src", "module.py"), "w") as f: f.write("def test(): pass") files = FileUtils.walk_directory(tmpdir, ["*.pyc", "__pycache__/*"], 100) assert len(files) == 2 def test_walk_directory_with_ignore(self) -> None: import os import tempfile with tempfile.TemporaryDirectory() as tmpdir: os.makedirs(os.path.join(tmpdir, "__pycache__")) with open(os.path.join(tmpdir, "main.py"), "w") as f: f.write("print('hello')") with open(os.path.join(tmpdir, "__pycache__", "cache.pyc"), "w") as f: f.write("cached") files = FileUtils.walk_directory(tmpdir, ["*.pyc", "__pycache__/*"], 100) assert len(files) == 1 assert "__pycache__" not in files[0] def test_get_directory_tree(self) -> None: import os import tempfile with tempfile.TemporaryDirectory() as tmpdir: os.makedirs(os.path.join(tmpdir, "src")) os.makedirs(os.path.join(tmpdir, "tests")) with open(os.path.join(tmpdir, "main.py"), "w") as f: f.write("") with open(os.path.join(tmpdir, "src", "module.py"), "w") as f: f.write("") tree = FileUtils.get_directory_tree(tmpdir, ["*.pyc"], 3) assert len(tree) > 0 assert any("main.py" in line for line in tree)