|
1 | 1 | import os
|
2 | 2 | import tempfile
|
| 3 | +import shutil |
| 4 | +from unittest.mock import patch |
| 5 | +from git import Repo |
3 | 6 | from coderoller.source_repo_flattener import flatten_repo
|
| 7 | +from coderoller.flatten_repo import main |
4 | 8 |
|
5 | 9 |
|
6 | 10 | def test_flatten_repo():
|
@@ -82,3 +86,67 @@ def test_hidden_files_and_directories():
|
82 | 86 | assert (
|
83 | 87 | ".hidden_file.py" not in flattened_content
|
84 | 88 | ), "Hidden file should be excluded"
|
| 89 | + |
| 90 | + |
| 91 | +@patch.object(Repo, "clone_from") |
| 92 | +def test_flatten_repo_from_git(mock_clone_from): |
| 93 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 94 | + # Mock the clone_from method to copy a local repository structure instead |
| 95 | + def mock_clone(repo_url, to_path): |
| 96 | + shutil.copytree(temp_dir, to_path, dirs_exist_ok=True) |
| 97 | + |
| 98 | + mock_clone_from.side_effect = mock_clone |
| 99 | + |
| 100 | + # Create a mock repository structure |
| 101 | + os.makedirs(os.path.join(temp_dir, "src")) |
| 102 | + |
| 103 | + readme_content = "# This is the README" |
| 104 | + with open(os.path.join(temp_dir, "README.md"), "w") as f: |
| 105 | + f.write(readme_content) |
| 106 | + |
| 107 | + python_content = 'print("Hello, World!")' |
| 108 | + with open(os.path.join(temp_dir, "src", "main.py"), "w") as f: |
| 109 | + f.write(python_content) |
| 110 | + |
| 111 | + json_content = '{"key": "value"}' |
| 112 | + with open(os.path.join(temp_dir, "config.json"), "w") as f: |
| 113 | + f.write(json_content) |
| 114 | + |
| 115 | + # Test the CLI with a mock GitHub URL |
| 116 | + with patch( |
| 117 | + "sys.argv", ["coderoller-flatten-repo", "https://github.com/mock/repo.git"] |
| 118 | + ): |
| 119 | + main() |
| 120 | + |
| 121 | + # Check if the flattened file is created |
| 122 | + flattened_file_path = os.path.join(os.getcwd(), "repo.flat.md") |
| 123 | + assert os.path.exists(flattened_file_path), "Flattened file was not created" |
| 124 | + |
| 125 | + with open(flattened_file_path, "r") as f: |
| 126 | + flattened_content = f.read() |
| 127 | + |
| 128 | + # Check if the README content is included |
| 129 | + assert "## README" in flattened_content, "README section is missing" |
| 130 | + assert ( |
| 131 | + "```markdown" in flattened_content |
| 132 | + ), "README content is not properly formatted" |
| 133 | + assert readme_content in flattened_content, "README content is incorrect" |
| 134 | + |
| 135 | + # Check if the Python file content is included |
| 136 | + assert ( |
| 137 | + "## File: src/main.py" in flattened_content |
| 138 | + ), "Python file section is missing" |
| 139 | + assert ( |
| 140 | + "```python" in flattened_content |
| 141 | + ), "Python content is not properly formatted" |
| 142 | + assert python_content in flattened_content, "Python content is incorrect" |
| 143 | + |
| 144 | + # Check if the JSON file content is included |
| 145 | + assert ( |
| 146 | + "## File: config.json" in flattened_content |
| 147 | + ), "JSON file section is missing" |
| 148 | + assert "```json" in flattened_content, "JSON content is not properly formatted" |
| 149 | + assert json_content in flattened_content, "JSON content is incorrect" |
| 150 | + |
| 151 | + # Clean up the flattened file |
| 152 | + os.remove(flattened_file_path) |
0 commit comments