Skip to content

Commit 531d359

Browse files
committed
add simple tests
1 parent 1123b5a commit 531d359

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

src/aiida/tools/archive/create.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def create_archive(
141141
142142
:param backend: the backend to export from. If not specified, the default backend is used.
143143
144-
:param temp_dir: Directory to use for temporary files during archive creation.
144+
:param tmp_dir: Directory to use for temporary files during archive creation.
145145
If not specified, a temporary directory will be created in the same directory as the output file
146146
with a '.aiida-export-' prefix. This parameter is useful when the output directory has limited
147147
space or when you want to use a specific filesystem (e.g., faster storage) for temporary operations.
@@ -200,7 +200,7 @@ def querybuilder():
200200
# Taken from: https://stackoverflow.com/a/2113511
201201
if not os.access(tmp_dir, os.W_OK | os.X_OK):
202202
msg = f"Specified temporary directory '{tmp_dir}' is not writable"
203-
raise ArchiveExportError()
203+
raise ArchiveExportError(msg)
204204
tmp_prefix = None # Use default tempfile prefix
205205
else:
206206
# Create temporary directory in the same folder as the output file
@@ -311,9 +311,6 @@ def querybuilder():
311311
# Create and open the archive for writing.
312312
# We create in a temp dir then move to final place at end,
313313
# so that the user cannot end up with a half written archive on errors
314-
# import ipdb; ipdb.set_trace()
315-
tmp_dir = Path('/mount') # or whatever directory you want to use
316-
317314
try:
318315
with tempfile.TemporaryDirectory(dir=tmp_dir, prefix=tmp_prefix) as tmpdir:
319316
tmp_filename = Path(tmpdir) / 'export.zip'
@@ -406,10 +403,11 @@ def transform(d):
406403
shutil.move(tmp_filename, filename)
407404
except OSError as e:
408405
if e.errno == 28: # No space left on device
409-
raise ArchiveExportError(
406+
msg = (
410407
f"Insufficient disk space in temporary directory '{tmp_dir}'. "
411408
f'Consider using --tmp-dir to specify a location with more available space.'
412-
) from e
409+
)
410+
raise ArchiveExportError(msg) from e
413411
raise ArchiveExportError(f'Failed to create temporary directory: {e}') from e
414412

415413
EXPORT_LOGGER.report('Archive created successfully')

tests/tools/archive/test_simple.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from aiida.common.exceptions import IncompatibleStorageSchema, LicensingException
1919
from aiida.common.links import LinkType
2020
from aiida.tools.archive import create_archive, import_archive
21+
from aiida.tools.archive.exceptions import ArchiveExportError
2122

2223

2324
@pytest.mark.parametrize('entities', ['all', 'specific'])
@@ -154,3 +155,63 @@ def crashing_filter(_):
154155

155156
with pytest.raises(LicensingException):
156157
create_archive([struct], test_run=True, forbidden_licenses=crashing_filter)
158+
159+
160+
@pytest.mark.usefixtures("aiida_profile_clean")
161+
def test_tmp_dir_custom_valid(tmp_path):
162+
"""Test using a custom valid temporary directory."""
163+
from unittest.mock import patch
164+
165+
node = orm.Int(42).store()
166+
custom_tmp = tmp_path / 'custom_tmp'
167+
custom_tmp.mkdir()
168+
filename = tmp_path / 'export.aiida' # Put output file outside custom_tmp
169+
170+
with patch('tempfile.TemporaryDirectory') as mock_temp_dir:
171+
# Create the actual temp directory that the mock returns
172+
actual_temp_dir = custom_tmp / 'temp_dir'
173+
actual_temp_dir.mkdir()
174+
175+
mock_temp_dir.return_value.__enter__.return_value = str(actual_temp_dir)
176+
mock_temp_dir.return_value.__exit__.return_value = None
177+
178+
create_archive([node], filename=filename, tmp_dir=custom_tmp)
179+
180+
# Check that TemporaryDirectory was called with custom directory
181+
mock_temp_dir.assert_called_once_with(dir=custom_tmp, prefix=None)
182+
183+
@pytest.mark.usefixtures("aiida_profile_clean")
184+
def test_tmp_dir_validation_errors(tmp_path):
185+
"""Test tmp_dir validation errors."""
186+
187+
node = orm.Int(42).store()
188+
filename = tmp_path / 'export.aiida'
189+
190+
# Non-existent directory
191+
with pytest.raises(ArchiveExportError, match="does not exist"):
192+
create_archive([node], filename=filename, tmp_dir=tmp_path / 'nonexistent')
193+
194+
# File instead of directory
195+
not_a_dir = tmp_path / 'file.txt'
196+
not_a_dir.write_text('content')
197+
with pytest.raises(ArchiveExportError, match="is not a directory"):
198+
create_archive([node], filename=filename, tmp_dir=not_a_dir)
199+
200+
@pytest.mark.usefixtures("aiida_profile_clean")
201+
def test_tmp_dir_disk_space_error(tmp_path):
202+
"""Test disk space error handling."""
203+
from unittest.mock import patch
204+
205+
node = orm.Int(42).store()
206+
custom_tmp = tmp_path / 'custom_tmp'
207+
custom_tmp.mkdir()
208+
filename = tmp_path / 'export.aiida'
209+
210+
def mock_temp_dir_error(*args, **kwargs):
211+
error = OSError("No space left on device")
212+
error.errno = 28
213+
raise error
214+
215+
with patch('tempfile.TemporaryDirectory', side_effect=mock_temp_dir_error):
216+
with pytest.raises(ArchiveExportError, match="Insufficient disk space.*--tmp-dir"):
217+
create_archive([node], filename=filename, tmp_dir=custom_tmp)

0 commit comments

Comments
 (0)