@schwabyio/gta 0.13.0 → 0.14.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.
@@ -0,0 +1,234 @@
1
+ #!/usr/bin/env node
2
+ import { createRequire as __gtaCreateRequire } from 'node:module';
3
+ const require = __gtaCreateRequire(import.meta.url);
4
+ import {
5
+ failureDetails,
6
+ findCollection,
7
+ idOf,
8
+ isDirectory,
9
+ loadProject,
10
+ paint,
11
+ tallyOf
12
+ } from "./chunk-Q3VRXFS5.js";
13
+ import {
14
+ COLLECTIONS_DIR,
15
+ canonical,
16
+ redact,
17
+ resolveRequestSet,
18
+ runJob,
19
+ runSuite,
20
+ secretValues,
21
+ stepLabel,
22
+ stepLines
23
+ } from "./chunk-ZDHGCHIV.js";
24
+
25
+ // src/library/project.ts
26
+ import path from "node:path";
27
+ var OPTION_OF = { environmentType: "environment" };
28
+ var USE_FILE = ".gta-use.yml";
29
+ var openProject = async (folder, options = {}) => {
30
+ const root = await canonical(path.resolve(folder));
31
+ if (!await isDirectory(path.join(root, COLLECTIONS_DIR))) {
32
+ throw new Error(`${root} is not a Gravity project: it has no ${COLLECTIONS_DIR}/ folder.`);
33
+ }
34
+ const project = await loadProject({
35
+ root,
36
+ env: process.env,
37
+ overrides: overridesOf(options),
38
+ overrideLabel: (key) => `the ${OPTION_OF[key] ?? key} option`,
39
+ flagValues: options.flags ?? {},
40
+ optionalSettings: true
41
+ });
42
+ return projectOf(project);
43
+ };
44
+ function overridesOf(options) {
45
+ return {
46
+ ...options.environment !== void 0 ? { environmentType: options.environment } : {},
47
+ ...options.bail !== void 0 ? { bail: String(options.bail) } : {},
48
+ ...options.timeoutCollection !== void 0 ? { timeoutCollection: String(options.timeoutCollection) } : {}
49
+ };
50
+ }
51
+ function projectOf(project) {
52
+ const { root, settings } = project;
53
+ const environment = settings.environmentType;
54
+ const flags = project.flags.values;
55
+ return {
56
+ root,
57
+ name: project.name,
58
+ environment,
59
+ environments: project.environments,
60
+ flags,
61
+ collections: project.collections.map(idOf),
62
+ warnings: project.warnings,
63
+ async run(name, options = {}) {
64
+ const collection = findCollection(project.collections, name);
65
+ if (!collection) throw new Error(noSuchCollection(name, project));
66
+ checkValues(options.vars, "vars");
67
+ const steps = options.steps && collection.problems.length === 0 ? pickSteps(collection, options.steps) : null;
68
+ const { onResult } = options;
69
+ const file = collection.path;
70
+ const run = deadline(options.signal, settings.timeoutCollection);
71
+ const produced = /* @__PURE__ */ new Map();
72
+ const started = performance.now();
73
+ const outcome = await runJob(
74
+ {
75
+ file,
76
+ projectRoot: root,
77
+ environment,
78
+ steps,
79
+ bail: options.bail ?? settings.bail,
80
+ flags,
81
+ ...options.vars ? { vars: options.vars } : {}
82
+ },
83
+ {
84
+ signal: run.signal,
85
+ produced,
86
+ ...onResult ? { onResult: (result, ref) => onResult(result, { file, ...ref }) } : {}
87
+ }
88
+ );
89
+ return outcomeOf({
90
+ id: idOf(collection),
91
+ file,
92
+ stepCount: steps?.length ?? collection.doc.steps.length,
93
+ outcome,
94
+ steps: outcome.ok ? outcome.steps.map((ref) => ({ file, ...ref })) : [],
95
+ produced,
96
+ stopped: run.stopped(),
97
+ durationMs: performance.now() - started
98
+ });
99
+ },
100
+ async use(set, params = {}, options = {}) {
101
+ checkValues(params, "params");
102
+ checkValues(options.vars, "vars");
103
+ const found = await resolveRequestSet(root, project.global, set);
104
+ const collectionPath = path.join(root, COLLECTIONS_DIR, USE_FILE);
105
+ const collection = { steps: [{ use: set, with: params }] };
106
+ const context = {
107
+ collectionPath,
108
+ environmentName: environment,
109
+ flags,
110
+ dataRow: null,
111
+ collectionVars: null
112
+ };
113
+ const secrets = await secretValues(context);
114
+ const lines = (await stepLines(found.path)).steps;
115
+ const shown = (result) => {
116
+ const hidden = redact(result, secrets);
117
+ return { ...hidden, item: { ...hidden.item, path: found.path } };
118
+ };
119
+ const steps = [];
120
+ const run = deadline(options.signal, settings.timeoutCollection);
121
+ const produced = /* @__PURE__ */ new Map();
122
+ const started = performance.now();
123
+ let outcome;
124
+ try {
125
+ const summary = await runSuite({
126
+ collection,
127
+ collectionPath,
128
+ context,
129
+ bail: options.bail ?? settings.bail,
130
+ signal: run.signal,
131
+ produced,
132
+ ...options.vars ? { vars: options.vars } : {},
133
+ onResult: (_index, result) => {
134
+ const child = result.use?.child ?? 0;
135
+ const ref = { file: found.path, line: lines[child] ?? null, index: child };
136
+ steps.push(ref);
137
+ options.onResult?.(shown(result), ref);
138
+ }
139
+ });
140
+ outcome = {
141
+ ok: true,
142
+ summary: { ...summary, results: summary.results.map(shown) },
143
+ steps,
144
+ data: null
145
+ };
146
+ } catch (cause) {
147
+ outcome = { ok: false, message: cause instanceof Error ? cause.message : String(cause) };
148
+ }
149
+ return outcomeOf({
150
+ id: set,
151
+ file: found.path,
152
+ stepCount: found.doc.steps.length,
153
+ outcome,
154
+ steps,
155
+ produced,
156
+ stopped: run.stopped(),
157
+ durationMs: performance.now() - started
158
+ });
159
+ }
160
+ };
161
+ }
162
+ function deadline(signal, timeoutMs) {
163
+ const timeout = AbortSignal.timeout(timeoutMs);
164
+ return {
165
+ signal: signal ? AbortSignal.any([signal, timeout]) : timeout,
166
+ /** Why the run stopped before it finished, or null when nothing stopped it. */
167
+ stopped: () => timeout.aborted ? `Timed out after ${timeoutMs} ms (timeoutCollection)` : signal?.aborted ? "The run was cancelled" : null
168
+ };
169
+ }
170
+ function outcomeOf(run) {
171
+ const { outcome } = run;
172
+ const counted = tallyOf(run.id, run.stepCount, outcome, run.durationMs);
173
+ const tally = run.stopped && outcome.ok ? { ...counted, passed: false, error: run.stopped } : counted;
174
+ return {
175
+ passed: tally.passed,
176
+ id: run.id,
177
+ file: run.file,
178
+ error: tally.error,
179
+ summary: outcome.ok ? outcome.summary : {
180
+ total: run.stepCount,
181
+ passed: 0,
182
+ failed: 0,
183
+ errored: 0,
184
+ skipped: run.stepCount,
185
+ durationMs: run.durationMs,
186
+ results: []
187
+ },
188
+ steps: run.steps,
189
+ values: Object.fromEntries(run.produced),
190
+ failures: tally.passed ? "" : failureDetails([tally], paint(false)).join("\n").trimEnd()
191
+ };
192
+ }
193
+ function pickSteps(collection, selectors) {
194
+ const id = idOf(collection);
195
+ const { steps } = collection.doc;
196
+ if (selectors.length === 0) {
197
+ throw new Error(`steps is empty: name a step of ${id}, or leave steps out to run them all`);
198
+ }
199
+ const picked = /* @__PURE__ */ new Set();
200
+ for (const selector of selectors) {
201
+ if (typeof selector === "number") {
202
+ if (!Number.isInteger(selector) || selector < 1 || selector > steps.length) {
203
+ throw new Error(`${id} has no step ${selector}: its steps are 1 to ${steps.length}`);
204
+ }
205
+ picked.add(selector - 1);
206
+ continue;
207
+ }
208
+ const matching = steps.flatMap((step, index) => stepLabel(step) === selector ? [index] : []);
209
+ if (matching.length === 0) {
210
+ const names = steps.map((step) => `"${stepLabel(step)}"`).join(", ");
211
+ throw new Error(`${id} has no step called "${selector}". Its steps: ${names}`);
212
+ }
213
+ for (const index of matching) picked.add(index);
214
+ }
215
+ return [...picked].sort((a, b) => a - b);
216
+ }
217
+ function noSuchCollection(name, project) {
218
+ const ids = project.collections.map(idOf);
219
+ const folder = project.collections.some((c) => c.directory === name.replace(/\/+$/, ""));
220
+ const shown = ids.length > 30 ? `${ids.slice(0, 30).join(", ")}, \u2026` : ids.join(", ");
221
+ return (folder ? `"${name}" is a folder: run its collections one at a time.` : `No collection is called "${name}".`) + (ids.length > 0 ? ` This project has: ${shown}` : ` This project has no collections.`);
222
+ }
223
+ function checkValues(values, what) {
224
+ for (const [name, value] of Object.entries(values ?? {})) {
225
+ const kind = typeof value;
226
+ if (value !== null && kind !== "string" && kind !== "number" && kind !== "boolean") {
227
+ throw new TypeError(`${what}.${name} must be a string, number, boolean or null`);
228
+ }
229
+ }
230
+ }
231
+
232
+ export {
233
+ openProject
234
+ };