File: verify-swift-feature-testing.test-sh

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (239 lines) | stat: -rwxr-xr-x 7,195 bytes parent folder | download
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#! /usr/bin/env python3
# -*- python -*-
# RUN: %{python} %s '%swift_src_root' %existing-swift-features

import json
import pathlib
import re
import subprocess
import sys


# Tests that check for the behaviour of  experimental/upcoming features, so
# they cannot automatically be checked.
EXCEPTIONAL_FILES = [
    # Tests for ParserValidation not being defined in no-asserts compilers
    pathlib.Path("test/Frontend/experimental-features-no-asserts.swift"),
    # Tests for UnknownFeature not existing
    pathlib.Path("test/Frontend/upcoming_feature.swift"),
    # Tests for ModuleInterfaceExportAs being ignored
    pathlib.Path("test/ModuleInterface/swift-export-as.swift"),
    # Uses the pseudo-feature AvailabilityMacro=
    pathlib.Path("test/Sema/availability_define.swift"),
]

FEATURE_USAGE_RE = re.compile(
    r"-enable-(?:experimental|upcoming)-feature (?:-Xfrontend )?([A-Za-z0-9]*)"
)
# Be careful of not using REQUIRES or RUN with a colon after them or Lit will
# pick them up.
FEATURE_LIT_MARKER_RE = re.compile(r"swift_feature_([A-Za-z0-9]*)")


def find_test_files_with_features_usage(swift_src_root):
    # Look for every test file in the test directories with `RUN` lines that
    # mention `-enable-experimental-feature` or `-enable-upcoming-feature`.
    # Be careful of not using REQUIRES or RUN with a colon after them or Lit will
    # pick them up.
    output = subprocess.check_output(
        [
            "grep",
            "--extended-regexp",
            "--recursive",
            "-e",
            "RUN[:].*-enable-(experimental|upcoming)-feature",
            "--files-with-matches",
            str(swift_src_root / "test"),
            str(swift_src_root / "validation-test"),
        ],
        text=True,
    )
    return output.splitlines()


def find_test_files_with_marker_usage(swift_src_root):
    # Look for every test file in the test directories with `REQUIRES` lines
    # that mention `swift_feature_`.
    # Be careful of not using REQUIRES with a colon after them or Lit will
    # pick them up.
    output = subprocess.check_output(
        [
            "grep",
            "--extended-regexp",
            "--recursive",
            "-e",
            "REQUIRES[:].*swift_feature_",
            "--files-with-matches",
            str(swift_src_root / "test"),
            str(swift_src_root / "validation-test"),
        ],
        text=True,
    )
    return output.splitlines()


def find_run_lines(test_file):
    output = subprocess.check_output(
        [
            "grep",
            "--extended-regexp",
            "--no-filename",
            "-e",
            "RUN[:]",
            str(test_file),
        ],
        text=True,
    )
    return output.splitlines()


def find_requires_lines(test_file):
    output = subprocess.check_output(
        [
            "grep",
            "--extended-regexp",
            "--no-filename",
            "-e",
            "REQUIRES[:]",
            str(test_file),
        ],
        text=True,
    )
    return output.splitlines()


def check_existing_requires(test_file, feature):
    returncode = subprocess.call(
        [
            "grep",
            "--extended-regexp",
            "--quiet",
            "-e",
            "REQUIRES[:].*swift_feature_" + feature,
            str(test_file),
        ]
    )
    return returncode != 0


def check_existing_feature_usage(test_file, feature):
    returncode = subprocess.call(
        [
            "grep",
            "--extended-regexp",
            "--quiet",
            "-e",
            (
                "RUN[:].*-enable-(experimental|upcoming)-feature (-Xfrontend )?"
                + re.escape(feature)
            ),
            str(test_file),
        ]
    )
    return returncode != 0


def check_existing_error_message_checks(test_file, feature):
    returncode = subprocess.call(
        [
            "grep",
            "--extended-regexp",
            "--quiet",
            "-e",
            "requires '-enable-(experimental|upcoming)-feature " + feature + "'",
            str(test_file),
        ]
    )
    return returncode != 0


def check_test_file_feature_usage(test_file, existing_swift_features):
    run_lines = find_run_lines(test_file)
    features = set(
        feature for line in run_lines for feature in FEATURE_USAGE_RE.findall(line)
    )
    num_failures = 0
    for feature in features:
        # First, check this is a valid feature
        if feature not in existing_swift_features:
            print("error: {}: Unknown feature: {}".format(str(test_file), feature))
            num_failures += 1
            continue

        # No warning if the necessary `REQUIRES` is already there
        if not check_existing_requires(test_file, feature):
            continue

        # Some tests check for the errors themselves, so we can skip them as well
        if not check_existing_error_message_checks(test_file, feature):
            continue

        # For everything else, print a warning and add to the invalid exit code
        print(
            "error: {}: Missing '{}: swift_feature_{}'".format(
                str(test_file), "REQUIRES", feature
            )
        )
        num_failures += 1
    return num_failures == 0


def check_test_file_marker_usage(test_file):
    require_lines = find_requires_lines(test_file)
    features = set(
        feature
        for line in require_lines
        for feature in FEATURE_LIT_MARKER_RE.findall(line)
    )
    num_failures = 0
    for feature in features:
        # No warning if -enable-experimental/upcoming-feature is there
        if not check_existing_feature_usage(test_file, feature):
            continue

        # For everything else, print a warning and add to the invalid exit code
        print(
            "error: {}: Missing '-enable-experimental/upcoming-feature: {}'".format(
                str(test_file), feature
            )
        )
        num_failures += 1
    return num_failures == 0


def main():
    if len(sys.argv) < 3:
        print("Invalid number of arguments.")
        sys.exit(1)

    swift_src_root = pathlib.Path(sys.argv[1])
    existing_swift_features = set(json.loads(sys.argv[2]))

    num_failures = 0
    test_files_with_features_usage = find_test_files_with_features_usage(swift_src_root)
    for test_file in test_files_with_features_usage:
        test_file = pathlib.Path(test_file)
        # First lets check this is not one of the exceptional files
        if test_file.relative_to(swift_src_root) in EXCEPTIONAL_FILES:
            continue

        if not check_test_file_feature_usage(test_file, existing_swift_features):
            num_failures += 1

    test_files_with_marker_usage = find_test_files_with_marker_usage(swift_src_root)
    for test_file in test_files_with_marker_usage:
        test_file = pathlib.Path(test_file)

        # First lets check this is not one of the exceptional files
        if test_file.relative_to(swift_src_root) in EXCEPTIONAL_FILES:
            continue

        if not check_test_file_marker_usage(test_file):
            num_failures += 1

    if num_failures > 0:
        sys.exit(1)


if __name__ == "__main__":
    main()