Back to Repositories

Testing File Pattern Filtering Implementation in PR-Agent

This test suite validates the file filtering functionality in PR-Agent, focusing on the ignore patterns implementation. It verifies both glob and regex-based file filtering mechanisms while ensuring robust error handling for invalid patterns.

Test Coverage Overview

The test suite provides comprehensive coverage of the file filtering system.

Key areas tested include:
  • Default behavior with no ignore patterns
  • Glob pattern filtering
  • Regex pattern filtering
  • Invalid pattern handling
Edge cases are thoroughly covered, including empty pattern lists and malformed regex expressions.

Implementation Analysis

The testing approach utilizes pytest’s powerful fixtures and mocking capabilities to isolate file filtering logic.

Notable patterns include:
  • Dynamic test object creation using type()
  • Monkeypatch fixture for configuration modification
  • Explicit assertion messages for clear failure reporting

Technical Details

Testing tools and configuration:
  • pytest framework for test execution
  • monkeypatch fixture for runtime configuration modification
  • Custom object creation for file simulation
  • Integration with global_settings for configuration management

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through clear organization and robust validation.

Key practices include:
  • Descriptive test method names
  • Comprehensive docstrings
  • Isolated test cases
  • Explicit assertion messages
  • Proper error case handling

codium-ai/pr-agent

tests/unittest/test_file_filter.py

            
import pytest

from pr_agent.algo.file_filter import filter_ignored
from pr_agent.config_loader import global_settings


class TestIgnoreFilter:
    def test_no_ignores(self):
        """
        Test no files are ignored when no patterns are specified.
        """
        files = [
            type('', (object,), {'filename': 'file1.py'})(),
            type('', (object,), {'filename': 'file2.java'})(),
            type('', (object,), {'filename': 'file3.cpp'})(),
            type('', (object,), {'filename': 'file4.py'})(),
            type('', (object,), {'filename': 'file5.py'})()
        ]
        assert filter_ignored(files) == files, "Expected all files to be returned when no ignore patterns are given."

    def test_glob_ignores(self, monkeypatch):
        """
        Test files are ignored when glob patterns are specified.
        """
        monkeypatch.setattr(global_settings.ignore, 'glob', ['*.py'])

        files = [
            type('', (object,), {'filename': 'file1.py'})(),
            type('', (object,), {'filename': 'file2.java'})(),
            type('', (object,), {'filename': 'file3.cpp'})(),
            type('', (object,), {'filename': 'file4.py'})(),
            type('', (object,), {'filename': 'file5.py'})()
        ]
        expected = [
            files[1],
            files[2]
        ]

        filtered_files = filter_ignored(files)
        assert filtered_files == expected, f"Expected {[file.filename for file in expected]}, but got {[file.filename for file in filtered_files]}."

    def test_regex_ignores(self, monkeypatch):
        """
        Test files are ignored when regex patterns are specified.
        """
        monkeypatch.setattr(global_settings.ignore, 'regex', ['^file[2-4]\..*$'])

        files = [
            type('', (object,), {'filename': 'file1.py'})(),
            type('', (object,), {'filename': 'file2.java'})(),
            type('', (object,), {'filename': 'file3.cpp'})(),
            type('', (object,), {'filename': 'file4.py'})(),
            type('', (object,), {'filename': 'file5.py'})()
        ]
        expected = [
            files[0],
            files[4]
        ]

        filtered_files = filter_ignored(files)
        assert filtered_files == expected, f"Expected {[file.filename for file in expected]}, but got {[file.filename for file in filtered_files]}."

    def test_invalid_regex(self, monkeypatch):
        """
        Test invalid patterns are quietly ignored.
        """
        monkeypatch.setattr(global_settings.ignore, 'regex', ['(((||', '^file[2-4]\..*$'])

        files = [
            type('', (object,), {'filename': 'file1.py'})(),
            type('', (object,), {'filename': 'file2.java'})(),
            type('', (object,), {'filename': 'file3.cpp'})(),
            type('', (object,), {'filename': 'file4.py'})(),
            type('', (object,), {'filename': 'file5.py'})()
        ]
        expected = [
            files[0],
            files[4]
        ]

        filtered_files = filter_ignored(files)
        assert filtered_files == expected, f"Expected {[file.filename for file in expected]}, but got {[file.filename for file in filtered_files]}."