@blinkhost/cli 2.7.0 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/README.md +514 -2
- package/dist/api.d.ts +2 -2
- package/dist/api.js +57 -8
- package/dist/assistant.js +6 -1
- package/dist/cli.js +157 -9
- package/dist/guidance.d.ts +1 -1
- package/dist/guidance.js +43 -11
- package/dist/javascript-runtime/resource-limits.js +3 -0
- package/dist/javascript-runtime/resource-profiles.js +2 -0
- package/dist/local-dev.d.ts +3 -0
- package/dist/local-dev.js +145 -0
- package/dist/local-development/README.md +410 -0
- package/dist/local-development/artifact-watch.mjs +36 -0
- package/dist/local-development/blinkhost.local.example.json +15 -0
- package/dist/local-development/frontend-proxy.mjs +189 -0
- package/dist/local-development/hello.rs +24 -0
- package/dist/local-development/local-stack.mjs +511 -0
- package/dist/local-development/native-database.mjs +37 -0
- package/dist/local-development/provider-fixtures.example.json +36 -0
- package/dist/local-development/provider-fixtures.mjs +203 -0
- package/dist/local-development/provider-grants.mjs +36 -0
- package/dist/local-development/test-provider-example.mjs +74 -0
- package/dist/local-source-build.d.ts +39 -0
- package/dist/local-source-build.js +225 -0
- package/dist/local-source-watch.d.ts +24 -0
- package/dist/local-source-watch.js +87 -0
- package/dist/local-test-command.d.ts +49 -0
- package/dist/local-test-command.js +128 -0
- package/dist/local-test-config.d.ts +37 -0
- package/dist/local-test-config.js +174 -0
- package/dist/local-test-gates.d.ts +71 -0
- package/dist/local-test-gates.js +247 -0
- package/dist/local-test-go-runner.d.ts +27 -0
- package/dist/local-test-go-runner.js +209 -0
- package/dist/local-test-node-arguments.d.ts +2 -0
- package/dist/local-test-node-arguments.js +14 -0
- package/dist/local-test-node-reporter.d.ts +7 -0
- package/dist/local-test-node-reporter.js +159 -0
- package/dist/local-test-results.d.ts +65 -0
- package/dist/local-test-results.js +163 -0
- package/dist/local-test-rust-runner.d.ts +18 -0
- package/dist/local-test-rust-runner.js +252 -0
- package/dist/local-test-starters.d.ts +3 -0
- package/dist/local-test-starters.js +157 -0
- package/dist/logs.d.ts +43 -0
- package/dist/logs.js +417 -0
- package/dist/module-build-diagnostics.d.ts +10 -0
- package/dist/module-build-diagnostics.js +208 -0
- package/dist/module-build.js +4 -0
- package/dist/release-preparation-diagnostics.d.ts +38 -0
- package/dist/release-preparation-diagnostics.js +204 -0
- package/dist/remote.js +14 -0
- package/dist/templates.d.ts +1 -0
- package/dist/templates.js +4 -1
- package/dist/test-credentials.d.ts +7 -0
- package/dist/test-credentials.js +236 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/workflows.d.ts +39 -2
- package/dist/workflows.js +241 -14
- package/package.json +4 -3
- package/test-runners/python-unittest.py +266 -0
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
"""Native Python unittest adapter for BlinkHost's local customer check protocol.
|
|
2
|
+
|
|
3
|
+
Invoke this file through a structured BlinkHost check, using ordinary unittest
|
|
4
|
+
arguments (for example, discover -s tests). No BlinkHost account services are used.
|
|
5
|
+
"""
|
|
6
|
+
from contextlib import redirect_stdout
|
|
7
|
+
import io
|
|
8
|
+
import json
|
|
9
|
+
import os
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
import re
|
|
12
|
+
import sys
|
|
13
|
+
import time
|
|
14
|
+
import unittest
|
|
15
|
+
import uuid
|
|
16
|
+
|
|
17
|
+
MAX_TESTS = 10_000
|
|
18
|
+
MAX_BYTES = 1_048_576
|
|
19
|
+
ERROR = "The BlinkHost Python unittest adapter could not qualify this runner result."
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class CoverageOutput(io.StringIO):
|
|
23
|
+
"""Bound the optional library's JSON output before retaining numeric totals."""
|
|
24
|
+
size = 0
|
|
25
|
+
|
|
26
|
+
def write(self, value):
|
|
27
|
+
self.size += len(value.encode("utf-8"))
|
|
28
|
+
if self.size > MAX_BYTES:
|
|
29
|
+
raise ValueError(ERROR)
|
|
30
|
+
return super().write(value)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def start_coverage(argv):
|
|
34
|
+
sources, remaining = [], [argv[0]]
|
|
35
|
+
root = Path.cwd().resolve()
|
|
36
|
+
for arg in argv[1:]:
|
|
37
|
+
if arg == "--coverage" or arg.startswith("--coverage="):
|
|
38
|
+
value = "." if arg == "--coverage" else arg.split("=", 1)[1]
|
|
39
|
+
source = (root / value).resolve()
|
|
40
|
+
if (not value or Path(value).is_absolute() or not source.is_relative_to(root)
|
|
41
|
+
or not source.is_dir() or len(sources) >= 16):
|
|
42
|
+
raise ValueError(ERROR)
|
|
43
|
+
sources.append(str(source))
|
|
44
|
+
else:
|
|
45
|
+
remaining.append(arg)
|
|
46
|
+
if not sources:
|
|
47
|
+
return None, {"status": "not_collected"}, remaining
|
|
48
|
+
collector = None
|
|
49
|
+
try:
|
|
50
|
+
# The application supplies this optional dependency. Do not install it,
|
|
51
|
+
# read ambient coverage configuration or write a coverage database.
|
|
52
|
+
import coverage
|
|
53
|
+
if not re.fullmatch(r"7\.\d+(?:\.\d+)?", coverage.__version__):
|
|
54
|
+
raise ValueError(ERROR)
|
|
55
|
+
collector = coverage.Coverage(source=list(dict.fromkeys(sources)), branch=True,
|
|
56
|
+
data_file=None, config_file=False, omit=[str(Path(__file__).resolve())])
|
|
57
|
+
collector.start()
|
|
58
|
+
except Exception:
|
|
59
|
+
if collector is not None:
|
|
60
|
+
try:
|
|
61
|
+
collector.stop()
|
|
62
|
+
except Exception:
|
|
63
|
+
pass
|
|
64
|
+
collector = None
|
|
65
|
+
return collector, {"status": "unavailable"}, remaining
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def coverage_summary(collector, fallback, rows):
|
|
69
|
+
if collector is None or not any(row["status"] in ("passed", "failed") for row in rows):
|
|
70
|
+
return fallback
|
|
71
|
+
try:
|
|
72
|
+
with CoverageOutput() as output:
|
|
73
|
+
with redirect_stdout(output):
|
|
74
|
+
collector.json_report(outfile="-", pretty_print=False)
|
|
75
|
+
totals = json.loads(output.getvalue())["totals"]
|
|
76
|
+
metrics = {name: {"covered": totals[covered], "total": totals[total]}
|
|
77
|
+
for name, covered, total in (("lines", "covered_lines", "num_statements"),
|
|
78
|
+
("branches", "covered_branches", "num_branches"))}
|
|
79
|
+
for metric in metrics.values():
|
|
80
|
+
if (any(type(value) is not int or not 0 <= value <= 1_000_000_000 for value in metric.values())
|
|
81
|
+
or metric["covered"] > metric["total"]):
|
|
82
|
+
raise ValueError(ERROR)
|
|
83
|
+
return {"status": "collected", "metrics": metrics}
|
|
84
|
+
except Exception:
|
|
85
|
+
# Coverage failure cannot erase real test outcomes or invent coverage.
|
|
86
|
+
return fallback
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def context():
|
|
90
|
+
path = os.environ.get("BLINKHOST_TEST_RESULT_PATH", "")
|
|
91
|
+
run_id = os.environ.get("BLINKHOST_TEST_RUN_ID", "")
|
|
92
|
+
check_id = os.environ.get("BLINKHOST_TEST_CHECK_ID", "")
|
|
93
|
+
if (not os.path.isabs(path) or not os.path.basename(path)
|
|
94
|
+
or os.environ.get("BLINKHOST_TEST_PROFILE") != "native"
|
|
95
|
+
or not re.fullmatch(r"[a-z0-9][a-z0-9_-]{0,63}", check_id)):
|
|
96
|
+
raise ValueError(ERROR)
|
|
97
|
+
if str(uuid.UUID(run_id)) != run_id:
|
|
98
|
+
raise ValueError(ERROR)
|
|
99
|
+
return path, run_id, check_id
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def safe_text(value, maximum, run_context):
|
|
103
|
+
value = str(value)
|
|
104
|
+
value = re.sub(r"\x1b\[[0-?]*[ -/]*[@-~]", "", value)
|
|
105
|
+
value = re.sub(r"[\x00-\x08\x0b-\x1f\x7f-\x9f\u202a-\u202e\u2066-\u2069]", "", value)
|
|
106
|
+
value = re.sub(r"-----BEGIN ([A-Z ]*PRIVATE KEY)-----[\s\S]*?(?:-----END \1-----|$)",
|
|
107
|
+
"[REDACTED PRIVATE KEY]", value)
|
|
108
|
+
value = re.sub(r"\b(Bearer|Basic)\s+[^\s\"'<>]+", r"\1 [REDACTED]", value, flags=re.I)
|
|
109
|
+
value = re.sub(r"([\"']?(?:authorization|password|passwd|secret|token|api[_-]?key|access[_-]?token|refresh[_-]?token)[\"']?\s*[:=]\s*)(?:\"[^\"\n]*\"|'[^'\n]*'|[^\s,;}]+)",
|
|
110
|
+
r"\1[REDACTED]", value, flags=re.I)
|
|
111
|
+
value = re.sub(r"(https?://)[^\s/@]+:[^\s/@]+@", r"\1[REDACTED]@", value, flags=re.I)
|
|
112
|
+
for secret in (run_context[0], os.path.dirname(run_context[0]), run_context[1]):
|
|
113
|
+
value = value.replace(secret, "[LOCAL TEST CONTEXT]")
|
|
114
|
+
# Screen before truncating, including complete credentials at the byte boundary.
|
|
115
|
+
return value.encode("utf-8", errors="replace")[:maximum].decode("utf-8", errors="ignore").strip() or "(unavailable)"
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class StructuredResult(unittest.TextTestResult):
|
|
119
|
+
def __init__(self, stream, descriptions, verbosity):
|
|
120
|
+
super().__init__(stream, descriptions, verbosity)
|
|
121
|
+
self.run_context = context()
|
|
122
|
+
self.rows = []
|
|
123
|
+
self.active = None
|
|
124
|
+
self.unrepresentable = False
|
|
125
|
+
|
|
126
|
+
def startTest(self, test):
|
|
127
|
+
if self.active is not None or len(self.rows) >= MAX_TESTS:
|
|
128
|
+
raise ValueError(ERROR)
|
|
129
|
+
if type(test).__module__ == "unittest.loader" and type(test).__name__ == "_FailedTest":
|
|
130
|
+
self.unrepresentable = True
|
|
131
|
+
self.active = {"test": test, "start": time.monotonic_ns(), "status": None,
|
|
132
|
+
"messages": [], "stack": None}
|
|
133
|
+
super().startTest(test)
|
|
134
|
+
|
|
135
|
+
def mark(self, test, status, message=None, error=None):
|
|
136
|
+
parent = getattr(test, "test_case", test)
|
|
137
|
+
if self.active is None or self.active["test"] is not parent:
|
|
138
|
+
# Class/module fixture and collection failures are command failures;
|
|
139
|
+
# they must never become fabricated application test records.
|
|
140
|
+
self.unrepresentable = True
|
|
141
|
+
return
|
|
142
|
+
current = self.active
|
|
143
|
+
if current["status"] != "failed":
|
|
144
|
+
current["status"] = status
|
|
145
|
+
if message is not None and len(current["messages"]) < 4:
|
|
146
|
+
current["messages"].append(safe_text(message, 1024, self.run_context))
|
|
147
|
+
if error is not None and current["stack"] is None:
|
|
148
|
+
current["stack"] = safe_text(self._exc_info_to_string(error, test), 16_384, self.run_context)
|
|
149
|
+
|
|
150
|
+
def addSuccess(self, test):
|
|
151
|
+
super().addSuccess(test)
|
|
152
|
+
self.mark(test, "passed")
|
|
153
|
+
|
|
154
|
+
def addFailure(self, test, err):
|
|
155
|
+
super().addFailure(test, err)
|
|
156
|
+
self.mark(test, "failed", str(err[1]), err)
|
|
157
|
+
|
|
158
|
+
def addError(self, test, err):
|
|
159
|
+
super().addError(test, err)
|
|
160
|
+
self.mark(test, "failed", str(err[1]), err)
|
|
161
|
+
|
|
162
|
+
def addSkip(self, test, reason):
|
|
163
|
+
super().addSkip(test, reason)
|
|
164
|
+
if getattr(test, "test_case", None) is not None:
|
|
165
|
+
# Subtests remain part of one declared parent test, matching testsRun.
|
|
166
|
+
if self.active is None or self.active["test"] is not test.test_case:
|
|
167
|
+
self.unrepresentable = True
|
|
168
|
+
else:
|
|
169
|
+
if self.active["status"] is None:
|
|
170
|
+
self.active["status"] = "skipped"
|
|
171
|
+
if len(self.active["messages"]) < 4:
|
|
172
|
+
self.active["messages"].append(safe_text("Skipped subtest: " + reason, 1024, self.run_context))
|
|
173
|
+
else:
|
|
174
|
+
self.mark(test, "skipped", reason)
|
|
175
|
+
|
|
176
|
+
def addExpectedFailure(self, test, err):
|
|
177
|
+
super().addExpectedFailure(test, err)
|
|
178
|
+
self.mark(test, "skipped", "Expected failure", err)
|
|
179
|
+
|
|
180
|
+
def addUnexpectedSuccess(self, test):
|
|
181
|
+
super().addUnexpectedSuccess(test)
|
|
182
|
+
self.mark(test, "failed", "Unexpected success of an expected-failure test")
|
|
183
|
+
|
|
184
|
+
def addSubTest(self, test, subtest, err):
|
|
185
|
+
super().addSubTest(test, subtest, err)
|
|
186
|
+
if err is not None:
|
|
187
|
+
self.mark(test, "failed", str(err[1]), err)
|
|
188
|
+
else:
|
|
189
|
+
self.mark(test, "passed")
|
|
190
|
+
|
|
191
|
+
def stopTest(self, test):
|
|
192
|
+
current = self.active
|
|
193
|
+
if current is None or current["test"] is not test:
|
|
194
|
+
raise ValueError(ERROR)
|
|
195
|
+
if current["status"] is None:
|
|
196
|
+
# unittest calls stopTest while a KeyboardInterrupt propagates. Keep
|
|
197
|
+
# cancellation intact and refuse an incomplete successful report.
|
|
198
|
+
self.unrepresentable = True
|
|
199
|
+
self.active = None
|
|
200
|
+
super().stopTest(test)
|
|
201
|
+
return
|
|
202
|
+
duration = (time.monotonic_ns() - current["start"]) // 1_000_000
|
|
203
|
+
if not 0 <= duration <= 1_000_000_000:
|
|
204
|
+
raise ValueError(ERROR)
|
|
205
|
+
row = {"id": "python-" + str(len(self.rows) + 1),
|
|
206
|
+
"name": safe_text(test.id(), 1024, self.run_context),
|
|
207
|
+
"status": current["status"], "duration_ms": duration}
|
|
208
|
+
if current["messages"]:
|
|
209
|
+
row["message"] = safe_text("\n".join(current["messages"]), 4096, self.run_context)
|
|
210
|
+
if current["stack"]:
|
|
211
|
+
row["stack"] = current["stack"]
|
|
212
|
+
self.rows.append(row)
|
|
213
|
+
self.active = None
|
|
214
|
+
super().stopTest(test)
|
|
215
|
+
|
|
216
|
+
def write_report(self, coverage=None):
|
|
217
|
+
if self.active is not None or self.unrepresentable or len(self.rows) != self.testsRun:
|
|
218
|
+
raise ValueError(ERROR)
|
|
219
|
+
if self.wasSuccessful() != (not any(row["status"] == "failed" for row in self.rows)):
|
|
220
|
+
raise ValueError(ERROR)
|
|
221
|
+
path, run_id, check_id = self.run_context
|
|
222
|
+
encoded = json.dumps({"schema": "blinkhost.local-test-results/v1", "run_id": run_id,
|
|
223
|
+
"check_id": check_id, "profile": "native",
|
|
224
|
+
"runner": "python:unittest/" + sys.version.split()[0], "tests": self.rows,
|
|
225
|
+
"coverage": coverage or {"status": "not_collected"}}, ensure_ascii=True).encode("utf-8")
|
|
226
|
+
if len(encoded) > MAX_BYTES:
|
|
227
|
+
raise ValueError(ERROR)
|
|
228
|
+
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_NOFOLLOW", 0)
|
|
229
|
+
with os.fdopen(os.open(path, flags, 0o600), "wb") as stream:
|
|
230
|
+
stream.write(encoded)
|
|
231
|
+
stream.flush()
|
|
232
|
+
os.fsync(stream.fileno())
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
class StructuredRunner(unittest.TextTestRunner):
|
|
236
|
+
def __init__(self, *args, **kwargs):
|
|
237
|
+
super().__init__(*args, resultclass=StructuredResult, **kwargs)
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def main():
|
|
241
|
+
try:
|
|
242
|
+
context() # Validate profile/context before importing any customer tests.
|
|
243
|
+
if sys.version_info < (3, 10):
|
|
244
|
+
raise ValueError(ERROR)
|
|
245
|
+
sys.path.insert(0, os.getcwd())
|
|
246
|
+
collector, coverage, argv = start_coverage(sys.argv)
|
|
247
|
+
try:
|
|
248
|
+
program = unittest.TestProgram(module=None, argv=argv, testRunner=StructuredRunner, exit=False)
|
|
249
|
+
finally:
|
|
250
|
+
if collector is not None:
|
|
251
|
+
try:
|
|
252
|
+
collector.stop()
|
|
253
|
+
except Exception:
|
|
254
|
+
collector = None
|
|
255
|
+
program.result.write_report(coverage_summary(collector, coverage, program.result.rows))
|
|
256
|
+
return 0 if program.result.wasSuccessful() else 1
|
|
257
|
+
except KeyboardInterrupt:
|
|
258
|
+
return 130
|
|
259
|
+
except (Exception, SystemExit):
|
|
260
|
+
# Do not expose result paths, raw parser exceptions or run identifiers.
|
|
261
|
+
print(ERROR, file=sys.stderr)
|
|
262
|
+
return 2
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
if __name__ == "__main__":
|
|
266
|
+
sys.exit(main())
|