mirror of
https://github.com/Orange-OpenSource/hurl.git
synced 2024-12-24 11:32:00 +03:00
Add type hints to test_hurl.py
This commit is contained in:
parent
1705f30f72
commit
1c8713ebe4
@ -1,5 +1,5 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# test hurl file
|
# Test Hurl files.
|
||||||
#
|
#
|
||||||
import codecs
|
import codecs
|
||||||
import sys
|
import sys
|
||||||
@ -8,9 +8,11 @@ import os
|
|||||||
import platform
|
import platform
|
||||||
import check_json_output
|
import check_json_output
|
||||||
import re
|
import re
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
|
||||||
def decode_string(encoded):
|
def decode_string(encoded: bytes) -> str:
|
||||||
|
"""Decodes bytes to string from infering encoding."""
|
||||||
if encoded.startswith(codecs.BOM_UTF8):
|
if encoded.startswith(codecs.BOM_UTF8):
|
||||||
return encoded.decode("utf-8-sig")
|
return encoded.decode("utf-8-sig")
|
||||||
elif encoded.startswith(codecs.BOM_UTF16):
|
elif encoded.startswith(codecs.BOM_UTF16):
|
||||||
@ -20,9 +22,10 @@ def decode_string(encoded):
|
|||||||
return encoded.decode()
|
return encoded.decode()
|
||||||
|
|
||||||
|
|
||||||
# return linux-fedora, linux, osx or windows
|
def get_os() -> str:
|
||||||
# can add more specific linux variant if needed
|
"""Returns `linux-fedora`, `linux`, `osx` or `windows`
|
||||||
def get_os():
|
can add more specific linux variant if needed
|
||||||
|
"""
|
||||||
if platform.system() == "Linux":
|
if platform.system() == "Linux":
|
||||||
if os.path.exists("/etc/fedora-release"):
|
if os.path.exists("/etc/fedora-release"):
|
||||||
return "linux-fedora"
|
return "linux-fedora"
|
||||||
@ -35,8 +38,13 @@ def get_os():
|
|||||||
raise Error("Invalid Platform " + platform.system())
|
raise Error("Invalid Platform " + platform.system())
|
||||||
|
|
||||||
|
|
||||||
def test(hurl_file):
|
def test(hurl_file: str) -> int:
|
||||||
|
"""Runs a Hurl file and returns the exit code.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
hurl_file -- the Hurl file to run
|
||||||
|
use_cargo -- true to run hurl with 'cargo run', else just run 'hurl"
|
||||||
|
"""
|
||||||
options_file = hurl_file.replace(".hurl", ".options")
|
options_file = hurl_file.replace(".hurl", ".options")
|
||||||
|
|
||||||
# For .curl file, we can have specific os expected file in order to test
|
# For .curl file, we can have specific os expected file in order to test
|
||||||
@ -71,7 +79,6 @@ def test(hurl_file):
|
|||||||
env[name] = value
|
env[name] = value
|
||||||
|
|
||||||
cmd = ["hurl", hurl_file] + options
|
cmd = ["hurl", hurl_file] + options
|
||||||
# cmd = ["cargo", "run", "--bin", "hurl", "--", hurl_file] + options
|
|
||||||
print(" ".join(cmd))
|
print(" ".join(cmd))
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env
|
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env
|
||||||
@ -86,7 +93,7 @@ def test(hurl_file):
|
|||||||
stderr = decode_string(result.stderr).strip()
|
stderr = decode_string(result.stderr).strip()
|
||||||
if stderr != "":
|
if stderr != "":
|
||||||
print(stderr)
|
print(stderr)
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
# stdout
|
# stdout
|
||||||
f = hurl_file.replace(".hurl", ".out")
|
f = hurl_file.replace(".hurl", ".out")
|
||||||
@ -96,7 +103,7 @@ def test(hurl_file):
|
|||||||
if actual != expected:
|
if actual != expected:
|
||||||
print(">>> error in stdout")
|
print(">>> error in stdout")
|
||||||
print(f"actual: <{actual}>\nexpected: <{expected}>")
|
print(f"actual: <{actual}>\nexpected: <{expected}>")
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
# stdout with textual pattern / line per line
|
# stdout with textual pattern / line per line
|
||||||
f = hurl_file.replace(".hurl", ".out.pattern")
|
f = hurl_file.replace(".hurl", ".out.pattern")
|
||||||
@ -120,7 +127,7 @@ def test(hurl_file):
|
|||||||
print(
|
print(
|
||||||
f"expected: <{expected_lines[i]}> (translated to regex <{expected_pattern_lines[i]}>)"
|
f"expected: <{expected_lines[i]}> (translated to regex <{expected_pattern_lines[i]}>)"
|
||||||
)
|
)
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
# stdout (json)
|
# stdout (json)
|
||||||
if os.path.exists(json_output_file):
|
if os.path.exists(json_output_file):
|
||||||
@ -145,7 +152,7 @@ def test(hurl_file):
|
|||||||
if expected != actual:
|
if expected != actual:
|
||||||
print(">>> error in stderr")
|
print(">>> error in stderr")
|
||||||
print(f"actual: <{actual}>\nexpected: <{expected}>")
|
print(f"actual: <{actual}>\nexpected: <{expected}>")
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
# stderr with textual pattern / line per line
|
# stderr with textual pattern / line per line
|
||||||
f = hurl_file.replace(".hurl", ".err.pattern")
|
f = hurl_file.replace(".hurl", ".err.pattern")
|
||||||
@ -161,7 +168,7 @@ def test(hurl_file):
|
|||||||
"actual: %d lines\nexpected: %d lines"
|
"actual: %d lines\nexpected: %d lines"
|
||||||
% (len(actual_lines), len(expected_lines))
|
% (len(actual_lines), len(expected_lines))
|
||||||
)
|
)
|
||||||
sys.exit(1)
|
return 1
|
||||||
for i in range(len(expected_pattern_lines)):
|
for i in range(len(expected_pattern_lines)):
|
||||||
if not re.match(expected_pattern_lines[i], actual_lines[i]):
|
if not re.match(expected_pattern_lines[i], actual_lines[i]):
|
||||||
print(f">>> error in stderr in line {i+1}")
|
print(f">>> error in stderr in line {i+1}")
|
||||||
@ -169,7 +176,7 @@ def test(hurl_file):
|
|||||||
print(
|
print(
|
||||||
f"expected: <{expected_lines[i]}> (translated to regex <{expected_pattern_lines[i]}>)"
|
f"expected: <{expected_lines[i]}> (translated to regex <{expected_pattern_lines[i]}>)"
|
||||||
)
|
)
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
# curl output
|
# curl output
|
||||||
if os.path.exists(curl_file):
|
if os.path.exists(curl_file):
|
||||||
@ -189,14 +196,14 @@ def test(hurl_file):
|
|||||||
print("curl commands error at %s" % (curl_file))
|
print("curl commands error at %s" % (curl_file))
|
||||||
print("expected: %d commands" % len(expected_commands))
|
print("expected: %d commands" % len(expected_commands))
|
||||||
print("actual: %d commands" % len(actual_commands))
|
print("actual: %d commands" % len(actual_commands))
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
for i in range(len(expected_commands)):
|
for i in range(len(expected_commands)):
|
||||||
if actual_commands[i] != expected_commands[i]:
|
if actual_commands[i] != expected_commands[i]:
|
||||||
print("curl command error at %s:%i" % (curl_file, i + 1))
|
print("curl command error at %s:%i" % (curl_file, i + 1))
|
||||||
print("expected: %s" % expected_commands[i])
|
print("expected: %s" % expected_commands[i])
|
||||||
print("actual: %s" % actual_commands[i])
|
print("actual: %s" % actual_commands[i])
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
|
|
||||||
def parse_pattern(s):
|
def parse_pattern(s):
|
||||||
@ -210,8 +217,13 @@ def parse_pattern(s):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
for hurl_file in sys.argv[1:]:
|
parser = argparse.ArgumentParser()
|
||||||
test(hurl_file)
|
parser.add_argument("file", type=str, nargs="+", metavar="FILE")
|
||||||
|
args = parser.parse_args()
|
||||||
|
for hurl_file in args.file:
|
||||||
|
ret = test(hurl_file=hurl_file)
|
||||||
|
if ret != 0:
|
||||||
|
sys.exit(ret)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user