Compare commits

...

1 Commits

Author SHA1 Message Date
Claude Bot
9c99a692c7 refactor: deduplicate 5 commonly repeated functions
This consolidates duplicate functions that were scattered across the codebase:

1. **Platform detection functions** (isWindows, isLinux, isMacOS, isPosix)
   - Created internal/platform.ts module for built-in JS modules
   - Updated test files to import from harness instead of redefining

2. **Patch test snapshot normalization**
   - Created harness-patch.ts for shared normalizeBunSnapshotForPatch
   - Consolidated identical implementations from 2 test files

3. **Removed inline isLinux redefinition**
   - net.ts was defining isLinux twice, now uses shared import

Files affected:
- src/js/internal/platform.ts (new shared module)
- src/js/internal/fs/glob.ts (use shared isWindows)
- src/js/node/net.ts (use shared isWindows and isLinux)
- test/harness-patch.ts (new shared test utility)
- test/cli/install/bun-install-patch.test.ts
- test/regression/issue/patch-bounds-check.test.ts
- test/js/node/test/common/index.js
- test/napi/node-napi-tests/test/common/index.js

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-18 06:13:12 +00:00
8 changed files with 36 additions and 26 deletions

View File

@@ -1,8 +1,7 @@
import type { GlobScanOptions } from "bun";
const { validateObject, validateString, validateFunction, validateArray } = require("internal/validators");
const { sep } = require("node:path");
const isWindows = process.platform === "win32";
const { isWindows } = require("internal/platform");
interface GlobOptions {
/** @default process.cwd() */

View File

@@ -0,0 +1,16 @@
/**
* Internal platform detection utilities
* Used by built-in modules to check the current platform
*/
const isWindows = process.platform === "win32";
const isMacOS = process.platform === "darwin";
const isLinux = process.platform === "linux";
const isPosix = isMacOS || isLinux;
export default {
isWindows,
isMacOS,
isLinux,
isPosix,
};

View File

@@ -39,7 +39,7 @@ const ArrayPrototypePush = Array.prototype.push;
const MathMax = Math.max;
const { UV_ECANCELED, UV_ETIMEDOUT } = process.binding("uv");
const isWindows = process.platform === "win32";
const { isWindows, isLinux } = require("internal/platform");
const getDefaultAutoSelectFamily = $zig("node_net_binding.zig", "getDefaultAutoSelectFamily");
const setDefaultAutoSelectFamily = $zig("node_net_binding.zig", "setDefaultAutoSelectFamily");
@@ -2272,7 +2272,6 @@ Server.prototype.listen = function listen(port, hostname, onListen) {
port = 0;
}
const isLinux = process.platform === "linux";
if (!Number.isSafeInteger(port) || port < 0) {
if (path) {

View File

@@ -1,15 +1,9 @@
import { $ } from "bun";
import { beforeAll, describe, expect, it, setDefaultTimeout, test } from "bun:test";
import { bunEnv, bunExe, normalizeBunSnapshot as normalizeBunSnapshot_, tempDirWithFiles } from "harness";
import { bunEnv, bunExe, tempDirWithFiles } from "harness";
import { normalizeBunSnapshotForPatch as normalizeBunSnapshot } from "../../harness-patch";
import { join } from "path";
const normalizeBunSnapshot = (str: string) => {
str = normalizeBunSnapshot_(str);
str = str.replace(/.*Resolved, downloaded and extracted.*\n?/g, "");
str = str.replaceAll("fstatat()", "stat()");
return str;
};
beforeAll(() => {
setDefaultTimeout(1000 * 60 * 5);
});

12
test/harness-patch.ts Normal file
View File

@@ -0,0 +1,12 @@
import { normalizeBunSnapshot as normalizeBunSnapshotBase } from "./harness";
/**
* Normalize Bun snapshot output for patch-related tests
* Removes package resolution messages and normalizes fstatat() to stat()
*/
export const normalizeBunSnapshotForPatch = (str: string) => {
str = normalizeBunSnapshotBase(str);
str = str.replace(/.*Resolved, downloaded and extracted.*\n?/g, "");
str = str.replaceAll("fstatat()", "stat()");
return str;
};

View File

@@ -159,12 +159,10 @@ if (process.argv.length === 2 &&
}
}
const isWindows = process.platform === 'win32';
const { isWindows, isLinux, isMacOS } = require('../../../harness');
const isSunOS = process.platform === 'sunos';
const isFreeBSD = process.platform === 'freebsd';
const isOpenBSD = process.platform === 'openbsd';
const isLinux = process.platform === 'linux';
const isMacOS = process.platform === 'darwin';
const isASan = process.config.variables.asan === 1;
const isRiscv64 = process.arch === 'riscv64';
const isDebug = process.features.debug;

View File

@@ -136,12 +136,10 @@ if (process.argv.length === 2 &&
}
}
const isWindows = process.platform === 'win32';
const { isWindows, isLinux, isMacOS } = require('../../../../harness');
const isSunOS = process.platform === 'sunos';
const isFreeBSD = process.platform === 'freebsd';
const isOpenBSD = process.platform === 'openbsd';
const isLinux = process.platform === 'linux';
const isMacOS = process.platform === 'darwin';
const isASan = process.config.variables.asan === 1;
const isRiscv64 = process.arch === 'riscv64';
const isDebug = process.features.debug;

View File

@@ -1,12 +1,6 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe, normalizeBunSnapshot as normalizeBunSnapshot_, tempDirWithFiles } from "harness";
const normalizeBunSnapshot = (str: string) => {
str = normalizeBunSnapshot_(str);
str = str.replace(/.*Resolved, downloaded and extracted.*\n?/g, "");
str = str.replaceAll("fstatat()", "stat()");
return str;
};
import { bunEnv, bunExe, tempDirWithFiles } from "harness";
import { normalizeBunSnapshotForPatch as normalizeBunSnapshot } from "../../harness-patch";
test("patch application should handle out-of-bounds line numbers gracefully", async () => {
const dir = tempDirWithFiles("patch-bounds-test", {