2021-01-13 20:57:33 +03:00
|
|
|
#!/usr/bin/env python3
|
2022-07-01 14:30:30 +03:00
|
|
|
# Test Hurl files.
|
2021-01-13 20:57:33 +03:00
|
|
|
#
|
2021-02-12 15:22:46 +03:00
|
|
|
import codecs
|
2021-01-13 20:57:33 +03:00
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
import os
|
2021-02-12 15:22:46 +03:00
|
|
|
import platform
|
2022-06-29 12:40:54 +03:00
|
|
|
import re
|
2022-07-01 14:30:30 +03:00
|
|
|
import argparse
|
2021-02-12 15:22:46 +03:00
|
|
|
|
|
|
|
|
2022-07-01 14:30:30 +03:00
|
|
|
def decode_string(encoded: bytes) -> str:
|
2022-10-01 11:13:50 +03:00
|
|
|
"""Decodes bytes to string from inferring encoding."""
|
2021-02-12 15:22:46 +03:00
|
|
|
if encoded.startswith(codecs.BOM_UTF8):
|
2022-02-05 08:56:33 +03:00
|
|
|
return encoded.decode("utf-8-sig")
|
2021-02-12 15:22:46 +03:00
|
|
|
elif encoded.startswith(codecs.BOM_UTF16):
|
2022-02-05 08:56:33 +03:00
|
|
|
encoded = encoded[len(codecs.BOM_UTF16) :]
|
|
|
|
return encoded.decode("utf-16")
|
2021-02-12 15:22:46 +03:00
|
|
|
else:
|
2022-07-21 11:11:47 +03:00
|
|
|
# No BOM to determine encoding, try utf-8
|
|
|
|
return encoded.decode("utf-8")
|
2021-02-12 15:22:46 +03:00
|
|
|
|
|
|
|
|
2022-07-01 14:30:30 +03:00
|
|
|
def get_os() -> str:
|
|
|
|
"""Returns `linux-fedora`, `linux`, `osx` or `windows`
|
|
|
|
can add more specific linux variant if needed
|
|
|
|
"""
|
2022-02-05 08:56:33 +03:00
|
|
|
if platform.system() == "Linux":
|
2022-05-17 19:34:59 +03:00
|
|
|
if os.path.exists("/etc/fedora-release"):
|
|
|
|
return "linux-fedora"
|
2022-02-05 08:56:33 +03:00
|
|
|
return "linux"
|
|
|
|
elif platform.system() == "Darwin":
|
|
|
|
return "osx"
|
|
|
|
elif platform.system() == "Windows":
|
|
|
|
return "windows"
|
2021-02-12 15:22:46 +03:00
|
|
|
else:
|
2022-10-07 23:09:24 +03:00
|
|
|
raise ValueError("Invalid Platform " + platform.system())
|
2021-02-12 15:22:46 +03:00
|
|
|
|
2021-01-13 20:57:33 +03:00
|
|
|
|
2022-07-21 11:11:47 +03:00
|
|
|
def test(hurl_file: str):
|
|
|
|
"""Runs a Hurl file, exit the process if there is an error.
|
2022-01-28 11:23:50 +03:00
|
|
|
|
2022-07-01 14:30:30 +03:00
|
|
|
Arguments:
|
|
|
|
hurl_file -- the Hurl file to run
|
|
|
|
"""
|
2022-02-05 08:56:33 +03:00
|
|
|
options_file = hurl_file.replace(".hurl", ".options")
|
2022-01-28 11:23:50 +03:00
|
|
|
|
|
|
|
# For .curl file, we can have specific os expected file in order to test
|
|
|
|
# os differences like included path (/ vs \ path components separator)
|
2022-02-05 08:56:33 +03:00
|
|
|
os_curl_file = hurl_file.replace(".hurl", "." + get_os() + ".curl")
|
2022-01-28 11:23:50 +03:00
|
|
|
if os.path.exists(os_curl_file):
|
|
|
|
curl_file = os_curl_file
|
|
|
|
else:
|
2022-02-05 08:56:33 +03:00
|
|
|
curl_file = hurl_file.replace(".hurl", ".curl")
|
2022-01-28 11:23:50 +03:00
|
|
|
|
2022-02-05 08:56:33 +03:00
|
|
|
profile_file = hurl_file.replace(".hurl", ".profile")
|
2021-05-14 20:31:18 +03:00
|
|
|
|
2021-01-13 20:57:33 +03:00
|
|
|
options = []
|
|
|
|
if os.path.exists(options_file):
|
2022-07-21 11:11:47 +03:00
|
|
|
options = open(options_file, encoding="utf-8").read().strip().split("\n")
|
2021-05-14 20:31:18 +03:00
|
|
|
if os.path.exists(curl_file):
|
2022-02-05 08:56:33 +03:00
|
|
|
options.append("--verbose")
|
2021-11-08 22:54:19 +03:00
|
|
|
|
2021-11-09 01:06:40 +03:00
|
|
|
env = os.environ.copy()
|
|
|
|
if os.path.exists(profile_file):
|
2022-07-21 11:11:47 +03:00
|
|
|
for line in open(profile_file, encoding="utf-8").readlines():
|
2021-11-09 01:06:40 +03:00
|
|
|
line = line.strip()
|
2022-02-05 08:56:33 +03:00
|
|
|
if line == "":
|
2021-11-09 01:06:40 +03:00
|
|
|
continue
|
2022-02-05 08:56:33 +03:00
|
|
|
index = line.index("=")
|
2021-11-09 01:06:40 +03:00
|
|
|
name = line[:index]
|
2022-02-05 08:56:33 +03:00
|
|
|
value = line[(index + 1) :]
|
2021-11-09 01:06:40 +03:00
|
|
|
env[name] = value
|
|
|
|
|
2022-02-05 08:56:33 +03:00
|
|
|
cmd = ["hurl", hurl_file] + options
|
|
|
|
print(" ".join(cmd))
|
|
|
|
result = subprocess.run(
|
|
|
|
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env
|
|
|
|
)
|
2021-10-08 09:48:29 +03:00
|
|
|
|
2022-02-05 08:56:33 +03:00
|
|
|
# exit code
|
|
|
|
f = hurl_file.replace(".hurl", ".exit")
|
2022-08-05 21:53:08 +03:00
|
|
|
if os.path.exists(f):
|
|
|
|
expected = int(open(f, encoding="utf-8").read().strip())
|
|
|
|
else:
|
|
|
|
expected = 0
|
2021-01-13 20:57:33 +03:00
|
|
|
if result.returncode != expected:
|
2022-02-05 08:56:33 +03:00
|
|
|
print(">>> error in return code")
|
|
|
|
print(f"expected: {expected} actual:{result.returncode}")
|
2021-10-08 09:48:29 +03:00
|
|
|
stderr = decode_string(result.stderr).strip()
|
2022-02-05 08:56:33 +03:00
|
|
|
if stderr != "":
|
2021-10-08 09:48:29 +03:00
|
|
|
print(stderr)
|
2022-07-21 11:11:47 +03:00
|
|
|
sys.exit(1)
|
2021-01-13 20:57:33 +03:00
|
|
|
|
|
|
|
# stdout
|
2022-02-05 08:56:33 +03:00
|
|
|
f = hurl_file.replace(".hurl", ".out")
|
2021-01-13 20:57:33 +03:00
|
|
|
if os.path.exists(f):
|
2022-02-05 08:56:33 +03:00
|
|
|
expected = open(f, "rb").read()
|
|
|
|
actual = result.stdout
|
2022-06-29 12:40:54 +03:00
|
|
|
if actual != expected:
|
2022-02-05 08:56:33 +03:00
|
|
|
print(">>> error in stdout")
|
|
|
|
print(f"actual: <{actual}>\nexpected: <{expected}>")
|
2022-07-21 11:11:47 +03:00
|
|
|
sys.exit(1)
|
2021-02-12 15:22:46 +03:00
|
|
|
|
2022-06-29 12:40:54 +03:00
|
|
|
# stdout with textual pattern / line per line
|
|
|
|
f = hurl_file.replace(".hurl", ".out.pattern")
|
|
|
|
if os.path.exists(f):
|
2022-07-21 11:11:47 +03:00
|
|
|
expected = open(f, encoding="utf-8").read()
|
2022-06-29 12:40:54 +03:00
|
|
|
actual = decode_string(result.stdout)
|
|
|
|
expected_lines = expected.split("\n")
|
|
|
|
expected_pattern_lines = [parse_pattern(line) for line in expected_lines]
|
|
|
|
actual_lines = actual.split("\n")
|
|
|
|
if len(actual_lines) != len(expected_pattern_lines):
|
|
|
|
print(">>> error in stderr / mismatch in number of lines")
|
|
|
|
print(
|
2022-07-21 11:11:47 +03:00
|
|
|
f"actual: {len(actual_lines)} lines\nexpected: {len(expected_lines)} lines"
|
2022-06-29 12:40:54 +03:00
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
for i in range(len(expected_pattern_lines)):
|
|
|
|
if not re.match(expected_pattern_lines[i], actual_lines[i]):
|
|
|
|
print(f">>> error in stdout in line {i+1}")
|
|
|
|
print(f"actual: <{actual_lines[i]}>")
|
|
|
|
print(
|
|
|
|
f"expected: <{expected_lines[i]}> (translated to regex <{expected_pattern_lines[i]}>)"
|
|
|
|
)
|
2022-07-21 11:11:47 +03:00
|
|
|
sys.exit(1)
|
2022-06-29 12:40:54 +03:00
|
|
|
|
2021-01-13 20:57:33 +03:00
|
|
|
# stderr
|
2022-08-26 14:23:23 +03:00
|
|
|
f = hurl_file.replace(".hurl", ".err")
|
2021-01-13 20:57:33 +03:00
|
|
|
if os.path.exists(f):
|
2022-07-21 11:11:47 +03:00
|
|
|
expected = open(f, encoding="utf-8").read().strip()
|
2021-02-12 15:22:46 +03:00
|
|
|
actual = decode_string(result.stderr).strip()
|
2022-06-29 12:40:54 +03:00
|
|
|
if actual != expected:
|
2022-02-05 08:56:33 +03:00
|
|
|
print(">>> error in stderr")
|
|
|
|
print(f"actual: <{actual}>\nexpected: <{expected}>")
|
2021-02-12 15:22:46 +03:00
|
|
|
sys.exit(1)
|
2021-01-13 20:57:33 +03:00
|
|
|
|
2022-06-29 12:40:54 +03:00
|
|
|
# stderr with textual pattern / line per line
|
|
|
|
f = hurl_file.replace(".hurl", ".err.pattern")
|
|
|
|
if os.path.exists(f):
|
2022-07-21 11:11:47 +03:00
|
|
|
expected = open(f, encoding="utf-8").read()
|
2022-06-29 12:40:54 +03:00
|
|
|
actual = decode_string(result.stderr)
|
|
|
|
expected_lines = expected.split("\n")
|
|
|
|
expected_pattern_lines = [parse_pattern(line) for line in expected_lines]
|
|
|
|
actual_lines = re.split(r"\r?\n", actual)
|
|
|
|
if len(actual_lines) != len(expected_pattern_lines):
|
|
|
|
print(">>> error in stderr / mismatch in number of lines")
|
|
|
|
print(
|
2022-07-21 11:11:47 +03:00
|
|
|
f"actual: {len(actual_lines)} lines\nexpected: {len(expected_lines)} lines"
|
2022-06-29 12:40:54 +03:00
|
|
|
)
|
2022-07-21 11:11:47 +03:00
|
|
|
sys.exit(1)
|
2022-06-29 12:40:54 +03:00
|
|
|
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}")
|
|
|
|
print(f"actual: <{actual_lines[i]}>")
|
|
|
|
print(
|
|
|
|
f"expected: <{expected_lines[i]}> (translated to regex <{expected_pattern_lines[i]}>)"
|
|
|
|
)
|
2022-07-21 11:11:47 +03:00
|
|
|
sys.exit(1)
|
2022-06-29 12:40:54 +03:00
|
|
|
|
2021-05-14 20:31:18 +03:00
|
|
|
# curl output
|
|
|
|
if os.path.exists(curl_file):
|
|
|
|
expected_commands = []
|
2022-07-21 11:11:47 +03:00
|
|
|
for line in open(curl_file, encoding="utf-8").readlines():
|
2022-02-05 08:56:33 +03:00
|
|
|
line = line.strip()
|
|
|
|
if line == "" or line.startswith("#"):
|
|
|
|
continue
|
|
|
|
expected_commands.append(line)
|
2021-05-14 20:31:18 +03:00
|
|
|
|
|
|
|
actual = decode_string(result.stderr).strip()
|
2022-02-05 08:56:33 +03:00
|
|
|
actual_commands = [
|
|
|
|
line[2:] for line in actual.split("\n") if line.startswith("* curl")
|
|
|
|
]
|
2021-05-14 20:31:18 +03:00
|
|
|
|
|
|
|
if len(actual_commands) != len(expected_commands):
|
2022-07-21 11:11:47 +03:00
|
|
|
print(f"curl commands error at {curl_file}")
|
|
|
|
print(f"expected: {len(expected_commands)} commands")
|
|
|
|
print(f"actual: {len(actual_commands)} commands")
|
|
|
|
sys.exit(1)
|
2021-05-14 20:31:18 +03:00
|
|
|
|
|
|
|
for i in range(len(expected_commands)):
|
|
|
|
if actual_commands[i] != expected_commands[i]:
|
2022-07-21 11:11:47 +03:00
|
|
|
print(f"curl command error at {curl_file}: {i + 1}")
|
|
|
|
print(f"expected: {expected_commands[i]}")
|
|
|
|
print(f"actual: {actual_commands[i]}")
|
|
|
|
sys.exit(1)
|
2021-05-14 20:31:18 +03:00
|
|
|
|
2021-10-12 08:29:36 +03:00
|
|
|
|
2022-07-21 14:13:52 +03:00
|
|
|
def parse_pattern(s: str) -> str:
|
|
|
|
"""Transform a stderr pattern to a regex"""
|
2022-06-29 12:40:54 +03:00
|
|
|
# Escape regex metacharacters
|
2022-10-07 23:09:24 +03:00
|
|
|
for c in ["\\", ".", "(", ")", "[", "]", "^", "$", "*", "+", "?", "|"]:
|
2022-06-29 12:40:54 +03:00
|
|
|
s = s.replace(c, "\\" + c)
|
|
|
|
|
|
|
|
s = re.sub("~+", ".*", s)
|
|
|
|
s = "^" + s + "$"
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
2021-01-13 20:57:33 +03:00
|
|
|
def main():
|
2022-07-01 14:30:30 +03:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("file", type=str, nargs="+", metavar="FILE")
|
|
|
|
args = parser.parse_args()
|
|
|
|
for hurl_file in args.file:
|
2022-07-21 11:11:47 +03:00
|
|
|
test(hurl_file=hurl_file)
|
2021-01-13 20:57:33 +03:00
|
|
|
|
|
|
|
|
2022-02-05 08:56:33 +03:00
|
|
|
if __name__ == "__main__":
|
2021-01-13 20:57:33 +03:00
|
|
|
main()
|