|
18 | 18 | from aiida.common.exceptions import IncompatibleStorageSchema, LicensingException |
19 | 19 | from aiida.common.links import LinkType |
20 | 20 | from aiida.tools.archive import create_archive, import_archive |
| 21 | +from aiida.tools.archive.exceptions import ArchiveExportError |
21 | 22 |
|
22 | 23 |
|
23 | 24 | @pytest.mark.parametrize('entities', ['all', 'specific']) |
@@ -154,3 +155,63 @@ def crashing_filter(_): |
154 | 155 |
|
155 | 156 | with pytest.raises(LicensingException): |
156 | 157 | 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