Skip to content

Commit

Permalink
Merge branch 'main' into zack/shell-cp
Browse files Browse the repository at this point in the history
  • Loading branch information
zackradisic committed Apr 16, 2024
2 parents 28390fd + 3df202f commit c0daac0
Show file tree
Hide file tree
Showing 43 changed files with 393 additions and 340 deletions.
4 changes: 4 additions & 0 deletions packages/bun-internal-test/src/banned.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
"@import(\"root\").bun.": "Only import 'bun' once",
"std.mem.indexOfAny": "Use bun.strings.indexAny or bun.strings.indexAnyComptime",
"std.debug.print": "Don't let this be committed",
" == undefined": "This is by definition Undefined Behavior.",
" != undefined": "This is by definition Undefined Behavior.",
"undefined == ": "This is by definition Undefined Behavior.",
"undefined != ": "This is by definition Undefined Behavior.",
"": ""
}
6 changes: 6 additions & 0 deletions scripts/make-old-js.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ $npm_client = "npm"
# & ${npm_client} i

$root = Join-Path (Split-Path -Path $MyInvocation.MyCommand.Definition -Parent) "..\"

# search for .cmd or .exe
$esbuild = Join-Path $root "node_modules\.bin\esbuild.cmd"
if (!(Test-Path $esbuild)) {
$esbuild = Join-Path $root "node_modules\.bin\esbuild.exe"
}


$env:NODE_ENV = "production"

Expand Down
10 changes: 10 additions & 0 deletions src/bun.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3119,6 +3119,16 @@ pub fn assert(value: bool) callconv(callconv_inline) void {
}
}

/// This has no effect on the real code but capturing 'a' and 'b' into parameters makes assertion failures much easier inspect in a debugger.
pub inline fn assert_eql(a: anytype, b: anytype) void {
return assert(a == b);
}

/// This has no effect on the real code but capturing 'a' and 'b' into parameters makes assertion failures much easier inspect in a debugger.
pub inline fn assert_neql(a: anytype, b: anytype) void {
return assert(a != b);
}

pub inline fn unsafeAssert(condition: bool) void {
if (!condition) {
unreachable;
Expand Down
18 changes: 1 addition & 17 deletions src/install/semver.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,6 @@ pub const String = extern struct {
};
}

pub inline fn init(
buf: string,
in: string,
) String {
if (comptime Environment.isDebug) {
const out = realInit(buf, in);
if (!out.isInline()) {
assert(@as(u64, @bitCast(out.slice(buf)[0..8].*)) != undefined);
}

return out;
} else {
return realInit(buf, in);
}
}

pub const Formatter = struct {
str: *const String,
buf: string,
Expand Down Expand Up @@ -150,7 +134,7 @@ pub const String = extern struct {
}
};

fn realInit(
pub fn init(
buf: string,
in: string,
) String {
Expand Down
2 changes: 1 addition & 1 deletion src/js/node/assert.js → src/js/node/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const util = require("node:util");

var isDeepEqual = Bun.deepEquals;
var __commonJS = (cb, mod) =>
var __commonJS = (cb, mod: typeof module | undefined = undefined) =>
function () {
return mod || (0, cb[Object.keys(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/js/node/crypto.js → src/js/node/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function getArrayBufferOrView(buffer, name, encoding) {
const crypto = globalThis.crypto;
const globalCrypto = crypto;

var __commonJS = (cb, mod) =>
var __commonJS = (cb, mod: typeof module | undefined = undefined) =>
function () {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion src/js/node/querystring.js → src/js/node/querystring.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
var __commonJS =
(cb, mod: typeof module | undefined = undefined) =>
() => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);

var Buffer = require("node:buffer").Buffer;

Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/js/node/stream.js → src/js/node/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const {

var __getOwnPropNames = Object.getOwnPropertyNames;

var __commonJS = (cb, mod) =>
var __commonJS = (cb, mod: typeof module | undefined = undefined) =>
function __require2() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/js/node/wasi.js → src/js/node/wasi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const nodeFsConstants = $processBindingConstants.fs;

var __getOwnPropNames = Object.getOwnPropertyNames;

var __commonJS = (cb, mod) =>
var __commonJS = (cb, mod: typeof module | undefined = undefined) =>
function __require2() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
Expand Down
2 changes: 1 addition & 1 deletion src/js/node/zlib.js → src/js/node/zlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Util = require("node:util");
const { isAnyArrayBuffer, isArrayBufferView } = require("node:util/types");

var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) =>
var __commonJS = (cb, mod: typeof module | undefined = undefined) =>
function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
Expand Down
2 changes: 1 addition & 1 deletion src/js/private.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ declare module "bun" {
var TOML: {
parse(contents: string): any;
};
function jest(): typeof import("bun:test");
function jest(path: string): typeof import("bun:test");
var main: string;
var tty: Array<{ hasColors: boolean }>;
var FFI: any;
Expand Down
20 changes: 11 additions & 9 deletions src/shell/interpreter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const stderr_no = 2;

pub fn OOM(e: anyerror) noreturn {
if (comptime bun.Environment.allow_assert) {
if (e != error.OutOfMemory) @panic("Ruh roh");
if (e != error.OutOfMemory) bun.outOfMemory();
}
@panic("Out of memory");
}
Expand Down Expand Up @@ -2123,7 +2123,7 @@ pub const Interpreter = struct {
return;
}

unreachable;
@panic("Invalid child to Expansion, this indicates a bug in Bun. Please file a report on Github.");
}

fn onGlobWalkDone(this: *Expansion, task: *ShellGlobTask) void {
Expand Down Expand Up @@ -2742,7 +2742,7 @@ pub const Interpreter = struct {
return;
}

unreachable;
@panic("Invalid child to Assigns expression, this indicates a bug in Bun. Please file a report on Github.");
}
};

Expand Down Expand Up @@ -2910,7 +2910,7 @@ pub const Interpreter = struct {
parent: ParentPtr,
io: IO,
) *Binary {
var binary = interpreter.allocator.create(Binary) catch |err| std.debug.panic("Ruh roh: {any}\n", .{err});
var binary = interpreter.allocator.create(Binary) catch bun.outOfMemory();
binary.node = node;
binary.base = .{ .kind = .binary, .interpreter = interpreter, .shell = shell_state };
binary.parent = parent;
Expand Down Expand Up @@ -3234,7 +3234,7 @@ pub const Interpreter = struct {
if (ptr == @as(usize, @intCast(child.ptr.repr._ptr))) break :brk i;
}
}
unreachable;
@panic("Invalid pipeline state");
};

log("pipeline child done {x} ({d}) i={d}", .{ @intFromPtr(this), exit_code, idx });
Expand Down Expand Up @@ -4347,7 +4347,7 @@ pub const Interpreter = struct {
parent: ParentPtr,
io: IO,
) *Cmd {
var cmd = interpreter.allocator.create(Cmd) catch |err| std.debug.panic("Ruh roh: {any}\n", .{err});
var cmd = interpreter.allocator.create(Cmd) catch bun.outOfMemory();
cmd.* = .{
.base = .{ .kind = .cmd, .interpreter = interpreter, .shell = shell_state },
.node = node,
Expand Down Expand Up @@ -4522,7 +4522,8 @@ pub const Interpreter = struct {
this.next();
return;
}
unreachable;

@panic("Expected Cmd child to be Assigns or Expansion. This indicates a bug in Bun. Please file a GitHub issue. ");
}

fn initSubproc(this: *Cmd) void {
Expand Down Expand Up @@ -7128,7 +7129,8 @@ pub const Interpreter = struct {
while (!(this.state == .err or this.state == .done)) {
switch (this.state) {
.waiting_io => return,
.idle, .done, .err => unreachable,
.idle => @panic("Unexpected \"idle\" state in Pwd. This indicates a bug in Bun. Please file a GitHub issue."),
.done, .err => unreachable,
}
}

Expand Down Expand Up @@ -9693,7 +9695,7 @@ pub const Interpreter = struct {

pub fn next(this: *Exit) void {
switch (this.state) {
.idle => unreachable,
.idle => @panic("Unexpected \"idle\" state in Exit. This indicates a bug in Bun. Please file a GitHub issue."),
.waiting_io => {
return;
},
Expand Down
15 changes: 10 additions & 5 deletions test/js/bun/shell/bunshell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { mkdir, mkdtemp, realpath, rm, stat } from "fs/promises";
import { bunEnv, bunExe, runWithErrorPromise, tempDirWithFiles } from "harness";
import { tmpdir } from "os";
import { join, sep } from "path";
import { TestBuilder, sortedShellOutput } from "./util";
import { createTestBuilder, sortedShellOutput } from "./util";
const TestBuilder = createTestBuilder(import.meta.path);

$.env(bunEnv);
$.cwd(process.cwd());
Expand Down Expand Up @@ -797,9 +798,10 @@ describe("deno_task", () => {

TestBuilder.command`echo 1 | echo 2 && echo 3`.stdout("2\n3\n").runAsTest("pipe in conditional");

await TestBuilder.command`echo $(sleep 0.1 && echo 2 & echo 1) | BUN_DEBUG_QUIET_LOGS=1 BUN_TEST_VAR=1 ${BUN} -e 'await process.stdin.pipe(process.stdout)'`
TestBuilder.command`echo $(sleep 0.1 && echo 2 & echo 1) | BUN_DEBUG_QUIET_LOGS=1 BUN_TEST_VAR=1 ${BUN} -e 'await process.stdin.pipe(process.stdout)'`
.stdout("1 2\n")
.run();
.todo("& not supported")
.runAsTest("complicated pipeline");

TestBuilder.command`echo 2 | echo 1 | BUN_TEST_VAR=1 ${BUN} -e 'process.stdin.pipe(process.stdout)'`
.stdout("1\n")
Expand Down Expand Up @@ -834,9 +836,12 @@ describe("deno_task", () => {
});

describe("redirects", async function igodf() {
await TestBuilder.command`echo 5 6 7 > test.txt`.fileEquals("test.txt", "5 6 7\n").run();
TestBuilder.command`echo 5 6 7 > test.txt`.fileEquals("test.txt", "5 6 7\n").runAsTest("basic redirect");

await TestBuilder.command`echo 1 2 3 && echo 1 > test.txt`.stdout("1 2 3\n").fileEquals("test.txt", "1\n").run();
TestBuilder.command`echo 1 2 3 && echo 1 > test.txt`
.stdout("1 2 3\n")
.fileEquals("test.txt", "1\n")
.runAsTest("basic redirect with &&");

// subdir
TestBuilder.command`mkdir subdir && cd subdir && echo 1 2 3 > test.txt`
Expand Down
3 changes: 2 additions & 1 deletion test/js/bun/shell/commands/basename.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { $ } from "bun";
import { describe, test, expect } from "bun:test";
import { TestBuilder } from "../test_builder";
import { createTestBuilder } from "../test_builder";
const TestBuilder = createTestBuilder(import.meta.path);

$.nothrow();
describe("basename", async () => {
Expand Down
3 changes: 2 additions & 1 deletion test/js/bun/shell/commands/dirname.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { $ } from "bun";
import { describe, test, expect } from "bun:test";
import { TestBuilder } from "../test_builder";
import { createTestBuilder } from "../test_builder";
const TestBuilder = createTestBuilder(import.meta.path);

$.nothrow();
describe("dirname", async () => {
Expand Down
3 changes: 2 additions & 1 deletion test/js/bun/shell/commands/exit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { $ } from "bun";
import { describe, test, expect } from "bun:test";
import { TestBuilder } from "../test_builder";
import { createTestBuilder } from "../test_builder";
const TestBuilder = createTestBuilder(import.meta.path);
import { sortedShellOutput } from "../util";
import { join } from "path";

Expand Down
3 changes: 2 additions & 1 deletion test/js/bun/shell/commands/false.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { $ } from "bun";
import { describe, test, expect } from "bun:test";
import { TestBuilder } from "../test_builder";
import { createTestBuilder } from "../test_builder";
const TestBuilder = createTestBuilder(import.meta.path);

$.nothrow();
describe("false", async () => {
Expand Down
3 changes: 2 additions & 1 deletion test/js/bun/shell/commands/mv.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { $ } from "bun";
import { describe, test, expect } from "bun:test";
import { TestBuilder } from "../test_builder";
import { createTestBuilder } from "../test_builder";
const TestBuilder = createTestBuilder(import.meta.path);
import { sortedShellOutput } from "../util";
import { join } from "path";

Expand Down
3 changes: 2 additions & 1 deletion test/js/bun/shell/commands/rm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { $ } from "bun";
import path from "path";
import { mkdirSync, writeFileSync } from "node:fs";
import { ShellOutput } from "bun";
import { TestBuilder, sortedShellOutput } from "../util";
import { createTestBuilder, sortedShellOutput } from "../util";
const TestBuilder = createTestBuilder(import.meta.path);

const fileExists = async (path: string): Promise<boolean> =>
$`ls -d ${path}`.then(o => o.stdout.toString() === `${path}\n`);
Expand Down
3 changes: 2 additions & 1 deletion test/js/bun/shell/commands/seq.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { $ } from "bun";
import { describe } from "bun:test";
import { TestBuilder } from "../test_builder";
import { createTestBuilder } from "../test_builder";
const TestBuilder = createTestBuilder(import.meta.path);

$.nothrow();
describe("seq", async () => {
Expand Down
3 changes: 2 additions & 1 deletion test/js/bun/shell/commands/true.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { $ } from "bun";
import { describe, test, expect } from "bun:test";
import { TestBuilder } from "../test_builder";
import { createTestBuilder } from "../test_builder";
const TestBuilder = createTestBuilder(import.meta.path);

$.nothrow();
describe("true", async () => {
Expand Down
3 changes: 2 additions & 1 deletion test/js/bun/shell/env.positionals.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { $, spawn } from "bun";
import { describe, test, expect } from "bun:test";
import { TestBuilder } from "./test_builder";
import { createTestBuilder } from "./test_builder";
const TestBuilder = createTestBuilder(import.meta.path);
import { bunEnv, bunExe } from "harness";
import * as path from "node:path";

Expand Down
3 changes: 2 additions & 1 deletion test/js/bun/shell/exec.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { $ } from "bun";
import { describe, test, expect } from "bun:test";
import { TestBuilder } from "./test_builder";
import { createTestBuilder } from "./test_builder";
const TestBuilder = createTestBuilder(import.meta.path);
import { bunEnv } from "harness";

const BUN = process.argv0;
Expand Down
14 changes: 10 additions & 4 deletions test/js/bun/shell/leak.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { bunEnv } from "harness";
import { appendFileSync, closeSync, openSync, writeFileSync } from "node:fs";
import { tmpdir, devNull } from "os";
import { join } from "path";
import { TestBuilder } from "./util";
import { createTestBuilder } from "./util";
const TestBuilder = createTestBuilder(import.meta.path);
type TestBuilder = InstanceType<typeof TestBuilder>;

$.env(bunEnv);
$.cwd(process.cwd());
Expand Down Expand Up @@ -50,7 +52,7 @@ const TESTS: [name: string, builder: () => TestBuilder, runs?: number][] = [
];

describe("fd leak", () => {
function fdLeakTest(name: string, builder: () => TestBuilder, runs: number = 500, threshold: number = 5) {
function fdLeakTest(name: string, builder: () => TestBuilder, runs: number = 1000, threshold: number = 5) {
test(`fdleak_${name}`, async () => {
Bun.gc(true);
const baseline = openSync(devNull, "r");
Expand Down Expand Up @@ -83,13 +85,15 @@ describe("fd leak", () => {
writeFileSync(tempfile, testcode);

const impl = /* ts */ `
const TestBuilder = createTestBuilder(import.meta.path);
const threshold = ${threshold}
let prev: number | undefined = undefined;
let prevprev: number | undefined = undefined;
for (let i = 0; i < ${runs}; i++) {
Bun.gc(true);
await (async function() {
await ${builder.toString().slice("() =>".length)}.quiet().run()
await ${builder.toString().slice("() =>".length)}.quiet().runAsTest('iter:', i)
})()
Bun.gc(true);
Bun.gc(true);
Expand All @@ -111,7 +115,9 @@ describe("fd leak", () => {
env: bunEnv,
});
// console.log('STDOUT:', stdout.toString(), '\n\nSTDERR:', stderr.toString());
console.log("\n\nSTDERR:", stderr.toString());
if (exitCode != 0) {
console.log("\n\nSTDERR:", stderr.toString());
}
expect(exitCode).toBe(0);
}, 100_000);
}
Expand Down
Loading

0 comments on commit c0daac0

Please sign in to comment.