OLD | NEW |
| (Empty) |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import argparse | |
6 import unittest | |
7 | |
8 import mock | |
9 | |
10 from infra_libs import app | |
11 from infra_libs import logs | |
12 from infra_libs import ts_mon | |
13 | |
14 | |
15 class MockApplication(app.BaseApplication): | |
16 main = mock.Mock() | |
17 | |
18 | |
19 class AppTest(unittest.TestCase): | |
20 def setUp(self): | |
21 self.mock_logs_process_argparse_options = mock.patch( | |
22 'infra_libs.logs.process_argparse_options').start() | |
23 self.mock_ts_mon_process_argparse_options = mock.patch( | |
24 'infra_libs.ts_mon.process_argparse_options').start() | |
25 | |
26 def tearDown(self): | |
27 mock.patch.stopall() | |
28 | |
29 def test_initialises_standard_modules(self): | |
30 with self.assertRaises(SystemExit): | |
31 MockApplication().run(['appname']) | |
32 | |
33 self.mock_logs_process_argparse_options.assert_called_once() | |
34 self.mock_ts_mon_process_argparse_options.assert_called_once() | |
35 | |
36 def test_initialises_standard_modules_except_ts_mon(self): | |
37 class Application(MockApplication): | |
38 USES_TS_MON = False | |
39 | |
40 with self.assertRaises(SystemExit): | |
41 Application().run(['appname']) | |
42 | |
43 self.mock_logs_process_argparse_options.assert_called_once() | |
44 self.assertFalse(self.mock_ts_mon_process_argparse_options.called) | |
45 | |
46 def test_initialises_standard_modules_except_logs(self): | |
47 class Application(MockApplication): | |
48 USES_STANDARD_LOGGING = False | |
49 | |
50 with self.assertRaises(SystemExit): | |
51 Application().run(['appname']) | |
52 | |
53 self.assertFalse(self.mock_logs_process_argparse_options.called) | |
54 self.mock_ts_mon_process_argparse_options.assert_called_once() | |
55 | |
56 def test_argparse_options(self): | |
57 test_case = self | |
58 class App(MockApplication): | |
59 def add_argparse_options(self, p): | |
60 test_case.assertIsInstance(p, argparse.ArgumentParser) | |
61 | |
62 def process_argparse_options(self, o): | |
63 test_case.assertIsInstance(o, argparse.Namespace) | |
64 | |
65 a = App() | |
66 with self.assertRaises(SystemExit): | |
67 a.run(['appname']) | |
68 | |
69 def test_exit_status(self): | |
70 class App(app.BaseApplication): | |
71 def main(self, opts): | |
72 return 42 | |
73 | |
74 with self.assertRaises(SystemExit) as cm: | |
75 App().run(['appname']) | |
76 self.assertEqual(42, cm.exception.code) | |
77 | |
78 def test_catches_exceptions(self): | |
79 a = MockApplication() | |
80 a.main.side_effect = Exception | |
81 ts_mon.reset_for_unittest(disable=True) | |
82 | |
83 with self.assertRaises(SystemExit) as cm: | |
84 a.run(['appname']) | |
85 self.assertEqual(1, cm.exception.code) | |
86 | |
87 def test_catches_exceptions_with_ts_mon_disabled(self): | |
88 class Application(MockApplication): | |
89 USES_TS_MON = False | |
90 | |
91 a = Application() | |
92 a.main.side_effect = Exception | |
93 ts_mon.reset_for_unittest(disable=True) | |
94 | |
95 with self.assertRaises(SystemExit) as cm: | |
96 a.run(['appname']) | |
97 self.assertEqual(1, cm.exception.code) | |
OLD | NEW |