@@ -153,28 +153,37 @@ def write_batch_file(tmp_path: Path, contents: str) -> str:
153153 return str (batch_path )
154154
155155
156- def open_checkpoint_file (tmp_path : Path , contents : str ) -> TextIOWrapper :
156+ def write_checkpoint_file (tmp_path : Path , contents : str ) -> str :
157157 checkpoint_path = tmp_path / 'checkpoint.sql'
158158 checkpoint_path .write_text (contents , encoding = 'utf-8' )
159- return checkpoint_path . open ( 'a' , encoding = 'utf-8' )
159+ return str ( checkpoint_path )
160160
161161
162162def test_replay_checkpoint_file_returns_zero_without_replayable_batch (tmp_path : Path ) -> None :
163163 batch_path = write_batch_file (tmp_path , 'select 1;\n ' )
164164
165165 assert batch_mode .replay_checkpoint_file (batch_path , None , resume = True ) == 0
166166
167- with open_checkpoint_file (tmp_path , 'select 1;\n ' ) as checkpoint :
168- with pytest .raises (batch_mode .CheckpointReplayError , match = 'incompatible with reading from the standard input' ):
169- batch_mode .replay_checkpoint_file ('-' , checkpoint , resume = True )
167+ checkpoint = write_checkpoint_file (tmp_path , 'select 1;\n ' )
168+
169+ with pytest .raises (batch_mode .CheckpointReplayError , match = 'incompatible with reading from the standard input' ):
170+ batch_mode .replay_checkpoint_file ('-' , checkpoint , resume = True )
171+
172+
173+ def test_replay_checkpoint_file_returns_zero_when_checkpoint_is_missing (tmp_path : Path ) -> None :
174+ batch_path = write_batch_file (tmp_path , 'select 1;\n ' )
175+ checkpoint_path = str (tmp_path / 'missing-checkpoint.sql' )
176+
177+ assert batch_mode .replay_checkpoint_file (batch_path , checkpoint_path , resume = True ) == 0
170178
171179
172180def test_replay_checkpoint_file_rejects_checkpoint_longer_than_batch (tmp_path : Path ) -> None :
173181 batch_path = write_batch_file (tmp_path , 'select 1;\n ' )
174182
175- with open_checkpoint_file (tmp_path , 'select 1;\n select 2;\n ' ) as checkpoint :
176- with pytest .raises (batch_mode .CheckpointReplayError , match = 'Checkpoint script longer than batch script.' ):
177- batch_mode .replay_checkpoint_file (batch_path , checkpoint , resume = True )
183+ checkpoint = write_checkpoint_file (tmp_path , 'select 1;\n select 2;\n ' )
184+
185+ with pytest .raises (batch_mode .CheckpointReplayError , match = 'Checkpoint script longer than batch script.' ):
186+ batch_mode .replay_checkpoint_file (batch_path , checkpoint , resume = True )
178187
179188
180189@pytest .mark .skipif (os .name == 'nt' , reason = 'todo: unknown' )
@@ -183,9 +192,10 @@ def test_replay_checkpoint_file_rejects_batch_read_error(monkeypatch, tmp_path:
183192
184193 monkeypatch .setattr (batch_mode , 'statements_from_filehandle' , lambda _handle : (_ for _ in ()).throw (ValueError ('bad batch' )))
185194
186- with open_checkpoint_file (tmp_path , 'select 1;\n ' ) as checkpoint :
187- with pytest .raises (batch_mode .CheckpointReplayError , match = f'Error reading --batch file: { batch_path } : bad batch' ):
188- batch_mode .replay_checkpoint_file (batch_path , checkpoint , resume = True )
195+ checkpoint = write_checkpoint_file (tmp_path , 'select 1;\n ' )
196+
197+ with pytest .raises (batch_mode .CheckpointReplayError , match = f'Error reading --batch file: { batch_path } : bad batch' ):
198+ batch_mode .replay_checkpoint_file (batch_path , checkpoint , resume = True )
189199
190200
191201@pytest .mark .skipif (os .name == 'nt' , reason = 'todo: unknown' )
@@ -203,9 +213,10 @@ def fake_statements_from_filehandle(handle):
203213
204214 monkeypatch .setattr (batch_mode , 'statements_from_filehandle' , fake_statements_from_filehandle )
205215
206- with open_checkpoint_file (tmp_path , 'select 1;\n ' ) as checkpoint :
207- with pytest .raises (batch_mode .CheckpointReplayError , match = f'Error reading --batch file: { batch_path } : bad batch iterator' ):
208- batch_mode .replay_checkpoint_file (batch_path , checkpoint , resume = True )
216+ checkpoint = write_checkpoint_file (tmp_path , 'select 1;\n ' )
217+
218+ with pytest .raises (batch_mode .CheckpointReplayError , match = f'Error reading --batch file: { batch_path } : bad batch iterator' ):
219+ batch_mode .replay_checkpoint_file (batch_path , checkpoint , resume = True )
209220
210221
211222@pytest .mark .skipif (os .name == 'nt' , reason = 'todo: unknown' )
@@ -219,27 +230,30 @@ def fake_statements_from_filehandle(handle):
219230
220231 monkeypatch .setattr (batch_mode , 'statements_from_filehandle' , fake_statements_from_filehandle )
221232
222- with open_checkpoint_file (tmp_path , 'select 1;\n ' ) as checkpoint :
223- with pytest .raises (batch_mode .CheckpointReplayError , match = f'Error reading --checkpoint file: { checkpoint .name } : bad checkpoint' ):
224- batch_mode .replay_checkpoint_file (batch_path , checkpoint , resume = True )
233+ checkpoint = write_checkpoint_file (tmp_path , 'select 1;\n ' )
234+
235+ with pytest .raises (batch_mode .CheckpointReplayError , match = f'Error reading --checkpoint file: { checkpoint } : bad checkpoint' ):
236+ batch_mode .replay_checkpoint_file (batch_path , checkpoint , resume = True )
225237
226238
227239def test_replay_checkpoint_file_rejects_missing_files (tmp_path : Path ) -> None :
228240 batch_path = str (tmp_path / 'missing.sql' )
229241
230- with open_checkpoint_file (tmp_path , 'select 1;\n ' ) as checkpoint :
231- with pytest .raises (batch_mode .CheckpointReplayError , match = 'FileNotFoundError' ):
232- batch_mode .replay_checkpoint_file (batch_path , checkpoint , resume = True )
242+ checkpoint = write_checkpoint_file (tmp_path , 'select 1;\n ' )
243+
244+ with pytest .raises (batch_mode .CheckpointReplayError , match = 'FileNotFoundError' ):
245+ batch_mode .replay_checkpoint_file (batch_path , checkpoint , resume = True )
233246
234247
235248def test_replay_checkpoint_file_rejects_open_errors (monkeypatch , tmp_path : Path ) -> None :
236249 batch_path = write_batch_file (tmp_path , 'select 1;\n ' )
237250
238251 monkeypatch .setattr (batch_mode .click , 'open_file' , lambda * _args , ** _kwargs : (_ for _ in ()).throw (OSError ('open failed' )))
239252
240- with open_checkpoint_file (tmp_path , 'select 1;\n ' ) as checkpoint :
241- with pytest .raises (batch_mode .CheckpointReplayError , match = 'OSError' ):
242- batch_mode .replay_checkpoint_file (batch_path , checkpoint , resume = True )
253+ checkpoint = write_checkpoint_file (tmp_path , 'select 1;\n ' )
254+
255+ with pytest .raises (batch_mode .CheckpointReplayError , match = 'OSError' ):
256+ batch_mode .replay_checkpoint_file (batch_path , checkpoint , resume = True )
243257
244258
245259@pytest .mark .parametrize (
@@ -514,10 +528,10 @@ def test_main_batch_without_progress_bar_skips_checkpoint_prefix(monkeypatch, tm
514528 )
515529 monkeypatch .setattr (batch_mode , 'sys' , make_fake_sys (stdin_tty = True ))
516530
517- with open_checkpoint_file (tmp_path , 'select 1;\n select 2;\n ' ) as checkpoint :
518- cli_args = DummyCliArgs (batch = batch_path , checkpoint = checkpoint , resume = True )
531+ checkpoint = write_checkpoint_file (tmp_path , 'select 1;\n select 2;\n ' )
519532
520- result = main_batch_without_progress_bar (DummyMyCli (), cli_args )
533+ cli_args = DummyCliArgs (batch = batch_path , checkpoint = checkpoint , resume = True )
534+ result = main_batch_without_progress_bar (DummyMyCli (), cli_args )
521535
522536 assert result == 0
523537 assert dispatch_calls == [('select 3;' , 2 )]
@@ -534,10 +548,10 @@ def test_main_batch_without_progress_bar_skips_only_matching_duplicate_prefix(mo
534548 )
535549 monkeypatch .setattr (batch_mode , 'sys' , make_fake_sys (stdin_tty = True ))
536550
537- with open_checkpoint_file (tmp_path , 'select 1;\n ' ) as checkpoint :
538- cli_args = DummyCliArgs (batch = batch_path , checkpoint = checkpoint , resume = True )
551+ checkpoint = write_checkpoint_file (tmp_path , 'select 1;\n ' )
539552
540- result = main_batch_without_progress_bar (DummyMyCli (), cli_args )
553+ cli_args = DummyCliArgs (batch = batch_path , checkpoint = checkpoint , resume = True )
554+ result = main_batch_without_progress_bar (DummyMyCli (), cli_args )
541555
542556 assert result == 0
543557 assert dispatch_calls == [('select 1;' , 1 ), ('select 2;' , 2 )]
@@ -554,10 +568,10 @@ def test_main_batch_without_progress_bar_fails_on_mismatched_checkpoint(monkeypa
554568 )
555569 monkeypatch .setattr (batch_mode , 'sys' , make_fake_sys (stdin_tty = True ))
556570
557- with open_checkpoint_file (tmp_path , 'select 9;\n ' ) as checkpoint :
558- cli_args = DummyCliArgs (batch = batch_path , checkpoint = checkpoint , resume = True )
571+ checkpoint = write_checkpoint_file (tmp_path , 'select 9;\n ' )
559572
560- result = main_batch_without_progress_bar (DummyMyCli (), cli_args )
573+ cli_args = DummyCliArgs (batch = batch_path , checkpoint = checkpoint , resume = True )
574+ result = main_batch_without_progress_bar (DummyMyCli (), cli_args )
561575
562576 assert result == 1
563577 assert dispatch_calls == []
@@ -574,10 +588,10 @@ def test_main_batch_without_progress_bar_succeeds_when_checkpoint_skips_all(monk
574588 )
575589 monkeypatch .setattr (batch_mode , 'sys' , make_fake_sys (stdin_tty = True ))
576590
577- with open_checkpoint_file (tmp_path , 'select 1;\n select 2;\n ' ) as checkpoint :
578- cli_args = DummyCliArgs (batch = batch_path , checkpoint = checkpoint , resume = True )
591+ checkpoint = write_checkpoint_file (tmp_path , 'select 1;\n select 2;\n ' )
579592
580- result = main_batch_without_progress_bar (DummyMyCli (), cli_args )
593+ cli_args = DummyCliArgs (batch = batch_path , checkpoint = checkpoint , resume = True )
594+ result = main_batch_without_progress_bar (DummyMyCli (), cli_args )
581595
582596 assert result == 0
583597 assert dispatch_calls == []
@@ -597,10 +611,10 @@ def test_main_batch_with_progress_bar_skips_checkpoint_prefix_and_counts_all_sta
597611 )
598612 monkeypatch .setattr (batch_mode , 'sys' , make_fake_sys (stdin_tty = True ))
599613
600- with open_checkpoint_file (tmp_path , 'select 1;\n ' ) as checkpoint :
601- cli_args = DummyCliArgs (batch = batch_path , checkpoint = checkpoint , resume = True )
614+ checkpoint = write_checkpoint_file (tmp_path , 'select 1;\n ' )
602615
603- result = main_batch_with_progress_bar (DummyMyCli (), cli_args )
616+ cli_args = DummyCliArgs (batch = batch_path , checkpoint = checkpoint , resume = True )
617+ result = main_batch_with_progress_bar (DummyMyCli (), cli_args )
604618
605619 assert result == 0
606620 assert dispatch_calls == [('select 2;' , 1 ), ('select 3;' , 2 )]
@@ -614,13 +628,13 @@ def test_main_batch_with_progress_bar_returns_error_when_checkpoint_replay_fails
614628 monkeypatch .setattr (batch_mode .click , 'secho' , lambda message , err , fg : messages .append ((message , err , fg )))
615629 monkeypatch .setattr (batch_mode , 'sys' , make_fake_sys (stdin_tty = True ))
616630
617- with open_checkpoint_file (tmp_path , 'select 9;\n ' ) as checkpoint :
618- cli_args = DummyCliArgs (batch = batch_path , checkpoint = checkpoint , resume = True )
631+ checkpoint = write_checkpoint_file (tmp_path , 'select 9;\n ' )
619632
620- result = main_batch_with_progress_bar (DummyMyCli (), cli_args )
633+ cli_args = DummyCliArgs (batch = batch_path , checkpoint = checkpoint , resume = True )
634+ result = main_batch_with_progress_bar (DummyMyCli (), cli_args )
621635
622636 assert result == 1
623- assert messages == [(f'Error replaying --checkpoint file: { checkpoint . name } : Statement mismatch: select 9;.' , True , 'red' )]
637+ assert messages == [(f'Error replaying --checkpoint file: { checkpoint } : Statement mismatch: select 9;.' , True , 'red' )]
624638
625639
626640def test_main_batch_without_progress_bar_returns_error_when_iteration_fails (monkeypatch ) -> None :
0 commit comments