e2e-spec-resolution.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { existsSync, readdirSync, statSync } from "node:fs";
  2. import { isAbsolute, relative, resolve } from "node:path";
  3. interface ResolveE2EScheduledSpecFilesOptions {
  4. baseDir: string;
  5. exclude: string[];
  6. specs: string[];
  7. }
  8. interface AssertNonEmptyE2EScheduledSpecFilesOptions
  9. extends ResolveE2EScheduledSpecFilesOptions {
  10. argv: string[];
  11. envSpec?: string;
  12. requestedSpecs: string[];
  13. }
  14. export function resolveE2EScheduledSpecFiles({
  15. baseDir,
  16. exclude,
  17. specs,
  18. }: ResolveE2EScheduledSpecFilesOptions): string[] {
  19. const includeMatchers = specs.map((spec) => createSpecMatcher(baseDir, spec));
  20. const excludeMatchers = exclude.map((spec) => createSpecMatcher(baseDir, spec));
  21. const candidateFiles = collectCandidateSpecFiles(baseDir, specs);
  22. return candidateFiles
  23. .filter((file) => includeMatchers.some((matcher) => matcher(file)))
  24. .filter((file) => !excludeMatchers.some((matcher) => matcher(file)))
  25. .map((file) => toSlash(relative(baseDir, file)))
  26. .sort();
  27. }
  28. export function assertNonEmptyE2EScheduledSpecFiles(
  29. options: AssertNonEmptyE2EScheduledSpecFilesOptions,
  30. ): string[] {
  31. const scheduled = resolveE2EScheduledSpecFiles(options);
  32. if (scheduled.length > 0) {
  33. return scheduled;
  34. }
  35. // 修复原因:WDIO 在部分 runner 上可在没有调度 worker 时只跑 onPrepare/build 并返回 0;
  36. // 这里在任何构建副作用前解析有效 spec,空集合直接失败,避免 CI 之后只看到缺 summary.json。
  37. throw new Error(
  38. [
  39. "Desktop E2E has no scheduled spec files after applying excludes.",
  40. `baseDir=${options.baseDir}`,
  41. `specs=${JSON.stringify(options.specs)}`,
  42. `exclude=${JSON.stringify(options.exclude)}`,
  43. `requestedSpecs=${JSON.stringify(options.requestedSpecs)}`,
  44. `ZCODE_E2E_SPEC=${options.envSpec ?? ""}`,
  45. `argv=${JSON.stringify(options.argv)}`,
  46. ].join("\n"),
  47. );
  48. }
  49. function collectCandidateSpecFiles(baseDir: string, specs: string[]): string[] {
  50. const files = new Set<string>();
  51. for (const spec of specs) {
  52. const root = resolveSpecSearchRoot(baseDir, spec);
  53. if (!existsSync(root)) {
  54. continue;
  55. }
  56. const stat = statSync(root);
  57. if (stat.isFile()) {
  58. if (isSpecFile(root)) {
  59. files.add(normalizeAbsolutePath(root));
  60. }
  61. continue;
  62. }
  63. for (const file of walkSpecFiles(root)) {
  64. files.add(file);
  65. }
  66. }
  67. return [...files].sort();
  68. }
  69. function resolveSpecSearchRoot(baseDir: string, pattern: string): string {
  70. const normalized = normalizePattern(baseDir, pattern);
  71. const firstGlobIndex = normalized.search(/[*?[{]/u);
  72. if (firstGlobIndex < 0) {
  73. return normalizeAbsolutePath(normalized);
  74. }
  75. const slashBeforeGlob = normalized.lastIndexOf("/", firstGlobIndex);
  76. return normalizeAbsolutePath(slashBeforeGlob < 0 ? baseDir : normalized.slice(0, slashBeforeGlob));
  77. }
  78. function* walkSpecFiles(root: string): Generator<string> {
  79. for (const entry of readdirSync(root, { withFileTypes: true })) {
  80. if (entry.name === "node_modules" || entry.name === ".e2e-artifacts") {
  81. continue;
  82. }
  83. const path = normalizeAbsolutePath(resolve(root, entry.name));
  84. if (entry.isDirectory()) {
  85. yield* walkSpecFiles(path);
  86. continue;
  87. }
  88. if (entry.isFile() && isSpecFile(path)) {
  89. yield path;
  90. }
  91. }
  92. }
  93. function createSpecMatcher(baseDir: string, pattern: string): (file: string) => boolean {
  94. const normalizedPattern = normalizePattern(baseDir, pattern);
  95. if (!hasGlob(normalizedPattern)) {
  96. const absolute = normalizeAbsolutePath(normalizedPattern);
  97. return (file) => normalizeAbsolutePath(file) === absolute;
  98. }
  99. const regex = globPatternToRegExp(normalizedPattern);
  100. return (file) => regex.test(normalizeAbsolutePath(file));
  101. }
  102. function normalizePattern(baseDir: string, pattern: string): string {
  103. const trimmed = pattern.trim();
  104. return normalizeAbsolutePath(isAbsolute(trimmed) ? trimmed : resolve(baseDir, trimmed));
  105. }
  106. function normalizeAbsolutePath(path: string): string {
  107. return toSlash(resolve(path));
  108. }
  109. function toSlash(path: string): string {
  110. return path.replace(/\\/g, "/");
  111. }
  112. function isSpecFile(path: string): boolean {
  113. return path.endsWith(".test.ts");
  114. }
  115. function hasGlob(pattern: string): boolean {
  116. return /[*?[{]/u.test(pattern);
  117. }
  118. function globPatternToRegExp(pattern: string): RegExp {
  119. let source = "^";
  120. for (let index = 0; index < pattern.length; index += 1) {
  121. const char = pattern[index]!;
  122. if (char === "*") {
  123. if (pattern[index + 1] === "*") {
  124. if (pattern[index + 2] === "/") {
  125. source += "(?:.*/)?";
  126. index += 2;
  127. } else {
  128. source += ".*";
  129. index += 1;
  130. }
  131. } else {
  132. source += "[^/]*";
  133. }
  134. continue;
  135. }
  136. source += escapeRegExp(char);
  137. }
  138. return new RegExp(`${source}$`, "u");
  139. }
  140. function escapeRegExp(char: string): string {
  141. return /[\\^$+?.()|[\]{}]/u.test(char) ? `\\${char}` : char;
  142. }
  143. /**
  144. * 桌面 E2E 只支持串行 worker。
  145. * Bugfix(评审 CR-01,2026-11-18):worker 共用 .e2e-home(beforeSession 递归删除)与 launcher 持有的
  146. * 账号 mock 状态(case-mode、Team 闲时灰度等)。并行时后写入的 worker 会改掉先启动 worker 随后读到的
  147. * 配置,所以在调度前拒绝其他取值,不能运行中才出现交错。要支持并行,须先为每个 worker 隔离 home 与 mock。
  148. */
  149. export function resolveE2EMaxInstances(env: Record<string, string | undefined>): number {
  150. const raw = env.ZCODE_E2E_MAX_INSTANCES?.trim();
  151. if (!raw || raw === "1") return 1;
  152. throw new Error(
  153. `ZCODE_E2E_MAX_INSTANCES=${raw} is not supported: desktop E2E workers share .e2e-home and the launcher-owned account mock, so they must run serially (use 1 or leave it unset).`,
  154. );
  155. }