2021-01-13 20:57:33 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# echo hurl file
|
|
|
|
# The file is parsed and output exactly as the input
|
|
|
|
#
|
2021-02-12 15:22:46 +03:00
|
|
|
import codecs
|
2021-07-25 09:27:34 +03:00
|
|
|
import os
|
2021-01-13 20:57:33 +03:00
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
|
2022-02-05 08:56:33 +03:00
|
|
|
|
2021-02-12 15:22:46 +03:00
|
|
|
def decode_string(encoded):
|
|
|
|
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:
|
|
|
|
return encoded.decode()
|
|
|
|
|
|
|
|
|
2021-01-13 20:57:33 +03:00
|
|
|
def test(format_type, hurl_file):
|
2022-02-05 08:56:33 +03:00
|
|
|
output_file = hurl_file.replace(".hurl", "." + format_type)
|
2021-07-25 09:27:34 +03:00
|
|
|
if not os.path.exists(output_file):
|
|
|
|
return
|
2023-04-18 09:00:08 +03:00
|
|
|
cmd = ["hurlfmt", "--out", format_type, hurl_file]
|
2022-02-05 08:56:33 +03:00
|
|
|
print(" ".join(cmd))
|
2021-01-13 20:57:33 +03:00
|
|
|
result = subprocess.run(cmd, stdout=subprocess.PIPE)
|
2022-10-24 12:37:11 +03:00
|
|
|
expected = open(output_file, encoding="utf-8").read()
|
2021-02-12 15:22:46 +03:00
|
|
|
actual = decode_string(result.stdout)
|
2021-01-13 20:57:33 +03:00
|
|
|
if actual != expected:
|
2022-05-11 14:54:33 +03:00
|
|
|
print(f">>> error in stdout for {format_type}")
|
2022-02-05 08:56:33 +03:00
|
|
|
print(f"actual: <{actual}>\nexpected: <{expected}>")
|
2021-01-13 20:57:33 +03:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
if len(sys.argv) < 2:
|
2022-02-05 08:56:33 +03:00
|
|
|
print("usage: test_format.py json|html HURL_FILE..")
|
2021-01-13 20:57:33 +03:00
|
|
|
sys.exit(1)
|
2022-02-05 08:56:33 +03:00
|
|
|
format_type = sys.argv[1]
|
2021-01-13 20:57:33 +03:00
|
|
|
|
|
|
|
for hurl_file in sys.argv[2:]:
|
|
|
|
test(format_type, hurl_file)
|
|
|
|
|
|
|
|
|
2022-02-05 08:56:33 +03:00
|
|
|
if __name__ == "__main__":
|
2021-01-13 20:57:33 +03:00
|
|
|
main()
|