Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added UTF-8 detection to test runner #276

Merged
merged 2 commits into from Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 19 additions & 7 deletions test.py
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 = []

Expand Down Expand Up @@ -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])

Expand Down
7 changes: 7 additions & 0 deletions tests.json
Expand Up @@ -1641,6 +1641,7 @@
]
},
"required": ["punycode"],
"encoding": "UTF-8",
"expected": {
"stderr": "",
"returncode": 0,
Expand All @@ -1656,6 +1657,7 @@
]
},
"required": ["punycode"],
"encoding": "UTF-8",
"expected": {
"stderr": "",
"returncode": 0,
Expand All @@ -1671,6 +1673,7 @@
]
},
"required": ["punycode"],
"encoding": "UTF-8",
"expected": {
"stderr": "",
"returncode": 0,
Expand Down Expand Up @@ -2149,6 +2152,7 @@
]
},
"required": ["punycode2idn"],
"encoding": "UTF-8",
"expected": {
"stdout": "http://räksmörgås/\n",
"stderr": "",
Expand All @@ -2165,6 +2169,7 @@
]
},
"required": ["punycode2idn"],
"encoding": "UTF-8",
"expected": {
"stdout": "räksmörgås\n",
"stderr": "",
Expand All @@ -2181,6 +2186,7 @@
]
},
"required": ["punycode2idn"],
"encoding": "UTF-8",
"expected": {
"stdout": "http://xn-----/\n",
"stderr": "",
Expand All @@ -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",
Expand Down