Skip to content

Commit 8e16110

Browse files
committed
Move path validation, and fix failing tests
1 parent 5faa2d1 commit 8e16110

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

src/aiida/tools/archive/create.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -187,26 +187,6 @@ def querybuilder():
187187
name: traversal_rules.get(name, rule.default) for name, rule in GraphTraversalRules.EXPORT.value.items()
188188
}
189189

190-
# Handle temporary directory configuration
191-
tmp_prefix = '.aiida-export-'
192-
if tmp_dir is not None:
193-
tmp_dir = Path(tmp_dir)
194-
if not tmp_dir.exists():
195-
EXPORT_LOGGER.warning(f"Specified temporary directory '{tmp_dir}' doesn't exist. Creating it.")
196-
tmp_dir.mkdir(parents=False)
197-
if not tmp_dir.is_dir():
198-
msg = f"Specified temporary directory '{tmp_dir}' is not a directory"
199-
raise ArchiveExportError(msg)
200-
# Check if directory is writable
201-
# Taken from: https://stackoverflow.com/a/2113511
202-
if not os.access(tmp_dir, os.W_OK | os.X_OK):
203-
msg = f"Specified temporary directory '{tmp_dir}' is not writable"
204-
raise ArchiveExportError(msg)
205-
206-
else:
207-
# Create temporary directory in the same folder as the output file
208-
tmp_dir = filename.parent
209-
210190
initial_summary = get_init_summary(
211191
archive_version=archive_format.latest_version,
212192
outfile=filename,
@@ -308,10 +288,29 @@ def querybuilder():
308288

309289
EXPORT_LOGGER.report(f'Creating archive with:\n{tabulate(count_summary)}')
310290

291+
# Handle temporary directory configuration
292+
tmp_prefix = '.aiida-export-'
293+
if tmp_dir is not None:
294+
tmp_dir = Path(tmp_dir)
295+
if not tmp_dir.exists():
296+
EXPORT_LOGGER.warning(f"Specified temporary directory '{tmp_dir}' doesn't exist. Creating it.")
297+
tmp_dir.mkdir(parents=False)
298+
if not tmp_dir.is_dir():
299+
msg = f"Specified temporary directory '{tmp_dir}' is not a directory"
300+
raise ArchiveExportError(msg)
301+
# Check if directory is writable
302+
# Taken from: https://stackoverflow.com/a/2113511
303+
if not os.access(tmp_dir, os.W_OK | os.X_OK):
304+
msg = f"Specified temporary directory '{tmp_dir}' is not writable"
305+
raise ArchiveExportError(msg)
306+
307+
else:
308+
# Create temporary directory in the same folder as the output file
309+
tmp_dir = filename.parent
310+
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-
315314
try:
316315
tmp_dir.mkdir(parents=True, exist_ok=True)
317316
with tempfile.TemporaryDirectory(dir=tmp_dir, prefix=tmp_prefix) as tmpdir:

tests/tools/archive/test_simple.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def test_tmp_dir_custom_valid(tmp_path):
178178
create_archive([node], filename=filename, tmp_dir=custom_tmp)
179179

180180
# Check that TemporaryDirectory was called with custom directory
181-
mock_temp_dir.assert_called_once_with(dir=custom_tmp, prefix=None)
181+
mock_temp_dir.assert_called_once_with(dir=custom_tmp, prefix='.aiida-export-')
182182

183183

184184
@pytest.mark.usefixtures('aiida_profile_clean')
@@ -188,10 +188,6 @@ def test_tmp_dir_validation_errors(tmp_path):
188188
node = orm.Int(42).store()
189189
filename = tmp_path / 'export.aiida'
190190

191-
# Non-existent directory
192-
with pytest.raises(ArchiveExportError, match='does not exist'):
193-
create_archive([node], filename=filename, tmp_dir=tmp_path / 'nonexistent')
194-
195191
# File instead of directory
196192
not_a_dir = tmp_path / 'file.txt'
197193
not_a_dir.write_text('content')

0 commit comments

Comments
 (0)