@terreno/test 57.17.0 → 57.19.2
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/CHANGELOG.md +1681 -0
- package/README.md +6 -0
- package/dist/assertions/expectAPIError.d.ts +18 -0
- package/dist/assertions/expectAPIError.js +33 -0
- package/dist/assertions/strictResponseProperties.d.ts +14 -0
- package/dist/assertions/strictResponseProperties.js +37 -0
- package/dist/cache/mongoTestCache.js +2 -2
- package/dist/cli/collectFiles.d.ts +38 -0
- package/dist/cli/collectFiles.js +73 -0
- package/dist/cli/testHygiene.d.ts +134 -0
- package/dist/cli/testHygiene.js +357 -0
- package/dist/cli/testHygieneCli.d.ts +2 -0
- package/dist/cli/testHygieneCli.js +5 -0
- package/dist/cli/testHygieneRules.d.ts +58 -0
- package/dist/cli/testHygieneRules.js +517 -0
- package/dist/cli/testShard.d.ts +155 -0
- package/dist/cli/testShard.js +377 -0
- package/dist/cli/testShardCli.d.ts +2 -0
- package/dist/cli/testShardCli.js +5 -0
- package/dist/expo/authTokenStorage.d.ts +76 -0
- package/dist/expo/authTokenStorage.js +160 -0
- package/dist/expo/expoModuleMocks.d.ts +24 -0
- package/dist/expo/expoModuleMocks.js +456 -0
- package/dist/expo/libraryModuleMocks.d.ts +17 -0
- package/dist/expo/libraryModuleMocks.js +347 -0
- package/dist/expo/mockHelpers.d.ts +39 -0
- package/dist/expo/mockHelpers.js +45 -0
- package/dist/expo/mockModuleAliases.d.ts +50 -0
- package/dist/expo/mockModuleAliases.js +174 -0
- package/dist/expo/reactNativeMock.d.ts +39 -0
- package/dist/expo/reactNativeMock.js +421 -0
- package/dist/expo/registerExpoTestEnvironment.d.ts +69 -0
- package/dist/expo/registerExpoTestEnvironment.js +163 -0
- package/dist/expo/renderWithTerreno.d.ts +47 -0
- package/dist/expo/renderWithTerreno.js +47 -0
- package/dist/http/authWithToken.d.ts +20 -0
- package/dist/http/authWithToken.js +30 -0
- package/dist/index.d.ts +14 -1
- package/dist/index.js +33 -1
- package/dist/logging/silenceLogs.js +0 -2
- package/dist/mocks/aiSdk.d.ts +77 -0
- package/dist/mocks/aiSdk.js +102 -0
- package/dist/mocks/cloudTasks.d.ts +91 -0
- package/dist/mocks/cloudTasks.js +197 -0
- package/dist/mocks/expoPush.d.ts +53 -0
- package/dist/mocks/expoPush.js +57 -0
- package/dist/mocks/gcs.d.ts +101 -0
- package/dist/mocks/gcs.js +162 -0
- package/dist/mocks/googleAuth.d.ts +69 -0
- package/dist/mocks/googleAuth.js +55 -0
- package/dist/mocks/mockGroup.d.ts +11 -0
- package/dist/mocks/mockGroup.js +21 -0
- package/dist/mocks/notifiers.d.ts +21 -0
- package/dist/mocks/notifiers.js +26 -0
- package/dist/mocks/sendgrid.d.ts +22 -0
- package/dist/mocks/sendgrid.js +32 -0
- package/dist/mocks/twilio.d.ts +72 -0
- package/dist/mocks/twilio.js +64 -0
- package/dist/preload/registerBackendPreload.js +4 -1
- package/dist/time/freezeTime.d.ts +16 -0
- package/dist/time/freezeTime.js +49 -0
- package/dist/transaction/testTransaction.d.ts +43 -6
- package/dist/transaction/testTransaction.js +371 -92
- package/dist/utils/waitUntil.d.ts +22 -0
- package/dist/utils/waitUntil.js +34 -0
- package/package.json +53 -8
- package/src/assertions/expectAPIError.ts +51 -0
- package/src/assertions/strictResponseProperties.ts +42 -0
- package/src/cache/mongoTestCache.ts +2 -2
- package/src/cli/collectFiles.ts +115 -0
- package/src/cli/testHygiene.ts +539 -0
- package/src/cli/testHygieneCli.ts +4 -0
- package/src/cli/testHygieneRules.ts +719 -0
- package/src/cli/testShard.ts +532 -0
- package/src/cli/testShardCli.ts +4 -0
- package/src/expo/authTokenStorage.ts +198 -0
- package/src/expo/expoModuleMocks.ts +503 -0
- package/src/expo/libraryModuleMocks.ts +411 -0
- package/src/expo/mockHelpers.ts +73 -0
- package/src/expo/mockModuleAliases.ts +252 -0
- package/src/expo/reactNativeMock.ts +534 -0
- package/src/expo/registerExpoTestEnvironment.ts +260 -0
- package/src/expo/renderWithTerreno.ts +79 -0
- package/src/http/authWithToken.ts +38 -0
- package/src/index.ts +82 -1
- package/src/logging/silenceLogs.ts +0 -2
- package/src/mocks/aiSdk.ts +199 -0
- package/src/mocks/cloudTasks.ts +323 -0
- package/src/mocks/expoPush.ts +134 -0
- package/src/mocks/gcs.ts +305 -0
- package/src/mocks/googleAuth.ts +136 -0
- package/src/mocks/mockGroup.ts +32 -0
- package/src/mocks/notifiers.ts +36 -0
- package/src/mocks/sendgrid.ts +57 -0
- package/src/mocks/twilio.ts +143 -0
- package/src/preload/registerBackendPreload.ts +4 -1
- package/src/time/freezeTime.ts +61 -0
- package/src/transaction/testTransaction.ts +456 -124
- package/src/utils/waitUntil.ts +47 -0
- package/biome.jsonc +0 -20
- package/bunfig.toml +0 -16
- package/src/cache/mongoTestCache.test.ts +0 -103
- package/src/env/setTerrenoTestEnv.test.ts +0 -26
- package/src/http/authAsUser.test.ts +0 -40
- package/src/http/getBaseServer.test.ts +0 -45
- package/src/logging/setupEnvironment.test.ts +0 -24
- package/src/logging/silenceLogs.test.ts +0 -52
- package/src/logging/testLogger.test.ts +0 -58
- package/src/mocks/sentryBun.test.ts +0 -230
- package/src/mongo/connection.test.ts +0 -43
- package/src/mongo/mongoServer.test.ts +0 -99
- package/src/preload/registerBackendPreload.test.ts +0 -10
- package/src/transaction/testTransaction.test.ts +0 -64
- package/src/utils/helpers.test.ts +0 -46
- package/tsconfig.json +0 -18
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface WaitUntilOptions {
|
|
2
|
+
/** Maximum time to poll before throwing. Keep it below the test's own timeout so this error wins. */
|
|
3
|
+
timeoutMs?: number;
|
|
4
|
+
/** Delay between polling attempts. */
|
|
5
|
+
intervalMs?: number;
|
|
6
|
+
/** Named in the timeout error so a failure says what never happened. */
|
|
7
|
+
label?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Polls `predicate` until it returns (or resolves to) true, or throws after `timeoutMs`.
|
|
11
|
+
*
|
|
12
|
+
* Use it instead of a fixed sleep: it returns as soon as the condition holds and fails with the
|
|
13
|
+
* label and elapsed time when it never does. Elapsed time uses `performance.now()`, so polling
|
|
14
|
+
* still advances while `Date.now()` is frozen by `freezeTime` or `setSystemTime`.
|
|
15
|
+
*/
|
|
16
|
+
export declare const waitUntil: (predicate: () => boolean | Promise<boolean>, { timeoutMs, intervalMs, label }?: WaitUntilOptions) => Promise<void>;
|
|
17
|
+
/** Waits until a Bun `mock()` / `spyOn()` function has been called at least `callCount` times. */
|
|
18
|
+
export declare const waitForCalls: (mockFn: {
|
|
19
|
+
mock: {
|
|
20
|
+
calls: unknown[];
|
|
21
|
+
};
|
|
22
|
+
}, callCount: number, options?: WaitUntilOptions) => Promise<void>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.waitForCalls = exports.waitUntil = void 0;
|
|
4
|
+
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
5
|
+
/**
|
|
6
|
+
* Polls `predicate` until it returns (or resolves to) true, or throws after `timeoutMs`.
|
|
7
|
+
*
|
|
8
|
+
* Use it instead of a fixed sleep: it returns as soon as the condition holds and fails with the
|
|
9
|
+
* label and elapsed time when it never does. Elapsed time uses `performance.now()`, so polling
|
|
10
|
+
* still advances while `Date.now()` is frozen by `freezeTime` or `setSystemTime`.
|
|
11
|
+
*/
|
|
12
|
+
const waitUntil = async (predicate, { timeoutMs = 1000, intervalMs = 10, label } = {}) => {
|
|
13
|
+
const start = performance.now();
|
|
14
|
+
for (;;) {
|
|
15
|
+
if (await predicate()) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const elapsed = performance.now() - start;
|
|
19
|
+
if (elapsed >= timeoutMs) {
|
|
20
|
+
const labelSuffix = label ? ` waiting for: ${label}` : "";
|
|
21
|
+
throw new Error(`waitUntil timed out after ${Math.round(elapsed)}ms${labelSuffix}`);
|
|
22
|
+
}
|
|
23
|
+
await delay(intervalMs);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
exports.waitUntil = waitUntil;
|
|
27
|
+
/** Waits until a Bun `mock()` / `spyOn()` function has been called at least `callCount` times. */
|
|
28
|
+
const waitForCalls = async (mockFn, callCount, options = {}) => {
|
|
29
|
+
await (0, exports.waitUntil)(() => mockFn.mock.calls.length >= callCount, {
|
|
30
|
+
label: `mock to be called ${callCount} time(s) (called ${mockFn.mock.calls.length})`,
|
|
31
|
+
...options,
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
exports.waitForCalls = waitForCalls;
|
package/package.json
CHANGED
|
@@ -1,35 +1,58 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "Josh Gachnang",
|
|
3
|
+
"bin": {
|
|
4
|
+
"terreno-test-hygiene": "./dist/cli/testHygieneCli.js",
|
|
5
|
+
"terreno-test-shard": "./dist/cli/testShardCli.js"
|
|
6
|
+
},
|
|
3
7
|
"bugs": {
|
|
4
8
|
"url": "https://github.com/TerrenoLabs/terreno/issues"
|
|
5
9
|
},
|
|
6
10
|
"dependencies": {
|
|
11
|
+
"@types/supertest": "^7.2.0",
|
|
7
12
|
"express": "^5.2.1",
|
|
8
13
|
"luxon": "^3.7.2",
|
|
14
|
+
"mongodb-memory-server": "^11.0.1",
|
|
15
|
+
"mongodb-memory-server-core": "^11.0.1",
|
|
16
|
+
"mongodb-memory-server-global": "^11.0.1",
|
|
9
17
|
"qs": "^6.15.0",
|
|
18
|
+
"supertest": "^7.2.2",
|
|
19
|
+
"typescript": "~6.0.3",
|
|
10
20
|
"winston": "^3.18.3"
|
|
11
21
|
},
|
|
12
22
|
"description": "Shared Bun test helpers, MongoDB preload utilities, and HTTP fixtures — part of Terreno, the batteries-included TypeScript framework for universal apps.",
|
|
13
23
|
"devDependencies": {
|
|
14
24
|
"@biomejs/biome": "^2.3.6",
|
|
25
|
+
"@reduxjs/toolkit": "^2.11.1",
|
|
26
|
+
"@terreno/ui": "57.19.2",
|
|
27
|
+
"@testing-library/react-native": "^13.2.0",
|
|
15
28
|
"@types/bun": "^1.4.0",
|
|
16
29
|
"@types/express": "^5.0.6",
|
|
17
30
|
"@types/luxon": "^3.7.1",
|
|
18
|
-
"@types/
|
|
19
|
-
"mongodb-memory-server": "^11.0.1",
|
|
20
|
-
"mongodb-memory-server-core": "^11.0.1",
|
|
21
|
-
"mongodb-memory-server-global": "^11.0.1",
|
|
31
|
+
"@types/react": "~19.2.14",
|
|
22
32
|
"mongoose": "9.7.4",
|
|
23
|
-
"
|
|
24
|
-
"
|
|
33
|
+
"react": "19.2.3",
|
|
34
|
+
"react-native": "0.86.2",
|
|
35
|
+
"react-redux": "^9.2.0"
|
|
25
36
|
},
|
|
26
37
|
"exports": {
|
|
27
38
|
".": {
|
|
28
39
|
"default": "./dist/index.js",
|
|
29
40
|
"types": "./dist/index.d.ts"
|
|
30
41
|
},
|
|
42
|
+
"./expo/*": {
|
|
43
|
+
"default": "./src/expo/*.ts",
|
|
44
|
+
"types": "./dist/expo/*.d.ts"
|
|
45
|
+
},
|
|
31
46
|
"./preload/memoryMongo": "./src/preload/memoryMongo.ts"
|
|
32
47
|
},
|
|
48
|
+
"files": [
|
|
49
|
+
"LICENSE",
|
|
50
|
+
"CHANGELOG.md",
|
|
51
|
+
"README.md",
|
|
52
|
+
"dist/**/*",
|
|
53
|
+
"src/**/*.ts",
|
|
54
|
+
"!src/**/*.test.ts"
|
|
55
|
+
],
|
|
33
56
|
"homepage": "https://github.com/TerrenoLabs/terreno#readme",
|
|
34
57
|
"keywords": [
|
|
35
58
|
"bun",
|
|
@@ -42,7 +65,29 @@
|
|
|
42
65
|
"main": "dist/index.js",
|
|
43
66
|
"name": "@terreno/test",
|
|
44
67
|
"peerDependencies": {
|
|
45
|
-
"
|
|
68
|
+
"@terreno/ui": "57.19.2",
|
|
69
|
+
"@testing-library/react-native": "^13.2.0",
|
|
70
|
+
"mongoose": "^8.0.0 || ^9.0.0",
|
|
71
|
+
"react": "19.2.3",
|
|
72
|
+
"react-native": "0.86.2",
|
|
73
|
+
"react-redux": "^9.2.0"
|
|
74
|
+
},
|
|
75
|
+
"peerDependenciesMeta": {
|
|
76
|
+
"@terreno/ui": {
|
|
77
|
+
"optional": true
|
|
78
|
+
},
|
|
79
|
+
"@testing-library/react-native": {
|
|
80
|
+
"optional": true
|
|
81
|
+
},
|
|
82
|
+
"react": {
|
|
83
|
+
"optional": true
|
|
84
|
+
},
|
|
85
|
+
"react-native": {
|
|
86
|
+
"optional": true
|
|
87
|
+
},
|
|
88
|
+
"react-redux": {
|
|
89
|
+
"optional": true
|
|
90
|
+
}
|
|
46
91
|
},
|
|
47
92
|
"publishConfig": {
|
|
48
93
|
"access": "public"
|
|
@@ -64,5 +109,5 @@
|
|
|
64
109
|
"test:coverage": "bun run ../scripts/check-coverage.ts"
|
|
65
110
|
},
|
|
66
111
|
"types": "dist/index.d.ts",
|
|
67
|
-
"version": "57.
|
|
112
|
+
"version": "57.19.2"
|
|
68
113
|
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import {expect} from "bun:test";
|
|
2
|
+
|
|
3
|
+
export interface ExpectedAPIError {
|
|
4
|
+
status: number;
|
|
5
|
+
title?: string | RegExp;
|
|
6
|
+
detail?: string | RegExp;
|
|
7
|
+
code?: string;
|
|
8
|
+
/** Partial match against `meta.fields`, the per-field validation messages. */
|
|
9
|
+
fields?: Record<string, unknown>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface ResponseLike {
|
|
13
|
+
status: number;
|
|
14
|
+
body: unknown;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const expectText = (actual: unknown, expected: string | RegExp): void => {
|
|
18
|
+
if (expected instanceof RegExp) {
|
|
19
|
+
expect(String(actual)).toMatch(expected);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
expect(actual).toBe(expected);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Asserts a supertest response is the JSONAPI error body `@terreno/api` sends for an `APIError`:
|
|
27
|
+
* `{status, title, detail?, code?, meta?: {fields?}}`. Only the keys you pass are checked.
|
|
28
|
+
*/
|
|
29
|
+
export const expectAPIError = (res: ResponseLike, expected: ExpectedAPIError): void => {
|
|
30
|
+
const body = (res.body ?? {}) as {
|
|
31
|
+
code?: unknown;
|
|
32
|
+
detail?: unknown;
|
|
33
|
+
meta?: {fields?: unknown};
|
|
34
|
+
status?: unknown;
|
|
35
|
+
title?: unknown;
|
|
36
|
+
};
|
|
37
|
+
expect(res.status).toBe(expected.status);
|
|
38
|
+
expect(body.status).toBe(expected.status);
|
|
39
|
+
if (expected.title !== undefined) {
|
|
40
|
+
expectText(body.title, expected.title);
|
|
41
|
+
}
|
|
42
|
+
if (expected.detail !== undefined) {
|
|
43
|
+
expectText(body.detail, expected.detail);
|
|
44
|
+
}
|
|
45
|
+
if (expected.code !== undefined) {
|
|
46
|
+
expect(body.code).toBe(expected.code);
|
|
47
|
+
}
|
|
48
|
+
if (expected.fields !== undefined) {
|
|
49
|
+
expect(body.meta?.fields ?? {}).toMatchObject(expected.fields);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const isPlainObject = (value: unknown): value is Record<string, unknown> =>
|
|
2
|
+
typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Lists the dot-separated path of every property in `value`. Arrays add no path segment, so
|
|
6
|
+
* `{data: [{email}]}` yields `data` and `data.email`; that lets one allowlist cover list and
|
|
7
|
+
* single-document responses.
|
|
8
|
+
*/
|
|
9
|
+
export const collectPropertyPaths = (value: unknown, prefix = ""): string[] => {
|
|
10
|
+
if (Array.isArray(value)) {
|
|
11
|
+
return [...new Set(value.flatMap((item) => collectPropertyPaths(item, prefix)))];
|
|
12
|
+
}
|
|
13
|
+
if (!isPlainObject(value)) {
|
|
14
|
+
return [];
|
|
15
|
+
}
|
|
16
|
+
return Object.entries(value).flatMap(([key, child]) => {
|
|
17
|
+
const path = prefix ? `${prefix}.${key}` : key;
|
|
18
|
+
return [path, ...collectPropertyPaths(child, path)];
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const isPathAllowed = (path: string, allowedPaths: string[]): boolean =>
|
|
23
|
+
allowedPaths.some(
|
|
24
|
+
(allowed) =>
|
|
25
|
+
allowed === path || allowed.startsWith(`${path}.`) || path.startsWith(`${allowed}.`)
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Fails when a response exposes any property outside `allowedPaths`.
|
|
30
|
+
*
|
|
31
|
+
* Guards permission-scoped endpoints against leaking fields (PHI, secrets) when someone adds a
|
|
32
|
+
* field to a model or a populate. A path is allowed when it is listed, is an ancestor of a listed
|
|
33
|
+
* path, or sits under a listed path (listing `address` allows `address.city`).
|
|
34
|
+
*/
|
|
35
|
+
export const assertStrictResponseProperties = (body: unknown, allowedPaths: string[]): void => {
|
|
36
|
+
const unexpected = collectPropertyPaths(body).filter(
|
|
37
|
+
(path) => !isPathAllowed(path, allowedPaths)
|
|
38
|
+
);
|
|
39
|
+
if (unexpected.length > 0) {
|
|
40
|
+
throw new Error(`Unexpected field(s) in response: ${unexpected.join(", ")}`);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
@@ -202,8 +202,8 @@ const loadTestDataIntoDb = async (cachedCollections: Record<string, unknown[]>):
|
|
|
202
202
|
}
|
|
203
203
|
const convertedDocs = docs.map((doc) => convertValue(doc));
|
|
204
204
|
restorePromises.push(
|
|
205
|
-
|
|
206
|
-
.
|
|
205
|
+
requireDb()
|
|
206
|
+
.collection(collectionName)
|
|
207
207
|
.insertMany(convertedDocs as Record<string, unknown>[], {ordered: false})
|
|
208
208
|
);
|
|
209
209
|
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {existsSync, readdirSync, statSync} from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
/** Glob patterns (relative to the working directory) that identify a test file by default. */
|
|
5
|
+
export const DEFAULT_TEST_PATTERNS: readonly string[] = ["**/*.test.ts", "**/*.test.tsx"];
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Directory names skipped at any depth while walking. Build output, native projects, and
|
|
9
|
+
* Playwright `e2e/` suites are never `bun test` files, and walking `node_modules` is slow.
|
|
10
|
+
*/
|
|
11
|
+
export const DEFAULT_EXCLUDED_DIRS: readonly string[] = [
|
|
12
|
+
".expo",
|
|
13
|
+
".git",
|
|
14
|
+
"android",
|
|
15
|
+
"build",
|
|
16
|
+
"coverage",
|
|
17
|
+
"dist",
|
|
18
|
+
"e2e",
|
|
19
|
+
"ios",
|
|
20
|
+
"node_modules",
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
export interface CollectFilesOptions {
|
|
24
|
+
/** Files or directories to search, absolute or relative to `cwd`. */
|
|
25
|
+
paths: string[];
|
|
26
|
+
/** Directory that patterns and returned paths are relative to. */
|
|
27
|
+
cwd: string;
|
|
28
|
+
/** Globs matched against each file's `cwd`-relative POSIX path. Defaults to `DEFAULT_TEST_PATTERNS`. */
|
|
29
|
+
patterns?: readonly string[];
|
|
30
|
+
/** Directory names skipped at any depth. Defaults to `DEFAULT_EXCLUDED_DIRS`. */
|
|
31
|
+
excludeDirs?: readonly string[];
|
|
32
|
+
/** Globs matched against each file's `cwd`-relative POSIX path; matches are dropped. */
|
|
33
|
+
exclude?: readonly string[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface CollectFilesResult {
|
|
37
|
+
/** Sorted, de-duplicated `cwd`-relative POSIX paths. */
|
|
38
|
+
files: string[];
|
|
39
|
+
/** Input paths that do not exist. */
|
|
40
|
+
missingPaths: string[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Converts a native path to forward-slash form so globs and output are platform independent. */
|
|
44
|
+
export const toPosixPath = (filePath: string): string => filePath.replaceAll("\\", "/");
|
|
45
|
+
|
|
46
|
+
/** Returns whether `filePath` matches at least one glob. */
|
|
47
|
+
export const matchesAnyGlob = ({
|
|
48
|
+
filePath,
|
|
49
|
+
globs,
|
|
50
|
+
}: {
|
|
51
|
+
filePath: string;
|
|
52
|
+
globs: readonly string[];
|
|
53
|
+
}): boolean => globs.some((glob) => new Bun.Glob(glob).match(filePath));
|
|
54
|
+
|
|
55
|
+
const walkFiles = ({
|
|
56
|
+
dir,
|
|
57
|
+
excludeDirs,
|
|
58
|
+
out,
|
|
59
|
+
}: {
|
|
60
|
+
dir: string;
|
|
61
|
+
excludeDirs: ReadonlySet<string>;
|
|
62
|
+
out: string[];
|
|
63
|
+
}): void => {
|
|
64
|
+
for (const entry of readdirSync(dir, {withFileTypes: true})) {
|
|
65
|
+
if (excludeDirs.has(entry.name)) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const fullPath = path.join(dir, entry.name);
|
|
69
|
+
if (entry.isDirectory()) {
|
|
70
|
+
walkFiles({dir: fullPath, excludeDirs, out});
|
|
71
|
+
} else if (entry.isFile()) {
|
|
72
|
+
out.push(fullPath);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Recursively collects files under `paths` whose `cwd`-relative path matches `patterns`,
|
|
79
|
+
* skipping excluded directory names and `exclude` globs. Discovery is filesystem driven so a
|
|
80
|
+
* newly added file is always picked up without editing a hand-maintained list.
|
|
81
|
+
*/
|
|
82
|
+
export const collectFiles = ({
|
|
83
|
+
paths,
|
|
84
|
+
cwd,
|
|
85
|
+
patterns = DEFAULT_TEST_PATTERNS,
|
|
86
|
+
excludeDirs = DEFAULT_EXCLUDED_DIRS,
|
|
87
|
+
exclude = [],
|
|
88
|
+
}: CollectFilesOptions): CollectFilesResult => {
|
|
89
|
+
const excludedDirNames = new Set(excludeDirs);
|
|
90
|
+
const missingPaths: string[] = [];
|
|
91
|
+
const absoluteFiles: string[] = [];
|
|
92
|
+
|
|
93
|
+
for (const target of paths) {
|
|
94
|
+
const absoluteTarget = path.resolve(cwd, target);
|
|
95
|
+
if (!existsSync(absoluteTarget)) {
|
|
96
|
+
missingPaths.push(target);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (statSync(absoluteTarget).isFile()) {
|
|
100
|
+
absoluteFiles.push(absoluteTarget);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
walkFiles({dir: absoluteTarget, excludeDirs: excludedDirNames, out: absoluteFiles});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const files = [
|
|
107
|
+
...new Set(absoluteFiles.map((file) => toPosixPath(path.relative(cwd, file)))),
|
|
108
|
+
].filter(
|
|
109
|
+
(file) =>
|
|
110
|
+
matchesAnyGlob({filePath: file, globs: patterns}) &&
|
|
111
|
+
!matchesAnyGlob({filePath: file, globs: exclude})
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
return {files: files.sort(), missingPaths};
|
|
115
|
+
};
|