xref: /openbsd-src/gnu/llvm/llvm/utils/rsp_bisect_test/test.py (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
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, '--test', test_script, '--rsp', rsp, '--other-rel-path',
25      '../Other'
26  ]
27  res = subprocess.run(args, capture_output=True, encoding='UTF-8')
28  if len(sys.argv) > 1 and sys.argv[1] == '-v':
29    print('Ran {} with return code {}'.format(args, res.returncode))
30    print('Stdout:')
31    print(res.stdout)
32    print('Stderr:')
33    print(res.stderr)
34  if res.returncode != (0 if success else 1):
35    print(res.stdout)
36    print(res.stderr)
37    raise AssertionError('unexpected bisection return code for ' + str(args))
38  return res.stdout
39
40
41# Test that an empty rsp file fails.
42with open(rsp, 'w') as f:
43  pass
44
45run_bisect(False, test1)
46
47# Test that an rsp file without any paths fails.
48with open(rsp, 'w') as f:
49  f.write('hello\nfoo\n')
50
51run_bisect(False, test1)
52
53# Test that an rsp file with one path succeeds.
54with open(rsp, 'w') as f:
55  f.write('./foo\n')
56
57output = run_bisect(True, test1)
58assert './foo' in output
59
60# Test that an rsp file with one path and one extra arg succeeds.
61with open(rsp, 'w') as f:
62  f.write('hello\n./foo\n')
63
64output = run_bisect(True, test1)
65assert './foo' in output
66
67# Test that an rsp file with three paths and one extra arg succeeds.
68with open(rsp, 'w') as f:
69  f.write('hello\n./foo\n./bar\n./baz\n')
70
71output = run_bisect(True, test1)
72assert './foo' in output
73
74with open(rsp, 'w') as f:
75  f.write('hello\n./bar\n./foo\n./baz\n')
76
77output = run_bisect(True, test1)
78assert './foo' in output
79
80with open(rsp, 'w') as f:
81  f.write('hello\n./bar\n./baz\n./foo\n')
82
83output = run_bisect(True, test1)
84assert './foo' in output
85
86output = run_bisect(True, test2)
87assert './foo' in output
88
89with open(rsp + '.0', 'r') as f:
90  contents = f.read()
91  assert ' ../Other/./foo' in contents
92
93with open(rsp + '.1', 'r') as f:
94  contents = f.read()
95  assert ' ./foo' in contents
96
97os.remove(rsp)
98os.remove(rsp + '.0')
99os.remove(rsp + '.1')
100
101print('Success!')
102