|
13 | 13 |
|
14 | 14 | SCRIPT = pathlib.Path(__file__).resolve().with_name('seed-issue-21397.py') |
15 | 15 | FIXTURE = json.loads(SCRIPT.with_name('fixtures').joinpath('issue-21397.json').read_text()) |
| 16 | +# Windows cannot delete a file SQLite still holds open, so cleanup must close connections first. |
| 17 | +OPEN_DB_PROBE = ''' |
| 18 | +import runpy, shutil, sqlite3, sys |
| 19 | +opened, connect, rmtree = [], sqlite3.connect, shutil.rmtree |
| 20 | +def tracked(*args, **kwargs): |
| 21 | + opened.append(connect(*args, **kwargs)) |
| 22 | + return opened[-1] |
| 23 | +def checked(*args, **kwargs): |
| 24 | + for db in opened: |
| 25 | + try: |
| 26 | + db.total_changes |
| 27 | + except sqlite3.ProgrammingError: |
| 28 | + continue |
| 29 | + sys.exit('rmtree ran while a SQLite connection was open') |
| 30 | + return rmtree(*args, **kwargs) |
| 31 | +sqlite3.connect, shutil.rmtree = tracked, checked |
| 32 | +sys.argv = sys.argv[1:] |
| 33 | +runpy.run_path(sys.argv[0], run_name='__main__') |
| 34 | +''' |
16 | 35 |
|
17 | 36 |
|
18 | 37 | def snapshot(directory): |
@@ -49,9 +68,9 @@ def setUp(self): |
49 | 68 | (self.source_home / 'settings.json').write_text('{"sentinel": true}\n') |
50 | 69 | self.before = snapshot(self.source_home) |
51 | 70 |
|
52 | | - def run_seeder(self, *args): |
| 71 | + def run_seeder(self, *args, probe=()): |
53 | 72 | env = {**os.environ, 'HOME': str(self.fake_home)} |
54 | | - return subprocess.run([sys.executable, '-I', str(SCRIPT), '--source', str(self.source), *args], |
| 73 | + return subprocess.run([sys.executable, '-I', *probe, str(SCRIPT), '--source', str(self.source), *args], |
55 | 74 | env=env, capture_output=True, text=True) |
56 | 75 |
|
57 | 76 | def assertSourceUnchanged(self): |
@@ -151,9 +170,10 @@ def test_failed_seed_removes_only_its_new_home(self): |
151 | 170 | db.close() |
152 | 171 | self.before = snapshot(self.source_home) |
153 | 172 | home = self.tmp / 'broken' |
154 | | - result = self.run_seeder('--home', str(home)) |
| 173 | + result = self.run_seeder('--home', str(home), probe=('-c', OPEN_DB_PROBE)) |
155 | 174 | self.assertNotEqual(result.returncode, 0) |
156 | | - self.assertFalse(home.exists()) |
| 175 | + self.assertIn('orchestration_v2_effect_outbox', result.stderr) |
| 176 | + self.assertFalse(home.exists(), result.stderr) |
157 | 177 | self.assertSourceUnchanged() |
158 | 178 |
|
159 | 179 |
|
|
0 commit comments