Skip to content

Commit 99713f3

Browse files
committed
fix: do not search node_modules directories for test files
Closes #412
1 parent 1734d12 commit 99713f3

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

lib/utils.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
11
import * as path from "@std/path";
22
import { assertEquals, assertRejects } from "@std/assert";
3-
import { getDntVersion, runCommand, valueToUrl } from "./utils.ts";
3+
import { getDntVersion, glob, runCommand, valueToUrl } from "./utils.ts";
4+
5+
Deno.test("glob should not search node_modules or excluded dirs", async () => {
6+
const rootDir = await Deno.makeTempDir();
7+
try {
8+
const testFilePaths = [
9+
["mod.test.ts"],
10+
["sub", "mod.test.ts"],
11+
["node_modules", "pkg", "mod.test.ts"],
12+
["sub", "node_modules", "pkg", "mod.test.ts"],
13+
["npm", "mod.test.ts"],
14+
];
15+
for (const filePath of testFilePaths) {
16+
const absPath = path.join(rootDir, ...filePath);
17+
await Deno.mkdir(path.dirname(absPath), { recursive: true });
18+
await Deno.writeTextFile(absPath, "");
19+
}
20+
21+
const paths = await glob({
22+
pattern: "**/*.test.ts",
23+
rootDir,
24+
excludeDirs: [path.join(rootDir, "npm")],
25+
});
26+
27+
assertEquals(
28+
paths.map((p) => path.relative(rootDir, p)).sort(),
29+
["mod.test.ts", path.join("sub", "mod.test.ts")],
30+
);
31+
} finally {
32+
await Deno.remove(rootDir, { recursive: true });
33+
}
34+
});
435

536
Deno.test({
637
name: "should error when command doesn't exist",

lib/utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
import { expandGlob } from "@std/fs/expand-glob";
44
import * as path from "@std/path";
55

6-
/** Gets the files found in the provided root dir path based on the glob. */
6+
/**
7+
* Gets the files found in the provided root dir path based on the glob.
8+
*
9+
* Any `node_modules` directory is never searched.
10+
*/
711
export async function glob(options: {
812
pattern: string;
913
rootDir: string;
@@ -14,7 +18,7 @@ export async function glob(options: {
1418
root: options.rootDir,
1519
extended: true,
1620
globstar: true,
17-
exclude: options.excludeDirs,
21+
exclude: [...options.excludeDirs, "**/node_modules"],
1822
});
1923
for await (const entry of entries) {
2024
if (entry.isFile) {

mod.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ export interface BuildOptions {
9898
skipSourceOutput?: boolean;
9999
/** Root directory to find test files in. Defaults to the cwd. */
100100
rootTestDir?: string;
101-
/** Glob pattern to use to find tests files. Defaults to `deno test`'s pattern. */
101+
/**
102+
* Glob pattern to use to find tests files. Defaults to `deno test`'s pattern.
103+
*
104+
* Note that `node_modules` directories are never searched.
105+
*/
102106
testPattern?: string;
103107
/**
104108
* Specifiers to map from and to.

0 commit comments

Comments
 (0)