Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(274)

Side by Side Diff: components/cronet/tools/api_static_checks_unittest.py

Issue 2440613003: [Cronet] Enforce implementation does not call through API classes (Closed)
Patch Set: add exception for Exception.getMessage() call Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/cronet/tools/api_static_checks.py ('k') | components/cronet/tools_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
2 # Copyright 2016 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """api_static_checks_unittest.py - Unittests for api_static_checks.py"""
7
8
9 import contextlib
10 from cStringIO import StringIO
11 import os
12 import shutil
13 import sys
14 import tempfile
15 import unittest
16
17 from tools import api_static_checks
18
19
20 ERROR_PREFIX = (
21 """ERROR: Found the following calls from implementation classes through
22 API classes. These could fail if older API is used that
23 does not contain newer methods. Please call through a
24 wrapper class from VersionSafeCallbacks.
25 """)
26
27
28 @contextlib.contextmanager
29 def capture_output():
30 # A contextmanger that collects the stdout and stderr of wrapped code
31
32 oldout,olderr = sys.stdout, sys.stderr
33 try:
34 out=[StringIO(), StringIO()]
35 sys.stdout,sys.stderr = out
36 yield out
37 finally:
38 sys.stdout,sys.stderr = oldout, olderr
39 out[0] = out[0].getvalue()
40 out[1] = out[1].getvalue()
41
42
43 class ApiStaticCheckUnitTest(unittest.TestCase):
44 def setUp(self):
45 self.temp_dir = tempfile.mkdtemp()
46 os.chdir(self.temp_dir)
47
48
49 def tearDown(self):
50 shutil.rmtree(self.temp_dir)
51
52
53 def make_jar(self, java, class_name):
54 # Compile |java| wrapped in a class named |class_name| to a jar file and
55 # return jar filename.
56
57 java_filename = class_name + '.java'
58 class_filename = class_name + '.class'
59 jar_filename = class_name + '.jar'
60
61 with open(java_filename, 'w') as java_file:
62 java_file.write('public class %s {' % class_name)
63 java_file.write(java)
64 java_file.write('}')
65 os.system('javac %s' % java_filename)
66 os.system('jar cf %s %s' % (jar_filename, class_filename))
67 return jar_filename
68
69
70 def run_test(self, api_java, impl_java):
71 api_jar = self.make_jar(api_java, 'Api')
72 impl_jar = self.make_jar(impl_java, 'Impl')
73 with capture_output() as return_output:
74 return_code = api_static_checks.main(
75 ['--api_jar', api_jar, '--impl_jar', impl_jar])
76 return [return_code, return_output[0]]
77
78
79 def test_success(self):
80 # Test simple classes with functions
81 self.assertEqual(self.run_test('void a(){}', 'void b(){}'), [True, ''])
82 # Test simple classes with functions calling themselves
83 self.assertEqual(self.run_test(
84 'void a(){} void b(){a();}', 'void c(){} void d(){c();}'), [True, ''])
85
86
87 def test_failure(self):
88 # Test static call
89 self.assertEqual(self.run_test(
90 'public static void a(){}', 'void b(){Api.a();}'),
91 [False, ERROR_PREFIX + 'Impl/b -> Api/a:()V\n'])
92 # Test virtual call
93 self.assertEqual(self.run_test(
94 'public void a(){}', 'void b(){new Api().a();}'),
95 [False, ERROR_PREFIX + 'Impl/b -> Api/a:()V\n'])
OLDNEW
« no previous file with comments | « components/cronet/tools/api_static_checks.py ('k') | components/cronet/tools_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698