|
1 | 1 | """Unit tests for GitClient checkout functionality""" |
2 | 2 |
|
3 | 3 | import os |
| 4 | +import tarfile |
4 | 5 | import tempfile |
5 | 6 | import unittest |
6 | 7 |
|
@@ -59,5 +60,86 @@ def test_checkout_to_existing_directory(self): |
59 | 60 | self.assertFalse(success, 'Checkout should fail for non-empty directory') |
60 | 61 |
|
61 | 62 |
|
| 63 | +class TestExportRepository(unittest.TestCase): |
| 64 | + """Test cases for GitClient export_repository method""" |
| 65 | + |
| 66 | + def setUp(self): |
| 67 | + # Create a temporary directory for testing |
| 68 | + self.test_dir = tempfile.mkdtemp() |
| 69 | + self.repo_path = os.path.join(self.test_dir, 'test_repo') |
| 70 | + self.export_dir = os.path.join(self.test_dir, 'exports') |
| 71 | + os.makedirs(self.export_dir, exist_ok=True) |
| 72 | + |
| 73 | + self.test_repo_url = 'https://github.com/octocat/Hello-World.git' |
| 74 | + self.client = GitClient(self.repo_path) |
| 75 | + |
| 76 | + def tearDown(self): |
| 77 | + if os.path.exists(self.test_dir): |
| 78 | + rmtree(self.test_dir) |
| 79 | + |
| 80 | + def test_export_specific_branch(self): |
| 81 | + """Test exporting the repository at a specific branch""" |
| 82 | + success = self.client.checkout(self.test_repo_url, version='test') |
| 83 | + |
| 84 | + if not success: |
| 85 | + self.fail('Failed to clone test repository') |
| 86 | + |
| 87 | + basepath = os.path.join(self.export_dir, 'repo_export') |
| 88 | + filepath = self.client.export_repository('test', basepath) |
| 89 | + |
| 90 | + self.assertTrue(os.path.exists(filepath), 'Export file should exist') |
| 91 | + self.assertGreater( |
| 92 | + os.path.getsize(filepath), 0, 'Export file should not be empty' |
| 93 | + ) |
| 94 | + # Verify it's a valid tar.gz file |
| 95 | + try: |
| 96 | + if filepath and isinstance(filepath, str): |
| 97 | + with tarfile.open(filepath, 'r:gz') as tar: |
| 98 | + members = tar.getnames() |
| 99 | + self.assertIn('README', members, 'Should contain README file') |
| 100 | + else: |
| 101 | + self.fail('Exported file path is invalid') |
| 102 | + except tarfile.ReadError: |
| 103 | + self.fail('Exported file should be a valid tar.gz archive') |
| 104 | + |
| 105 | + def test_export_with_local_changes_uses_temp_dir(self): |
| 106 | + """Test that export uses temp directory when there are local changes""" |
| 107 | + # First clone the repository |
| 108 | + success = self.client.checkout(self.test_repo_url) |
| 109 | + if not success: |
| 110 | + self.fail('Failed to clone test repository') |
| 111 | + |
| 112 | + # Create a local change |
| 113 | + test_file = os.path.join(self.repo_path, 'test_change.txt') |
| 114 | + with open(test_file, 'w', encoding='utf-8') as f: |
| 115 | + f.write('test change') |
| 116 | + |
| 117 | + basepath = os.path.join(self.export_dir, 'local_changes_test') |
| 118 | + filepath = self.client.export_repository(None, basepath) |
| 119 | + |
| 120 | + self.assertIsNotNone(filepath, 'Export should succeed even with local changes') |
| 121 | + self.assertTrue(os.path.exists(filepath), 'Export file should exist') |
| 122 | + self.assertGreater( |
| 123 | + os.path.getsize(filepath), 0, 'Export file should not be empty' |
| 124 | + ) |
| 125 | + |
| 126 | + # Clean up |
| 127 | + os.remove(test_file) |
| 128 | + |
| 129 | + def test_export_invalid_branch(self): |
| 130 | + """Test exporting a non-existent branch should fail gracefully""" |
| 131 | + success = self.client.checkout(self.test_repo_url) |
| 132 | + |
| 133 | + if not success: |
| 134 | + self.fail('Failed to clone test repository') |
| 135 | + |
| 136 | + basepath = os.path.join(self.export_dir, 'nonexistent_export') |
| 137 | + result = self.client.export_repository('nonexistent-branch-12345', basepath) |
| 138 | + |
| 139 | + self.assertEqual( |
| 140 | + result, False, 'Export should return False for non-existent ref' |
| 141 | + ) |
| 142 | + |
| 143 | + |
62 | 144 | if __name__ == '__main__': |
63 | 145 | unittest.main() |
0 commit comments