blob: 4a04e8e05f19227dd6de17523c52718f42f12599 [file] [edit]
#!/usr/bin/env python3
# Copyright (C) 2026 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import argparse
import os
import platform
import sys
from webkitcorepy import AutoInstall
libraries = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
AutoInstall.set_directory(os.path.join(libraries, 'autoinstalled', 'python-{}-{}'.format(sys.version_info[0], platform.machine())))
from webkitexpectationspy.linter import ExpectationsLinter
from webkitexpectationspy.suites.api_tests import APITestSuite
from webkitexpectationspy.suites.base import TestSuiteFormat
from webkitexpectationspy.suites.layout_tests import LayoutTestSuite
_SUITES = {
'generic': TestSuiteFormat,
'api-tests': APITestSuite,
'layout-tests': LayoutTestSuite,
}
def main(argv):
parser = argparse.ArgumentParser(
description='Lint a test expectations file.')
parser.add_argument('file', help='Path to the expectations file to lint.')
parser.add_argument(
'--suite', choices=sorted(_SUITES), default='generic',
help='Test suite format to use (default: generic).')
parser.add_argument(
'--fix', action='store_true',
help='Apply auto-fixes in place.')
args = parser.parse_args(argv)
if not os.path.isfile(args.file):
sys.stderr.write('error: file not found: {}\n'.format(args.file))
return 2
with open(args.file, 'r', encoding='utf-8') as f:
content = f.read()
linter = ExpectationsLinter(content, args.file, suite=_SUITES[args.suite]())
warnings = linter.lint()
for warning in warnings:
print(warning)
if args.fix and any(w.fixable for w in warnings):
fixed = linter.apply_fixes()
# Some warnings (cross-line reordering) are only fixable via a
# full-file reorder, which apply_fixes() does not handle.
relinter = ExpectationsLinter(fixed, args.file, suite=_SUITES[args.suite]())
relinter.lint()
fixed = relinter.generate_sorted_content()
if fixed != content:
with open(args.file, 'w', encoding='utf-8') as f:
f.write(fixed)
print('applied fixes to {}'.format(args.file))
return 1 if warnings else 0
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))