bivium 0.10.0 → 0.11.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.
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  plural2
3
- } from "./shared-a8ebfhzf.js";
3
+ } from "./shared-mpjk8met.js";
4
4
  import {
5
5
  BiviumError2
6
- } from "./shared-118ck8qt.js";
6
+ } from "./shared-p3qfqmwd.js";
7
7
  import {
8
8
  globToRegExp,
9
9
  matcher,
@@ -11,17 +11,17 @@ import {
11
11
  inRoot2,
12
12
  checkConfig2,
13
13
  loadConfig2
14
- } from "./shared-px5a17nj.js";
14
+ } from "./shared-qaqjsxsk.js";
15
15
  import {
16
16
  nowMs
17
17
  } from "./shared-graedbt1.js";
18
18
  import {
19
19
  version
20
- } from "./shared-x17wzptz.js";
20
+ } from "./shared-jp8yw63q.js";
21
21
 
22
22
  // src/compute.ts
23
- import { readFile as readFile3, stat } from "node:fs/promises";
24
- import { join as join3, posix as posix6, resolve } from "node:path";
23
+ import { readFile as readFile4, stat } from "node:fs/promises";
24
+ import { join as join3, posix as posix6, resolve as resolve2 } from "node:path";
25
25
 
26
26
  // src/cache.ts
27
27
  import { existsSync } from "node:fs";
@@ -4531,6 +4531,7 @@ function planTests(graph, changed, config, {
4531
4531
  suite.name,
4532
4532
  tests.get(suite.name)?.length ?? 0
4533
4533
  ])),
4534
+ tests: Object.fromEntries(tests),
4534
4535
  full: [...full],
4535
4536
  packages: bumped.notes,
4536
4537
  settings: settingNotes,
@@ -4810,15 +4811,153 @@ async function settingChanges({
4810
4811
  return result;
4811
4812
  }
4812
4813
 
4814
+ // src/shards.ts
4815
+ import { readFile as readFile3 } from "node:fs/promises";
4816
+ import { resolve } from "node:path";
4817
+ var TIMINGS_SHAPE = `{ "version": 1, "files": { "<path>": <ms> } }`;
4818
+ function isRecord3(value) {
4819
+ return typeof value === "object" && value !== null && !Array.isArray(value);
4820
+ }
4821
+ function parseTimings(text) {
4822
+ const invalid = (what) => ({
4823
+ recorded: new Map,
4824
+ problem: `${what} (expected ${TIMINGS_SHAPE})`
4825
+ });
4826
+ let value;
4827
+ try {
4828
+ value = JSON.parse(text);
4829
+ } catch (error) {
4830
+ return invalid(error instanceof Error ? error.message : "not JSON");
4831
+ }
4832
+ if (!isRecord3(value) || value.version !== 1 || !isRecord3(value.files)) {
4833
+ return invalid("not a version 1 timings file");
4834
+ }
4835
+ const recorded = new Map;
4836
+ for (const [path, ms] of Object.entries(value.files)) {
4837
+ if (typeof ms !== "number" || !Number.isFinite(ms) || ms < 0) {
4838
+ return invalid(`${JSON.stringify(path)} has no duration in milliseconds`);
4839
+ }
4840
+ recorded.set(path, ms);
4841
+ }
4842
+ return { recorded };
4843
+ }
4844
+ function hasCode(error, code) {
4845
+ return error instanceof Error && "code" in error && error.code === code;
4846
+ }
4847
+ async function loadTimings(root, suite) {
4848
+ let text;
4849
+ try {
4850
+ text = await readFile3(resolve(root, suite.timings), "utf-8");
4851
+ } catch (error) {
4852
+ if (hasCode(error, "ENOENT"))
4853
+ return { recorded: new Map };
4854
+ const why = error instanceof Error ? error.message : String(error);
4855
+ return { recorded: new Map, problem: `unreadable: ${why}` };
4856
+ }
4857
+ return parseTimings(text);
4858
+ }
4859
+ function timingsWarning(command, suite, problem) {
4860
+ const instead = command === "run" ? "passing the files in path order" : "splitting by file count";
4861
+ return `bivium ${command}: ${suite.timings}: ${problem}; ${instead}. Delete it, or rewrite it with bun test --timings=${suite.timings} --update-timings
4862
+ `;
4863
+ }
4864
+ function durationsOf(tests, recorded) {
4865
+ const known = tests.flatMap((test) => {
4866
+ const ms = recorded.get(test);
4867
+ return ms === undefined ? [] : [ms];
4868
+ });
4869
+ if (known.length === 0)
4870
+ return { recorded, medianMs: 1, cold: true };
4871
+ known.sort((a, b) => a - b);
4872
+ const middle = known.length >> 1;
4873
+ const upper = known[middle] ?? 1;
4874
+ const medianMs = known.length % 2 === 1 ? upper : ((known[middle - 1] ?? 1) + upper) / 2;
4875
+ return { recorded, medianMs, cold: false };
4876
+ }
4877
+ function timedLongestFirst(files, recorded, medianMs) {
4878
+ return files.map((file) => ({ file, ms: recorded.get(file) ?? medianMs })).sort((a, b) => b.ms - a.ms || byCodeUnit(a.file, b.file));
4879
+ }
4880
+ function longestFirst(files, { recorded, medianMs }) {
4881
+ return timedLongestFirst(files, recorded, medianMs).map(({ file }) => file);
4882
+ }
4883
+ function assign(timed, count) {
4884
+ const shards = Array.from({ length: count }, (_, i) => ({
4885
+ shard: i + 1,
4886
+ files: [],
4887
+ estimatedMs: 0
4888
+ }));
4889
+ for (const { file, ms } of timed) {
4890
+ let least = shards[0];
4891
+ for (const shard of shards) {
4892
+ if (least === undefined || shard.estimatedMs < least.estimatedMs) {
4893
+ least = shard;
4894
+ }
4895
+ }
4896
+ if (least === undefined)
4897
+ break;
4898
+ least.files.push(file);
4899
+ least.estimatedMs += ms;
4900
+ }
4901
+ return shards;
4902
+ }
4903
+ function makespanOf(shards) {
4904
+ return Math.max(...shards.map((shard) => shard.estimatedMs));
4905
+ }
4906
+ function countFor(timed, spec) {
4907
+ const cap = Math.min(spec.max, timed.length);
4908
+ const capMs = makespanOf(assign(timed, cap));
4909
+ const targetMs = capMs > spec.targetMs ? capMs : spec.targetMs;
4910
+ const totalMs = timed.reduce((sum, { ms }) => sum + ms, 0);
4911
+ const fewest = Math.max(1, Math.ceil(totalMs / targetMs));
4912
+ for (let count = fewest;count < cap; count++) {
4913
+ if (makespanOf(assign(timed, count)) <= targetMs)
4914
+ return count;
4915
+ }
4916
+ return cap;
4917
+ }
4918
+ function splitShards(files, { recorded, medianMs, cold }, spec) {
4919
+ if (files.length === 0)
4920
+ return [];
4921
+ const timed = timedLongestFirst(files, recorded, medianMs);
4922
+ const count = typeof spec === "number" || cold ? Math.min(typeof spec === "number" ? spec : spec.max, files.length) : countFor(timed, spec);
4923
+ return assign(timed, count).map((shard) => ({
4924
+ ...shard,
4925
+ estimatedMs: Math.round(shard.estimatedMs)
4926
+ }));
4927
+ }
4928
+ async function shardPlan(root, config, plan) {
4929
+ const sharded = config.suites.filter((suite) => suite.shards !== undefined);
4930
+ const loaded = await Promise.all(sharded.map((suite) => loadTimings(root, suite)));
4931
+ const sharding = { shards: {}, timings: {}, cold: new Set };
4932
+ for (const [i, suite] of sharded.entries()) {
4933
+ const recorded = loaded[i]?.recorded ?? new Map;
4934
+ const problem = loaded[i]?.problem;
4935
+ const tests = plan.tests[suite.name] ?? [];
4936
+ const selection = plan.suites[suite.name];
4937
+ const files = selection?.mode === "some" ? selection.files : selection?.mode === "all" ? tests : [];
4938
+ const durations = durationsOf(tests, recorded);
4939
+ if (durations.cold)
4940
+ sharding.cold.add(suite.name);
4941
+ sharding.shards[suite.name] = splitShards(files, durations, suite.shards ?? 1);
4942
+ sharding.timings[suite.name] = {
4943
+ file: suite.timings,
4944
+ timed: files.filter((file) => recorded.has(file)).length,
4945
+ medianMs: durations.medianMs,
4946
+ ...problem !== undefined && { problem }
4947
+ };
4948
+ }
4949
+ return sharding;
4950
+ }
4951
+
4813
4952
  // src/compute.ts
4814
- function hasCode(error, codes) {
4953
+ function hasCode2(error, codes) {
4815
4954
  return error instanceof Error && "code" in error && typeof error.code === "string" && codes.includes(error.code);
4816
4955
  }
4817
4956
  async function readIfFile(path) {
4818
4957
  try {
4819
- return await readFile3(path, "utf-8");
4958
+ return await readFile4(path, "utf-8");
4820
4959
  } catch (error) {
4821
- if (hasCode(error, ["ENOENT", "EISDIR"]))
4960
+ if (hasCode2(error, ["ENOENT", "EISDIR"]))
4822
4961
  return;
4823
4962
  throw error;
4824
4963
  }
@@ -4864,7 +5003,7 @@ async function isFile(path) {
4864
5003
  try {
4865
5004
  return (await stat(path)).isFile();
4866
5005
  } catch (error) {
4867
- if (hasCode(error, ["ENOENT", "ENOTDIR"]))
5006
+ if (hasCode2(error, ["ENOENT", "ENOTDIR"]))
4868
5007
  return false;
4869
5008
  throw error;
4870
5009
  }
@@ -4985,7 +5124,7 @@ async function computePlan2(root, base, config, {
4985
5124
  blobs: dir !== undefined,
4986
5125
  mergeBase
4987
5126
  }),
4988
- readFile3(join3(root, "package.json"), "utf-8").catch(() => {
5127
+ readFile4(join3(root, "package.json"), "utf-8").catch(() => {
4989
5128
  return;
4990
5129
  }),
4991
5130
  ExtractionCache.open(dir)
@@ -5095,7 +5234,7 @@ async function prepare2({
5095
5234
  config,
5096
5235
  configFile
5097
5236
  }) {
5098
- const root = resolve(cwd);
5237
+ const root = resolve2(cwd);
5099
5238
  let resolved;
5100
5239
  if (config) {
5101
5240
  resolved = resolveConfig2(checkConfig2(config));
@@ -5120,6 +5259,7 @@ async function plan2(options = {}) {
5120
5259
  ...options.cache !== undefined && { cache: options.cache },
5121
5260
  mergeBase: true
5122
5261
  });
5262
+ const { shards, timings } = await shardPlan(root, config, computed.plan);
5123
5263
  return {
5124
5264
  suites: computed.plan.suites,
5125
5265
  cases: computed.plan.cases,
@@ -5133,8 +5273,10 @@ async function plan2(options = {}) {
5133
5273
  narrowed: computed.narrowed.map(({ narrower, result }) => ({
5134
5274
  name: narrower.name,
5135
5275
  result
5136
- }))
5276
+ })),
5277
+ shards,
5278
+ timings
5137
5279
  };
5138
5280
  }
5139
5281
 
5140
- export { QUERY, UNDECLARED_READS, RELATIVE, aliasPath, createResolver, SCANNED, formatOf, extract, stringsOf, finish, gotoPage, walksByTypes, importersOf, affectedBy, sorted, isPackageFile, formatSelection, orphanOf, readSources2, collectPackages2, extractRevision2, graphView2, computePlan2, prepare2, plan2 };
5282
+ export { QUERY, UNDECLARED_READS, RELATIVE, aliasPath, createResolver, SCANNED, formatOf, extract, stringsOf, finish, gotoPage, walksByTypes, importersOf, affectedBy, sorted, isPackageFile, formatSelection, orphanOf, loadTimings, timingsWarning, durationsOf, longestFirst, shardPlan, readSources2, collectPackages2, extractRevision2, graphView2, computePlan2, prepare2, plan2 };