@@ -81,36 +81,42 @@ def in_memory_db():
8181@pytest .fixture
8282def db_file ():
8383 """Return a path to a temporary SQLite file with schema applied."""
84- with tempfile .NamedTemporaryFile (suffix = ".db" , delete = False ) as f :
85- path = f .name
86- with sqlite3 .connect (path ) as conn :
84+ fd , path = tempfile .mkstemp (suffix = ".db" )
85+ os .close (fd )
86+ conn = sqlite3 .connect (path )
87+ try :
8788 cursor = conn .cursor ()
8889 _ensure_schema (cursor )
8990 conn .commit ()
91+ finally :
92+ conn .close ()
9093 yield path
9194 os .unlink (path )
9295
9396
9497@pytest .fixture
9598def db_file_with_settings ():
9699 """A temp db file with MigrationSettings already populated."""
97- with tempfile .NamedTemporaryFile (suffix = ".db" , delete = False ) as f :
98- path = f . name
100+ fd , path = tempfile .mkstemp (suffix = ".db" )
101+ os . close ( fd )
99102 settings = MigrationSettings (
100103 root_id = "syn1" ,
101104 dest_storage_location_id = "99" ,
102105 source_storage_location_ids = [],
103106 file_version_strategy = "new" ,
104107 include_table_files = False ,
105108 )
106- with sqlite3 .connect (path ) as conn :
109+ conn = sqlite3 .connect (path )
110+ try :
107111 cursor = conn .cursor ()
108112 _ensure_schema (cursor )
109113 cursor .execute (
110114 "INSERT INTO migration_settings (settings) VALUES (?)" ,
111115 (json .dumps (settings .to_dict ()),),
112116 )
113117 conn .commit ()
118+ finally :
119+ conn .close ()
114120 yield path , settings
115121 os .unlink (path )
116122
@@ -1290,9 +1296,9 @@ async def test_successful_indexing_returns_migration_result(self):
12901296 client = _make_mock_client ()
12911297 entity = _make_entity ("syn3" )
12921298
1293- with tempfile .TemporaryDirectory () as tmpdir :
1294- db_path = os .path . join ( tmpdir , "test.db" )
1295-
1299+ fd , db_path = tempfile .mkstemp ( suffix = ".db" )
1300+ os .close ( fd )
1301+ try :
12961302 with (
12971303 patch (f"{ MODULE } .Synapse.get_client" , return_value = client ),
12981304 patch (f"{ MODULE } .utils.id_of" , return_value = "syn3" ),
@@ -1308,6 +1314,8 @@ async def test_successful_indexing_returns_migration_result(self):
13081314 db_path = db_path ,
13091315 synapse_client = client ,
13101316 )
1317+ finally :
1318+ os .unlink (db_path )
13111319
13121320 assert isinstance (result , MigrationResult )
13131321 assert result .db_path == db_path
@@ -1320,8 +1328,9 @@ async def test_indexing_error_is_reraised(self):
13201328 indexing_err = IndexingError ("syn3" , concrete_types .FILE_ENTITY )
13211329 indexing_err .__cause__ = underlying
13221330
1323- with tempfile .TemporaryDirectory () as tmpdir :
1324- db_path = os .path .join (tmpdir , "test.db" )
1331+ fd , db_path = tempfile .mkstemp (suffix = ".db" )
1332+ os .close (fd )
1333+ try :
13251334 with (
13261335 patch (f"{ MODULE } .Synapse.get_client" , return_value = client ),
13271336 patch (f"{ MODULE } .utils.id_of" , return_value = "syn3" ),
@@ -1338,6 +1347,8 @@ async def test_indexing_error_is_reraised(self):
13381347 db_path = db_path ,
13391348 synapse_client = client ,
13401349 )
1350+ finally :
1351+ os .unlink (db_path )
13411352
13421353
13431354# =============================================================================
@@ -2309,13 +2320,16 @@ async def _failing_migrate():
23092320class TestMigrateIndexedFilesAsync :
23102321 @pytest .mark .asyncio
23112322 async def test_raises_if_no_settings_in_db (self ):
2312- with tempfile .NamedTemporaryFile (suffix = ".db" , delete = False ) as f :
2313- path = f . name
2323+ fd , path = tempfile .mkstemp (suffix = ".db" )
2324+ os . close ( fd )
23142325 try :
2315- with sqlite3 .connect (path ) as conn :
2326+ conn = sqlite3 .connect (path )
2327+ try :
23162328 cursor = conn .cursor ()
23172329 _ensure_schema (cursor )
23182330 conn .commit ()
2331+ finally :
2332+ conn .close ()
23192333
23202334 client = _make_mock_client ()
23212335 with patch (f"{ MODULE } .Synapse.get_client" , return_value = client ):
0 commit comments