2016-03-16 18:58:10 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Copyright (C) 2016 the V8 project authors. All rights reserved.
|
|
|
|
# This code is governed by the BSD license found in the LICENSE file.
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
import argparse
|
2021-07-16 15:39:57 +02:00
|
|
|
import glob, os, sys
|
2016-03-16 18:58:10 +01:00
|
|
|
|
|
|
|
from lib.expander import Expander
|
|
|
|
from lib.test import Test
|
|
|
|
|
2022-05-02 16:56:46 +02:00
|
|
|
# base name of the files to be ignored
|
|
|
|
ignored_files = [
|
|
|
|
'.DS_Store',
|
|
|
|
]
|
|
|
|
|
2016-03-16 18:58:10 +01:00
|
|
|
def print_error(*values):
|
|
|
|
print('ERROR:', *values, file=sys.stderr)
|
|
|
|
|
2016-04-18 22:13:52 +02:00
|
|
|
# When a directory contains at least one file with a `.case` extension, it
|
|
|
|
# should be interpreted as a "case directory"
|
|
|
|
def is_case_dir(location):
|
|
|
|
for file_name in os.listdir(location):
|
|
|
|
if file_name.lower().endswith('.case'):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-03-16 18:58:10 +01:00
|
|
|
def find_cases(location):
|
2021-07-16 15:39:57 +02:00
|
|
|
# single file
|
2016-03-16 18:58:10 +01:00
|
|
|
if os.path.isfile(location):
|
|
|
|
return location, [os.path.dirname(location)]
|
|
|
|
|
2021-07-16 15:39:57 +02:00
|
|
|
# directory with case files
|
2016-04-18 22:13:52 +02:00
|
|
|
if is_case_dir(location):
|
2016-03-16 18:58:10 +01:00
|
|
|
return None, [location]
|
2021-07-16 15:39:57 +02:00
|
|
|
|
|
|
|
# other directory, return all contents other than hidden "dot files"
|
|
|
|
return None, glob.glob(os.path.join(location, '*'))
|
2016-03-16 18:58:10 +01:00
|
|
|
|
|
|
|
def clean(args):
|
|
|
|
for (subdir, _, fileNames) in os.walk(args.directory):
|
|
|
|
for fileName in map(lambda x: os.path.join(subdir, x), fileNames):
|
2022-05-02 16:56:46 +02:00
|
|
|
if os.path.basename(fileName) in ignored_files:
|
|
|
|
continue
|
2016-03-16 18:58:10 +01:00
|
|
|
test = Test(fileName)
|
|
|
|
test.load()
|
|
|
|
if test.is_generated():
|
|
|
|
print('Deleting file "' + fileName + '"...')
|
|
|
|
os.remove(fileName)
|
|
|
|
|
|
|
|
def create(args):
|
|
|
|
caseFile, caseDirs = find_cases(args.cases)
|
|
|
|
|
|
|
|
for caseDir in caseDirs:
|
|
|
|
exp = Expander(caseDir)
|
|
|
|
for test in exp.expand('utf-8', caseFile):
|
|
|
|
if args.out:
|
|
|
|
try:
|
2021-07-27 21:07:52 +02:00
|
|
|
test_file = os.path.join(args.out, test.file_name)
|
|
|
|
test_mtime = os.path.getmtime(test_file)
|
2016-03-16 18:58:10 +01:00
|
|
|
|
|
|
|
if args.no_clobber:
|
|
|
|
print_error(
|
|
|
|
'Refusing to overwrite file: ' + test.file_name)
|
|
|
|
exit(1)
|
|
|
|
|
2021-07-27 19:20:57 +02:00
|
|
|
if not args.regenerate:
|
|
|
|
source_files = test.source_file_names
|
|
|
|
if all(test_mtime > os.path.getmtime(f) for f in source_files):
|
|
|
|
continue
|
|
|
|
|
2021-07-27 21:07:52 +02:00
|
|
|
existing = Test(test_file)
|
|
|
|
existing.load()
|
2016-04-25 18:20:23 +02:00
|
|
|
if not existing.is_generated():
|
2016-03-16 18:58:10 +01:00
|
|
|
print_error(
|
|
|
|
'Refusing to overwrite non-generated file: ' +
|
|
|
|
test.file_name)
|
|
|
|
exit(1)
|
2021-07-27 21:07:52 +02:00
|
|
|
except (OSError, IOError):
|
2016-03-16 18:58:10 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
test.write(args.out, parents=args.parents)
|
|
|
|
else:
|
|
|
|
print(test.to_string())
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Test262 test generator tool')
|
|
|
|
subparsers = parser.add_subparsers()
|
|
|
|
|
|
|
|
create_parser = subparsers.add_parser('create',
|
|
|
|
help='''Generate test material''')
|
2021-07-16 15:39:57 +02:00
|
|
|
create_parser.add_argument('-o', '--out', help='''The directory in which to write
|
|
|
|
compiled tests. If unspecified, tests will be written to standard output.''')
|
2016-03-16 18:58:10 +01:00
|
|
|
create_parser.add_argument('-p', '--parents', action='store_true',
|
|
|
|
help='''Create non-existent directories as necessary.''')
|
|
|
|
create_parser.add_argument('-n', '--no-clobber', action='store_true',
|
2021-07-16 15:39:57 +02:00
|
|
|
help='''Abort if any test file already exists.''')
|
2021-07-27 19:20:57 +02:00
|
|
|
create_parser.add_argument('-r', '--regenerate', action='store_true',
|
|
|
|
help='''Regenerate test files that are already newer than their source data.''')
|
2016-03-16 18:58:10 +01:00
|
|
|
create_parser.add_argument('cases',
|
|
|
|
help='''Test cases to generate. May be a file or a directory.''')
|
|
|
|
create_parser.set_defaults(func=create)
|
|
|
|
|
|
|
|
clean_parser = subparsers.add_parser('clean',
|
|
|
|
help='''Remove previously-generated files''')
|
|
|
|
clean_parser.add_argument('directory',
|
|
|
|
help='''Remove any generated tests from this directory''')
|
|
|
|
clean_parser.set_defaults(func=clean)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
args.func(args)
|