Skip to content

Commit 2ac4c04

Browse files
Fix Bun test defaults (#1860)
1 parent 6e6a509 commit 2ac4c04

10 files changed

Lines changed: 79 additions & 9 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './index.ts';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "@plugins/bun6",
3+
"scripts": {
4+
"test": "bun --config=\"$(git rev-parse --show-toplevel)/bunfig.toml\" test"
5+
}
6+
}

packages/knip/fixtures/plugins/bun7/bun.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './index.ts';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "@plugins/bun7",
3+
"devDependencies": {
4+
"bun": "*"
5+
}
6+
}

packages/knip/src/plugins/bun/index.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
import parseArgs from '../../util/parse-args.ts';
22
import type { IsPluginEnabled, Plugin, Resolve, ResolveConfig } from '../../types/config.ts';
3-
import { toDeferResolve, toEntry } from '../../util/input.ts';
3+
import { isFile } from '../../util/fs.ts';
4+
import { toDeferResolve, toEntry, toIgnore } from '../../util/input.ts';
45
import type { BunfigConfig } from './types.ts';
56

67
// https://bun.sh/docs/cli/test
78

89
const title = 'Bun';
910

10-
const enablers = ['bun'];
11+
const enablers =
12+
'This plugin is enabled when a `bun.lock` or `bun.lockb` file is found or a `bun test` script is configured.';
13+
14+
const getBunTestArgs = (script: string) => {
15+
const args = script.split(/\s+/);
16+
const bunIndex = args.indexOf('bun');
17+
const testIndex = bunIndex === -1 ? -1 : args.indexOf('test', bunIndex + 1);
18+
if (args.slice(bunIndex + 1, testIndex).includes('run')) return;
19+
return testIndex === -1 ? undefined : args.slice(testIndex + 1);
20+
};
1121

1222
const hasBunTest = (scripts: Record<string, string> | undefined) =>
13-
scripts && Object.values(scripts).some(script => /(?<=^|\s)bun test/.test(script));
23+
scripts && Object.values(scripts).some(script => typeof script === 'string' && getBunTestArgs(script));
1424

15-
const isEnabled: IsPluginEnabled = ({ manifest }) => !!hasBunTest(manifest.scripts);
25+
const isEnabled: IsPluginEnabled = ({ cwd, manifest }) =>
26+
isFile(cwd, 'bun.lock') || isFile(cwd, 'bun.lockb') || !!hasBunTest(manifest.scripts);
1627

1728
const config = ['bunfig.toml'];
1829

@@ -33,15 +44,19 @@ const toPatterns = (arg: string) => {
3344
const resolve: Resolve = options => {
3445
const scripts = { ...options.rootManifest?.scripts, ...options.manifest.scripts };
3546
for (const script of Object.values(scripts)) {
36-
if (/(?<=^|\s)bun test/.test(script)) {
37-
const parsed = parseArgs(script.split(' '), { string: ['timeout', 'rerun-each', 'preload'] });
38-
const args = parsed._.filter(id => id !== 'bun' && id !== 'test');
39-
const inputs = (args.length === 0 ? patterns : args.flatMap(toPatterns)).map(toEntry);
47+
const bunTestArgs = getBunTestArgs(script);
48+
if (bunTestArgs) {
49+
const parsed = parseArgs(bunTestArgs, { string: ['timeout', 'rerun-each', 'preload'] });
50+
const args = parsed._;
51+
const inputs = [
52+
toIgnore('bun', 'dependencies'),
53+
...(args.length === 0 ? patterns : args.flatMap(toPatterns)).map(toEntry),
54+
];
4055
for (const specifier of [parsed.preload ?? []].flat()) inputs.push(toDeferResolve(specifier));
4156
return inputs;
4257
}
4358
}
44-
return [];
59+
return [toIgnore('bun', 'dependencies'), ...patterns.map(toEntry)];
4560
};
4661

4762
const plugin: Plugin = {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import assert from 'node:assert/strict';
2+
import test from 'node:test';
3+
import { main } from '../../src/index.ts';
4+
import baseCounters from '../helpers/baseCounters.ts';
5+
import { createOptions } from '../helpers/create-options.ts';
6+
import { resolve } from '../helpers/resolve.ts';
7+
8+
const cwd = resolve('fixtures/plugins/bun6');
9+
10+
test('Find dependencies with the Bun plugin (options before test)', async () => {
11+
const options = await createOptions({ cwd });
12+
const { counters } = await main(options);
13+
14+
assert.deepEqual(counters, {
15+
...baseCounters,
16+
processed: 2,
17+
total: 2,
18+
});
19+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import assert from 'node:assert/strict';
2+
import test from 'node:test';
3+
import { main } from '../../src/index.ts';
4+
import baseCounters from '../helpers/baseCounters.ts';
5+
import { createOptions } from '../helpers/create-options.ts';
6+
import { resolve } from '../helpers/resolve.ts';
7+
8+
const cwd = resolve('fixtures/plugins/bun7');
9+
10+
test('Find dependencies with the Bun plugin (lockfile only)', async () => {
11+
const options = await createOptions({ cwd });
12+
const { counters } = await main(options);
13+
14+
assert.deepEqual(counters, {
15+
...baseCounters,
16+
processed: 2,
17+
total: 2,
18+
});
19+
});

0 commit comments

Comments
 (0)