diff --git a/test.py b/test.py index 8ee83949..f1c489c5 100644 --- a/test.py +++ b/test.py @@ -30,6 +30,7 @@ from subprocess import PIPE, run, Popen from dataclasses import dataclass, asdict from typing import Any, Optional, TextIO +import locale PROGNAME = "trurl" TESTFILE = "tests.json" @@ -69,6 +70,12 @@ def check_valgrind(): return False +def getcharmap(): + process = Popen("locale charmap", shell=True, stdout=PIPE, stderr=PIPE, encoding="utf-8"); + output, error = process.communicate() + return output.strip() + + class TestCase: def __init__(self, testIndex, runnerCmd, baseCmd, **testCase): self.testIndex = testIndex @@ -172,6 +179,7 @@ def printDetail(self, verbose: bool = False, failed: bool = False): def main(argc, argv): ret = EXIT_SUCCESS baseDir = path.dirname(path.realpath(argv[0])) + locale.setlocale(locale.LC_ALL, "") # python on windows does not always seem to find the # executable if it is in a different output directory than # the python script, even if it is in the current working @@ -182,7 +190,7 @@ def main(argc, argv): if sys.platform == "win32" or sys.platform == "cygwin": baseCmd += ".exe" - with open(path.join(baseDir, TESTFILE), "r") as file: + with open(path.join(baseDir, TESTFILE), "r", encoding="utf-8") as file: allTests = json.load(file) testIndexesToRun = [] @@ -237,12 +245,16 @@ def main(argc, argv): numTestsSkipped = 0 for testIndex in testIndexesToRun: # skip tests if required features are not met - if "required" in allTests[testIndex]: - required = allTests[testIndex]["required"] - if not set(required).issubset(set(features)): - print(f"Missing feature, skipping test {testIndex + 1}.") - numTestsSkipped += 1 - continue + required = allTests[testIndex].get("required", None) + if required and not set(required).issubset(set(features)): + print(f"Missing feature, skipping test {testIndex + 1}.") + numTestsSkipped += 1 + continue + encoding = allTests[testIndex].get("encoding", None) + if encoding and encoding != getcharmap(): + print(f"Invalid locale, skipping test {testIndex + 1}.") + numTestsSkipped += 1 + continue; test = TestCase(testIndex + 1, runnerCmd, baseCmd, **allTests[testIndex]) diff --git a/tests.json b/tests.json index 9986223c..4bfd266e 100644 --- a/tests.json +++ b/tests.json @@ -1641,6 +1641,7 @@ ] }, "required": ["punycode"], + "encoding": "UTF-8", "expected": { "stderr": "", "returncode": 0, @@ -1656,6 +1657,7 @@ ] }, "required": ["punycode"], + "encoding": "UTF-8", "expected": { "stderr": "", "returncode": 0, @@ -1671,6 +1673,7 @@ ] }, "required": ["punycode"], + "encoding": "UTF-8", "expected": { "stderr": "", "returncode": 0, @@ -2149,6 +2152,7 @@ ] }, "required": ["punycode2idn"], + "encoding": "UTF-8", "expected": { "stdout": "http://räksmörgås/\n", "stderr": "", @@ -2165,6 +2169,7 @@ ] }, "required": ["punycode2idn"], + "encoding": "UTF-8", "expected": { "stdout": "räksmörgås\n", "stderr": "", @@ -2181,6 +2186,7 @@ ] }, "required": ["punycode2idn"], + "encoding": "UTF-8", "expected": { "stdout": "http://xn-----/\n", "stderr": "", @@ -2196,6 +2202,7 @@ ] }, "required": ["punycode2idn"], + "encoding": "UTF-8", "expected": { "stdout": "http://xn-----/\n", "stderr": "trurl note: Error converting url to IDN [Bad hostname]\n",