blob: d7ab9aac10bfc8d0ec8488f3dc3ef85313574100 [file] [log] [blame]
Tamas Kenez46367092018-12-12 10:34:16 +01001#!/usr/bin/env python
2# Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
3# for details. All rights reserved. Use of this source code is governed by a
4# BSD-style license that can be found in the LICENSE file.
5
Tamas Kenez4d22f982018-12-13 12:09:45 +01006from __future__ import absolute_import
7from __future__ import division
8from __future__ import print_function
Ian Zerny3150aec2019-02-18 13:50:31 +01009
Tamas Kenez46367092018-12-12 10:34:16 +010010import gradle
Ian Zerny3f54e222019-02-12 10:51:17 +010011import jdk
Tamas Kenez46367092018-12-12 10:34:16 +010012import os
13import subprocess
14import sys
Ian Zerny3150aec2019-02-18 13:50:31 +010015
Tamas Kenez46367092018-12-12 10:34:16 +010016import utils
17
Tamas Kenez46367092018-12-12 10:34:16 +010018EXCEPTION_LINE = 'Intentional exception for testing retrace.'
19EXPECTED_LINES = [
20 'com.android.tools.r8.utils.SelfRetraceTest.foo3(SelfRetraceTest.java:13)',
21 'com.android.tools.r8.utils.SelfRetraceTest.foo2(SelfRetraceTest.java:17)',
22 'com.android.tools.r8.utils.SelfRetraceTest.foo1(SelfRetraceTest.java:21)',
23 'com.android.tools.r8.utils.SelfRetraceTest.test(SelfRetraceTest.java:26)',
24 'com.android.tools.r8.R8.run(R8.java:',
25]
26
27def main():
Tamas Kenez4d22f982018-12-13 12:09:45 +010028 args = sys.argv[1:]
29 if len(args) == 0:
30 gradle.RunGradle(['r8lib'])
31 r8lib = utils.R8LIB_JAR
Ian Zerny3150aec2019-02-18 13:50:31 +010032 r8map = utils.R8LIB + '.map'
33 elif len(args) == 2:
34 r8lib = args[0]
35 r8map = args[1]
36 elif len(args) == 1 and args[0] == '--help':
37 print('Usage: test_self_retrace.py [<path-to-r8lib-jar> <path-to-r8lib-map]')
38 print('If the path is missing the script builds and uses ' + utils.R8LIB_JAR)
39 return
Tamas Kenez4d22f982018-12-13 12:09:45 +010040 else:
Ian Zerny3150aec2019-02-18 13:50:31 +010041 raise Exception("Only two argument allowed, see '--help'.")
Tamas Kenez46367092018-12-12 10:34:16 +010042
43 # Run 'r8 --help' which throws an exception.
Ian Zerny3f54e222019-02-12 10:51:17 +010044 cmd = [
45 jdk.GetJavaExecutable(),'-cp', r8lib, 'com.android.tools.r8.R8', '--help'
46 ]
Tamas Kenez46367092018-12-12 10:34:16 +010047 os.environ["R8_THROW_EXCEPTION_FOR_TESTING_RETRACE"] = "1"
48 utils.PrintCmd(cmd)
49 p = subprocess.Popen(cmd, stderr=subprocess.PIPE)
50 _, stacktrace = p.communicate()
51 assert(p.returncode != 0)
52 assert(EXCEPTION_LINE in stacktrace)
53 # r8lib must be minified, original class names must not be present.
54 assert('SelfRetraceTest' not in stacktrace)
55
56 # Run the retrace tool.
Ian Zerny3150aec2019-02-18 13:50:31 +010057 cmd = [jdk.GetJavaExecutable(), '-jar', utils.RETRACE_JAR, r8map]
Tamas Kenez46367092018-12-12 10:34:16 +010058 utils.PrintCmd(cmd)
59 p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
60 retrace_stdout, _ = p.communicate(stacktrace)
61 assert p.returncode == 0
62 retrace_lines = retrace_stdout.splitlines()
63 line_index = -1
64 for line in retrace_lines:
65 if line_index < 0:
66 if 'java.lang.RuntimeException' in line:
67 assert(EXCEPTION_LINE in line)
68 line_index = 0;
69 else:
70 assert EXPECTED_LINES[line_index] in line
71 line_index += 1
72 if line_index >= len(EXPECTED_LINES):
73 break
74 assert(line_index >= 0)
75
76if __name__ == '__main__':
77 sys.exit(main())