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