Skip to content

Commit

Permalink
Merge pull request #27 from tawanda-kembo/feat/improve-ignore-rules
Browse files Browse the repository at this point in the history
fix: Improve .gitignore handling and add support for directory patterns
  • Loading branch information
tawandakembo authored Aug 20, 2024
2 parents 58b7f60 + aecf01c commit a9abb82
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 17 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,6 @@ __pycache__/
# Distribution / packaging
*.egg-info/
build/
dist/
dist/
.aider*
.aider.*
1 change: 1 addition & 0 deletions code_collator/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .collate import collate_codebase, main # noqa: F401
25 changes: 21 additions & 4 deletions code_collator/collate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import argparse
from pathlib import Path
import logging
from fnmatch import fnmatch


def setup_logging():
"""Set up logging configuration."""
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
force=True
format='%(asctime)s - %(levelname)s - %(message)s'
)


Expand Down Expand Up @@ -42,10 +42,27 @@ def read_gitignore(path):

def should_ignore(file_path, ignore_patterns):
"""Check if a file should be ignored based on .gitignore patterns and if it's in the .git directory."""
from fnmatch import fnmatch
if '.git' in Path(file_path).parts:
return True
return any(fnmatch(file_path, pattern) for pattern in ignore_patterns)

relative_path = os.path.relpath(file_path)
path_parts = relative_path.split(os.sep)

for pattern in ignore_patterns:
if pattern.endswith('/'):
# Directory pattern
if any(fnmatch(part, pattern[:-1]) for part in path_parts):
return True
elif '/' in pattern:
# Path pattern
if fnmatch(relative_path, pattern):
return True
else:
# File pattern
if fnmatch(os.path.basename(file_path), pattern):
return True

return False


def collate_codebase(path, output_file):
Expand Down
Binary file modified output.md
Binary file not shown.
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

here = pathlib.Path(__file__).parent.resolve()


def get_version():
version = os.environ.get('PACKAGE_VERSION', '0.0.0')
return version


setup(
name="code-collator",
version=get_version(),
Expand All @@ -29,7 +31,7 @@ def get_version():
"Programming Language :: Python :: 3.11",
],
keywords="cli, development, documentation",
packages=find_packages(),
packages=find_packages(include=['code_collator', 'code_collator.*']),
python_requires=">=3.6, <4",
install_requires=[
# Add any dependencies here
Expand All @@ -43,4 +45,4 @@ def get_version():
"Bug Reports": "https://github.com/tawanda-kembo/code-collator/issues",
"Source": "https://github.com/tawanda-kembo/code-collator",
},
)
)
25 changes: 15 additions & 10 deletions tests/test_collate.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import sys
import os
import pytest
from unittest.mock import mock_open, patch
from code_collator import collate
import logging
from unittest.mock import mock_open, patch
from code_collator.collate import is_binary_file, read_gitignore, should_ignore, main

# Add the parent directory to sys.path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))


def test_is_binary_file():
with patch('builtins.open', mock_open(read_data=b'\x00binary\xff')):
assert collate.is_binary_file('test.bin') is True
assert is_binary_file('test.bin') is True

with patch('builtins.open', mock_open(read_data=b'hello world')):
assert collate.is_binary_file('test.txt') is False
assert is_binary_file('test.txt') is False


def test_read_gitignore():
with patch('builtins.open', mock_open(read_data='*.pyc\n__pycache__\n')):
patterns = collate.read_gitignore('.')
patterns = read_gitignore('.')
assert patterns == ['*.pyc', '__pycache__']


def test_should_ignore():
patterns = ['*.pyc', '__pycache__']
assert collate.should_ignore('test.pyc', patterns)
assert collate.should_ignore('test.py', patterns) is False
assert collate.should_ignore('.git/config', patterns)
assert should_ignore('test.pyc', patterns)
assert should_ignore('test.py', patterns) is False
assert should_ignore('.git/config', patterns)


@pytest.fixture
Expand All @@ -37,7 +42,7 @@ def mock_file_system(tmp_path):
# def test_collate_codebase(mock_file_system, caplog):
# caplog.set_level(logging.INFO)
# output_file = mock_file_system / "output.md"
# collate.collate_codebase(str(mock_file_system), str(output_file))
# collate_codebase(str(mock_file_system), str(output_file))

# with open(output_file, 'r') as f:
# content = f.read()
Expand All @@ -57,7 +62,7 @@ def mock_file_system(tmp_path):
def test_main(mock_file_system, caplog):
caplog.set_level(logging.INFO)
with patch('sys.argv', ['collate', '-p', str(mock_file_system), '-o', 'output.md']):
collate.main()
main()

assert "Starting code collation" in caplog.text
assert "Code collation completed" in caplog.text

0 comments on commit a9abb82

Please sign in to comment.