Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"test",
"scripts/verify-built-boot.ts",
"scripts/warm-dep-cache.ts",
"../../scripts/lib/cli-test-files.ts",
"../../scripts/lib/public-config.ts",
"../../scripts/lib/tagged-source-version.ts",
"../../scripts/lib/third-party-licenses.ts"
Expand Down
12 changes: 11 additions & 1 deletion apps/web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import pdfjsPkg from "pdfjs-dist/package.json" with { type: "json" };

import { DEV_PROXIED_PATH_PREFIXES } from "@t3tools/shared/devProxy";

import { cliTestFiles } from "../../scripts/lib/cli-test-files";
import { loadRepoEnv } from "../../scripts/lib/public-config";
import { resolveTaggedSourceVersion } from "../../scripts/lib/tagged-source-version";
import { thirdPartyLicensesPlugin } from "../../scripts/lib/third-party-licenses";
Expand Down Expand Up @@ -95,6 +96,12 @@ const unitTestProject = {
},
} satisfies TestProjectInlineConfiguration;

// Starting the browser project costs several seconds; skip whichever project
// a run of named test files does not need.
const namedTestFiles = await cliTestFiles();
const namesBrowserTest = namedTestFiles?.some((file) => /\.browser\.test\.tsx?$/.test(file));
const namesUnitTest = namedTestFiles?.some((file) => !/\.browser\.test\.tsx?$/.test(file));

// Hash checkout paths to ports 20000-23999; Vitest's fixed port collides under inherited strictPort.
// Stay below ephemeral ranges: Windows reserves Hyper-V/WSL blocks there and EACCES is not skipped.
const browserTestApiPort = 20_000 + ((Hash.string(import.meta.dirname) >>> 0) % 4_000);
Expand Down Expand Up @@ -426,7 +433,10 @@ export default defineConfig(({ command }) => {
},
worker: { format: "es" },
test: {
projects: [defineProject(unitTestProject), defineProject(browserTestProject)],
projects: [
...(namesUnitTest === false ? [] : [defineProject(unitTestProject)]),
...(namesBrowserTest === false ? [] : [defineProject(browserTestProject)]),
],
},
};
});
27 changes: 27 additions & 0 deletions scripts/lib/cli-test-files.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, it } from "vite-plus/test";

import { cliTestFiles } from "./cli-test-files.ts";

const root = `${import.meta.dirname}/../..`;
const vitest = ["/bin/node", "/x/node_modules/vitest/vitest.mjs"];
const files = (...args: string[]) => cliTestFiles([...vitest, ...args], root);
const thisFile = "scripts/lib/cli-test-files.test.ts";
const otherFile = "apps/web/src/lib/assistantCitationNavigation.test.ts";

describe("cliTestFiles", () => {
it("returns the named test files, not flag values", async () => {
expect(await files("run", "--passWithNoTests", `${thisFile}:12`, "-t", "a name")).toEqual([
thisFile,
]);
expect(await files("run", "--exclude", otherFile, thisFile)).toEqual([thisFile]);
});

it("keeps every project unless each filter is an existing test file", async () => {
expect(await files("run")).toBeUndefined();
expect(await files("run", thisFile, "cli-test-files")).toBeUndefined();
expect(await files("run", "scripts/lib/missing.test.ts")).toBeUndefined();
expect(await files("run", "--unknownFlag", thisFile)).toBeUndefined();
expect(await files("run", "--root", "apps/web", thisFile)).toBeUndefined();
expect(await files(thisFile)).toBeUndefined();
});
});
42 changes: 42 additions & 0 deletions scripts/lib/cli-test-files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Option from "effect/Option";
import * as Path from "effect/Path";

const TEST_FILE = /\.(test|spec)\.[cm]?[jt]sx?$/;

const existingTestFiles = Effect.fn("cliTestFiles.existingTestFiles")(function* (
filter: ReadonlyArray<string>,
cwd: string,
) {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const repoRoot = path.resolve(import.meta.dirname, "../..");
const files: string[] = [];
for (const arg of filter) {
const file = path.resolve(cwd, arg.replace(/:\d+$/, ""));
if (!TEST_FILE.test(file)) return undefined;
const info = yield* fs.stat(file).pipe(Effect.option);
if (Option.isNone(info) || info.value.type !== "File") return undefined;
files.push(path.relative(repoRoot, file).replaceAll("\\", "/"));
}
return files;
});

/**
* Repo-relative paths of the test files a `vitest run` command line names, or undefined
* unless every filter is an existing test file. Configs use it to set up only the
* projects that own those files; undefined keeps every project.
*/
export async function cliTestFiles(argv = process.argv, cwd = process.cwd()) {
const vitest = argv.findIndex((arg) => /[\\/]vitest(\.mjs)?$/.test(arg));
if (vitest === -1 || argv[vitest + 1] !== "run") return undefined;
// Loaded only for test runs so dev servers and builds skip them.
const [{ parseCLI }, NodeServices] = await Promise.all([
import("vite-plus/test/node"),
import("@effect/platform-node/NodeServices"),
]);
const { filter, options } = parseCLI(["vitest", ...argv.slice(vitest + 1)]);
if (options.root !== undefined || filter.length === 0) return undefined;
return Effect.runPromise(existingTestFiles(filter, cwd).pipe(Effect.provide(NodeServices.layer)));
}
42 changes: 30 additions & 12 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defineConfig } from "vite-plus";
import * as NodeURL from "node:url";
import { remoteCacheUrl } from "./infra/task-cache/src/remoteCache.ts";
import { cachedTestTasks } from "./scripts/lib/cached-tests.ts";
import { cliTestFiles } from "./scripts/lib/cli-test-files.ts";

const TEST_EXCLUDE = [
"**/.repos/**",
Expand All @@ -28,6 +29,19 @@ const TEST_CONFIG_PACKAGES = [
// from their own directory, as CI does, so they keep one plain project.
const isRepoRootTestRun = process.cwd() === import.meta.dirname;

// Setting up every package's project costs 5-20 s; a run that names test files
// only needs the projects that own them.
const ownedTestProjects = await (async () => {
const files = isRepoRootTestRun ? await cliTestFiles() : undefined;
if (!files) return undefined;
const owner = (file: string) => {
if (file.startsWith("native/kde-snap-shot/")) return "apps/desktop";
return TEST_CONFIG_PACKAGES.find((dir) => file.startsWith(`${dir}/`)) ?? "root";
};
return new Set(files.map(owner));
})();
const includesTestProject = (project: string) => ownedTestProjects?.has(project) ?? true;

const RESTRICTED_IMPORT_PATHS = [
{
name: "react-native",
Expand Down Expand Up @@ -154,18 +168,22 @@ export default defineConfig({
...(isRepoRootTestRun
? {
projects: [
...TEST_CONFIG_PACKAGES,
{
extends: true,
test: {
name: "root",
exclude: [
...TEST_EXCLUDE,
...TEST_CONFIG_PACKAGES.map((dir) => `${dir}/**`),
"native/kde-snap-shot/**",
],
},
},
...TEST_CONFIG_PACKAGES.filter(includesTestProject),
...(includesTestProject("root")
? [
{
extends: true,
test: {
name: "root",
exclude: [
...TEST_EXCLUDE,
...TEST_CONFIG_PACKAGES.map((dir) => `${dir}/**`),
"native/kde-snap-shot/**",
],
},
},
]
: []),
],
}
: {}),
Expand Down
Loading