|
| 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