| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Copyright © 2024 Valve Corporation |
| 4 |
# SPDX-License-Identifier: MIT |
| 5 |
|
| 6 |
import argparse |
| 7 |
import collections |
| 8 |
import subprocess |
| 9 |
import os |
| 10 |
import re |
| 11 |
import sys |
| 12 |
import tempfile |
| 13 |
import textwrap |
| 14 |
from pathlib import Path |
| 15 |
|
| 16 |
def trim_blank_lines(string, trailing): |
| 17 |
lines = string.split('\n') |
| 18 |
string = '' |
| 19 |
empty_line = True |
| 20 |
for i in range(len(lines)): |
| 21 |
line_index = len(lines) - 1 - i if trailing else i |
| 22 |
if empty_line and lines[line_index].strip() == '': |
| 23 |
continue |
| 24 |
|
| 25 |
write_newline = not empty_line if trailing else line_index < len(lines) - 1 |
| 26 |
newline = '\n' if write_newline else '' |
| 27 |
if trailing: |
| 28 |
string = lines[line_index] + newline + string |
| 29 |
else: |
| 30 |
string += lines[line_index] + newline |
| 31 |
|
| 32 |
empty_line = False |
| 33 |
|
| 34 |
return string |
| 35 |
|
| 36 |
class TestFileChange: |
| 37 |
def __init__(self, expected, result): |
| 38 |
self.expected = expected |
| 39 |
|
| 40 |
# Apply the indentation of the expectation to the result |
| 41 |
indentation = 1000 |
| 42 |
for expected_line in expected.split('\n'): |
| 43 |
if match := re.match(r'^(\s*)\S', expected_line): |
| 44 |
line_indentation = len(match.group(1)) |
| 45 |
if indentation > line_indentation: |
| 46 |
indentation = line_indentation |
| 47 |
|
| 48 |
self.result = '' |
| 49 |
result = result.split('\n') |
| 50 |
for i in range(len(result)): |
| 51 |
result_line = result[i] |
| 52 |
indentation_str = '' if result_line.strip() == '' else ' '*indentation |
| 53 |
self.result += indentation_str + result_line + ('\n' if i < len(result) - 1 else '') |
| 54 |
|
| 55 |
if __name__ == '__main__': |
| 56 |
parser = argparse.ArgumentParser() |
| 57 |
parser.add_argument('--build-dir', '-B', required=False) |
| 58 |
parser.add_argument('--test-filter', '-f', required=False) |
| 59 |
parser.add_argument('--update-all', '-u', action='store_true') |
| 60 |
args = parser.parse_args() |
| 61 |
|
| 62 |
bin_path = 'src/compiler/nir/nir_tests' |
| 63 |
if args.build_dir: |
| 64 |
bin_path = args.build_dir + '/' + bin_path |
| 65 |
|
| 66 |
if not os.path.isfile(bin_path): |
| 67 |
print(f'{bin_path} \033[91m does not exist!\033[0m') |
| 68 |
exit(1) |
| 69 |
|
| 70 |
build_args = ['meson', 'compile'] |
| 71 |
if args.build_dir: |
| 72 |
build_args.append(f'-C{args.build_dir}') |
| 73 |
subprocess.run(build_args) |
| 74 |
|
| 75 |
test_args = [bin_path] |
| 76 |
if args.test_filter: |
| 77 |
test_args.append(f'--gtest_filter={args.test_filter}') |
| 78 |
|
| 79 |
env = os.environ.copy() |
| 80 |
if args.update_all: |
| 81 |
env['NIR_TEST_DUMP_SHADERS'] = 'true' |
| 82 |
|
| 83 |
output = subprocess.run(test_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, env=env) |
| 84 |
|
| 85 |
expected_pattern = re.compile(r'BEGIN EXPECTED\(([\d\w\W/.-_]+)\)') |
| 86 |
|
| 87 |
expectations = collections.defaultdict(list) |
| 88 |
|
| 89 |
current_file = None |
| 90 |
current_result = None |
| 91 |
current_expected = None |
| 92 |
inside_result = False |
| 93 |
inside_expected = False |
| 94 |
|
| 95 |
# Parse the output of the test binary and gather the changed shaders. |
| 96 |
for output_line in output.stdout.split('\n'): |
| 97 |
if output_line.startswith('BEGIN RESULT'): |
| 98 |
inside_result = True |
| 99 |
current_result = '' |
| 100 |
continue |
| 101 |
|
| 102 |
if output_line.startswith('BEGIN EXPECTED'): |
| 103 |
match = expected_pattern.match(output_line) |
| 104 |
current_file = match.group(1).removeprefix('../') |
| 105 |
inside_expected = True |
| 106 |
current_expected = '' |
| 107 |
continue |
| 108 |
|
| 109 |
if output_line.startswith('END'): |
| 110 |
if current_result is not None and current_expected is not None: |
| 111 |
# remove trailing and leading blank lines |
| 112 |
current_result = trim_blank_lines(current_result, True) |
| 113 |
current_result = trim_blank_lines(current_result, False) |
| 114 |
current_expected = trim_blank_lines(current_expected, True) |
| 115 |
current_expected = trim_blank_lines(current_expected, False) |
| 116 |
|
| 117 |
expectations[current_file].append(TestFileChange(current_expected, current_result)) |
| 118 |
|
| 119 |
current_result = None |
| 120 |
current_expected = None |
| 121 |
|
| 122 |
inside_result = False |
| 123 |
inside_expected = False |
| 124 |
continue |
| 125 |
|
| 126 |
if inside_result: |
| 127 |
current_result += output_line + '\n' |
| 128 |
|
| 129 |
if inside_expected: |
| 130 |
current_expected += output_line + '\n' |
| 131 |
|
| 132 |
patches = [] |
| 133 |
|
| 134 |
# Generate patches for the changed shaders. |
| 135 |
for file in expectations: |
| 136 |
changes = expectations[file] |
| 137 |
|
| 138 |
updated_test_file = '' |
| 139 |
|
| 140 |
with open(file) as test_file: |
| 141 |
updated_test_file = str(test_file.read()) |
| 142 |
|
| 143 |
for change in changes: |
| 144 |
updated_test_file = updated_test_file.replace(change.expected, change.result) |
| 145 |
|
| 146 |
# change.expected == change.result can be the case when using NIR_TEST_DUMP_SHADERS. |
| 147 |
if change.expected in updated_test_file and change.expected != change.result: |
| 148 |
print(f'Duplicate test case in {file}!') |
| 149 |
exit(1) |
| 150 |
|
| 151 |
with tempfile.NamedTemporaryFile(delete_on_close=False) as tmp: |
| 152 |
tmp.write(bytes(updated_test_file, encoding="utf-8")) |
| 153 |
tmp.close() |
| 154 |
|
| 155 |
diff = subprocess.run( |
| 156 |
['git', 'diff', '--no-index', file, tmp.name], |
| 157 |
stdout=subprocess.PIPE, |
| 158 |
stderr=subprocess.STDOUT, |
| 159 |
universal_newlines=True, |
| 160 |
) |
| 161 |
patch = diff.stdout.replace(tmp.name, '/' + file) |
| 162 |
|
| 163 |
print(patch) |
| 164 |
|
| 165 |
patches.append(patch) |
| 166 |
|
| 167 |
if len(patches) != 0: |
| 168 |
sys.stdout.write('\033[96mApply the changes listed above?\033[0m [Y/n]') |
| 169 |
response = None |
| 170 |
try: |
| 171 |
response = input() |
| 172 |
except KeyboardInterrupt: |
| 173 |
print() |
| 174 |
sys.exit(1) |
| 175 |
|
| 176 |
if response in ['', 'y', 'Y']: |
| 177 |
for patch in patches: |
| 178 |
apply = subprocess.Popen( |
| 179 |
['git', 'apply', '--allow-empty'], |
| 180 |
stdin=subprocess.PIPE, |
| 181 |
) |
| 182 |
apply.communicate(input=bytes(patch, encoding="utf-8")) |