@maniumek/suwgate 0.3.0 → 0.5.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.
Files changed (53) hide show
  1. package/README.md +112 -20
  2. package/dist/cli.js +38 -3
  3. package/dist/dev/db.d.ts +1 -1
  4. package/dist/dev/db.js +25 -0
  5. package/dist/dev/logs.js +9 -1
  6. package/dist/dev/settings.d.ts +2 -0
  7. package/dist/dev/settings.js +5 -2
  8. package/dist/eslint/index.js +4 -1
  9. package/dist/gate/check.d.ts +10 -0
  10. package/dist/gate/check.js +91 -19
  11. package/dist/gate/config.d.ts +12 -0
  12. package/dist/gate/config.js +87 -2
  13. package/dist/gate/contract.d.ts +6 -0
  14. package/dist/gate/guardsStep.d.ts +3 -1
  15. package/dist/gate/guardsStep.js +14 -3
  16. package/dist/gate/plan.d.ts +20 -1
  17. package/dist/gate/plan.js +89 -22
  18. package/dist/gate/report.d.ts +1 -0
  19. package/dist/gate/report.js +12 -3
  20. package/dist/gate/repository.d.ts +1 -0
  21. package/dist/gate/repository.js +8 -0
  22. package/dist/gate/runner.js +18 -3
  23. package/dist/guards/comments/hash.js +22 -1
  24. package/dist/guards/dotnet/appsettingsBound.js +29 -2
  25. package/dist/guards/repository/humanDocs.d.ts +2 -0
  26. package/dist/guards/repository/humanDocs.js +87 -0
  27. package/dist/guards/repository/imagePins.js +1 -1
  28. package/dist/guards/repository/index.js +4 -0
  29. package/dist/guards/repository/justModules.d.ts +1 -0
  30. package/dist/guards/repository/justModules.js +29 -11
  31. package/dist/guards/repository/noRawColors.js +1 -1
  32. package/dist/guards/repository/scriptsCalled.js +38 -11
  33. package/dist/guards/repository/testTiming.d.ts +2 -0
  34. package/dist/guards/repository/testTiming.js +198 -0
  35. package/dist/profiles/contract.js +81 -38
  36. package/dist/profiles/criteriaRan.d.ts +1 -0
  37. package/dist/profiles/criteriaRan.js +44 -1
  38. package/dist/profiles/database.js +11 -4
  39. package/dist/profiles/dotnet.d.ts +1 -0
  40. package/dist/profiles/dotnet.js +28 -5
  41. package/dist/profiles/dotnetGuards.js +17 -1
  42. package/dist/profiles/library.js +4 -1
  43. package/dist/profiles/node.js +1 -0
  44. package/dist/profiles/presets/knip.d.ts +1 -0
  45. package/dist/profiles/presets/knip.js +4 -2
  46. package/dist/profiles/presets/playwright.d.ts +3 -1
  47. package/dist/profiles/presets/playwright.js +7 -1
  48. package/dist/profiles/shell.d.ts +3 -1
  49. package/dist/profiles/shell.js +22 -7
  50. package/dist/profiles/verdict.js +6 -2
  51. package/dist/profiles/web.js +24 -5
  52. package/package.json +1 -1
  53. package/schema/suwgate.schema.json +62 -3
@@ -6,6 +6,7 @@ interface TestSource {
6
6
  report: string;
7
7
  patterns: string[];
8
8
  skipped?: string;
9
+ excludeTraits?: string[];
9
10
  }
10
11
  export interface CriteriaRanInput {
11
12
  root: string;
@@ -2,6 +2,7 @@ import { execFileSync } from "node:child_process";
2
2
  import { readFileSync } from "node:fs";
3
3
  import path from "node:path";
4
4
  import { CRITERIA_FILE, criteriaConfig } from "./criteria.js";
5
+ import { excludedTraits } from "./dotnet.js";
5
6
  import { readRun } from "./reports.js";
6
7
  import { quoted, read, tracked, verdictCommand } from "./shell.js";
7
8
  const REPORTED = {
@@ -24,6 +25,39 @@ export function names(text, id, format) {
24
25
  return new RegExp(`Trait\\(\\s*"criterion"\\s*,\\s*"${id.replace(/[.]/g, "\\.")}"\\s*\\)`).test(text);
25
26
  return ['"', "'", "`"].some((quote) => text.includes(`${quote}${id}:`));
26
27
  }
28
+ const ATTRIBUTE = /^\s*\[/;
29
+ const TYPE = /\b(class|record|struct)\s+\w+/;
30
+ function attributesAround(lines, index) {
31
+ let first = index;
32
+ while (first > 0 && ATTRIBUTE.test(lines[first - 1] ?? ""))
33
+ first--;
34
+ let last = index;
35
+ while (last + 1 < lines.length && ATTRIBUTE.test(lines[last + 1] ?? ""))
36
+ last++;
37
+ return lines.slice(first, last + 1).join("\n");
38
+ }
39
+ function escaped(text) {
40
+ return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
41
+ }
42
+ function hasTrait(attributes, trait) {
43
+ const [name = "", value = ""] = trait.split("=");
44
+ return new RegExp(`Trait\\(\\s*"${escaped(name)}"\\s*,\\s*"${escaped(value)}"\\s*\\)`).test(attributes);
45
+ }
46
+ function typeAttributes(lines, index) {
47
+ const typeLine = lines.slice(0, index).findLastIndex((line) => TYPE.test(line));
48
+ return typeLine > 0 && ATTRIBUTE.test(lines[typeLine - 1] ?? "") ? attributesAround(lines, typeLine - 1) : "";
49
+ }
50
+ function outsideTheGate(text, id, traits) {
51
+ if (traits.length === 0)
52
+ return false;
53
+ const lines = text.split("\n");
54
+ const naming = lines.flatMap((line, index) => (names(line, id, "xunit") ? [index] : []));
55
+ return (naming.length > 0 &&
56
+ naming.every((index) => {
57
+ const attributes = `${attributesAround(lines, index)}\n${typeAttributes(lines, index)}`;
58
+ return traits.some((trait) => hasTrait(attributes, trait));
59
+ }));
60
+ }
27
61
  function proven(passed, id) {
28
62
  return passed.some((title) => title.startsWith(`${id}:`) || title.includes(` ${id}:`));
29
63
  }
@@ -54,10 +88,18 @@ export function criteriaRan(input) {
54
88
  files: listed(input.root, source.patterns).map((file) => ({ file, text: readFileSync(path.join(input.root, file), "utf8") })),
55
89
  }));
56
90
  return ids.flatMap((id) => {
57
- const naming = texts.flatMap(({ source, files }) => files.filter((file) => names(file.text, id, source.format)).map((file) => ({ source, file: file.file })));
91
+ const naming = texts.flatMap(({ source, files }) => files
92
+ .filter((file) => names(file.text, id, source.format))
93
+ .map((file) => ({ source, file: file.file, outside: outsideTheGate(file.text, id, source.excludeTraits ?? []) })));
58
94
  const checked = naming.filter((entry) => entry.source.skipped === undefined);
59
95
  if (checked.length === 0)
60
96
  return [];
97
+ if (checked.every((entry) => entry.outside)) {
98
+ const traits = [...new Set(checked.flatMap((entry) => entry.source.excludeTraits ?? []))].join(", ");
99
+ return [
100
+ `${id}: named only by tests outside the gate (${traits}, excludeTraits of dotnet-test in suwgate.yml): ${checked.map((entry) => entry.file).join(", ")}. The gate never runs them, so nothing in it proves the criterion: add a test the gate runs that names it, with a stand-in for the outside service (a fake model or server), and keep the outside test for the real one`,
101
+ ];
102
+ }
61
103
  if (checked.some((entry) => proven(runs.get(entry.source.step)?.passed ?? [], id)))
62
104
  return [];
63
105
  const unreported = [...new Set(checked.filter((entry) => runs.get(entry.source.step) === null).map((entry) => entry.source.step))];
@@ -92,6 +134,7 @@ export function criteriaRanInput(context) {
92
134
  report: path.posix.join(reported.step, REPORT_FILE[reported.format]),
93
135
  patterns: kind.paths,
94
136
  ...(context.skippedSteps.includes(reported.step) ? { skipped: "skipped in suwgate.yml" } : {}),
137
+ ...(reported.format === "xunit" && excludedTraits(context).length > 0 ? { excludeTraits: excludedTraits(context) } : {}),
95
138
  },
96
139
  ];
97
140
  });
@@ -1,23 +1,30 @@
1
- import { has, script } from "./shell.js";
1
+ import { databaseUpCommand, has, script, suwgate } from "./shell.js";
2
2
  export const DATABASE_UP = "database-up";
3
3
  const COMPOSE_FILES = ["compose.yaml", "compose.yml", "docker-compose.yaml", "docker-compose.yml"];
4
+ const IN_CI = 'if [ -n "${CI:-}" ]; then echo "CI: the workflow starts the database (database: true of the reusable gate workflow)"; exit 0; fi';
4
5
  const UP = script([
5
- 'if [ -n "${CI:-}" ]; then echo "CI: the workflow starts the database (database: true of the reusable gate workflow)"; exit 0; fi',
6
+ IN_CI,
6
7
  "docker compose --profile db up -d --wait db >/dev/null 2>&1 || {",
7
8
  ' echo "postgres did not start — see: docker compose --profile db logs db" >&2',
8
9
  " exit 1",
9
10
  "}",
10
11
  ]);
12
+ const OWN_UP = script([IN_CI, `${suwgate("dev db up")} >/dev/null`]);
13
+ function databaseUp(context) {
14
+ if (databaseUpCommand(context) !== null)
15
+ return OWN_UP;
16
+ return composeFile(context) ? UP : null;
17
+ }
11
18
  export function composeFile(context) {
12
19
  return COMPOSE_FILES.some((file) => has(context, file));
13
20
  }
14
21
  export const databaseSteps = [
15
22
  {
16
23
  id: DATABASE_UP,
17
- title: "the local database for the tests (docker compose --profile db up --wait db; in CI the workflow starts it)",
24
+ title: "the local database for the tests (docker compose --profile db up --wait db, or dev.database.up of suwgate.yml; in CI the workflow starts it)",
18
25
  profile: "database",
19
26
  needs: ["docker"],
20
- run: (context) => (composeFile(context) ? UP : null),
27
+ run: databaseUp,
21
28
  limitSeconds: 120,
22
29
  },
23
30
  ];
@@ -2,6 +2,7 @@ import type { Step, StepContext } from "../gate/contract.ts";
2
2
  export declare const NO_IDLE_BUILD_PROCESSES = "export MSBUILDDISABLENODEREUSE=1 DOTNET_CLI_USE_MSBUILD_SERVER=0";
3
3
  export declare const DOTNET_BUILD = "dotnet-build";
4
4
  export declare const DOTNET_TEST = "dotnet-test";
5
+ export declare function excludedTraits(context: StepContext): string[];
5
6
  export declare function inCheckoutEnv(context: StepContext, command: string): string;
6
7
  export declare function guardTests(context: StepContext): string | null;
7
8
  export declare function witnessed(context: StepContext, tests: string): string;
@@ -1,6 +1,6 @@
1
1
  import { DATABASE_UP } from "./database.js";
2
2
  import { requiredGuards } from "./dotnetGuards.js";
3
- import { CHECKOUT_ENV, changedIn, hasDevSection, quoted, read, runDirectory, tracked, verdictCommand, withChanged } from "./shell.js";
3
+ import { CHECKOUT_ENV, changedIn, hasDevSection, quoted, read, runDirectory, settingsOf, tracked, verdictCommand, withChanged, } from "./shell.js";
4
4
  const DOTNET_INPUT = /\.(cs|csproj|props|targets|slnx|sln|rsp)$|(^|\/)(\.editorconfig|BannedSymbols\.txt|global\.json|packages\.lock\.json|nuget\.config)$/;
5
5
  const GUARD_PACKAGE = /<(PackageReference|ProjectReference)\b[^>]*(?:Include|Update)="[^"]*Suwgate\.(Architecture|Database)\b/;
6
6
  const TEST_PROJECT = /<PackageReference\b[^>]*Include="xunit\.v3"|<IsTestProject>true<\/IsTestProject>/;
@@ -10,6 +10,27 @@ const TEST_OPTIONS = '--no-build --no-progress --zero-tests-policy strict --repo
10
10
  const FORMAT = `${NO_IDLE_BUILD_PROCESSES} && dotnet tool restore >/dev/null`;
11
11
  export const DOTNET_BUILD = "dotnet-build";
12
12
  export const DOTNET_TEST = "dotnet-test";
13
+ const TRAIT = "^[^=\\s]+=[^=\\s]+$";
14
+ const DOTNET_TEST_SETTINGS = {
15
+ type: "object",
16
+ additionalProperties: false,
17
+ properties: {
18
+ excludeTraits: {
19
+ description: "Traits (name=value) of the tests outside the gate, e.g. live=true for tests that call a real model: dotnet-test leaves them out (--filter-not-trait), and criteria-ran says so when a criterion is named only by them.",
20
+ type: "array",
21
+ minItems: 1,
22
+ uniqueItems: true,
23
+ items: { type: "string", pattern: TRAIT },
24
+ },
25
+ },
26
+ };
27
+ export function excludedTraits(context) {
28
+ return settingsOf(context, DOTNET_TEST).excludeTraits ?? [];
29
+ }
30
+ function testOptions(context) {
31
+ const traits = excludedTraits(context);
32
+ return traits.length === 0 ? TEST_OPTIONS : `${TEST_OPTIONS} --filter-not-trait ${traits.map(quoted).join(" ")}`;
33
+ }
13
34
  function dotnetChanged(context) {
14
35
  return changedIn(context, DOTNET_INPUT).length > 0;
15
36
  }
@@ -27,7 +48,7 @@ export function guardTests(context) {
27
48
  });
28
49
  if (projects.length === 0)
29
50
  return null;
30
- return projects.map((project) => `dotnet test --project ${quoted(project)} ${TEST_OPTIONS}`).join(" && ");
51
+ return projects.map((project) => `dotnet test --project ${quoted(project)} ${testOptions(context)}`).join(" && ");
31
52
  }
32
53
  export function witnessed(context, tests) {
33
54
  return [
@@ -35,7 +56,7 @@ export function witnessed(context, tests) {
35
56
  'export SUWGATE_EVIDENCE="$run/evidence"',
36
57
  tests,
37
58
  verdictCommand(`tests xunit "$run/results" ${quoted("dotnet test")}`),
38
- verdictCommand(`guards "$run/evidence" ${requiredGuards(context).map(quoted).join(" ")}`),
59
+ `{ [ $# -gt 0 ] || ${verdictCommand(`guards "$run/evidence" ${requiredGuards(context).map(quoted).join(" ")}`)}; }`,
39
60
  ].join(" && ");
40
61
  }
41
62
  export const dotnetSteps = [
@@ -62,15 +83,17 @@ export const dotnetSteps = [
62
83
  },
63
84
  {
64
85
  id: DOTNET_TEST,
65
- title: "tests (dotnet test, xUnit v3, with the checkout's databases when suwgate.yml has dev; fails on 0 tests run and unless every .NET guard of the profiles ran; --quick: the projects that test with Suwgate.Architecture or Suwgate.Database)",
86
+ title: "tests (dotnet test, xUnit v3, with the checkout's databases when suwgate.yml has dev; fails on 0 tests run and, without arguments, unless every .NET guard of the profiles ran; --quick: the projects that test with Suwgate.Architecture or Suwgate.Database)",
66
87
  profile: "dotnet",
67
88
  needs: ["dotnet"],
68
89
  after: [DOTNET_BUILD, DATABASE_UP],
69
- run: (context) => inCheckoutEnv(context, witnessed(context, `dotnet test ${TEST_OPTIONS}`)),
90
+ run: (context) => inCheckoutEnv(context, witnessed(context, `dotnet test ${testOptions(context)} "$@"`)),
70
91
  quick: (context) => {
71
92
  const command = guardTests(context);
72
93
  return command === null ? null : inCheckoutEnv(context, witnessed(context, command));
73
94
  },
95
+ args: "dotnet test, xUnit v3 on Microsoft.Testing.Platform",
96
+ settingsSchema: DOTNET_TEST_SETTINGS,
74
97
  limitSeconds: 600,
75
98
  },
76
99
  ];
@@ -13,8 +13,24 @@ const ARCHITECTURE = [
13
13
  "no-fixture-in-field",
14
14
  "build-settings",
15
15
  ];
16
+ const ARCHITECTURE_SETTINGS = {
17
+ "no-escape-hatches": {
18
+ type: "object",
19
+ additionalProperties: false,
20
+ properties: {
21
+ nullForgivingInTests: {
22
+ type: "boolean",
23
+ description: "Allow the null-forgiving ! in the sources of test projects (folders ending in .Tests); dynamic stays banned there.",
24
+ },
25
+ },
26
+ },
27
+ };
16
28
  export const dotnetGuards = [
17
- ...ARCHITECTURE.map((name) => ({ id: `architecture/${name}`, profile: "dotnet" })),
29
+ ...ARCHITECTURE.map((name) => ({
30
+ id: `architecture/${name}`,
31
+ profile: "dotnet",
32
+ ...(ARCHITECTURE_SETTINGS[name] === undefined ? {} : { settingsSchema: ARCHITECTURE_SETTINGS[name] }),
33
+ })),
18
34
  {
19
35
  id: "database/migration-script-names",
20
36
  profile: "database",
@@ -47,7 +47,7 @@ export function visual(context) {
47
47
  return [
48
48
  "pnpm exec storybook build --quiet -o storybook-static >/dev/null",
49
49
  runDirectory("visual"),
50
- `docker run --rm --init --ipc=host --security-opt label=disable -v "$PWD:/work" -v "$run:/suwgate-run" -w /work -e CI -e PLAYWRIGHT_JSON_OUTPUT_FILE=/suwgate-run/playwright.json mcr.microsoft.com/playwright:v${version}-noble node_modules/.bin/playwright test -c ${VISUAL_CONFIG} --reporter=line,json`,
50
+ `docker run --rm --init --ipc=host --security-opt label=disable -v "$PWD:/work" -v "$run:/suwgate-run" -w /work -e CI -e PLAYWRIGHT_JSON_OUTPUT_FILE=/suwgate-run/playwright.json mcr.microsoft.com/playwright:v${version}-noble node_modules/.bin/playwright test -c ${VISUAL_CONFIG} --reporter=line,json "$@"`,
51
51
  verdictCommand(`tests playwright "$run/playwright.json" ${quoted("playwright (visual)")}`),
52
52
  ].join(" && ");
53
53
  }
@@ -84,6 +84,7 @@ export const librarySteps = [
84
84
  needs: ["pnpm"],
85
85
  after,
86
86
  run: stories,
87
+ args: "vitest run --project stories",
87
88
  limitSeconds: 600,
88
89
  },
89
90
  {
@@ -111,6 +112,7 @@ export const librarySteps = [
111
112
  needs: ["pnpm", "node"],
112
113
  after: ["attw"],
113
114
  run: consumerApp,
115
+ args: "playwright test",
114
116
  limitSeconds: 900,
115
117
  },
116
118
  {
@@ -120,6 +122,7 @@ export const librarySteps = [
120
122
  needs: ["pnpm", "docker"],
121
123
  after,
122
124
  run: visual,
125
+ args: "playwright test, in the Playwright image",
123
126
  limitSeconds: 900,
124
127
  },
125
128
  {
@@ -68,6 +68,7 @@ export function nodeSteps(profile) {
68
68
  needs: ["pnpm"],
69
69
  after,
70
70
  run: unitTests,
71
+ args: "vitest run",
71
72
  limitSeconds: 300,
72
73
  },
73
74
  {
@@ -1,5 +1,6 @@
1
1
  export interface KnipConfig {
2
2
  ignore?: string[];
3
+ ignoreIssues?: Record<string, string[]>;
3
4
  ignoreDependencies?: string[];
4
5
  playwright?: {
5
6
  config?: string[];
@@ -1,6 +1,7 @@
1
1
  import { existsSync, readFileSync } from "node:fs";
2
2
  import path from "node:path";
3
- const STEP_TOOLS = ["publint", "@arethetypeswrong/cli", "openapi-typescript"];
3
+ const STEP_TOOLS = ["prettier", "publint", "@arethetypeswrong/cli", "openapi-typescript"];
4
+ const GENERATED_ISSUES = ["files", "exports", "nsExports", "types", "nsTypes", "enumMembers", "namespaceMembers", "duplicates"];
4
5
  const GENERATED = ["src/api/openapi.d.ts"];
5
6
  const PLAYWRIGHT_CONFIGS = ["e2e/playwright.config.ts", "visual/playwright.config.ts", "gate/consumer/playwright.config.ts"];
6
7
  function dependencies(directory) {
@@ -21,7 +22,8 @@ export function knip(config = {}, directory = process.cwd()) {
21
22
  const playwrightConfigs = [...PLAYWRIGHT_CONFIGS.filter(present), ...(config.playwright?.config ?? [])];
22
23
  return {
23
24
  ...config,
24
- ignore: [...GENERATED.filter(present), ...(config.ignore ?? [])],
25
+ ignore: [...(config.ignore ?? [])],
26
+ ignoreIssues: { ...Object.fromEntries(GENERATED.filter(present).map((file) => [file, GENERATED_ISSUES])), ...config.ignoreIssues },
25
27
  ignoreDependencies: [...STEP_TOOLS.filter((tool) => installed.includes(tool)), ...(config.ignoreDependencies ?? [])],
26
28
  ...(playwrightConfigs.length === 0 ? {} : { playwright: { ...config.playwright, config: playwrightConfigs } }),
27
29
  };
@@ -2,8 +2,10 @@ interface Use {
2
2
  baseURL?: string;
3
3
  [key: string]: unknown;
4
4
  }
5
+ type ReporterDescription = readonly [string] | readonly [string, unknown];
5
6
  interface PlaywrightConfig {
6
7
  use?: Use;
8
+ reporter?: string | readonly ReporterDescription[];
7
9
  [key: string]: unknown;
8
10
  }
9
11
  export declare function e2eTests<Config extends PlaywrightConfig>(config: Config): {
@@ -12,7 +14,6 @@ export declare function e2eTests<Config extends PlaywrightConfig>(config: Config
12
14
  workers: number;
13
15
  retries: number;
14
16
  failOnFlakyTests: boolean;
15
- reporter: "list";
16
17
  timeout: number;
17
18
  projects: {
18
19
  name: string;
@@ -25,6 +26,7 @@ export declare function e2eTests<Config extends PlaywrightConfig>(config: Config
25
26
  };
26
27
  }[];
27
28
  } & Config & {
29
+ reporter: ReporterDescription[];
28
30
  use: {
29
31
  baseURL?: string;
30
32
  locale: string;
@@ -1,4 +1,10 @@
1
1
  const ci = () => Boolean(process.env["CI"]);
2
+ const GATE_REPORT = "PLAYWRIGHT_JSON_OUTPUT_FILE";
3
+ function withGateReport(reporter) {
4
+ const listed = typeof reporter === "string" ? reporter.split(",").map((name) => [name.trim()]) : [...reporter];
5
+ const wanted = (process.env[GATE_REPORT] ?? "") !== "" && !listed.some(([name]) => name === "json");
6
+ return wanted ? [...listed, ["json"]] : listed;
7
+ }
2
8
  export function e2eTests(config) {
3
9
  return {
4
10
  testDir: "../src",
@@ -6,10 +12,10 @@ export function e2eTests(config) {
6
12
  workers: 1,
7
13
  retries: ci() ? 1 : 0,
8
14
  failOnFlakyTests: ci(),
9
- reporter: "list",
10
15
  timeout: 60_000,
11
16
  projects: [{ name: "chromium", use: { browserName: "chromium", viewport: { width: 1440, height: 900 } } }],
12
17
  ...config,
18
+ reporter: withGateReport(config.reporter ?? "list"),
13
19
  use: { locale: "en-US", trace: "retain-on-failure", screenshot: "only-on-failure", ...config.use },
14
20
  };
15
21
  }
@@ -10,8 +10,10 @@ export declare function script(lines: readonly string[]): string;
10
10
  export declare function onPath(tool: string): boolean;
11
11
  export declare function suwgate(args: string): string;
12
12
  export declare function hasDevSection(context: StepContext): boolean;
13
+ export declare function databaseUpCommand(context: StepContext): string | null;
14
+ export declare function settingsOf<Settings>(context: StepContext, step: string): Partial<Settings>;
13
15
  export declare const CHECKOUT_ENV: string[];
14
16
  export declare function runDirectory(step: string): string;
15
17
  export declare function verdictCommand(args: string): string;
16
18
  export declare function countedVitest(step: string, command: string): string;
17
- export declare function countedPlaywright(step: string, command: string): string[];
19
+ export declare function countedPlaywright(step: string, command: string, reporters?: string | null): string[];
@@ -67,15 +67,30 @@ export function onPath(tool) {
67
67
  export function suwgate(args) {
68
68
  return `${quoted(process.execPath)} ${quoted(CLI)} ${args}`;
69
69
  }
70
- export function hasDevSection(context) {
70
+ const MAX_ALIASES = 10_000;
71
+ function devSection(context) {
71
72
  try {
72
- const parsed = parseYaml(readFileSync(path.join(context.root, "suwgate.yml"), "utf8"));
73
- return typeof parsed === "object" && parsed !== null && "dev" in parsed;
73
+ const parsed = parseYaml(readFileSync(path.join(context.root, "suwgate.yml"), "utf8"), { maxAliasCount: MAX_ALIASES });
74
+ if (typeof parsed !== "object" || parsed === null || !("dev" in parsed))
75
+ return null;
76
+ const dev = parsed.dev;
77
+ return typeof dev === "object" && dev !== null ? dev : {};
74
78
  }
75
79
  catch {
76
- return false;
80
+ return null;
77
81
  }
78
82
  }
83
+ export function hasDevSection(context) {
84
+ return devSection(context) !== null;
85
+ }
86
+ export function databaseUpCommand(context) {
87
+ const database = devSection(context)?.["database"];
88
+ const up = typeof database === "object" && database !== null ? database["up"] : undefined;
89
+ return typeof up === "string" ? up : null;
90
+ }
91
+ export function settingsOf(context, step) {
92
+ return context.settings[step] ?? {};
93
+ }
79
94
  export const CHECKOUT_ENV = [`variables="$(${suwgate("dev checkout-env")})"`, 'eval "$variables"'];
80
95
  export function runDirectory(step) {
81
96
  return `run="\${SUWGATE_RUN:-$(mktemp -d)}/${step}" && mkdir -p "$run"`;
@@ -86,14 +101,14 @@ export function verdictCommand(args) {
86
101
  export function countedVitest(step, command) {
87
102
  return [
88
103
  runDirectory(step),
89
- `${command} --reporter=default --reporter=json --outputFile.json="$run/vitest.json"`,
104
+ `${command} --reporter=default --reporter=json --outputFile.json="$run/vitest.json" "$@"`,
90
105
  verdictCommand(`tests vitest "$run/vitest.json" ${quoted(`vitest (${step})`)}`),
91
106
  ].join(" && ");
92
107
  }
93
- export function countedPlaywright(step, command) {
108
+ export function countedPlaywright(step, command, reporters = "list,json") {
94
109
  return [
95
110
  runDirectory(step),
96
- `PLAYWRIGHT_JSON_OUTPUT_FILE="$run/playwright.json" ${command} --reporter=list,json`,
111
+ `PLAYWRIGHT_JSON_OUTPUT_FILE="$run/playwright.json" ${command}${reporters === null ? "" : ` --reporter=${reporters}`} "$@"`,
97
112
  verdictCommand(`tests playwright "$run/playwright.json" ${quoted(`playwright (${step})`)}`),
98
113
  ];
99
114
  }
@@ -14,8 +14,12 @@ const STRICT_FAMILY = [
14
14
  ];
15
15
  export function testsVerdict(format, location, tool) {
16
16
  const run = readRun(format, location);
17
- if (run === null)
18
- return `${tool} wrote no report, so suwgate cannot tell that any test ran: run it again and look at its output above`;
17
+ if (run === null) {
18
+ const hint = format === "playwright"
19
+ ? "; Playwright writes it when its reporters include json, which e2eTests() of @maniumek/suwgate/playwright adds: keep the config's reporter inside e2eTests(), and pass no --reporter after --"
20
+ : "";
21
+ return `${tool} wrote no report, so suwgate cannot tell that any test ran: run it again and look at its output above${hint}`;
22
+ }
19
23
  if (run.executed > 0)
20
24
  return null;
21
25
  return `${tool} ran 0 tests (every test skipped or filtered out, or none found), and a gate that ran nothing proves nothing: add a test that runs, or skip the step in suwgate.yml (steps.skip) with a reason`;
@@ -2,14 +2,29 @@ import { API_CONTRACT } from "./contract.js";
2
2
  import { DATABASE_UP } from "./database.js";
3
3
  import { DOTNET_BUILD, DOTNET_TEST } from "./dotnet.js";
4
4
  import { PNPM_INSTALL } from "./node.js";
5
- import { CHECKOUT_ENV, countedPlaywright, has, hasDevSection, quoted, script, suwgate } from "./shell.js";
5
+ import { CHECKOUT_ENV, countedPlaywright, has, hasDevSection, quoted, script, settingsOf, suwgate } from "./shell.js";
6
+ const E2E = "e2e";
6
7
  const E2E_CONFIG = "e2e/playwright.config.ts";
7
8
  const PLAYWRIGHT = `pnpm exec playwright test -c ${E2E_CONFIG}`;
9
+ const E2E_SETTINGS = {
10
+ type: "object",
11
+ additionalProperties: false,
12
+ properties: {
13
+ before: {
14
+ description: "Commands run in the repository root after the database is fresh and before Playwright, in order (e.g. rm -rf .artifacts/e2e/versions); a failing one stops the step.",
15
+ type: "array",
16
+ minItems: 1,
17
+ items: { type: "string", minLength: 1 },
18
+ },
19
+ },
20
+ };
8
21
  function e2e(context) {
9
22
  if (!has(context, E2E_CONFIG))
10
23
  return null;
24
+ const before = (settingsOf(context, E2E).before ?? []).map((command) => `(cd ${quoted(context.root)} && ${command})`);
25
+ const playwright = countedPlaywright(E2E, PLAYWRIGHT, null);
11
26
  if (!hasDevSection(context))
12
- return script(["set -euo pipefail", ...countedPlaywright("e2e", PLAYWRIGHT)]);
27
+ return script(["set -euo pipefail", ...before, ...playwright]);
13
28
  return script([
14
29
  "set -euo pipefail",
15
30
  ...CHECKOUT_ENV,
@@ -17,17 +32,21 @@ function e2e(context) {
17
32
  `${suwgate("dev db up")} >/dev/null`,
18
33
  `${suwgate("dev db psql")} -q -d postgres -c "DROP DATABASE IF EXISTS $E2E_DATABASE WITH (FORCE)" -c "CREATE DATABASE $E2E_DATABASE"`,
19
34
  `mkdir -p ${quoted(`${context.root}/.artifacts/logs`)}`,
20
- ...countedPlaywright("e2e", PLAYWRIGHT),
35
+ ...before,
36
+ ...playwright,
21
37
  ]);
22
38
  }
23
39
  export const webSteps = [
24
40
  {
25
- id: "e2e",
41
+ id: E2E,
26
42
  title: `browser tests against the real server and a fresh database of this checkout (playwright, ${E2E_CONFIG}; 0 tests run fails)`,
27
43
  profile: "web",
28
44
  needs: ["pnpm"],
29
- after: [PNPM_INSTALL, DOTNET_BUILD, DATABASE_UP, DOTNET_TEST, API_CONTRACT],
45
+ after: [PNPM_INSTALL, DOTNET_BUILD, DATABASE_UP],
46
+ orderAfter: [DOTNET_TEST, API_CONTRACT],
30
47
  run: e2e,
48
+ args: "playwright test",
49
+ settingsSchema: E2E_SETTINGS,
31
50
  limitSeconds: 600,
32
51
  },
33
52
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maniumek/suwgate",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "One quality gate for every repository: suwgate check runs the steps of the repository's profiles and its guards, configured in suwgate.yml",
5
5
  "license": "MIT",
6
6
  "author": "Manomenu",
@@ -35,6 +35,11 @@
35
35
  }
36
36
  }
37
37
  },
38
+ "env": { "$ref": "#/$defs/env", "description": "Variables set for every step (a step's own env in steps.configure wins)." },
39
+ "unset": {
40
+ "$ref": "#/$defs/unset",
41
+ "description": "Variables no step sees, removed before the steps run, e.g. [Models__*, LLM_MODEL_*] so no step reaches a real model."
42
+ },
38
43
  "steps": {
39
44
  "type": "object",
40
45
  "additionalProperties": false,
@@ -73,6 +78,29 @@
73
78
  "reason": { "$ref": "#/$defs/reason" }
74
79
  }
75
80
  }
81
+ },
82
+ "configure": {
83
+ "description": "Knobs of the profiles' own steps, by step id: their ceiling, more steps to wait for, their environment, and the step's own settings, which the step's schema checks. Replacing a step's command is a skip with its reason plus an added step.",
84
+ "type": "object",
85
+ "propertyNames": { "$ref": "#/$defs/id" },
86
+ "additionalProperties": {
87
+ "type": "object",
88
+ "additionalProperties": false,
89
+ "properties": {
90
+ "limitSeconds": { "type": "integer", "minimum": 1, "description": "The step is killed above this." },
91
+ "after": {
92
+ "type": "array",
93
+ "uniqueItems": true,
94
+ "items": { "$ref": "#/$defs/id" },
95
+ "description": "More steps that must finish first, on top of the step's own."
96
+ },
97
+ "env": { "$ref": "#/$defs/env" },
98
+ "unset": { "$ref": "#/$defs/unset" },
99
+ "settings": {
100
+ "description": "The step's own settings, checked by the step's settingsSchema (README: the settings of the steps)."
101
+ }
102
+ }
103
+ }
76
104
  }
77
105
  }
78
106
  },
@@ -105,7 +133,16 @@
105
133
  "properties": {
106
134
  "guard": { "type": "string", "pattern": "^([a-z]+/)?[a-z][a-z0-9-]*$" },
107
135
  "target": { "type": "string", "minLength": 1 },
108
- "reason": { "$ref": "#/$defs/reason" }
136
+ "reason": { "$ref": "#/$defs/reason" },
137
+ "message": {
138
+ "type": "string",
139
+ "minLength": 1,
140
+ "description": "Narrows the exception to the target's findings whose message contains this text; without it, every finding of the target is excepted. Repository guards only."
141
+ },
142
+ "once": {
143
+ "type": "boolean",
144
+ "description": "A one-time exception: it holds only while master's suwgate.yml does not have it yet (on the branch that adds it). After the merge it excepts nothing and is never stale; the report asks to delete it. Repository guards only."
145
+ }
109
146
  }
110
147
  }
111
148
  },
@@ -158,7 +195,7 @@
158
195
  }
159
196
  },
160
197
  "database": {
161
- "description": "Defaults to the app name for all three.",
198
+ "description": "The app's database: user, password and name default to the app name; up and psql replace the compose service with the repository's own commands.",
162
199
  "type": "object",
163
200
  "additionalProperties": false,
164
201
  "properties": {
@@ -168,7 +205,17 @@
168
205
  "minLength": 1,
169
206
  "description": "A development password: the database is published on the loopback only."
170
207
  },
171
- "name": { "type": "string", "minLength": 1 }
208
+ "name": { "type": "string", "minLength": 1 },
209
+ "up": {
210
+ "type": "string",
211
+ "minLength": 1,
212
+ "description": "A bash command, run in the repository root, that starts the database (or leaves it running) and returns once it answers, e.g. scripts/.internal/db.sh up. suwgate dev db up, the database-up step and the e2e step run it instead of docker compose."
213
+ },
214
+ "psql": {
215
+ "type": "string",
216
+ "minLength": 1,
217
+ "description": "A bash command that runs psql in the database as its owner and takes psql's arguments after it, e.g. scripts/.internal/db.sh psql. suwgate dev db psql and the e2e step use it instead of docker compose exec."
218
+ }
172
219
  }
173
220
  },
174
221
  "server": {
@@ -221,6 +268,18 @@
221
268
  "$defs": {
222
269
  "id": { "type": "string", "pattern": "^[a-z][a-z0-9-]*$" },
223
270
  "reason": { "type": "string", "pattern": "\\S", "description": "Why: a sentence a reviewer can judge." },
271
+ "env": {
272
+ "description": "Variables set for the commands, by name; quote a value YAML would read as a number or a boolean (\"1\").",
273
+ "type": "object",
274
+ "propertyNames": { "pattern": "^[A-Za-z_][A-Za-z0-9_]*$" },
275
+ "additionalProperties": { "type": "string" }
276
+ },
277
+ "unset": {
278
+ "description": "Variables removed from the environment, by name; * and ? match any characters (Models__* clears every Models__ setting), without regard to case, as .NET reads configuration.",
279
+ "type": "array",
280
+ "uniqueItems": true,
281
+ "items": { "type": "string", "pattern": "^[A-Za-z0-9_*?]+$" }
282
+ },
224
283
  "port": { "type": "integer", "minimum": 1, "maximum": 65535 },
225
284
  "relativePath": { "type": "string", "pattern": "^[^/\\\\][^\\\\]*$" }
226
285
  }