diff --git a/repo_to_text/main.py b/repo_to_text/main.py index 6858e7c..bbf7e60 100644 --- a/repo_to_text/main.py +++ b/repo_to_text/main.py @@ -81,7 +81,7 @@ def remove_empty_dirs(tree_output: str, path='.') -> str: logging.debug('Empty directory removal complete') return '\n'.join(final_lines) -def save_repo_to_text(path='.') -> str: +def save_repo_to_text(path='.', output_dir=None) -> str: logging.debug(f'Starting to save repo structure to text for path: {path}') gitignore_spec = load_gitignore(path) tree_structure = get_tree_structure(path, gitignore_spec) @@ -91,6 +91,12 @@ def save_repo_to_text(path='.') -> str: timestamp = datetime.utcnow().strftime('%Y-%m-%d-%H-%M-%S-UTC') output_file = f'repo_snapshot_{timestamp}.txt' + # Determine the full path to the output file + if output_dir: + if not os.path.exists(output_dir): + os.makedirs(output_dir) + output_file = os.path.join(output_dir, output_file) + with open(output_file, 'w') as file: project_name = os.path.basename(os.path.abspath(path)) file.write(f'Directory: {project_name}\n\n') @@ -140,11 +146,12 @@ def save_repo_to_text(path='.') -> str: def main(): parser = argparse.ArgumentParser(description='Convert repository structure and contents to text') parser.add_argument('--debug', action='store_true', help='Enable debug logging') + parser.add_argument('--output-dir', type=str, help='Directory to save the output file') args = parser.parse_args() setup_logging(debug=args.debug) logging.debug('repo-to-text script started') - save_repo_to_text() + save_repo_to_text(output_dir=args.output_dir) logging.debug('repo-to-text script finished') if __name__ == '__main__': diff --git a/tests/test_main.py b/tests/test_main.py index e69de29..b3f8274 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -0,0 +1,31 @@ +import os +import subprocess +import pytest +import time + +def test_repo_to_text(): + # Remove any existing snapshot files to avoid conflicts + for file in os.listdir('.'): + if file.startswith('repo_snapshot_') and file.endswith('.txt'): + os.remove(file) + + # Run the repo-to-text command + result = subprocess.run(['repo-to-text'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + # Assert that the command ran without errors + assert result.returncode == 0, f"Command failed with error: {result.stderr.decode('utf-8')}" + + # Check for the existence of the new snapshot file + snapshot_files = [f for f in os.listdir('.') if f.startswith('repo_snapshot_') and f.endswith('.txt')] + assert len(snapshot_files) == 1, "No snapshot file created or multiple files created" + + # Verify that the snapshot file is not empty + with open(snapshot_files[0], 'r') as f: + content = f.read() + assert len(content) > 0, "Snapshot file is empty" + + # Clean up the generated snapshot file + os.remove(snapshot_files[0]) + +if __name__ == "__main__": + pytest.main()