Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring test integ #1376

Merged
merged 2 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bin/test/test_integ.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ cd integration || exit
./integration.py
./test_curl_commands.sh
./test_html_output.py tests_ok/*.html tests_failed/*.html
./ad_hoc.sh
./report.py

2 changes: 1 addition & 1 deletion contrib/windows/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Install git, llvm, nsis and python3

```powershell
choco install --confirm --no-progress git winlibs-llvm-free nsis
choco install --confirm --no-progress python3 --version "3.8.0"
choco install --confirm --no-progress python3 --version "3.9.13"
python -m pip install --upgrade pip --quiet
```

Expand Down
34 changes: 0 additions & 34 deletions integration/ad_hoc.sh

This file was deleted.

9 changes: 9 additions & 0 deletions integration/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import test_lint
import test_format
import test_hurl
import test_script
import platform


def get_files(glob_expr):
Expand Down Expand Up @@ -32,6 +34,13 @@ def main():
+ get_files("ssl/*.hurl")
if accept(f)
]
# Run test scripts
extension = "ps1" if platform.system() == "Windows" else "sh"
script_files = get_files("tests_ok/*." + extension) + get_files(
"tests_failed/*." + extension
)
for f in sorted(script_files):
test_script.test(f)

print("test integration ok!")

Expand Down
94 changes: 94 additions & 0 deletions integration/test_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env python3
# Test script file.
#
import codecs
import sys
import subprocess
import os
import argparse
from typing import Optional


def decode_string(encoded: bytes) -> str:
"""Decodes bytes to string from inferring encoding."""
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:
# No BOM to determine encoding, try utf-8
return encoded.decode("utf-8")


def test(script_file: str):
"""Runs a script, exit the process if there is an error.

Arguments:
script_file -- the script file to run
"""
cmd = (
"powershell.exe -Command " + script_file + " ; exit $LASTEXITCODE"
if script_file.endswith("ps1")
else script_file
)
# subprocess.run(['powershell', '-Command', script_file + " ; exit $LASTEXITCODE"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(cmd)
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# exit code
expected = get_expected_exit_code(script_file)
if result.returncode != expected:
print(">>> error in return code")
print(f"expected: {expected} actual:{result.returncode}")
stderr = decode_string(result.stderr).strip()
if stderr != "":
print(stderr)
stdout = decode_string(result.stdout).strip()
if stdout != "":
sys.setdefaultencoding("utf-8")
sys.stdout.write(stdout + "\n")

sys.exit(1)

# stdout
expected = get_expected_stdout(script_file)
actual = result.stdout
if actual != expected:
print(">>> error in stdout")
print(f"actual: <{actual}>\nexpected: <{expected}>")
sys.exit(1)


def get_expected_exit_code(script_file: str) -> int:
"""Runs expected exit code for a given test script"""

f = os.path.splitext(script_file)[0] + ".exit"
if os.path.exists(f):
expected = int(open(f, encoding="utf-8").read().strip())
else:
expected = 0
return expected


def get_expected_stdout(script_file: str) -> Optional[str]:
"""Runs expected stdout for a given test script"""

f = os.path.splitext(script_file)[0] + ".out"
if os.path.exists(f):
value = open(f, "rb").read()
else:
value = None
return value


def main():
parser = argparse.ArgumentParser()
parser.add_argument("file", type=str, nargs="+", metavar="FILE")
args = parser.parse_args()
for script_file in args.file:
test(script_file=script_file)


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions integration/tests_failed/file_not_found.err
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error: hurl: cannot access 'does_not_exist.hurl': No such file or directory
1 change: 1 addition & 0 deletions integration/tests_failed/file_not_found.exit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
Empty file.
4 changes: 4 additions & 0 deletions integration/tests_failed/file_not_found.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
hurl does_not_exist.hurl

4 changes: 4 additions & 0 deletions integration/tests_failed/file_not_found.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -Eeuo pipefail
hurl does_not_exist.hurl

1 change: 1 addition & 0 deletions integration/tests_ok/hurlfmt.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GET http://localhost:8000/hello
4 changes: 4 additions & 0 deletions integration/tests_ok/hurlfmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -Eeuo pipefail
echo 'GET http://localhost:8000/hello' | hurlfmt --color

1 change: 1 addition & 0 deletions integration/tests_ok/multiple.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!Hello World!
4 changes: 4 additions & 0 deletions integration/tests_ok/multiple.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
hurl tests_ok\hello.hurl tests_ok\hello.hurl

4 changes: 4 additions & 0 deletions integration/tests_ok/multiple.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -Eeuo pipefail
hurl tests_ok/hello.hurl tests_ok/hello.hurl

1 change: 1 addition & 0 deletions integration/tests_ok/stdin.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
5 changes: 5 additions & 0 deletions integration/tests_ok/stdin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -Eeuo pipefail
echo "GET http://localhost:8000/hello" | hurl