diff options
author | Tiancong Wang <tcwang@google.com> | 2019-08-07 15:18:51 -0700 |
---|---|---|
committer | Tiancong Wang <tcwang@google.com> | 2019-08-09 01:10:17 +0000 |
commit | eb729be4771ebbbc520231e28efef32a5db7f8af (patch) | |
tree | 3a846dd34584fb9af9b703e9ab6fae66a749f393 /crosperf/crosperf_unittest.py | |
parent | e05c52149e50172f7011ad5c2194b6852f3e1e90 (diff) | |
download | toolchain-utils-eb729be4771ebbbc520231e28efef32a5db7f8af.tar.gz |
crosperf: Print traceback instead of exception type.
I found that the error message of crosperf is sometimes frustrating
that when you trigger an exception somewhere in the crosperf tool,
it will always return the exception type. Sometimes it's really hard
to find out where the exception/error happens in your code.
I think it might make sense to handle all exceptions with a
try...except block, to make the error message always appear at last,
so raising the original exception prints whole traceback, instead of
dropping it to general SystemExit.
BUG=None
TEST=Tested locally, and added unittest.
Change-Id: Id772af444bf3788011c74504c71939d2514f1752
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/1743146
Commit-Queue: Tiancong Wang <tcwang@google.com>
Tested-by: Tiancong Wang <tcwang@google.com>
Reviewed-by: George Burgess <gbiv@chromium.org>
Diffstat (limited to 'crosperf/crosperf_unittest.py')
-rwxr-xr-x | crosperf/crosperf_unittest.py | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/crosperf/crosperf_unittest.py b/crosperf/crosperf_unittest.py index c89c59f1..65a587a5 100755 --- a/crosperf/crosperf_unittest.py +++ b/crosperf/crosperf_unittest.py @@ -10,8 +10,10 @@ from __future__ import print_function import argparse +import mock +import tempfile +import os import StringIO - import unittest import crosperf @@ -44,7 +46,13 @@ class CrosperfTest(unittest.TestCase): input_file = StringIO.StringIO(EXPERIMENT_FILE_1) self.exp_file = experiment_file.ExperimentFile(input_file) - def test_convert_options_to_settings(self): + def testDryRun(self): + filehandle, filename = tempfile.mkstemp() + os.write(filehandle, EXPERIMENT_FILE_1) + crosperf.Main(['', filename, '--dry_run']) + os.remove(filename) + + def testConvertOptionsToSettings(self): parser = argparse.ArgumentParser() parser.add_argument( '-l', @@ -66,6 +74,18 @@ class CrosperfTest(unittest.TestCase): settings = crosperf.ConvertOptionsToSettings(options) self.assertFalse(settings.GetField('rerun')) + def testExceptionPrintTraceback(self): + """Test the main function can print traceback in exception.""" + + def mock_RunCrosperf(*_args, **_kwargs): + return 10 / 0 + + with mock.patch('crosperf.RunCrosperf', new=mock_RunCrosperf): + with self.assertRaises(ZeroDivisionError) as context: + crosperf.Main([]) + self.assertEqual('integer division or modulo by zero', + str(context.exception)) + if __name__ == '__main__': unittest.main() |