forked from aws/jsii
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_invoke_bin.py
More file actions
132 lines (107 loc) · 4.44 KB
/
Copy pathtest_invoke_bin.py
File metadata and controls
132 lines (107 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from os import environ
import platform
import subprocess
from datetime import datetime
import pytest
@pytest.fixture()
def silence_node_deprecation_warnings():
"""Ensures @jsii/check-node does not emit any warning that would cause the
test output assertions to be invalidated.
"""
variables = [
"JSII_SILENCE_WARNING_KNOWN_BROKEN_NODE_VERSION",
"JSII_SILENCE_WARNING_UNTESTED_NODE_VERSION",
"JSII_SILENCE_WARNING_DEPRECATED_NODE_VERSION",
"JSII_SILENCE_WARNING_END_OF_LIFE_NODE_VERSION",
]
store = {var: environ.get(var, "") for var in variables}
for var in variables:
environ[var] = "1"
# silence this for the next decades
environ["JSII_SILENCE_WARNING_END_OF_LIFE_NODE_VERSION"] = (
"14,16,18,20,22,24,26,28,30,32,34"
)
# Execute the test
yield
for var in variables:
environ[var] = store[var]
@pytest.fixture()
def disable_jsii_runtime_package_cache():
"""Disable the jsii runtime cache because it is problematic with InvokeBinScript."""
environ["JSII_RUNTIME_PACKAGE_CACHE"] = "disabled"
class TestInvokeBinScript:
@pytest.mark.skipif(
platform.system() == "Windows",
reason="jsii-pacmak does not generate windows scripts",
)
def test_invoke_script(
self, silence_node_deprecation_warnings, disable_jsii_runtime_package_cache
) -> None:
script_path = f".env/bin/calc"
result = subprocess.run([script_path], capture_output=True)
assert result.returncode == 0
assert result.stdout == b"Hello World!\n"
@pytest.mark.skipif(
platform.system() == "Windows",
reason="jsii-pacmak does not generate windows scripts",
)
def test_invoke_script_with_args(
self, silence_node_deprecation_warnings, disable_jsii_runtime_package_cache
) -> None:
script_path = f".env/bin/calc"
result = subprocess.run([script_path, "arg1", "arg2"], capture_output=True)
assert result.returncode == 0
assert result.stdout == b"Hello World!\n arguments: arg1, arg2\n"
@pytest.mark.skipif(
platform.system() == "Windows",
reason="jsii-pacmak does not generate windows scripts",
)
def test_invoke_script_with_failure(
self, silence_node_deprecation_warnings, disable_jsii_runtime_package_cache
) -> None:
script_path = f".env/bin/calc"
result = subprocess.run([script_path, "arg1", "fail"], capture_output=True)
assert result.returncode == 3
assert result.stdout == b"Hello World!\n arguments: arg1, fail\n"
assert result.stderr.startswith(b"error message to stderr\n")
@pytest.mark.skipif(
platform.system() == "Windows",
reason="jsii-pacmak does not generate windows scripts",
)
def test_invoke_script_with_line_flush(
self, silence_node_deprecation_warnings, disable_jsii_runtime_package_cache
) -> None:
"""Make sure lines are flushed immediately as they are generated, rather than
buffered to the end
"""
script_path = f".env/bin/calc"
proc = subprocess.Popen([script_path, "arg1", "delay"], stdout=subprocess.PIPE)
timed_lines = []
last_dt = None
if proc.stdout is not None:
for line in iter(proc.stdout.readline, b""):
last_dt = datetime.utcnow() if last_dt is None else last_dt
current_dt = datetime.utcnow()
delay_s = round((current_dt - last_dt).total_seconds())
timed_lines.append((line, delay_s))
last_dt = current_dt
exit_code = proc.wait()
assert exit_code == 0
expected_lines = [
b"Hello World!\n",
b" arguments: arg1, delay\n",
b"sleeping 1s 1\n",
b"sleeping 1s 2\n",
b"sleeping 1s 3\n",
b"sleeping 1s 4\n",
b"sleeping 1s 5\n",
]
# Check we got the right lines
assert [item[0] for item in timed_lines] == expected_lines
# Check that we didn't receive all lines at once (which would happen if they were buffered)
# The first 3 lines are printed immediately, so their delays should be around 0.
# The subsequent lines should have delays > 0 since they sleep for 1s.
for line, delay in timed_lines[3:]:
assert (
int(delay) >= 1
), f"Expected line {line!r} to be delayed, but got delay of {delay}s"