Skip to content

Commit cf9159c

Browse files
committed
Added tests, and an optional specification of the output folder
1 parent fa54ccf commit cf9159c

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

src/coderoller/source_repo_flattener.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,18 @@ def find_readme(root_folder: str) -> str:
4848
return ""
4949

5050

51-
def flatten_repo(root_folder: str):
51+
def flatten_repo(root_folder: str, output_folder: str | None = None):
5252
"""
5353
Flatten the source repository into a single markdown file.
5454
5555
Args:
5656
root_folder (str): The root folder of the repository.
57+
output_folder (str | None): The folder to save the flattened file. Defaults to the current working directory.
5758
"""
5859
repo_name = os.path.basename(os.path.normpath(root_folder))
59-
flattened_file_path = os.path.join(os.getcwd(), f"{repo_name}.flat.md")
60+
if output_folder is None:
61+
output_folder = os.getcwd()
62+
flattened_file_path = os.path.join(output_folder, f"{repo_name}.flat.md")
6063

6164
readme_path = find_readme(root_folder)
6265

tests/test_source_repo_flattener.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import os
2+
import tempfile
3+
from coderoller.source_repo_flattener import flatten_repo
4+
5+
6+
def test_flatten_repo():
7+
with tempfile.TemporaryDirectory() as temp_dir:
8+
# Create a mock repository structure
9+
os.makedirs(os.path.join(temp_dir, "src"))
10+
11+
readme_content = "# This is the README"
12+
with open(os.path.join(temp_dir, "README.md"), "w") as f:
13+
f.write(readme_content)
14+
15+
python_content = 'print("Hello, World!")'
16+
with open(os.path.join(temp_dir, "src", "main.py"), "w") as f:
17+
f.write(python_content)
18+
19+
json_content = '{"key": "value"}'
20+
with open(os.path.join(temp_dir, "config.json"), "w") as f:
21+
f.write(json_content)
22+
23+
output_dir = tempfile.mkdtemp()
24+
flatten_repo(temp_dir, output_dir)
25+
26+
# Check if the flattened file is created
27+
flattened_file_path = os.path.join(
28+
output_dir, f"{os.path.basename(temp_dir)}.flat.md"
29+
)
30+
assert os.path.exists(flattened_file_path), "Flattened file was not created"
31+
32+
with open(flattened_file_path, "r") as f:
33+
flattened_content = f.read()
34+
35+
# Check if the README content is included
36+
assert "## README" in flattened_content, "README section is missing"
37+
assert (
38+
"```markdown" in flattened_content
39+
), "README content is not properly formatted"
40+
assert readme_content in flattened_content, "README content is incorrect"
41+
42+
# Check if the Python file content is included
43+
assert (
44+
"## File: src/main.py" in flattened_content
45+
), "Python file section is missing"
46+
assert (
47+
"```python" in flattened_content
48+
), "Python content is not properly formatted"
49+
assert python_content in flattened_content, "Python content is incorrect"
50+
51+
# Check if the JSON file content is included
52+
assert (
53+
"## File: config.json" in flattened_content
54+
), "JSON file section is missing"
55+
assert "```json" in flattened_content, "JSON content is not properly formatted"
56+
assert json_content in flattened_content, "JSON content is incorrect"
57+
58+
59+
def test_hidden_files_and_directories():
60+
with tempfile.TemporaryDirectory() as temp_dir:
61+
# Create a hidden directory and file
62+
os.makedirs(os.path.join(temp_dir, ".hidden_dir"))
63+
with open(os.path.join(temp_dir, ".hidden_file.py"), "w") as f:
64+
f.write('print("This should not be included")')
65+
66+
output_dir = tempfile.mkdtemp()
67+
flatten_repo(temp_dir, output_dir)
68+
69+
# Check if the flattened file is created
70+
flattened_file_path = os.path.join(
71+
output_dir, f"{os.path.basename(temp_dir)}.flat.md"
72+
)
73+
assert os.path.exists(flattened_file_path), "Flattened file was not created"
74+
75+
with open(flattened_file_path, "r") as f:
76+
flattened_content = f.read()
77+
78+
# Check if hidden files and directories are excluded
79+
assert (
80+
".hidden_dir" not in flattened_content
81+
), "Hidden directory should be excluded"
82+
assert (
83+
".hidden_file.py" not in flattened_content
84+
), "Hidden file should be excluded"

0 commit comments

Comments
 (0)