mirror of
https://chromium.googlesource.com/chromium/tools/depot_tools.git
synced 2026-01-11 18:51:29 +00:00
Use exit code 130 for keyboard interrupts
Interrupting is reported with exit_code 1 in metrics and makes it harder to determine if a user canceled git cl. Change-Id: I99f35378dcf02cffa39a05317651fcd2c6a0b520 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4684512 Reviewed-by: Josip Sokcevic <sokcevic@chromium.org> Commit-Queue: Gavin Mak <gavinmak@google.com>
This commit is contained in:
@@ -219,8 +219,11 @@ def return_code_from_exception(exception):
|
||||
"""Returns the exit code that would result of raising the exception."""
|
||||
if exception is None:
|
||||
return 0
|
||||
if isinstance(exception[1], SystemExit):
|
||||
return exception[1].code
|
||||
e = exception[1]
|
||||
if isinstance(e, KeyboardInterrupt):
|
||||
return 130
|
||||
if isinstance(e, SystemExit):
|
||||
return e.code
|
||||
return 1
|
||||
|
||||
|
||||
|
||||
@@ -302,6 +302,22 @@ class MetricsCollectorTest(unittest.TestCase):
|
||||
self.assertEqual(cm.exception.code, 0)
|
||||
self.assert_collects_metrics({'exit_code': 0})
|
||||
|
||||
def test_handles_keyboard_interrupt(self):
|
||||
"""Tests that KeyboardInterrupt exits with 130 and metrics are collected."""
|
||||
self.FileRead.side_effect = [
|
||||
'{"is-googler": true, "countdown": 0, "opt-in": true, "version": 0}'
|
||||
]
|
||||
|
||||
@self.collector.collect_metrics('fun')
|
||||
def fun():
|
||||
raise KeyboardInterrupt
|
||||
|
||||
# When an exception is raised, we should catch it, update exit-code,
|
||||
# collect metrics, and re-raise it.
|
||||
with self.assertRaises(KeyboardInterrupt):
|
||||
fun()
|
||||
self.assert_collects_metrics({'exit_code': 130})
|
||||
|
||||
def test_handles_system_exit_non_zero(self):
|
||||
"""Tests that the sys.exit code is respected and metrics are collected."""
|
||||
self.FileRead.side_effect = [
|
||||
|
||||
Reference in New Issue
Block a user