Skip to content

Commit

Permalink
tests (2 failing)
Browse files Browse the repository at this point in the history
  • Loading branch information
abinthomasonline committed Aug 5, 2024
1 parent af036d1 commit 91cfd07
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
testpaths = tests
python_files = test_*.py
3 changes: 3 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ chardet
colorama
halo
pathspec
pre-commit
pytest
pytest-cov
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# Add any other dependencies your project needs
],
extras_require={
"dev": ["black", "pre-commit"],
"dev": ["black", "pre-commit", "pytest", "pytest-cov"],
},
classifiers=[
"Programming Language :: Python :: 3",
Expand Down
Empty file added tests/__init__.py
Empty file.
29 changes: 29 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest
from repopack.config import load_config, merge_configs, DEFAULT_CONFIG
from repopack.exceptions import ConfigurationError


def test_load_config(tmp_path):
config_file = tmp_path / "config.json"
config_file.write_text('{"output": {"file_path": "custom_output.txt"}}')

config = load_config(str(config_file))
assert config["output"]["file_path"] == "custom_output.txt"


def test_load_config_invalid_json(tmp_path):
config_file = tmp_path / "invalid_config.json"
config_file.write_text('{"output": {')

with pytest.raises(ConfigurationError):
load_config(str(config_file))


def test_merge_configs():
file_config = {"output": {"file_path": "file_output.txt"}}
cli_config = {"output": {"show_line_numbers": True}}
merged = merge_configs(file_config, cli_config)

assert merged["output"]["file_path"] == "file_output.txt"
assert merged["output"]["show_line_numbers"] == True
assert merged["output"]["style"] == DEFAULT_CONFIG["output"]["style"]
25 changes: 25 additions & 0 deletions tests/test_file_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from unittest.mock import patch, mock_open
from repopack.utils.file_handler import is_binary, sanitize_file


def test_is_binary():
with patch("builtins.open", mock_open(read_data=b"\x00\x01\x02\x03")):
assert is_binary("fake_binary_file")


def test_is_not_binary():
with patch("builtins.open", mock_open(read_data="Hello, World!")):
assert not is_binary("fake_text_file")


def test_sanitize_file():
config = {
"output": {"remove_comments": False, "remove_empty_lines": True, "show_line_numbers": True}
}
content = "Line 1\n\nLine 3\n"
expected = "1 | Line 1\n2 | Line 3"

with patch("builtins.open", mock_open(read_data=content.encode())):
result = sanitize_file("fake_file.txt", config)

assert result == expected
39 changes: 39 additions & 0 deletions tests/test_ignore_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from unittest.mock import patch
from repopack.utils.ignore_utils import (
get_ignore_patterns,
get_all_ignore_patterns,
create_ignore_filter,
)


def test_get_ignore_patterns(tmp_path):
ignore_file = tmp_path / ".gitignore"
ignore_file.write_text("*.log\n#comment\nnode_modules/")

patterns = get_ignore_patterns(".gitignore", str(tmp_path))
assert patterns == ["*.log", "node_modules/"]


def test_get_all_ignore_patterns():
config = {
"ignore": {
"use_default_patterns": True,
"use_gitignore": True,
"custom_patterns": ["*.custom"],
}
}
with patch("repopack.utils.ignore_utils.get_ignore_patterns", return_value=["*.gitignore"]):
patterns = get_all_ignore_patterns("/fake/path", config)

assert "*.log" in patterns # from DEFAULT_IGNORE_LIST
assert "*.gitignore" in patterns # from mocked .gitignore
assert "*.custom" in patterns # from custom patterns


def test_create_ignore_filter():
patterns = ["*.log", "node_modules/"]
ignore_filter = create_ignore_filter(patterns)

assert not ignore_filter("test.log")
assert not ignore_filter("node_modules/package.json")
assert ignore_filter("src/main.py")
16 changes: 16 additions & 0 deletions tests/test_tree_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from repopack.utils.tree_generator import generate_tree_string


def test_generate_tree_string():
files = ["src/main.py", "src/utils/helper.py", "tests/test_main.py", "README.md"]
expected = (
"README.md\n"
"src/\n"
" main.py\n"
" utils/\n"
" helper.py\n"
"tests/\n"
" test_main.py"
)
result = generate_tree_string(files)
assert result == expected

0 comments on commit 91cfd07

Please sign in to comment.