Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / typ / typ / tests / runner_test.py
1 # Copyright 2014 Dirk Pranke. All rights reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import tempfile
16
17 from textwrap import dedent as d
18
19
20 from typ import Host, Runner, TestCase, TestSet, TestInput
21 from typ import WinMultiprocessing
22
23
24 def _setup_process(child, context):  # pylint: disable=W0613
25     return context
26
27
28 def _teardown_process(child, context):  # pylint: disable=W0613
29     return context
30
31
32 class RunnerTests(TestCase):
33     def test_context(self):
34         r = Runner()
35         r.args.tests = ['typ.tests.runner_test.ContextTests']
36         r.context = {'foo': 'bar'}
37         r.setup_fn = _setup_process
38         r.teardown_fn = _teardown_process
39         r.win_multiprocessing = WinMultiprocessing.importable
40         ret, _, _ = r.run()
41         self.assertEqual(ret, 0)
42
43     def test_bad_default(self):
44         r = Runner()
45         ret = r.main([], foo='bar')
46         self.assertEqual(ret, 2)
47
48     def test_good_default(self):
49         r = Runner()
50         ret = r.main([], tests=['typ.tests.runner_test.ContextTests'])
51         self.assertEqual(ret, 0)
52
53
54 class TestSetTests(TestCase):
55     # This class exists to test the failures that can come up if you
56     # create your own test sets and bypass find_tests(); failures that
57     # would normally be caught there can occur later during test execution.
58
59     def test_missing_name(self):
60         test_set = TestSet()
61         test_set.parallel_tests = [TestInput('nonexistent test')]
62         r = Runner()
63         r.args.jobs = 1
64         ret, _, _ = r.run(test_set)
65         self.assertEqual(ret, 1)
66
67     def test_failing_load_test(self):
68         h = Host()
69         orig_wd = h.getcwd()
70         tmpdir = None
71         try:
72             tmpdir = h.mkdtemp()
73             h.chdir(tmpdir)
74             h.write_text_file('load_test.py', d("""\
75                 import unittest
76                 def load_tests(_, _2, _3):
77                     assert False
78                 """))
79             test_set = TestSet()
80             test_set.parallel_tests = [TestInput('load_test.BaseTest.test_x')]
81             r = Runner()
82             r.args.jobs = 1
83             ret, _, _ = r.run(test_set)
84             self.assertEqual(ret, 1)
85         finally:
86             h.chdir(orig_wd)
87             if tmpdir:
88                 h.rmtree(tmpdir)
89
90
91 class TestWinMultiprocessing(TestCase):
92     def make_host(self):
93         return Host()
94
95     def call(self, argv, platform=None, win_multiprocessing=None, **kwargs):
96         h = self.make_host()
97         orig_wd = h.getcwd()
98         tmpdir = None
99         try:
100             tmpdir = h.mkdtemp()
101             h.chdir(tmpdir)
102             h.capture_output()
103             if platform is not None:
104                 h.platform = platform
105             r = Runner(h)
106             if win_multiprocessing is not None:
107                 r.win_multiprocessing = win_multiprocessing
108             ret = r.main(argv, **kwargs)
109         finally:
110             out, err = h.restore_output()
111             h.chdir(orig_wd)
112             if tmpdir:
113                 h.rmtree(tmpdir)
114
115         return ret, out, err
116
117     def test_bad_value(self):
118         self.assertRaises(ValueError, self.call, [], win_multiprocessing='foo')
119
120     def test_ignore(self):
121         h = self.make_host()
122         if h.platform == 'win32':  # pragma: win32
123             self.assertRaises(ValueError, self.call, [],
124                               win_multiprocessing=WinMultiprocessing.ignore)
125         else:
126             result = self.call([],
127                                win_multiprocessing=WinMultiprocessing.ignore)
128             ret, out, err = result
129             self.assertEqual(ret, 1)
130             self.assertEqual(out, 'No tests to run.\n')
131             self.assertEqual(err, '')
132
133     def test_real_unimportable_main(self):
134         h = self.make_host()
135         tmpdir = None
136         orig_wd = h.getcwd()
137         out = err = None
138         out_str = err_str = ''
139         try:
140             tmpdir = h.mkdtemp()
141             h.chdir(tmpdir)
142             out = tempfile.NamedTemporaryFile(delete=False)
143             err = tempfile.NamedTemporaryFile(delete=False)
144             path_above_typ = h.realpath(h.dirname(__file__), '..', '..')
145             env = h.env.copy()
146             if 'PYTHONPATH' in env:  # pragma: untested
147                 env['PYTHONPATH'] = '%s%s%s' % (env['PYTHONPATH'],
148                                                 h.pathsep,
149                                                 path_above_typ)
150             else:  # pragma: untested.
151                 env['PYTHONPATH'] = path_above_typ
152
153             h.write_text_file('test', d("""
154                 import sys
155                 import typ
156                 importable = typ.WinMultiprocessing.importable
157                 sys.exit(typ.main(win_multiprocessing=importable))
158                 """))
159             h.stdout = out
160             h.stderr = err
161             ret = h.call_inline([h.python_interpreter, h.join(tmpdir, 'test')],
162                                 env=env)
163         finally:
164             h.chdir(orig_wd)
165             if tmpdir:
166                 h.rmtree(tmpdir)
167             if out:
168                 out.close()
169                 out = open(out.name)
170                 out_str = out.read()
171                 out.close()
172                 h.remove(out.name)
173             if err:
174                 err.close()
175                 err = open(err.name)
176                 err_str = err.read()
177                 err.close()
178                 h.remove(err.name)
179
180         self.assertEqual(ret, 1)
181         self.assertEqual(out_str, '')
182         self.assertIn('ValueError: The __main__ module ',
183                       err_str)
184
185     def test_single_job(self):
186         ret, out, err = self.call(['-j', '1'], platform='win32')
187         self.assertEqual(ret, 1)
188         self.assertIn('No tests to run.', out)
189         self.assertEqual(err, '')
190
191     def test_spawn(self):
192         ret, out, err = self.call([])
193         self.assertEqual(ret, 1)
194         self.assertIn('No tests to run.', out)
195         self.assertEqual(err, '')
196
197
198 class ContextTests(TestCase):
199     def test_context(self):
200         # This test is mostly intended to be called by
201         # RunnerTests.test_context, above. It is not interesting on its own.
202         if self.context:
203             self.assertEquals(self.context['foo'], 'bar')