Skip to content

Commit

Permalink
some more test cases - ai generated
Browse files Browse the repository at this point in the history
  • Loading branch information
abinthomasonline committed Aug 26, 2024
1 parent ae4cde3 commit f685e14
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pathspec
pre-commit
pytest
pytest-cov
twine
60 changes: 60 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import pytest
from unittest.mock import patch, MagicMock
from repopack.cli import run_cli
from repopack.exceptions import RepopackError, ConfigurationError


def test_run_cli_success():
with patch("repopack.cli.argparse.ArgumentParser.parse_args") as mock_parse_args, patch(
"repopack.cli.load_config"
) as mock_load_config, patch("repopack.cli.merge_configs") as mock_merge_configs, patch(
"repopack.cli.pack"
) as mock_pack, patch(
"repopack.cli.print_summary"
) as mock_print_summary, patch(
"repopack.cli.print_completion"
) as mock_print_completion:

mock_parse_args.return_value = MagicMock(directory=".", verbose=False)
mock_load_config.return_value = {}
mock_merge_configs.return_value = {
"output": {"file_path": "output.txt", "top_files_length": 5}
}
mock_pack.return_value = {
"total_files": 10,
"total_characters": 1000,
"file_char_counts": {},
}

run_cli()

mock_pack.assert_called_once()
mock_print_summary.assert_called_once()
mock_print_completion.assert_called_once()


def test_run_cli_repopack_error():
with patch("repopack.cli.argparse.ArgumentParser.parse_args") as mock_parse_args, patch(
"repopack.cli.load_config"
) as mock_load_config, patch("repopack.cli.merge_configs") as mock_merge_configs, patch(
"repopack.cli.pack"
) as mock_pack, patch(
"repopack.cli.logger.error"
) as mock_logger_error, patch(
"repopack.cli.Spinner"
) as mock_spinner:

mock_parse_args.return_value = MagicMock(directory=".", verbose=False)
mock_load_config.return_value = {}
mock_merge_configs.return_value = {
"output": {"file_path": "output.txt", "top_files_length": 5}
}
mock_pack.side_effect = RepopackError("Test error")
mock_spinner_instance = MagicMock()
mock_spinner.return_value = mock_spinner_instance

with pytest.raises(SystemExit):
run_cli()

mock_spinner_instance.fail.assert_called_with("Error during packing: Test error")
mock_logger_error.assert_called_with("Test error")
78 changes: 78 additions & 0 deletions tests/test_output_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import pytest
from repopack.output_generator import generate_output, generate_plain_output, generate_xml_output
from repopack.exceptions import OutputGenerationError


@pytest.fixture
def sample_data():
return {
"generationDate": "2023-04-01T12:00:00",
"treeString": "root/\n file1.txt\n file2.py",
"sanitizedFiles": [
{"path": "file1.txt", "content": "Content of file1"},
{"path": "file2.py", "content": "Content of file2"},
],
"config": {
"output": {
"style": "plain",
"remove_comments": False,
"show_line_numbers": False,
"header_text": "Custom header",
}
},
}


def test_generate_plain_output(sample_data):
output = generate_plain_output(sample_data)
assert "RepopackPy Output File" in output
assert "file1.txt" in output
assert "file2.py" in output
assert "Content of file1" in output
assert "Content of file2" in output
assert "Custom header" in output


def test_generate_xml_output(sample_data):
sample_data["config"]["output"]["style"] = "xml"
output = generate_xml_output(sample_data)
assert "<summary>" in output
assert '<file path="file1.txt">' in output
assert '<file path="file2.py">' in output
assert "Content of file1" in output
assert "Content of file2" in output
assert "<user_provided_header>" in output


def test_generate_output_plain(sample_data, tmp_path):
output_path = tmp_path / "output.txt"
generate_output(
"root",
sample_data["config"],
sample_data["sanitizedFiles"],
["file1.txt", "file2.py"],
str(output_path),
)
assert output_path.exists()
content = output_path.read_text()
assert "RepopackPy Output File" in content


def test_generate_output_xml(sample_data, tmp_path):
sample_data["config"]["output"]["style"] = "xml"
output_path = tmp_path / "output.xml"
generate_output(
"root",
sample_data["config"],
sample_data["sanitizedFiles"],
["file1.txt", "file2.py"],
str(output_path),
)
assert output_path.exists()
content = output_path.read_text()
assert "<summary>" in content


def test_generate_output_error():
with pytest.raises(OutputGenerationError):
generate_output("root", {}, [], [], "/nonexistent/path/output.txt")
84 changes: 84 additions & 0 deletions tests/test_packager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import pytest
from unittest.mock import patch, MagicMock
from repopack.packager import pack
from repopack.exceptions import RepopackError, FileProcessingError, OutputGenerationError


@pytest.fixture
def mock_config():
return {
"ignore": {"use_default_patterns": True, "use_gitignore": True, "custom_patterns": []},
"output": {"file_path": "output.txt", "top_files_length": 5},
}


def test_pack_success(mock_config, tmp_path):
root_dir = tmp_path / "test_repo"
root_dir.mkdir()
(root_dir / "file1.txt").write_text("Content 1")
(root_dir / "file2.py").write_text("Content 2")

with patch("repopack.packager.generate_output") as mock_generate_output, patch(
"repopack.packager.sanitize_files"
) as mock_sanitize_files:

mock_sanitize_files.return_value = [
{"path": "file1.txt", "content": "Content 1"},
{"path": "file2.py", "content": "Content 2"},
]

result = pack(str(root_dir), mock_config, "output.txt")

assert result["total_files"] == 2
assert result["total_characters"] == 18
assert len(result["file_char_counts"]) == 2
mock_generate_output.assert_called_once()


def test_pack_file_processing_error(mock_config, tmp_path):
root_dir = tmp_path / "test_repo"
root_dir.mkdir()

with patch("repopack.packager.sanitize_files") as mock_sanitize_files:
mock_sanitize_files.side_effect = FileProcessingError("test.txt", "Test error")

with pytest.raises(
RepopackError,
match="File processing error: Error processing file 'test.txt': Test error",
):
pack(str(root_dir), mock_config, "output.txt")


def test_pack_output_generation_error(mock_config, tmp_path):
root_dir = tmp_path / "test_repo"
root_dir.mkdir()
(root_dir / "file1.txt").write_text("Content")

with patch("repopack.packager.generate_output") as mock_generate_output, patch(
"repopack.packager.sanitize_files"
) as mock_sanitize_files:

mock_sanitize_files.return_value = [{"path": "file1.txt", "content": "Content"}]
mock_generate_output.side_effect = OutputGenerationError("Test error")

with pytest.raises(RepopackError) as excinfo:
pack(str(root_dir), mock_config, "output.txt")

assert str(excinfo.value) == "Output generation error: Error generating output: Test error"


def test_pack_os_error(mock_config):
with pytest.raises(RepopackError) as excinfo:
pack("/nonexistent/path", mock_config, "output.txt")

error_message = str(excinfo.value)

assert any(
phrase in error_message
for phrase in [
"OS error:",
"No such file or directory",
"Error processing files:",
"Error generating output:",
]
), f"Unexpected error message: {error_message}"

0 comments on commit f685e14

Please sign in to comment.