diff --git a/integration/test_hurl.py b/integration/test_hurl.py index c4ca7b590..a72cf8fd4 100755 --- a/integration/test_hurl.py +++ b/integration/test_hurl.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -# test hurl file +# Test Hurl files. # import codecs import sys @@ -8,9 +8,11 @@ import os import platform import check_json_output 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): return encoded.decode("utf-8-sig") elif encoded.startswith(codecs.BOM_UTF16): @@ -20,9 +22,10 @@ def decode_string(encoded): return encoded.decode() -# return linux-fedora, linux, osx or windows -# can add more specific linux variant if needed -def get_os(): +def get_os() -> str: + """Returns `linux-fedora`, `linux`, `osx` or `windows` + can add more specific linux variant if needed + """ if platform.system() == "Linux": if os.path.exists("/etc/fedora-release"): return "linux-fedora" @@ -35,8 +38,13 @@ def get_os(): 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") # 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 cmd = ["hurl", hurl_file] + options - # cmd = ["cargo", "run", "--bin", "hurl", "--", hurl_file] + options print(" ".join(cmd)) result = subprocess.run( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env @@ -86,7 +93,7 @@ def test(hurl_file): stderr = decode_string(result.stderr).strip() if stderr != "": print(stderr) - sys.exit(1) + return 1 # stdout f = hurl_file.replace(".hurl", ".out") @@ -96,7 +103,7 @@ def test(hurl_file): if actual != expected: print(">>> error in stdout") print(f"actual: <{actual}>\nexpected: <{expected}>") - sys.exit(1) + return 1 # stdout with textual pattern / line per line f = hurl_file.replace(".hurl", ".out.pattern") @@ -120,7 +127,7 @@ def test(hurl_file): print( f"expected: <{expected_lines[i]}> (translated to regex <{expected_pattern_lines[i]}>)" ) - sys.exit(1) + return 1 # stdout (json) if os.path.exists(json_output_file): @@ -145,7 +152,7 @@ def test(hurl_file): if expected != actual: print(">>> error in stderr") print(f"actual: <{actual}>\nexpected: <{expected}>") - sys.exit(1) + return 1 # stderr with textual pattern / line per line f = hurl_file.replace(".hurl", ".err.pattern") @@ -161,7 +168,7 @@ def test(hurl_file): "actual: %d lines\nexpected: %d lines" % (len(actual_lines), len(expected_lines)) ) - sys.exit(1) + return 1 for i in range(len(expected_pattern_lines)): if not re.match(expected_pattern_lines[i], actual_lines[i]): print(f">>> error in stderr in line {i+1}") @@ -169,7 +176,7 @@ def test(hurl_file): print( f"expected: <{expected_lines[i]}> (translated to regex <{expected_pattern_lines[i]}>)" ) - sys.exit(1) + return 1 # curl output if os.path.exists(curl_file): @@ -189,14 +196,14 @@ def test(hurl_file): print("curl commands error at %s" % (curl_file)) print("expected: %d commands" % len(expected_commands)) print("actual: %d commands" % len(actual_commands)) - sys.exit(1) + return 1 for i in range(len(expected_commands)): if actual_commands[i] != expected_commands[i]: print("curl command error at %s:%i" % (curl_file, i + 1)) print("expected: %s" % expected_commands[i]) print("actual: %s" % actual_commands[i]) - sys.exit(1) + return 1 def parse_pattern(s): @@ -210,8 +217,13 @@ def parse_pattern(s): def main(): - for hurl_file in sys.argv[1:]: - test(hurl_file) + parser = argparse.ArgumentParser() + 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__":