55import shutil
66import subprocess
77import sys
8+ from unittest import mock
89
910from inline_snapshot import snapshot
1011import pytest
@@ -27,7 +28,7 @@ def remove_git_data(local: GitDir) -> None:
2728
2829
2930@pytest .mark .parametrize (
30- "args, exitcode , logs, setup_repo" ,
31+ "args, exitcode, logs, setup_repo" ,
3132 [
3233 # install tests
3334 (
@@ -209,6 +210,40 @@ def test_main(
209210 assert normalize_message (out , git_dir = local_git_repo ) == logs
210211
211212
213+ @pytest .mark .parametrize (
214+ "args, logs, setup_repo" ,
215+ [
216+ (
217+ # update mirror file
218+ "sync" ,
219+ snapshot ("Updating all files [failed] KeyboardInterrupt" ),
220+ "syncer_tests/update_mirror" ,
221+ )
222+ ],
223+ )
224+ def test_main_with_keyboard_interrupt (
225+ args : str ,
226+ logs : str ,
227+ setup_repo : str | RelDir | None ,
228+ local_git_repo : GitDir ,
229+ test_data_path : AbsDir ,
230+ capsys : CaptureFixture ,
231+ ) -> None :
232+ if setup_repo :
233+ add_commit (local_git_repo , test_data_path / RelDir (setup_repo ))
234+ argv = ["-q" , * shlex .split (args )]
235+ mirror_existed_before = (local_git_repo / MIRROR_LOCK ).exists ()
236+ with (
237+ pytest .raises (SystemExit ) as e ,
238+ mock .patch .object (GitHelper , "wait" , mock .Mock (side_effect = [KeyboardInterrupt ])), # type: ignore [arg-type]
239+ ):
240+ main .main (argv , prog_name = MIRROR_NAME )
241+ assert e .value .code != 0
242+ assert (local_git_repo / MIRROR_LOCK ).exists () == mirror_existed_before
243+ out , _err = capsys .readouterr ()
244+ assert normalize_message (out , git_dir = local_git_repo ) == logs
245+
246+
212247@pytest .mark .slow
213248def test_pre_commit_with_mirror (
214249 local_git_repo : GitDir , test_data_path : AbsDir , capfd : CaptureFixture
0 commit comments