@rpcbase/test 0.337.0 → 0.339.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/dist/cli.js CHANGED
@@ -1,28 +1,48 @@
1
1
  #!/usr/bin/env node
2
- import { spawnSync, spawn } from "node:child_process";
2
+ import { spawnSync } from "node:child_process";
3
3
  import fs$1 from "node:fs";
4
4
  import fs from "node:fs/promises";
5
5
  import path from "node:path";
6
6
  import { createRequire } from "node:module";
7
- import { fileURLToPath } from "node:url";
8
7
  import fg from "fast-glob";
9
8
  import { createCoverageConfig } from "./coverage/config.js";
10
- import { createCollectCoverageMatcher, resolveCollectCoverageRoots, isInsideAnyRoot } from "./coverage/collect.js";
9
+ import { createCollectCoverageMatcher } from "./coverage/collect.js";
11
10
  import { loadCoverageOptions } from "./coverage/config-loader.js";
12
11
  import { removeCoverageFiles } from "./coverage/files.js";
13
12
  import { CoverageThresholdError, collectCoveredFiles, generateCoverageReport } from "./coverage/report.js";
13
+ import { runPlaywright } from "./runners/playwright.js";
14
+ import { runVitest, resolveNodeCoverageDir } from "./runners/vitest.js";
14
15
  const require$1 = createRequire(import.meta.url);
15
- const moduleDir = path.dirname(fileURLToPath(import.meta.url));
16
16
  const shouldForceTty = !process.stdout.isTTY && process.env.FORCE_COLOR === "true";
17
17
  if (shouldForceTty) {
18
18
  require$1("./register-tty.cjs");
19
19
  }
20
20
  const VITEST_COVERAGE_CANDIDATES = ["src/coverage.json"];
21
- const COMBINED_COVERAGE_ENV_VAR = "RB_TEST_COMBINED_COVERAGE";
22
- const isAider = process.env.IS_AIDER === "yes";
23
21
  if (process.env.IS_AIDER !== void 0 && process.env.IS_AIDER !== "yes") {
24
22
  console.warn("Warning: IS_AIDER is set to a value other than 'yes'.");
25
23
  }
24
+ const RB_CLI_OPTIONS = ["build-specs-map", "auto", "full", "show-mapping", "list"];
25
+ function isRbCliOption(arg) {
26
+ const normalized = arg.toLowerCase();
27
+ return RB_CLI_OPTIONS.some((option) => normalized === `--${option}` || normalized.startsWith(`--${option}=`));
28
+ }
29
+ function parseBooleanCliArg(rawArgs, option) {
30
+ const optionPrefix = `--${option}`;
31
+ const optionWithValuePrefix = `${optionPrefix}=`;
32
+ return rawArgs.some((arg) => {
33
+ if (arg === optionPrefix) {
34
+ return true;
35
+ }
36
+ if (!arg.startsWith(optionWithValuePrefix)) {
37
+ return false;
38
+ }
39
+ const value = arg.slice(optionWithValuePrefix.length).toLowerCase();
40
+ if (value === "false" || value === "0") {
41
+ return false;
42
+ }
43
+ return value.length > 0;
44
+ });
45
+ }
26
46
  function parseCliArgs(rawArgs) {
27
47
  const yargs = require$1("yargs/yargs");
28
48
  const parsed = yargs(rawArgs).help(false).version(false).strict(false).exitProcess(false).parserConfiguration({
@@ -46,27 +66,22 @@ function parseCliArgs(rawArgs) {
46
66
  type: "boolean",
47
67
  default: false
48
68
  }).parseSync();
49
- const full = Boolean(parsed.full);
50
- const auto = Boolean(parsed.auto) && !full;
51
- const passthroughArgs = [...Array.isArray(parsed._) ? parsed._ : [], ...Array.isArray(parsed["--"]) ? parsed["--"] : []].map((entry) => String(entry));
69
+ const passthroughArgs = [...Array.isArray(parsed._) ? parsed._ : [], ...Array.isArray(parsed["--"]) ? parsed["--"] : []].map((entry) => String(entry)).filter((arg) => !isRbCliOption(arg));
52
70
  return {
53
- buildSpecsMap: Boolean(parsed.buildSpecsMap),
54
- auto,
55
- showMapping: Boolean(parsed.showMapping),
56
- list: Boolean(parsed.list),
71
+ buildSpecsMap: parseBooleanCliArg(rawArgs, "build-specs-map"),
72
+ auto: parseBooleanCliArg(rawArgs, "auto") && !parseBooleanCliArg(rawArgs, "full"),
73
+ showMapping: parseBooleanCliArg(rawArgs, "show-mapping"),
74
+ list: parseBooleanCliArg(rawArgs, "list"),
57
75
  passthroughArgs
58
76
  };
59
77
  }
60
78
  async function runTests() {
61
79
  const args = parseCliArgs(process.argv.slice(2));
62
80
  const buildSpecsMap = args.buildSpecsMap;
63
- const auto = args.auto;
81
+ const auto = args.auto && !buildSpecsMap;
64
82
  const showMapping = args.showMapping;
65
83
  const list = args.list;
66
84
  const filteredArgs = args.passthroughArgs;
67
- if (buildSpecsMap && auto) {
68
- throw new Error("[rb-test] --auto cannot be combined with --build-specs-map");
69
- }
70
85
  if (showMapping && !auto) {
71
86
  throw new Error("[rb-test] --show-mapping requires --auto");
72
87
  }
@@ -139,48 +154,6 @@ runTests().then(() => process.exit(0)).catch((error) => {
139
154
  }
140
155
  process.exit(1);
141
156
  });
142
- async function runVitest(coverage, combinedConfig, userArgs, {
143
- disableCoverage = false
144
- } = {}) {
145
- const vitestArgs = ["run", "--passWithNoTests"];
146
- const vitestConfig = resolveVitestConfig();
147
- const hasCustomConfig = userArgs.some((arg) => {
148
- if (arg === "--config" || arg === "-c") {
149
- return true;
150
- }
151
- return arg.startsWith("--config=");
152
- });
153
- if (vitestConfig && !hasCustomConfig) {
154
- vitestArgs.push("--config", vitestConfig);
155
- }
156
- vitestArgs.push(...userArgs);
157
- const launcher = resolveVitestLauncher();
158
- const env = withRegisterShim(process.env);
159
- if (disableCoverage) {
160
- env.RB_DISABLE_COVERAGE = "1";
161
- }
162
- if (coverage?.enabled && !disableCoverage) {
163
- const nodeCoverageDir = resolveNodeCoverageDir(combinedConfig ?? coverage.config);
164
- await fs.mkdir(nodeCoverageDir, {
165
- recursive: true
166
- });
167
- env.NODE_V8_COVERAGE = nodeCoverageDir;
168
- }
169
- await spawnWithLogs({
170
- name: "Vitest",
171
- launcher,
172
- args: vitestArgs,
173
- env,
174
- successMessage: "Vitest suite passed!",
175
- failureMessage: "Vitest failed"
176
- });
177
- if (coverage?.enabled && !disableCoverage) {
178
- await convertNodeCoverage({
179
- config: combinedConfig ?? coverage.config,
180
- nodeCoverageDir: resolveNodeCoverageDir(combinedConfig ?? coverage.config)
181
- });
182
- }
183
- }
184
157
  async function buildSpecsMapFromCoverage({
185
158
  userArgs,
186
159
  combinedCoverage
@@ -568,122 +541,6 @@ function findWorkspaceRoot(projectRoot) {
568
541
  dir = parent;
569
542
  }
570
543
  }
571
- function runPlaywright(userArgs, {
572
- disableCoverage = false
573
- } = {}) {
574
- const configPath = fs$1.existsSync(path.join(process.cwd(), "playwright.config.ts")) ? path.join(process.cwd(), "playwright.config.ts") : path.join(moduleDir, "playwright.config.ts");
575
- const hasCustomConfig = userArgs.some((arg) => {
576
- if (arg === "--config" || arg === "-c") {
577
- return true;
578
- }
579
- return arg.startsWith("--config=");
580
- });
581
- const playwrightArgs = ["test"];
582
- if (!hasCustomConfig) {
583
- playwrightArgs.push("--config", configPath);
584
- }
585
- playwrightArgs.push(...userArgs);
586
- ensureJsxRuntimeShim(process.cwd());
587
- const launcher = resolvePlaywrightLauncher();
588
- const env = withRegisterShim(process.env);
589
- env[COMBINED_COVERAGE_ENV_VAR] = "1";
590
- if (disableCoverage) {
591
- env.RB_DISABLE_COVERAGE = "1";
592
- }
593
- return spawnWithLogs({
594
- name: "Playwright",
595
- launcher,
596
- args: playwrightArgs,
597
- env,
598
- successMessage: "Playwright suite passed!",
599
- failureMessage: "Playwright failed"
600
- });
601
- }
602
- function resolvePlaywrightLauncher() {
603
- const cliPath = resolveCliPath();
604
- if (cliPath) {
605
- return {
606
- command: process.execPath,
607
- args: [cliPath]
608
- };
609
- }
610
- const localBin = path.resolve(process.cwd(), "node_modules/.bin/playwright");
611
- if (fs$1.existsSync(localBin)) {
612
- return {
613
- command: localBin,
614
- args: []
615
- };
616
- }
617
- return {
618
- command: "playwright",
619
- args: []
620
- };
621
- }
622
- function resolveCliPath() {
623
- const searchRoots = [process.cwd(), moduleDir];
624
- for (const base of searchRoots) {
625
- try {
626
- const pkgPath = require$1.resolve("@playwright/test/package.json", {
627
- paths: [base]
628
- });
629
- const cliPath = path.join(path.dirname(pkgPath), "cli.js");
630
- if (fs$1.existsSync(cliPath)) {
631
- return cliPath;
632
- }
633
- } catch (_error) {
634
- }
635
- }
636
- return null;
637
- }
638
- function resolveVitestLauncher() {
639
- const searchRoots = [process.cwd(), moduleDir];
640
- for (const base of searchRoots) {
641
- try {
642
- const pkgPath = require$1.resolve("vitest/package.json", {
643
- paths: [base]
644
- });
645
- const pkgDir = path.dirname(pkgPath);
646
- const pkgJson = JSON.parse(fs$1.readFileSync(pkgPath, "utf8"));
647
- const binPath = typeof pkgJson.bin === "string" ? pkgJson.bin : pkgJson.bin?.vitest;
648
- if (binPath) {
649
- return {
650
- command: process.execPath,
651
- args: [path.join(pkgDir, binPath)]
652
- };
653
- }
654
- } catch (_error) {
655
- }
656
- }
657
- const localBin = path.resolve(process.cwd(), "node_modules/.bin/vitest");
658
- if (fs$1.existsSync(localBin)) {
659
- return {
660
- command: localBin,
661
- args: []
662
- };
663
- }
664
- return {
665
- command: "vitest",
666
- args: []
667
- };
668
- }
669
- function resolveVitestConfig() {
670
- const userConfig = findVitestConfig(process.cwd());
671
- if (userConfig) {
672
- return userConfig;
673
- }
674
- const bundledConfig = path.join(moduleDir, "vitest.config.js");
675
- return fs$1.existsSync(bundledConfig) ? bundledConfig : null;
676
- }
677
- function findVitestConfig(baseDir) {
678
- const candidates = ["vitest.config.ts", "vitest.config.js", "vitest.config.mjs"];
679
- for (const file of candidates) {
680
- const fullPath = path.join(baseDir, file);
681
- if (fs$1.existsSync(fullPath)) {
682
- return fullPath;
683
- }
684
- }
685
- return null;
686
- }
687
544
  async function loadVitestCoverageConfig() {
688
545
  const options = await loadCoverageOptions({
689
546
  optional: true,
@@ -731,58 +588,6 @@ async function cleanCoverageArtifacts(config) {
731
588
  force: true
732
589
  });
733
590
  }
734
- function resolveNodeCoverageDir(config) {
735
- return path.join(config.rootDir, "build", "vitest", "test-results", "node-coverage");
736
- }
737
- async function convertNodeCoverage(coverage) {
738
- const {
739
- config,
740
- nodeCoverageDir
741
- } = coverage;
742
- const entries = await fs.readdir(nodeCoverageDir).catch(() => []);
743
- const scripts = [];
744
- const scriptRoots = resolveCollectCoverageRoots(config.collectCoverageFrom, config.rootDir);
745
- for (const entry of entries) {
746
- if (!entry.endsWith(".json")) {
747
- continue;
748
- }
749
- const fullPath = path.join(nodeCoverageDir, entry);
750
- const payload = await readJson(fullPath);
751
- const results = Array.isArray(payload?.result) ? payload.result : [];
752
- for (const script of results) {
753
- const normalized = normalizeNodeScriptUrl(script.url, config.rootDir);
754
- if (!normalized) {
755
- continue;
756
- }
757
- if (isNodeModulesPath(normalized.absolutePath)) {
758
- continue;
759
- }
760
- if (!isInsideAnyRoot(normalized.absolutePath, scriptRoots)) {
761
- continue;
762
- }
763
- const source = await fs.readFile(normalized.absolutePath, "utf8").catch(() => "");
764
- scripts.push({
765
- absolutePath: normalized.absolutePath,
766
- relativePath: normalized.relativePath,
767
- source,
768
- functions: script.functions ?? [],
769
- url: script.url
770
- });
771
- }
772
- }
773
- if (scripts.length === 0) {
774
- return;
775
- }
776
- const outDir = path.join(config.rootDir, "build", "vitest", "coverage");
777
- await fs.mkdir(outDir, {
778
- recursive: true
779
- });
780
- const outputFile = path.join(outDir, config.coverageFileName);
781
- await fs.writeFile(outputFile, JSON.stringify({
782
- testId: "vitest",
783
- scripts
784
- }, null, 2), "utf8");
785
- }
786
591
  async function finalizeCoverage(config) {
787
592
  try {
788
593
  await generateCoverageReport(config);
@@ -801,121 +606,4 @@ async function readJson(filePath) {
801
606
  return null;
802
607
  }
803
608
  }
804
- function normalizeNodeScriptUrl(rawUrl, rootDir) {
805
- if (!rawUrl || rawUrl.startsWith("node:")) {
806
- return null;
807
- }
808
- let absolutePath = null;
809
- try {
810
- if (rawUrl.startsWith("file://")) {
811
- absolutePath = fileURLToPath(rawUrl);
812
- }
813
- } catch (_err) {
814
- }
815
- if (!absolutePath && path.isAbsolute(rawUrl)) {
816
- absolutePath = rawUrl;
817
- }
818
- if (!absolutePath) {
819
- return null;
820
- }
821
- const normalized = path.normalize(absolutePath);
822
- return {
823
- absolutePath: normalized,
824
- relativePath: path.relative(rootDir, normalized)
825
- };
826
- }
827
- function isNodeModulesPath(filePath) {
828
- return path.normalize(String(filePath ?? "")).split(path.sep).includes("node_modules");
829
- }
830
- function spawnWithLogs({
831
- name,
832
- launcher,
833
- args,
834
- env,
835
- successMessage,
836
- failureMessage
837
- }) {
838
- return new Promise((resolve, reject) => {
839
- const stdoutBuffer = [];
840
- const stderrBuffer = [];
841
- const child = spawn(launcher.command, [...launcher.args || [], ...args], {
842
- shell: false,
843
- env
844
- });
845
- child.stdout?.on("data", (data) => {
846
- if (!isAider) {
847
- process.stdout.write(data);
848
- }
849
- stdoutBuffer.push(data.toString());
850
- });
851
- child.stderr?.on("data", (data) => {
852
- if (!isAider) {
853
- process.stderr.write(data);
854
- }
855
- stderrBuffer.push(data.toString());
856
- });
857
- child.on("close", (code) => {
858
- if (code === 0) {
859
- if (successMessage) {
860
- console.log(successMessage);
861
- }
862
- resolve();
863
- } else {
864
- console.error(failureMessage || `${name} failed:`);
865
- if (isAider) {
866
- if (stdoutBuffer.length > 0) {
867
- console.error(stdoutBuffer.join(""));
868
- }
869
- if (stderrBuffer.length > 0) {
870
- console.error(stderrBuffer.join(""));
871
- }
872
- }
873
- reject(new Error(`${name} failed with exit code: ${code}`));
874
- }
875
- });
876
- child.on("error", (error) => {
877
- console.error(`Error spawning ${name}:`, error);
878
- reject(error);
879
- });
880
- });
881
- }
882
- function withRegisterShim(baseEnv) {
883
- const nodeOptions = appendNodeRequire(baseEnv.NODE_OPTIONS, path.join(moduleDir, "register-tty.cjs"));
884
- return {
885
- ...baseEnv,
886
- NODE_OPTIONS: nodeOptions
887
- };
888
- }
889
- function ensureJsxRuntimeShim(projectRoot) {
890
- const shimDir = path.join(projectRoot, "node_modules", "playwright");
891
- fs$1.mkdirSync(shimDir, {
892
- recursive: true
893
- });
894
- const shims = [{
895
- file: "jsx-runtime.js",
896
- target: "react/jsx-runtime"
897
- }, {
898
- file: "jsx-dev-runtime.js",
899
- target: "react/jsx-dev-runtime"
900
- }];
901
- for (const {
902
- file,
903
- target
904
- } of shims) {
905
- const filePath = path.join(shimDir, file);
906
- if (!fs$1.existsSync(filePath)) {
907
- const content = `export * from "${target}";
908
- export { default } from "${target}";
909
- `;
910
- fs$1.writeFileSync(filePath, content, "utf8");
911
- }
912
- }
913
- }
914
- function appendNodeRequire(existing, modulePath) {
915
- const flag = `--require=${modulePath}`;
916
- if (!existing || existing.length === 0) {
917
- return flag;
918
- }
919
- return `${existing} ${flag}`;
920
- }
921
609
  //# sourceMappingURL=cli.js.map