@@ -158,7 +158,7 @@ def crashing_filter(_):
158158
159159
160160@pytest .mark .usefixtures ('aiida_profile_clean' )
161- def test_tmp_dir (tmp_path , aiida_profile_clean ):
161+ def test_tmp_dir_basic (tmp_path ):
162162 """Test that tmp_dir parameter is used correctly."""
163163 node = orm .Int (42 ).store ()
164164 custom_tmp = tmp_path / 'custom_tmp'
@@ -201,3 +201,68 @@ def mock_temp_dir_error(*args, **kwargs):
201201 with patch ('tempfile.TemporaryDirectory' , side_effect = mock_temp_dir_error ):
202202 with pytest .raises (ArchiveExportError , match = 'Insufficient disk space.*--tmp-dir' ):
203203 create_archive ([node ], filename = filename , tmp_dir = custom_tmp )
204+
205+
206+ @pytest .mark .usefixtures ('aiida_profile_clean' )
207+ def test_tmp_dir_auto_create (tmp_path ):
208+ """Test automatic creation of non-existent tmp_dir."""
209+ node = orm .Int (42 ).store ()
210+ filename = tmp_path / 'export.aiida'
211+ custom_tmp = tmp_path / 'nonexistent_tmp' # Don't create it!
212+
213+ create_archive ([node ], filename = filename , tmp_dir = custom_tmp )
214+ assert filename .exists ()
215+ # Verify the directory was created
216+ assert custom_tmp .exists ()
217+
218+
219+ @pytest .mark .usefixtures ('aiida_profile_clean' )
220+ def test_tmp_dir_permission_error (tmp_path ):
221+ """Test tmp_dir permission validation."""
222+ import stat
223+
224+ node = orm .Int (42 ).store ()
225+ filename = tmp_path / 'export.aiida'
226+ readonly_tmp = tmp_path / 'readonly_tmp'
227+ readonly_tmp .mkdir ()
228+
229+ # Make directory read-only
230+ readonly_tmp .chmod (stat .S_IRUSR | stat .S_IRGRP | stat .S_IROTH )
231+
232+ try :
233+ with pytest .raises (ArchiveExportError , match = 'is not writable' ):
234+ create_archive ([node ], filename = filename , tmp_dir = readonly_tmp )
235+ finally :
236+ # Restore permissions for cleanup
237+ readonly_tmp .chmod (stat .S_IRWXU )
238+
239+
240+ @pytest .mark .usefixtures ('aiida_profile_clean' )
241+ def test_tmp_dir_default_behavior (tmp_path ):
242+ """Test default tmp_dir behavior (no tmp_dir specified)."""
243+ node = orm .Int (42 ).store ()
244+ filename = tmp_path / 'export.aiida'
245+
246+ # Don't specify tmp_dir - test the default path
247+ create_archive ([node ], filename = filename )
248+ assert filename .exists ()
249+
250+
251+ @pytest .mark .usefixtures ('aiida_profile_clean' )
252+ def test_tmp_dir_general_os_error (tmp_path ):
253+ """Test general OS error handling."""
254+ from unittest .mock import patch
255+
256+ node = orm .Int (42 ).store ()
257+ custom_tmp = tmp_path / 'custom_tmp'
258+ custom_tmp .mkdir ()
259+ filename = tmp_path / 'export.aiida'
260+
261+ def mock_temp_dir_error (* args , ** kwargs ):
262+ error = OSError ('Permission denied' )
263+ error .errno = 13 # Different from 28
264+ raise error
265+
266+ with patch ('aiida.tools.archive.create.tempfile.TemporaryDirectory' , side_effect = mock_temp_dir_error ):
267+ with pytest .raises (ArchiveExportError , match = 'Failed to create temporary directory' ):
268+ create_archive ([node ], filename = filename , tmp_dir = custom_tmp )
0 commit comments