1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
# ==--- opt_bug_reducer_test.py ------------------------------------------===#
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
#
# ==----------------------------------------------------------------------===#
import os
import platform
import re
import shutil
import subprocess
import unittest
import bug_reducer.swift_tools as swift_tools
@unittest.skipUnless(platform.system() == 'Darwin',
'func_bug_reducer is only available on Darwin for now')
class FuncBugReducerTestCase(unittest.TestCase):
verbose = False
def setUp(self):
self.file_dir = os.path.dirname(os.path.abspath(__file__))
self.reducer = os.path.join(os.path.dirname(self.file_dir),
'bug_reducer', 'bug_reducer.py')
self.build_dir = os.path.abspath(
os.environ['BUGREDUCE_TEST_SWIFT_OBJ_ROOT'])
(root, _) = os.path.splitext(os.path.abspath(__file__))
self.root_basename = ''.join(os.path.basename(root).split('_'))
self.tmp_dir = os.path.join(
os.path.abspath(os.environ['BUGREDUCE_TEST_TMP_DIR']),
self.root_basename)
subprocess.call(['mkdir', '-p', self.tmp_dir])
self.module_cache = os.path.join(self.tmp_dir, 'module_cache')
self.sdk = subprocess.check_output(['xcrun', '--sdk', 'macosx',
'--toolchain', 'Default',
'--show-sdk-path']).strip("\n")
self.tools = swift_tools.SwiftTools(self.build_dir)
self.passes = ['--pass=-bug-reducer-tester']
if os.access(self.tmp_dir, os.F_OK):
shutil.rmtree(self.tmp_dir)
os.makedirs(self.tmp_dir)
os.makedirs(self.module_cache)
def _get_test_file_path(self, module_name):
return os.path.join(self.file_dir,
'{}_{}.swift'.format(
self.root_basename, module_name))
def _get_sib_file_path(self, filename):
(root, ext) = os.path.splitext(filename)
return os.path.join(self.tmp_dir, os.path.basename(root) + '.sib')
def run_swiftc_command(self, name):
input_file_path = self._get_test_file_path(name)
sib_path = self._get_sib_file_path(input_file_path)
args = [self.tools.swiftc,
'-module-cache-path', self.module_cache,
'-sdk', self.sdk,
'-Onone', '-parse-as-library',
'-module-name', name,
'-emit-sib',
'-resource-dir', os.path.join(self.build_dir, 'lib', 'swift'),
'-o', sib_path,
input_file_path]
return self.run_check_call(args)
def run_check_call(self, args):
if FuncBugReducerTestCase.verbose:
print('Cmd: {}'.format(' '.join(args)))
result_code = subprocess.check_call(args)
if FuncBugReducerTestCase.verbose:
print('Result Code: {}'.format(result_code))
return result_code
def run_check_output(self, args):
if FuncBugReducerTestCase.verbose:
print('Cmd: {}'.format(' '.join(args)))
raw_output = subprocess.check_output(args)
if FuncBugReducerTestCase.verbose:
print('Raw Output: {}'.format(raw_output))
return raw_output
def test_basic(self):
name = 'testbasic'
result_code = self.run_swiftc_command(name)
assert result_code == 0, "Failed initial compilation"
args = [
self.reducer,
'func',
self.build_dir,
self._get_sib_file_path(self._get_test_file_path(name)),
'--sdk=%s' % self.sdk,
'--module-cache=%s' % self.module_cache,
'--module-name=%s' % name,
'--work-dir=%s' % self.tmp_dir,
('--extra-silopt-arg='
'-bug-reducer-tester-target-func=__TF_test_target'),
'--extra-silopt-arg=-bug-reducer-tester-failure-kind=opt-crasher'
]
args.extend(self.passes)
output = self.run_check_output(args).split("\n")
self.assertTrue("*** Successfully Reduced file!" in output)
self.assertTrue("*** Final Functions: " +
"$s9testbasic6foo413yyF" in output)
re_end = 'testfuncbugreducer_testbasic_'
re_end += '92196894259b5d6c98d1b77f19240904.sib'
output_file_re = re.compile(r'\*\*\* Final File: .*' + re_end)
output_matches = [
1 for o in output if output_file_re.match(o) is not None]
self.assertEqual(sum(output_matches), 1)
# Make sure our final output command does not have -emit-sib in
# the output. We want users to get sil output when they type in
# the relevant command.
self.assertEqual([], [o for o in output if '-emit-sib' in o])
if __name__ == '__main__':
unittest.main()
|