Skip to content

Commit

Permalink
Add win32 path.toNamespacedPath and align rest of node:path with Node
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Feb 2, 2024
1 parent 3a6318b commit f577a8b
Show file tree
Hide file tree
Showing 22 changed files with 4,756 additions and 1,338 deletions.
4 changes: 1 addition & 3 deletions src/bun.js/bindings/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ JSC_DEFINE_HOST_FUNCTION(Path_functionResolve,
JSC_DEFINE_HOST_FUNCTION(Path_functionToNamespacedPath,
(JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
auto argCount = static_cast<uint16_t>(callFrame->argumentCount());
// TODO:
return JSC::JSValue::encode(callFrame->argument(0));
DEFINE_CALLBACK_FUNCTION_BODY(Bun__Path__toNamespacedPath);
}

static JSC::JSObject* createPath(JSGlobalObject* globalThis, bool isWindows)
Expand Down
1 change: 1 addition & 0 deletions src/bun.js/bindings/headers.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3,214 changes: 2,765 additions & 449 deletions src/bun.js/node/types.zig

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/js/node/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function bound(obj) {
delimiter: obj.delimiter,
win32: undefined,
posix: undefined,
// Legacy internal API, docs-only deprecated: DEP0080
_makeLong: toNamespacedPath,
};
return result;
Expand Down
83 changes: 83 additions & 0 deletions test/js/node/path/basename.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { test, describe } from "bun:test";
import assert from "node:assert";
import path from "node:path";

describe("path.dirname", () => {
test("platform", () => {
assert.strictEqual(path.basename(__filename), "basename.test.js");
assert.strictEqual(path.basename(__filename, ".js"), "basename.test");
assert.strictEqual(path.basename(".js", ".js"), "");
assert.strictEqual(path.basename("js", ".js"), "js");
assert.strictEqual(path.basename("file.js", ".ts"), "file.js");
assert.strictEqual(path.basename("file", ".js"), "file");
assert.strictEqual(path.basename("file.js.old", ".js.old"), "file");
assert.strictEqual(path.basename(""), "");
assert.strictEqual(path.basename("/dir/basename.ext"), "basename.ext");
assert.strictEqual(path.basename("/basename.ext"), "basename.ext");
assert.strictEqual(path.basename("basename.ext"), "basename.ext");
assert.strictEqual(path.basename("basename.ext/"), "basename.ext");
assert.strictEqual(path.basename("basename.ext//"), "basename.ext");
assert.strictEqual(path.basename("aaa/bbb", "/bbb"), "bbb");
assert.strictEqual(path.basename("aaa/bbb", "a/bbb"), "bbb");
assert.strictEqual(path.basename("aaa/bbb", "bbb"), "bbb");
assert.strictEqual(path.basename("aaa/bbb//", "bbb"), "bbb");
assert.strictEqual(path.basename("aaa/bbb", "bb"), "b");
assert.strictEqual(path.basename("aaa/bbb", "b"), "bb");
assert.strictEqual(path.basename("/aaa/bbb", "/bbb"), "bbb");
assert.strictEqual(path.basename("/aaa/bbb", "a/bbb"), "bbb");
assert.strictEqual(path.basename("/aaa/bbb", "bbb"), "bbb");
assert.strictEqual(path.basename("/aaa/bbb//", "bbb"), "bbb");
assert.strictEqual(path.basename("/aaa/bbb", "bb"), "b");
assert.strictEqual(path.basename("/aaa/bbb", "b"), "bb");
assert.strictEqual(path.basename("/aaa/bbb"), "bbb");
assert.strictEqual(path.basename("/aaa/"), "aaa");
assert.strictEqual(path.basename("/aaa/b"), "b");
assert.strictEqual(path.basename("/a/b"), "b");
assert.strictEqual(path.basename("//a"), "a");
assert.strictEqual(path.basename("a", "a"), "");
});

test("win32", () => {
// On Windows a backslash acts as a path separator.
assert.strictEqual(path.win32.basename("\\dir\\basename.ext"), "basename.ext");
assert.strictEqual(path.win32.basename("\\basename.ext"), "basename.ext");
assert.strictEqual(path.win32.basename("basename.ext"), "basename.ext");
assert.strictEqual(path.win32.basename("basename.ext\\"), "basename.ext");
assert.strictEqual(path.win32.basename("basename.ext\\\\"), "basename.ext");
assert.strictEqual(path.win32.basename("foo"), "foo");
assert.strictEqual(path.win32.basename("aaa\\bbb", "\\bbb"), "bbb");
assert.strictEqual(path.win32.basename("aaa\\bbb", "a\\bbb"), "bbb");
assert.strictEqual(path.win32.basename("aaa\\bbb", "bbb"), "bbb");
assert.strictEqual(path.win32.basename("aaa\\bbb\\\\\\\\", "bbb"), "bbb");
assert.strictEqual(path.win32.basename("aaa\\bbb", "bb"), "b");
assert.strictEqual(path.win32.basename("aaa\\bbb", "b"), "bb");
assert.strictEqual(path.win32.basename("C:"), "");
assert.strictEqual(path.win32.basename("C:."), ".");
assert.strictEqual(path.win32.basename("C:\\"), "");
assert.strictEqual(path.win32.basename("C:\\dir\\base.ext"), "base.ext");
assert.strictEqual(path.win32.basename("C:\\basename.ext"), "basename.ext");
assert.strictEqual(path.win32.basename("C:basename.ext"), "basename.ext");
assert.strictEqual(path.win32.basename("C:basename.ext\\"), "basename.ext");
assert.strictEqual(path.win32.basename("C:basename.ext\\\\"), "basename.ext");
assert.strictEqual(path.win32.basename("C:foo"), "foo");
assert.strictEqual(path.win32.basename("file:stream"), "file:stream");
assert.strictEqual(path.win32.basename("a", "a"), "");
});

test("posix", () => {
// On unix a backslash is just treated as any other character.
assert.strictEqual(path.posix.basename("\\dir\\basename.ext"), "\\dir\\basename.ext");
assert.strictEqual(path.posix.basename("\\basename.ext"), "\\basename.ext");
assert.strictEqual(path.posix.basename("basename.ext"), "basename.ext");
assert.strictEqual(path.posix.basename("basename.ext\\"), "basename.ext\\");
assert.strictEqual(path.posix.basename("basename.ext\\\\"), "basename.ext\\\\");
assert.strictEqual(path.posix.basename("foo"), "foo");
});

test("posix with control characters", () => {
// POSIX filenames may include control characters
// c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html
const controlCharFilename = `Icon${String.fromCharCode(13)}`;
assert.strictEqual(path.posix.basename(`/a/b/${controlCharFilename}`), controlCharFilename);
});
});
Loading

0 comments on commit f577a8b

Please sign in to comment.