mirror of
https://github.com/kirill-markin/repo-to-text.git
synced 2025-12-05 19:12:24 -08:00
Refactor devide logic by files and more tests
This commit is contained in:
parent
6a434e5174
commit
dbfa602cd3
9 changed files with 922 additions and 0 deletions
107
tests/test_cli.py
Normal file
107
tests/test_cli.py
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
import os
|
||||
import pytest
|
||||
import tempfile
|
||||
import shutil
|
||||
from typing import Generator
|
||||
from unittest.mock import patch, MagicMock
|
||||
from repo_to_text.cli.cli import (
|
||||
create_default_settings_file,
|
||||
parse_args,
|
||||
main
|
||||
)
|
||||
|
||||
@pytest.fixture
|
||||
def temp_dir() -> Generator[str, None, None]:
|
||||
"""Create a temporary directory for testing."""
|
||||
temp_path = tempfile.mkdtemp()
|
||||
yield temp_path
|
||||
shutil.rmtree(temp_path)
|
||||
|
||||
def test_parse_args_defaults() -> None:
|
||||
"""Test parsing command line arguments with default values."""
|
||||
with patch('sys.argv', ['repo-to-text']):
|
||||
args = parse_args()
|
||||
assert args.input_dir == '.'
|
||||
assert not args.debug
|
||||
assert args.output_dir is None
|
||||
assert not args.create_settings
|
||||
assert not args.stdout
|
||||
assert args.ignore_patterns is None
|
||||
|
||||
def test_parse_args_with_values() -> None:
|
||||
"""Test parsing command line arguments with provided values."""
|
||||
test_args = [
|
||||
'repo-to-text',
|
||||
'input/path',
|
||||
'--debug',
|
||||
'--output-dir', 'output/path',
|
||||
'--ignore-patterns', '*.log', 'temp/'
|
||||
]
|
||||
with patch('sys.argv', test_args):
|
||||
args = parse_args()
|
||||
assert args.input_dir == 'input/path'
|
||||
assert args.debug
|
||||
assert args.output_dir == 'output/path'
|
||||
assert args.ignore_patterns == ['*.log', 'temp/']
|
||||
|
||||
def test_create_default_settings_file(temp_dir: str) -> None:
|
||||
"""Test creation of default settings file."""
|
||||
os.chdir(temp_dir)
|
||||
create_default_settings_file()
|
||||
|
||||
settings_file = '.repo-to-text-settings.yaml'
|
||||
assert os.path.exists(settings_file)
|
||||
|
||||
with open(settings_file, 'r') as f:
|
||||
content = f.read()
|
||||
assert 'gitignore-import-and-ignore: True' in content
|
||||
assert 'ignore-tree-and-content:' in content
|
||||
assert 'ignore-content:' in content
|
||||
|
||||
def test_create_default_settings_file_already_exists(temp_dir: str) -> None:
|
||||
"""Test handling of existing settings file."""
|
||||
os.chdir(temp_dir)
|
||||
# Create the file first
|
||||
create_default_settings_file()
|
||||
|
||||
# Try to create it again
|
||||
with pytest.raises(FileExistsError) as exc_info:
|
||||
create_default_settings_file()
|
||||
assert "already exists" in str(exc_info.value)
|
||||
|
||||
@patch('repo_to_text.cli.cli.save_repo_to_text')
|
||||
def test_main_normal_execution(mock_save_repo: MagicMock) -> None:
|
||||
"""Test main function with normal execution."""
|
||||
with patch('sys.argv', ['repo-to-text', '--stdout']):
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
main()
|
||||
assert exc_info.value.code == 0
|
||||
mock_save_repo.assert_called_once_with(
|
||||
path='.',
|
||||
output_dir=None,
|
||||
to_stdout=True,
|
||||
cli_ignore_patterns=None
|
||||
)
|
||||
|
||||
@patch('repo_to_text.cli.cli.create_default_settings_file')
|
||||
def test_main_create_settings(mock_create_settings: MagicMock) -> None:
|
||||
"""Test main function with create settings option."""
|
||||
with patch('sys.argv', ['repo-to-text', '--create-settings']):
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
main()
|
||||
assert exc_info.value.code == 0
|
||||
mock_create_settings.assert_called_once()
|
||||
|
||||
@patch('repo_to_text.cli.cli.setup_logging')
|
||||
@patch('repo_to_text.cli.cli.create_default_settings_file')
|
||||
def test_main_with_debug_logging(mock_create_settings: MagicMock, mock_setup_logging: MagicMock) -> None:
|
||||
"""Test main function with debug logging enabled."""
|
||||
with patch('sys.argv', ['repo-to-text', '--debug', '--create-settings']):
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
main()
|
||||
assert exc_info.value.code == 0
|
||||
mock_setup_logging.assert_called_once_with(debug=True)
|
||||
mock_create_settings.assert_called_once()
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__])
|
||||
285
tests/test_core.py
Normal file
285
tests/test_core.py
Normal file
|
|
@ -0,0 +1,285 @@
|
|||
import os
|
||||
import tempfile
|
||||
import shutil
|
||||
import pytest
|
||||
from typing import Generator
|
||||
from repo_to_text.core.core import (
|
||||
get_tree_structure,
|
||||
load_ignore_specs,
|
||||
should_ignore_file,
|
||||
is_ignored_path,
|
||||
remove_empty_dirs,
|
||||
save_repo_to_text
|
||||
)
|
||||
|
||||
@pytest.fixture
|
||||
def temp_dir() -> Generator[str, None, None]:
|
||||
"""Create a temporary directory for testing."""
|
||||
temp_path = tempfile.mkdtemp()
|
||||
yield temp_path
|
||||
shutil.rmtree(temp_path)
|
||||
|
||||
@pytest.fixture
|
||||
def sample_repo(temp_dir: str) -> str:
|
||||
"""Create a sample repository structure for testing."""
|
||||
# Create directories
|
||||
os.makedirs(os.path.join(temp_dir, "src"))
|
||||
os.makedirs(os.path.join(temp_dir, "tests"))
|
||||
|
||||
# Create sample files
|
||||
files = {
|
||||
"README.md": "# Test Project",
|
||||
".gitignore": """
|
||||
*.pyc
|
||||
__pycache__/
|
||||
.git/
|
||||
""",
|
||||
"src/main.py": "print('Hello World')",
|
||||
"tests/test_main.py": "def test_sample(): pass",
|
||||
".repo-to-text-settings.yaml": """
|
||||
gitignore-import-and-ignore: True
|
||||
ignore-tree-and-content:
|
||||
- ".git/"
|
||||
- ".repo-to-text-settings.yaml"
|
||||
ignore-content:
|
||||
- "README.md"
|
||||
"""
|
||||
}
|
||||
|
||||
for file_path, content in files.items():
|
||||
full_path = os.path.join(temp_dir, file_path)
|
||||
os.makedirs(os.path.dirname(full_path), exist_ok=True)
|
||||
with open(full_path, "w") as f:
|
||||
f.write(content)
|
||||
|
||||
return temp_dir
|
||||
|
||||
def test_is_ignored_path() -> None:
|
||||
"""Test the is_ignored_path function."""
|
||||
assert is_ignored_path(".git/config") is True
|
||||
assert is_ignored_path("repo-to-text_output.txt") is True
|
||||
assert is_ignored_path("src/main.py") is False
|
||||
assert is_ignored_path("normal_file.txt") is False
|
||||
|
||||
def test_load_ignore_specs(sample_repo: str) -> None:
|
||||
"""Test loading ignore specifications from files."""
|
||||
gitignore_spec, content_ignore_spec, tree_and_content_ignore_spec = load_ignore_specs(sample_repo)
|
||||
|
||||
assert gitignore_spec is not None
|
||||
assert content_ignore_spec is not None
|
||||
assert tree_and_content_ignore_spec is not None
|
||||
|
||||
# Test gitignore patterns
|
||||
assert gitignore_spec.match_file("test.pyc") is True
|
||||
assert gitignore_spec.match_file("__pycache__/cache.py") is True
|
||||
assert gitignore_spec.match_file(".git/config") is True
|
||||
|
||||
# Test content ignore patterns
|
||||
assert content_ignore_spec.match_file("README.md") is True
|
||||
|
||||
# Test tree and content ignore patterns
|
||||
assert tree_and_content_ignore_spec.match_file(".git/config") is True
|
||||
|
||||
def test_should_ignore_file(sample_repo: str) -> None:
|
||||
"""Test file ignoring logic."""
|
||||
gitignore_spec, content_ignore_spec, tree_and_content_ignore_spec = load_ignore_specs(sample_repo)
|
||||
|
||||
# Test various file paths
|
||||
assert should_ignore_file(
|
||||
".git/config",
|
||||
".git/config",
|
||||
gitignore_spec,
|
||||
content_ignore_spec,
|
||||
tree_and_content_ignore_spec
|
||||
) is True
|
||||
|
||||
assert should_ignore_file(
|
||||
"src/main.py",
|
||||
"src/main.py",
|
||||
gitignore_spec,
|
||||
content_ignore_spec,
|
||||
tree_and_content_ignore_spec
|
||||
) is False
|
||||
|
||||
def test_get_tree_structure(sample_repo: str) -> None:
|
||||
"""Test tree structure generation."""
|
||||
gitignore_spec, _, tree_and_content_ignore_spec = load_ignore_specs(sample_repo)
|
||||
tree_output = get_tree_structure(sample_repo, gitignore_spec, tree_and_content_ignore_spec)
|
||||
|
||||
# Basic structure checks
|
||||
assert "src" in tree_output
|
||||
assert "tests" in tree_output
|
||||
assert "main.py" in tree_output
|
||||
assert "test_main.py" in tree_output
|
||||
assert ".git" not in tree_output
|
||||
|
||||
def test_remove_empty_dirs(temp_dir: str) -> None:
|
||||
"""Test removal of empty directories from tree output."""
|
||||
# Create test directory structure
|
||||
os.makedirs(os.path.join(temp_dir, "src"))
|
||||
os.makedirs(os.path.join(temp_dir, "empty_dir"))
|
||||
os.makedirs(os.path.join(temp_dir, "tests"))
|
||||
|
||||
# Create some files
|
||||
with open(os.path.join(temp_dir, "src/main.py"), "w") as f:
|
||||
f.write("print('test')")
|
||||
with open(os.path.join(temp_dir, "tests/test_main.py"), "w") as f:
|
||||
f.write("def test(): pass")
|
||||
|
||||
# Create a mock tree output that matches the actual tree command format
|
||||
tree_output = (
|
||||
f"{temp_dir}\n"
|
||||
f"├── {os.path.join(temp_dir, 'src')}\n"
|
||||
f"│ └── {os.path.join(temp_dir, 'src/main.py')}\n"
|
||||
f"├── {os.path.join(temp_dir, 'empty_dir')}\n"
|
||||
f"└── {os.path.join(temp_dir, 'tests')}\n"
|
||||
f" └── {os.path.join(temp_dir, 'tests/test_main.py')}\n"
|
||||
)
|
||||
|
||||
filtered_output = remove_empty_dirs(tree_output, temp_dir)
|
||||
|
||||
# Check that empty_dir is removed but other directories remain
|
||||
assert "empty_dir" not in filtered_output
|
||||
assert os.path.join(temp_dir, "src") in filtered_output
|
||||
assert os.path.join(temp_dir, "tests") in filtered_output
|
||||
assert os.path.join(temp_dir, "src/main.py") in filtered_output
|
||||
assert os.path.join(temp_dir, "tests/test_main.py") in filtered_output
|
||||
|
||||
def test_save_repo_to_text(sample_repo: str) -> None:
|
||||
"""Test the main save_repo_to_text function."""
|
||||
# Create output directory
|
||||
output_dir = os.path.join(sample_repo, "output")
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
# Create .git directory to ensure it's properly ignored
|
||||
os.makedirs(os.path.join(sample_repo, ".git"))
|
||||
with open(os.path.join(sample_repo, ".git/config"), "w") as f:
|
||||
f.write("[core]\n\trepositoryformatversion = 0\n")
|
||||
|
||||
# Test file output
|
||||
output_file = save_repo_to_text(sample_repo, output_dir=output_dir)
|
||||
assert os.path.exists(output_file)
|
||||
assert os.path.dirname(output_file) == output_dir
|
||||
|
||||
# Check file contents
|
||||
with open(output_file, 'r') as f:
|
||||
content = f.read()
|
||||
|
||||
# Basic content checks
|
||||
assert "Directory Structure:" in content
|
||||
|
||||
# Check for expected files
|
||||
assert "src/main.py" in content
|
||||
assert "tests/test_main.py" in content
|
||||
|
||||
# Check for file contents
|
||||
assert "print('Hello World')" in content
|
||||
assert "def test_sample(): pass" in content
|
||||
|
||||
# Ensure ignored patterns are not in output
|
||||
assert ".git/config" not in content # Check specific file
|
||||
assert "repo-to-text_" not in content
|
||||
assert ".repo-to-text-settings.yaml" not in content
|
||||
|
||||
# Check that .gitignore content is not included
|
||||
assert "*.pyc" not in content
|
||||
assert "__pycache__" not in content
|
||||
|
||||
def test_save_repo_to_text_stdout(sample_repo: str) -> None:
|
||||
"""Test save_repo_to_text with stdout output."""
|
||||
output = save_repo_to_text(sample_repo, to_stdout=True)
|
||||
assert isinstance(output, str)
|
||||
assert "Directory Structure:" in output
|
||||
assert "src/main.py" in output
|
||||
assert "tests/test_main.py" in output
|
||||
|
||||
def test_load_ignore_specs_with_cli_patterns(sample_repo: str) -> None:
|
||||
"""Test loading ignore specs with CLI patterns."""
|
||||
cli_patterns = ["*.log", "temp/"]
|
||||
gitignore_spec, content_ignore_spec, tree_and_content_ignore_spec = load_ignore_specs(sample_repo, cli_patterns)
|
||||
|
||||
assert tree_and_content_ignore_spec.match_file("test.log") is True
|
||||
assert tree_and_content_ignore_spec.match_file("temp/file.txt") is True
|
||||
assert tree_and_content_ignore_spec.match_file("normal.txt") is False
|
||||
|
||||
def test_load_ignore_specs_without_gitignore(temp_dir: str) -> None:
|
||||
"""Test loading ignore specs when .gitignore is missing."""
|
||||
gitignore_spec, content_ignore_spec, tree_and_content_ignore_spec = load_ignore_specs(temp_dir)
|
||||
assert gitignore_spec is None
|
||||
assert content_ignore_spec is None
|
||||
assert tree_and_content_ignore_spec is not None
|
||||
|
||||
def test_get_tree_structure_with_special_chars(temp_dir: str) -> None:
|
||||
"""Test tree structure generation with special characters in paths."""
|
||||
# Create files with special characters
|
||||
special_dir = os.path.join(temp_dir, "special chars")
|
||||
os.makedirs(special_dir)
|
||||
with open(os.path.join(special_dir, "file with spaces.txt"), "w") as f:
|
||||
f.write("test")
|
||||
|
||||
tree_output = get_tree_structure(temp_dir)
|
||||
assert "special chars" in tree_output
|
||||
assert "file with spaces.txt" in tree_output
|
||||
|
||||
def test_should_ignore_file_edge_cases(sample_repo: str) -> None:
|
||||
"""Test edge cases for should_ignore_file function."""
|
||||
gitignore_spec, content_ignore_spec, tree_and_content_ignore_spec = load_ignore_specs(sample_repo)
|
||||
|
||||
# Test with dot-prefixed paths
|
||||
assert should_ignore_file(
|
||||
"./src/main.py",
|
||||
"./src/main.py",
|
||||
gitignore_spec,
|
||||
content_ignore_spec,
|
||||
tree_and_content_ignore_spec
|
||||
) is False
|
||||
|
||||
# Test with absolute paths
|
||||
abs_path = os.path.join(sample_repo, "src/main.py")
|
||||
rel_path = "src/main.py"
|
||||
assert should_ignore_file(
|
||||
abs_path,
|
||||
rel_path,
|
||||
gitignore_spec,
|
||||
content_ignore_spec,
|
||||
tree_and_content_ignore_spec
|
||||
) is False
|
||||
|
||||
def test_save_repo_to_text_with_binary_files(temp_dir: str) -> None:
|
||||
"""Test handling of binary files in save_repo_to_text."""
|
||||
# Create a binary file
|
||||
binary_path = os.path.join(temp_dir, "binary.bin")
|
||||
binary_content = b'\x00\x01\x02\x03'
|
||||
with open(binary_path, "wb") as f:
|
||||
f.write(binary_content)
|
||||
|
||||
output = save_repo_to_text(temp_dir, to_stdout=True)
|
||||
|
||||
# Check that the binary file is listed in the structure
|
||||
assert "binary.bin" in output
|
||||
# Check that the file content section exists with raw binary content
|
||||
expected_content = f"Contents of binary.bin:\n```\n{binary_content.decode('latin1')}\n```"
|
||||
assert expected_content in output
|
||||
|
||||
def test_save_repo_to_text_custom_output_dir(temp_dir: str) -> None:
|
||||
"""Test save_repo_to_text with custom output directory."""
|
||||
# Create a simple file structure
|
||||
with open(os.path.join(temp_dir, "test.txt"), "w") as f:
|
||||
f.write("test content")
|
||||
|
||||
# Create custom output directory
|
||||
output_dir = os.path.join(temp_dir, "custom_output")
|
||||
output_file = save_repo_to_text(temp_dir, output_dir=output_dir)
|
||||
|
||||
assert os.path.exists(output_file)
|
||||
assert os.path.dirname(output_file) == output_dir
|
||||
assert output_file.startswith(output_dir)
|
||||
|
||||
def test_get_tree_structure_empty_directory(temp_dir: str) -> None:
|
||||
"""Test tree structure generation for empty directory."""
|
||||
tree_output = get_tree_structure(temp_dir)
|
||||
# Should only contain the directory itself
|
||||
assert tree_output.strip() == "" or tree_output.strip() == temp_dir
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__])
|
||||
142
tests/test_utils.py
Normal file
142
tests/test_utils.py
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
import logging
|
||||
import pytest
|
||||
from typing import Generator
|
||||
from repo_to_text.utils.utils import setup_logging
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def reset_logger() -> Generator[None, None, None]:
|
||||
"""Reset root logger before each test."""
|
||||
root_logger = logging.getLogger()
|
||||
for handler in root_logger.handlers[:]:
|
||||
root_logger.removeHandler(handler)
|
||||
root_logger.setLevel(logging.WARNING) # Default level
|
||||
yield
|
||||
for handler in root_logger.handlers[:]:
|
||||
root_logger.removeHandler(handler)
|
||||
root_logger.setLevel(logging.WARNING) # Reset after test
|
||||
|
||||
def test_setup_logging_debug() -> None:
|
||||
"""Test setup_logging with debug mode."""
|
||||
root_logger = logging.getLogger()
|
||||
root_logger.handlers.clear() # Clear existing handlers
|
||||
root_logger.setLevel(logging.WARNING) # Reset to default
|
||||
|
||||
setup_logging(debug=True)
|
||||
assert len(root_logger.handlers) > 0
|
||||
assert root_logger.level == logging.DEBUG
|
||||
|
||||
def test_setup_logging_info() -> None:
|
||||
"""Test setup_logging with info mode."""
|
||||
root_logger = logging.getLogger()
|
||||
root_logger.handlers.clear() # Clear existing handlers
|
||||
root_logger.setLevel(logging.WARNING) # Reset to default
|
||||
|
||||
setup_logging(debug=False)
|
||||
assert len(root_logger.handlers) > 0
|
||||
assert root_logger.level == logging.INFO
|
||||
|
||||
def test_setup_logging_formatter() -> None:
|
||||
"""Test logging formatter setup."""
|
||||
setup_logging(debug=True)
|
||||
logger = logging.getLogger()
|
||||
handlers = logger.handlers
|
||||
|
||||
# Check if there's at least one handler
|
||||
assert len(handlers) > 0
|
||||
|
||||
# Check formatter
|
||||
formatter = handlers[0].formatter
|
||||
assert formatter is not None
|
||||
|
||||
# Test format string
|
||||
test_record = logging.LogRecord(
|
||||
name='test',
|
||||
level=logging.DEBUG,
|
||||
pathname='test.py',
|
||||
lineno=1,
|
||||
msg='Test message',
|
||||
args=(),
|
||||
exc_info=None
|
||||
)
|
||||
formatted = formatter.format(test_record)
|
||||
assert 'Test message' in formatted
|
||||
assert test_record.levelname in formatted
|
||||
|
||||
def test_setup_logging_multiple_calls() -> None:
|
||||
"""Test that multiple calls to setup_logging don't create duplicate handlers."""
|
||||
root_logger = logging.getLogger()
|
||||
root_logger.handlers.clear()
|
||||
|
||||
setup_logging(debug=True)
|
||||
initial_handler_count = len(root_logger.handlers)
|
||||
|
||||
# Call setup_logging again
|
||||
setup_logging(debug=True)
|
||||
assert len(root_logger.handlers) == initial_handler_count, "Should not create duplicate handlers"
|
||||
|
||||
def test_setup_logging_level_change() -> None:
|
||||
"""Test changing log levels between setup_logging calls."""
|
||||
root_logger = logging.getLogger()
|
||||
root_logger.handlers.clear()
|
||||
|
||||
# Start with debug
|
||||
setup_logging(debug=True)
|
||||
assert root_logger.level == logging.DEBUG
|
||||
|
||||
# Clear handlers before next setup
|
||||
root_logger.handlers.clear()
|
||||
|
||||
# Switch to info
|
||||
setup_logging(debug=False)
|
||||
assert root_logger.level == logging.INFO
|
||||
|
||||
def test_setup_logging_message_format() -> None:
|
||||
"""Test the actual format of logged messages."""
|
||||
setup_logging(debug=True)
|
||||
logger = logging.getLogger()
|
||||
|
||||
# Create a temporary handler to capture output
|
||||
import io
|
||||
log_capture = io.StringIO()
|
||||
handler = logging.StreamHandler(log_capture)
|
||||
# Use formatter that includes pathname
|
||||
handler.setFormatter(logging.Formatter('%(levelname)s %(name)s:%(pathname)s:%(lineno)d %(message)s'))
|
||||
logger.addHandler(handler)
|
||||
|
||||
# Ensure debug level is set
|
||||
logger.setLevel(logging.DEBUG)
|
||||
handler.setLevel(logging.DEBUG)
|
||||
|
||||
# Log a test message
|
||||
test_message = "Test log message"
|
||||
logger.debug(test_message)
|
||||
log_output = log_capture.getvalue()
|
||||
|
||||
# Verify format components
|
||||
assert test_message in log_output
|
||||
assert "DEBUG" in log_output
|
||||
assert "test_utils.py" in log_output
|
||||
|
||||
def test_setup_logging_error_messages() -> None:
|
||||
"""Test logging of error messages."""
|
||||
setup_logging(debug=False)
|
||||
logger = logging.getLogger()
|
||||
|
||||
# Create a temporary handler to capture output
|
||||
import io
|
||||
log_capture = io.StringIO()
|
||||
handler = logging.StreamHandler(log_capture)
|
||||
handler.setFormatter(logger.handlers[0].formatter)
|
||||
logger.addHandler(handler)
|
||||
|
||||
# Log an error message
|
||||
error_message = "Test error message"
|
||||
logger.error(error_message)
|
||||
log_output = log_capture.getvalue()
|
||||
|
||||
# Error messages should always be logged regardless of debug setting
|
||||
assert error_message in log_output
|
||||
assert "ERROR" in log_output
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__])
|
||||
Loading…
Add table
Add a link
Reference in a new issue