mirror of
https://github.com/Orange-OpenSource/hurl.git
synced 2024-12-17 21:31:49 +03:00
99b811fa70
Decode hurlfmt output and error stream as text supports both UTF-8 and UTF-16, both with and with BOM Add optional os-specific expected error file
45 lines
1.2 KiB
Python
Executable File
45 lines
1.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# echo hurl file
|
|
# The file is parsed and output exactly as the input
|
|
#
|
|
import codecs
|
|
import sys
|
|
import subprocess
|
|
|
|
def decode_string(encoded):
|
|
if encoded.startswith(codecs.BOM_UTF8):
|
|
return encoded.decode('utf-8-sig')
|
|
elif encoded.startswith(codecs.BOM_UTF16):
|
|
encoded = encoded[len(codecs.BOM_UTF16):]
|
|
return encoded.decode('utf-16')
|
|
else:
|
|
return encoded.decode()
|
|
|
|
|
|
def test(format_type, hurl_file):
|
|
cmd = ['hurlfmt', '--format', format_type, hurl_file]
|
|
print(' '.join(cmd))
|
|
result = subprocess.run(cmd, stdout=subprocess.PIPE)
|
|
json_file = hurl_file.replace('.hurl','.' + format_type)
|
|
expected = open(json_file, encoding='utf-8').read().strip()
|
|
actual = decode_string(result.stdout)
|
|
if actual != expected:
|
|
print('>>> error in stdout')
|
|
print(f'actual: <{actual}>\nexpected: <{expected}>')
|
|
sys.exit(1)
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
print('usage: test_format.py json|html HURL_FILE..')
|
|
sys.exit(1)
|
|
format_type = sys.argv[1]
|
|
|
|
for hurl_file in sys.argv[2:]:
|
|
test(format_type, hurl_file)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|
|
|