1*d415bd75Srobert#!/usr/bin/env python3 2*d415bd75Srobert#===----------------------------------------------------------------------===## 3*d415bd75Srobert# 4*d415bd75Srobert# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5*d415bd75Srobert# See https://llvm.org/LICENSE.txt for license information. 6*d415bd75Srobert# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7*d415bd75Srobert# 8*d415bd75Srobert#===----------------------------------------------------------------------===## 9*d415bd75Srobert 10*d415bd75Srobertimport os 11*d415bd75Srobertimport subprocess 12*d415bd75Srobertimport sys 13*d415bd75Srobertimport tempfile 14*d415bd75Srobert 15*d415bd75Srobertcur_dir = os.path.dirname(os.path.realpath(__file__)) 16*d415bd75Srobertbisect_script = os.path.join(cur_dir, "..", "rsp_bisect.py") 17*d415bd75Sroberttest1 = os.path.join(cur_dir, "test_script.py") 18*d415bd75Sroberttest2 = os.path.join(cur_dir, "test_script_inv.py") 19*d415bd75Srobertrsp = os.path.join(cur_dir, "rsp") 20*d415bd75Srobert 21*d415bd75Srobert 22*d415bd75Srobertdef run_bisect(success, test_script): 23*d415bd75Srobert args = [ 24*d415bd75Srobert bisect_script, '--test', test_script, '--rsp', rsp, '--other-rel-path', 25*d415bd75Srobert '../Other' 26*d415bd75Srobert ] 27*d415bd75Srobert res = subprocess.run(args, capture_output=True, encoding='UTF-8') 28*d415bd75Srobert if len(sys.argv) > 1 and sys.argv[1] == '-v': 29*d415bd75Srobert print('Ran {} with return code {}'.format(args, res.returncode)) 30*d415bd75Srobert print('Stdout:') 31*d415bd75Srobert print(res.stdout) 32*d415bd75Srobert print('Stderr:') 33*d415bd75Srobert print(res.stderr) 34*d415bd75Srobert if res.returncode != (0 if success else 1): 35*d415bd75Srobert print(res.stdout) 36*d415bd75Srobert print(res.stderr) 37*d415bd75Srobert raise AssertionError('unexpected bisection return code for ' + str(args)) 38*d415bd75Srobert return res.stdout 39*d415bd75Srobert 40*d415bd75Srobert 41*d415bd75Srobert# Test that an empty rsp file fails. 42*d415bd75Srobertwith open(rsp, 'w') as f: 43*d415bd75Srobert pass 44*d415bd75Srobert 45*d415bd75Srobertrun_bisect(False, test1) 46*d415bd75Srobert 47*d415bd75Srobert# Test that an rsp file without any paths fails. 48*d415bd75Srobertwith open(rsp, 'w') as f: 49*d415bd75Srobert f.write('hello\nfoo\n') 50*d415bd75Srobert 51*d415bd75Srobertrun_bisect(False, test1) 52*d415bd75Srobert 53*d415bd75Srobert# Test that an rsp file with one path succeeds. 54*d415bd75Srobertwith open(rsp, 'w') as f: 55*d415bd75Srobert f.write('./foo\n') 56*d415bd75Srobert 57*d415bd75Srobertoutput = run_bisect(True, test1) 58*d415bd75Srobertassert './foo' in output 59*d415bd75Srobert 60*d415bd75Srobert# Test that an rsp file with one path and one extra arg succeeds. 61*d415bd75Srobertwith open(rsp, 'w') as f: 62*d415bd75Srobert f.write('hello\n./foo\n') 63*d415bd75Srobert 64*d415bd75Srobertoutput = run_bisect(True, test1) 65*d415bd75Srobertassert './foo' in output 66*d415bd75Srobert 67*d415bd75Srobert# Test that an rsp file with three paths and one extra arg succeeds. 68*d415bd75Srobertwith open(rsp, 'w') as f: 69*d415bd75Srobert f.write('hello\n./foo\n./bar\n./baz\n') 70*d415bd75Srobert 71*d415bd75Srobertoutput = run_bisect(True, test1) 72*d415bd75Srobertassert './foo' in output 73*d415bd75Srobert 74*d415bd75Srobertwith open(rsp, 'w') as f: 75*d415bd75Srobert f.write('hello\n./bar\n./foo\n./baz\n') 76*d415bd75Srobert 77*d415bd75Srobertoutput = run_bisect(True, test1) 78*d415bd75Srobertassert './foo' in output 79*d415bd75Srobert 80*d415bd75Srobertwith open(rsp, 'w') as f: 81*d415bd75Srobert f.write('hello\n./bar\n./baz\n./foo\n') 82*d415bd75Srobert 83*d415bd75Srobertoutput = run_bisect(True, test1) 84*d415bd75Srobertassert './foo' in output 85*d415bd75Srobert 86*d415bd75Srobertoutput = run_bisect(True, test2) 87*d415bd75Srobertassert './foo' in output 88*d415bd75Srobert 89*d415bd75Srobertwith open(rsp + '.0', 'r') as f: 90*d415bd75Srobert contents = f.read() 91*d415bd75Srobert assert ' ../Other/./foo' in contents 92*d415bd75Srobert 93*d415bd75Srobertwith open(rsp + '.1', 'r') as f: 94*d415bd75Srobert contents = f.read() 95*d415bd75Srobert assert ' ./foo' in contents 96*d415bd75Srobert 97*d415bd75Srobertos.remove(rsp) 98*d415bd75Srobertos.remove(rsp + '.0') 99*d415bd75Srobertos.remove(rsp + '.1') 100*d415bd75Srobert 101*d415bd75Srobertprint('Success!') 102