summaryrefslogtreecommitdiff
path: root/llvm/utils/rsp_bisect_test/test.py
blob: 2433d8653e3166b0e2fb7ef3414b30074372a224 (plain)
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
#!/usr/bin/env python3
# ===----------------------------------------------------------------------===##
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# ===----------------------------------------------------------------------===##

import os
import subprocess
import sys
import tempfile

cur_dir = os.path.dirname(os.path.realpath(__file__))
bisect_script = os.path.join(cur_dir, "..", "rsp_bisect.py")
test1 = os.path.join(cur_dir, "test_script.py")
test2 = os.path.join(cur_dir, "test_script_inv.py")
rsp = os.path.join(cur_dir, "rsp")


def run_bisect(success, test_script):
    args = [
        bisect_script,
        "--test",
        test_script,
        "--rsp",
        rsp,
        "--other-rel-path",
        "../Other",
    ]
    res = subprocess.run(args, capture_output=True, encoding="UTF-8")
    if len(sys.argv) > 1 and sys.argv[1] == "-v":
        print("Ran {} with return code {}".format(args, res.returncode))
        print("Stdout:")
        print(res.stdout)
        print("Stderr:")
        print(res.stderr)
    if res.returncode != (0 if success else 1):
        print(res.stdout)
        print(res.stderr)
        raise AssertionError("unexpected bisection return code for " + str(args))
    return res.stdout


# Test that an empty rsp file fails.
with open(rsp, "w") as f:
    pass

run_bisect(False, test1)

# Test that an rsp file without any paths fails.
with open(rsp, "w") as f:
    f.write("hello\nfoo\n")

run_bisect(False, test1)

# Test that an rsp file with one path succeeds.
with open(rsp, "w") as f:
    f.write("./foo\n")

output = run_bisect(True, test1)
assert "./foo" in output

# Test that an rsp file with one path and one extra arg succeeds.
with open(rsp, "w") as f:
    f.write("hello\n./foo\n")

output = run_bisect(True, test1)
assert "./foo" in output

# Test that an rsp file with three paths and one extra arg succeeds.
with open(rsp, "w") as f:
    f.write("hello\n./foo\n./bar\n./baz\n")

output = run_bisect(True, test1)
assert "./foo" in output

with open(rsp, "w") as f:
    f.write("hello\n./bar\n./foo\n./baz\n")

output = run_bisect(True, test1)
assert "./foo" in output

with open(rsp, "w") as f:
    f.write("hello\n./bar\n./baz\n./foo\n")

output = run_bisect(True, test1)
assert "./foo" in output

output = run_bisect(True, test2)
assert "./foo" in output

with open(rsp + ".0", "r") as f:
    contents = f.read()
    assert " ../Other/./foo" in contents

with open(rsp + ".1", "r") as f:
    contents = f.read()
    assert " ./foo" in contents

os.remove(rsp)
os.remove(rsp + ".0")
os.remove(rsp + ".1")

print("Success!")