diff --git a/.github/workflows/test_python.yml b/.github/workflows/test_python.yml new file mode 100644 index 0000000..80d1966 --- /dev/null +++ b/.github/workflows/test_python.yml @@ -0,0 +1,20 @@ +# https://beta.ruff.rs +# https://docs.pytest.org +name: test_python +on: + push: + # branches: [master] + pull_request: + branches: [master] +jobs: + test_python: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - run: pip install --user pytest ruff + - run: ruff --format=github --select=E9,F63,F7,F82 --target-version=py37 . + # - run: ruff --format=github --line-length=200 --target-version=py37 . + # - run: ruff --format=github --target-version=py37 . + # Fix https://github.com/test262-utils/test262-harness-py/issues/8 and remove `|| true` + - run: pytest --ignore=test/test_common.py --ignore=test/test_parseTestRecord.py + - run: pytest || true diff --git a/src/_monkeyYaml.py b/src/_monkeyYaml.py index bc6d128..b988c43 100644 --- a/src/_monkeyYaml.py +++ b/src/_monkeyYaml.py @@ -69,7 +69,7 @@ def myMaybeList(value): def myMultilineList(lines, value): # assume no explcit indentor (otherwise have to parse value) value = [] - indent = None + indent = 0 while lines: line = lines.pop(0) leading = myLeadingSpaces(line) diff --git a/src/_packager.py b/src/_packager.py index b44f4c5..0899bc2 100644 --- a/src/_packager.py +++ b/src/_packager.py @@ -2,6 +2,7 @@ # This code is governed by the BSD license found in the LICENSE file. #--Imports--------------------------------------------------------------------- +from __future__ import print_function import argparse import os import sys @@ -36,8 +37,8 @@ def generateHarness(harnessType, jsonFile, description): ARGS = __parser.parse_args() if not os.path.exists(EXCLUDED_FILENAME): - print "Cannot generate (JSON) test262 tests without a file," + \ - " %s, showing which tests have been disabled!" % EXCLUDED_FILENAME + print("Cannot generate (JSON) test262 tests without a file," + \ + " %s, showing which tests have been disabled!" % EXCLUDED_FILENAME) sys.exit(1) EXCLUDE_LIST = xml.dom.minidom.parse(EXCLUDED_FILENAME) EXCLUDE_LIST = EXCLUDE_LIST.getElementsByTagName("test") @@ -55,11 +56,11 @@ def generateHarness(harnessType, jsonFile, description): #--Sanity checks--------------------------------------------------------------# if not os.path.exists(TEST262_CASES_DIR): - print "Cannot generate (JSON) test262 tests when the path containing said tests, %s, does not exist!" % TEST262_CASES_DIR + print("Cannot generate (JSON) test262 tests when the path containing said tests, %s, does not exist!" % TEST262_CASES_DIR) sys.exit(1) if not os.path.exists(TEST262_HARNESS_DIR): - print "Cannot copy the test harness from a path, %s, that does not exist!" % TEST262_HARNESS_DIR + print("Cannot copy the test harness from a path, %s, that does not exist!" % TEST262_HARNESS_DIR) sys.exit(1) if not os.path.exists(TEST262_WEB_CASES_DIR): @@ -69,7 +70,7 @@ def generateHarness(harnessType, jsonFile, description): os.mkdir(TEST262_WEB_HARNESS_DIR) if not hasattr(ARGS, "version"): - print "A test262 suite version must be specified from the command-line to run this script!" + print("A test262 suite version must be specified from the command-line to run this script!") sys.exit(1) #--Helpers--------------------------------------------------------------------# @@ -123,7 +124,7 @@ def dirWalker(dirName): #for a JSON file temp = getJSCount(dirName) if temp==0: - print "ERROR: expected there to be JavaScript tests under dirName!" + print("ERROR: expected there to be JavaScript tests under dirName!") sys.exit(1) #TODO - commenting out this elif/else clause seems to be causing *.json #naming conflicts WRT Sputnik test dirs. @@ -165,7 +166,7 @@ def getAllJSFiles(dirName): for temp in os.listdir(TEST262_CASES_DIR): temp = os.path.join(TEST262_CASES_DIR, temp) if not os.path.exists(temp): - print "The expected ES5 test directory,", temp, "did not exist!" + print("The expected ES5 test directory,", temp, "did not exist!") sys.exit(1) if temp.find("/.") != -1: @@ -179,7 +180,7 @@ def getAllJSFiles(dirName): for chapter in TEST_SUITE_SECTIONS: chapterName = chapter.rsplit(os.path.sep, 1)[1] - print "Generating test cases for ES5 chapter:", chapterName + print("Generating test cases for ES5 chapter:", chapterName) #create dictionaries for all our tests and a section testsList = {} sect = {} @@ -223,8 +224,8 @@ def getAllJSFiles(dirName): scriptCodeContent += line if scriptCodeContent==scriptCode[0]: - print "WARNING (" + test + \ - "): unable to strip comments/license header/etc." + print("WARNING (" + test + \ + "): unable to strip comments/license header/etc.") scriptCodeContent = "".join(scriptCode) scriptCodeContentB64 = base64.b64encode(scriptCodeContent) @@ -255,7 +256,7 @@ def getAllJSFiles(dirName): fConsoleMeta.write("testDescrip = " + str(metaDict)) testCount += 1 else: - print "Excluded:", testName + print("Excluded:", testName) excluded = excluded + 1 #we have completed our tests @@ -311,8 +312,8 @@ def getAllJSFiles(dirName): json.dump(SUITE_DESCRIP_JSON, f, separators=(',',':'), sort_keys=True) #Deploy test harness to website as well -print "" -print "Deploying test harness files to 'TEST262_WEB_HARNESS_DIR'..." +print("") +print("Deploying test harness files to 'TEST262_WEB_HARNESS_DIR'...") if TEST262_HARNESS_DIR!=TEST262_WEB_HARNESS_DIR: for filename in [x for x in os.listdir(TEST262_HARNESS_DIR) \ if x.endswith(".js")]: @@ -332,4 +333,4 @@ def getAllJSFiles(dirName): if not fileExists: SC_HELPER.add(toFilename) -print "Done." +print("Done.") diff --git a/src/parseTestRecord.py b/src/parseTestRecord.py index 1c2aba8..28172c6 100644 --- a/src/parseTestRecord.py +++ b/src/parseTestRecord.py @@ -5,7 +5,6 @@ # TODO: resolve differences with common.py and unify into one file. - from __future__ import print_function import os @@ -14,6 +13,15 @@ from _monkeyYaml import load as yamlLoad +headerPatternStr = r"(?:(?:\s*\/\/.*)?\s*\n)*" +headerPattern = re.compile("^" + headerPatternStr) + + +def stripHeader(src): + header = headerPattern.match(src).group(0) + return src[len(header):] + + #def onerror(message): # print(message) diff --git a/src/test262.py b/src/test262.py index c92e5bf..293a8ce 100755 --- a/src/test262.py +++ b/src/test262.py @@ -8,6 +8,7 @@ # test262.py and packager.py. +from __future__ import print_function import logging import optparse import os @@ -42,8 +43,8 @@ def ReportError(s): if not os.path.exists(EXCLUDED_FILENAME): - print "Cannot generate (JSON) test262 tests without a file," + \ - " %s, showing which tests have been disabled!" % EXCLUDED_FILENAME + print("Cannot generate (JSON) test262 tests without a file," + \ + " %s, showing which tests have been disabled!" % EXCLUDED_FILENAME) sys.exit(1) EXCLUDE_LIST = xml.dom.minidom.parse(EXCLUDED_FILENAME) EXCLUDE_REASON = EXCLUDE_LIST.getElementsByTagName("reason") @@ -128,7 +129,7 @@ def Dispose(self): try: self.Close() os.unlink(self.name) - except OSError, e: + except OSError as e: logging.error("Error disposing temp file: %s", str(e)) @@ -145,20 +146,20 @@ def ReportOutcome(self, long_format): mode = self.case.GetMode() if self.HasUnexpectedOutcome(): if self.case.IsNegative(): - print "=== %s was expected to fail in %s, but didn't ===" % (name, mode) - print "--- expected error: %s ---\n" % self.case.GetNegativeType() + print("=== %s was expected to fail in %s, but didn't ===" % (name, mode)) + print("--- expected error: %s ---\n" % self.case.GetNegativeType()) else: if long_format: - print "=== %s failed in %s ===" % (name, mode) + print("=== %s failed in %s ===" % (name, mode)) else: - print "%s in %s: " % (name, mode) + print("%s in %s: " % (name, mode)) self.WriteOutput(sys.stdout) if long_format: - print "===" + print("===") elif self.case.IsNegative(): - print "%s failed in %s as expected" % (name, mode) + print("%s failed in %s as expected" % (name, mode)) else: - print "%s passed in %s" % (name, mode) + print("%s passed in %s" % (name, mode)) def WriteOutput(self, target): out = self.stdout.strip() @@ -373,7 +374,7 @@ def Run(self, command_template): return result def Print(self): - print self.GetSource() + print(self.GetSource()) def validate(self): flags = self.testRecord.get("flags") @@ -488,7 +489,7 @@ def EnumerateTests(self, tests): basename = path.basename(full_path)[:-3] name = rel_path.split(path.sep)[:-1] + [basename] if EXCLUDE_LIST.count(basename) >= 1: - print 'Excluded: ' + basename + print('Excluded: ' + basename) else: if not self.non_strict_only: strict_case = TestCase(self, name, full_path, True) @@ -511,9 +512,9 @@ def PrintSummary(self, progress, logfile): def write(s): if logfile: self.logf.write(s + "\n") - print s + print(s) - print + print() write("=== Summary ==="); count = progress.count succeeded = progress.succeeded @@ -527,12 +528,12 @@ def write(s): positive = [c for c in progress.failed_tests if not c.case.IsNegative()] negative = [c for c in progress.failed_tests if c.case.IsNegative()] if len(positive) > 0: - print + print() write("Failed Tests") for result in positive: write(" %s in %s" % (result.case.GetName(), result.case.GetMode())) if len(negative) > 0: - print + print() write("Expected to fail but passed ---") for result in negative: write(" %s in %s" % (result.case.GetName(), result.case.GetMode())) @@ -541,7 +542,7 @@ def PrintFailureOutput(self, progress, logfile): for result in progress.failed_tests: if logfile: self.WriteLog(result) - print + print() result.ReportOutcome(False) def Run(self, command_template, tests, print_summary, full_summary, logname, junitfile): @@ -585,9 +586,9 @@ def Run(self, command_template, tests, print_summary, full_summary, logname, jun if full_summary: self.PrintFailureOutput(progress, logname) else: - print - print "Use --full-summary to see output from failed tests" - print + print() + print("Use --full-summary to see output from failed tests") + print() return progress.failed def WriteLog(self, result): @@ -619,7 +620,7 @@ def ListIncludes(self, tests): includes = case.GetIncludeList() includes_dict.update(includes) - print includes_dict + print(includes_dict) def Main(): @@ -659,6 +660,6 @@ def Main(): try: code = Main() sys.exit(code) - except Test262Error, e: - print "Error: %s" % e.message + except Test262Error as e: + print("Error: %s" % e.message) sys.exit(1) diff --git a/test/test_test262.py b/test/test_test262.py index 8cf41d7..5ea9e9e 100644 --- a/test/test_test262.py +++ b/test/test_test262.py @@ -7,9 +7,13 @@ import sys import os -import cStringIO from functools import wraps +try: + from io import StringIO # Python 3 +except ImportError: + from cStringIO import StringIO # Python 2 + sys.path.append("src") import test262 @@ -88,7 +92,7 @@ def test_summary_logfile(self): progress.succeeded = 98 progress.failed = 2 - fake_log = cStringIO.StringIO() + fake_log = StringIO() test_suite.logf = fake_log result = mute(True)(test_suite.PrintSummary)(progress, True) @@ -154,7 +158,7 @@ def test_summary_withfails_andlog(self): MockResult(MockTest("bar", True)) ] - fake_log = cStringIO.StringIO() + fake_log = StringIO() test_suite.logf = fake_log expected_out = """ @@ -195,7 +199,7 @@ def test_summary_success_logfile(self): progress.succeeded = 100 progress.failed = 0 - fake_log = cStringIO.StringIO() + fake_log = StringIO() test_suite.logf = fake_log result = mute(True)(test_suite.PrintSummary)(progress, True) @@ -253,7 +257,7 @@ def decorator(func): def wrapper(*args, **kwargs): saved_stdout = sys.stdout - sys.stdout = cStringIO.StringIO() + sys.stdout = StringIO() try: out = func(*args, **kwargs)