Skip to content

Commit 989c470

Browse files
Added export_repository support and corresponding tests for hg client.
Signed-off-by: Leander Stephen D'Souza <leanderdsouza1234@gmail.com>
1 parent 074406a commit 989c470

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

test/test_hg.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Integration tests for HgClient checkout functionality"""
22

33
import os
4+
import subprocess
45
import tempfile
56
import unittest
67
from shutil import which
@@ -61,5 +62,62 @@ def test_checkout_existing_directory(self):
6162
)
6263

6364

65+
@unittest.skipIf(not hg, '`hg` was not found')
66+
class TestExportRepository(unittest.TestCase):
67+
"""Integration tests for HgClient _export_repository functionality."""
68+
69+
def setUp(self):
70+
"""Set up test fixtures for each test"""
71+
self.test_dir = tempfile.mkdtemp(prefix='hg_test_')
72+
self.repo_path = os.path.join(self.test_dir, 'hello')
73+
self.export_base_path = os.path.join(self.test_dir, 'exported_repo')
74+
self.repo_url = 'https://www.mercurial-scm.org/repo/hello'
75+
76+
self.hg_client = HgClient(self.repo_path)
77+
78+
self._ensure_repo_cloned()
79+
80+
def tearDown(self):
81+
"""Clean up test fixtures"""
82+
if os.path.exists(self.test_dir):
83+
rmtree(self.test_dir)
84+
85+
def _ensure_repo_cloned(self):
86+
"""Ensure the test repository is cloned locally"""
87+
if not os.path.exists(self.repo_path):
88+
try:
89+
subprocess.run(
90+
['hg', 'clone', '--rev', 'tip', self.repo_url, self.repo_path],
91+
check=True,
92+
capture_output=True,
93+
text=True,
94+
)
95+
except subprocess.CalledProcessError as e:
96+
self.fail(f'Failed to clone repository: {e.stderr}')
97+
98+
def test_export_repository_specific_revision(self):
99+
"""Test exporting a specific revision"""
100+
result = self.hg_client._export_repository('1', self.export_base_path)
101+
self.assertTrue(result, "Export should succeed for revision '1'")
102+
103+
# Verify files were created correctly
104+
tar_gz_path = self.export_base_path + '.tar.gz'
105+
self.assertTrue(os.path.exists(tar_gz_path))
106+
107+
def test_export_repository_invalid_revision(self):
108+
"""Test exporting with an invalid revision"""
109+
invalid_revision = 'nonexistent123456789'
110+
111+
result = self.hg_client._export_repository(
112+
invalid_revision, self.export_base_path
113+
)
114+
self.assertFalse(result, 'Export should fail for invalid revision')
115+
116+
tar_gz_path = self.export_base_path + '.tar.gz'
117+
tar_path = self.export_base_path + '.tar'
118+
self.assertFalse(os.path.exists(tar_gz_path))
119+
self.assertFalse(os.path.exists(tar_path))
120+
121+
64122
if __name__ == '__main__':
65123
unittest.main()

vcs2l/clients/hg.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import gzip
12
import os
23
from shutil import which
34
from threading import Lock
@@ -339,6 +340,36 @@ def _check_color(self, cmd):
339340
if HgClient._config_color:
340341
cmd[1:1] = '--color', 'always'
341342

343+
def _export_repository(self, version, basepath):
344+
"""Export the hg repository at a given version to a tar.gz file."""
345+
self._check_executable()
346+
347+
cmd = [
348+
HgClient._executable,
349+
'archive',
350+
'-t',
351+
'tar',
352+
'-r',
353+
version,
354+
basepath + '.tar',
355+
]
356+
result = self._run_command(cmd)
357+
if result['returncode']:
358+
print('Failed to export hg repository: %s' % result['output'])
359+
return False
360+
361+
try:
362+
with open(basepath + '.tar', 'rb') as tar_file:
363+
with gzip.open(basepath + '.tar.gz', 'wb') as gzip_file:
364+
gzip_file.writelines(tar_file)
365+
finally:
366+
try:
367+
os.remove(basepath + '.tar')
368+
except OSError:
369+
print('Could not remove intermediate tar file %s.tar' % basepath)
370+
371+
return True
372+
342373
def checkout(self, url, version='', verbose=False, shallow=False, timeout=None):
343374
"""Checkout the repository from the given URL."""
344375
if url is None or url.strip() == '':

0 commit comments

Comments
 (0)