reporting-labs 0.6.7 → 0.6.8

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/README.md CHANGED
@@ -303,7 +303,10 @@ Every option is optional. `npx reporting-labs init` writes them all, with commen
303
303
  | `title` | `'Test report'` | Title in the header |
304
304
  | `logo` | – | Your logo next to the title: `'logo.png'` (a file next to the config, embedded in the report) or an https URL |
305
305
  | `project` | – | `{ name, version, team, url }` shown under the title |
306
- | `metadata` | `{}` | Chips in the header, e.g. `{ env: 'staging', build: '#1842' }`. `build` labels the run in the trend; in CI the run number is used when it is not set |
306
+ | `metadata` | `{}` | Chips in the header, e.g. `{ env: 'staging', build: '#1842' }`. `build` labels the run in the trend; in CI the run number is used when it is not set. The environment name is also read from the process environment and wins over `env` here: `ENV`, `TEST_ENV`, `APP_ENV`, `TARGET_ENV` and friends, or any variable ending in `_ENV` / `_ENVIRONMENT` (`OPENCART_ENV`), so a config that says `local` still labels CI reports `dev`, `qa`, `stage` |
307
+ | `envVar` | – | Name of the variable that holds the environment name, when the detection cannot guess it |
308
+
309
+ **Runtime overrides.** `REPORTING_LABS_METADATA_<KEY>` sets a header chip from the environment (`REPORTING_LABS_METADATA_ENV=qa`, `REPORTING_LABS_METADATA_RELEASE=2.3`) and `REPORTING_LABS_TITLE`, `_THEME`, `_PALETTE`, `_ACCENT`, `_LOGO` the matching option; they win over the config. The env chip resolves in this order: `REPORTING_LABS_METADATA_ENV`, the variable `envVar` names, the conventional names (`ENV`, `TEST_ENV`, `APP_ENV`, `TARGET_ENV`, `CI_ENVIRONMENT_NAME`, anything ending in `_ENV`), then `metadata.env` in the config.
307
310
  | `env` | – | Extra rows on the Environment card |
308
311
  | `links` | `{}` | Turn meta keys into links. `{id}` is replaced by the value. An object `{ url, display }` builds the URL from several fields of an object passed to `meta()`, see below |
309
312
  | `maskKeys` | `[]` | Extra keys to mask as `****` |
@@ -54,3 +54,10 @@ export default class ReportingLabsReporter implements Reporter {
54
54
  private serializeStep;
55
55
  private serializeAttachment;
56
56
  }
57
+ /**
58
+ * The environment name the run was pointed at, from the process environment: the variable named
59
+ * by `envVar` if given, else the usual names (ENV, TEST_ENV, APP_ENV, TARGET_ENV, …), else any
60
+ * variable whose name ends in _ENV or _ENVIRONMENT (OPENCART_ENV, app_env). Every project names
61
+ * it differently; this finds it without being told. Undefined when nothing fits.
62
+ */
63
+ export declare function detectEnvName(env: NodeJS.ProcessEnv, envVar?: string): string | undefined;
package/dist/reporter.js CHANGED
@@ -33,6 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.detectEnvName = detectEnvName;
36
37
  const fs = __importStar(require("fs"));
37
38
  const path = __importStar(require("path"));
38
39
  const os = __importStar(require("os"));
@@ -56,7 +57,32 @@ class ReportingLabsReporter {
56
57
  this.masker = (0, mask_1.makeMasker)();
57
58
  this.globalErrors = [];
58
59
  this.globalOutput = [];
59
- this.options = options;
60
+ // The env chip. A runtime variable beats the config file, the same precedence as everything
61
+ // else: the ENV / TEST_ENV / ENVIRONMENT / APP_ENV a CI job exports wins over metadata.env in
62
+ // the config, so a config that says env: 'local' still labels the pipeline's reports dev, qa,
63
+ // stage without anyone touching it. The config value applies when no variable is set.
64
+ // Runtime overrides, highest precedence: REPORTING_LABS_METADATA_<KEY> sets a header chip
65
+ // (REPORTING_LABS_METADATA_ENV=qa, REPORTING_LABS_METADATA_RELEASE=2.3) and REPORTING_LABS_TITLE /
66
+ // _THEME / _PALETTE / _ACCENT / _LOGO the matching option, so a pipeline can label a run without
67
+ // touching the config. Same contract as the Java reporter's REPORTING_LABS_* variables.
68
+ const metadata = { ...(options.metadata ?? {}) };
69
+ for (const [k, v] of Object.entries(process.env)) {
70
+ const m = /^REPORTING_LABS_METADATA_([A-Z0-9_]+)$/.exec(k);
71
+ if (m && v && v.trim())
72
+ metadata[m[1].toLowerCase()] = v.trim();
73
+ }
74
+ // Below the explicit override, the environment name the run was pointed at, found by convention
75
+ // (ENV, TEST_ENV, APP_ENV, TARGET_ENV, anything ending in _ENV) or the variable `envVar` names.
76
+ // It beats metadata.env in the config: a runtime value wins over the file, as everywhere else.
77
+ if (!process.env.REPORTING_LABS_METADATA_ENV) {
78
+ const detected = detectEnvName(process.env, options.envVar);
79
+ if (detected)
80
+ metadata.env = detected;
81
+ }
82
+ const scalar = (name, current) => { const v = process.env['REPORTING_LABS_' + name]; return v && v.trim() ? v.trim() : current; };
83
+ this.options = { ...options, metadata,
84
+ title: scalar('TITLE', options.title), theme: scalar('THEME', options.theme), palette: scalar('PALETTE', options.palette),
85
+ accent: scalar('ACCENT', options.accent), logo: scalar('LOGO', options.logo) };
60
86
  this.masker = (0, mask_1.makeMasker)(options.maskKeys ?? [], { knownValues: options.maskValues ?? [], fromEnv: options.maskFromEnv !== false });
61
87
  }
62
88
  printsToStdio() { return false; }
@@ -595,6 +621,35 @@ class ReportingLabsReporter {
595
621
  }
596
622
  exports.default = ReportingLabsReporter;
597
623
  /** Run number from the CI system, used to label history entries when metadata.build is not set. */
624
+ /** Variables whose name ends in ENV but never hold an environment name. */
625
+ const NOT_AN_ENV_VAR = new Set(['GITHUB_ENV', 'NODE_ENV', 'BASH_ENV', 'VIRTUAL_ENV', 'CONDA_DEFAULT_ENV', 'RUNNER_ENVIRONMENT', 'PIPENV_ACTIVE', 'ZSH_ENV', 'JAVA_ENV', 'DOTNET_ENVIRONMENT', 'ASPNETCORE_ENVIRONMENT', 'HOSTING_ENVIRONMENT']);
626
+ /** Names every project seems to pick first, in order. */
627
+ const ENV_VAR_NAMES = ['ENV', 'TEST_ENV', 'ENVIRONMENT', 'APP_ENV', 'TARGET_ENV', 'RUN_ENV', 'DEPLOY_ENV', 'ENV_NAME', 'TEST_ENVIRONMENT', 'TARGET_ENVIRONMENT', 'CI_ENVIRONMENT_NAME', 'DEPLOYMENT_ENVIRONMENT', 'STAGE'];
628
+ /** dev, qa, stage-2, app_qa, prod-eu: a short token, never a path, URL or sentence. */
629
+ const looksLikeEnvName = (v) => !!v && /^[A-Za-z][\w.-]{0,31}$/.test(v.trim());
630
+ /**
631
+ * The environment name the run was pointed at, from the process environment: the variable named
632
+ * by `envVar` if given, else the usual names (ENV, TEST_ENV, APP_ENV, TARGET_ENV, …), else any
633
+ * variable whose name ends in _ENV or _ENVIRONMENT (OPENCART_ENV, app_env). Every project names
634
+ * it differently; this finds it without being told. Undefined when nothing fits.
635
+ */
636
+ function detectEnvName(env, envVar) {
637
+ const get = (name) => { const v = env[name] ?? env[name.toLowerCase()]; return looksLikeEnvName(v) ? v.trim() : undefined; };
638
+ if (envVar)
639
+ return get(envVar) ?? (looksLikeEnvName(env[envVar]) ? env[envVar].trim() : undefined);
640
+ for (const n of ENV_VAR_NAMES) {
641
+ const v = get(n);
642
+ if (v)
643
+ return v;
644
+ }
645
+ const wild = Object.keys(env).filter(k => /_(ENV|ENVIRONMENT|ENV_NAME)$/i.test(k) && !NOT_AN_ENV_VAR.has(k.toUpperCase())).sort();
646
+ for (const k of wild) {
647
+ const v = env[k];
648
+ if (looksLikeEnvName(v))
649
+ return v.trim();
650
+ }
651
+ return undefined;
652
+ }
598
653
  function ciRunLabel(env) {
599
654
  const n = env.GITHUB_RUN_NUMBER || env.BUILD_NUMBER || env.CI_PIPELINE_IID || env.CIRCLE_BUILD_NUM || env.BUILD_BUILDNUMBER || env.BITBUCKET_BUILD_NUMBER;
600
655
  return n ? `#${n}` : undefined;
package/dist/types.d.ts CHANGED
@@ -25,6 +25,8 @@ export interface ReportingLabsOptions {
25
25
  embedVideos?: boolean;
26
26
  /** Key/value shown in the header (env, branch, build...). `build` labels the run in the history; when it is not set the CI run number is used. */
27
27
  metadata?: Record<string, string>;
28
+ /** Name of the environment variable that holds the environment name, when it is not one the reporter finds by itself (ENV, TEST_ENV, APP_ENV, anything ending in _ENV). */
29
+ envVar?: string;
28
30
  /** Extra sections rendered below the summary. HTML is allowed. */
29
31
  sections?: Array<{
30
32
  title: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reporting-labs",
3
- "version": "0.6.7",
3
+ "version": "0.6.8",
4
4
  "description": "reportingLabs – beautiful, customizable single-file HTML test reports. One line of config. Playwright adapter included, more runners on the way.",
5
5
  "keywords": [
6
6
  "test-report",