diff options
-rw-r--r-- | src/bun.js/node/types.zig | 21 | ||||
-rw-r--r-- | test/js/node/path/path.test.js | 259 |
2 files changed, 213 insertions, 67 deletions
diff --git a/src/bun.js/node/types.zig b/src/bun.js/node/types.zig index c858c9b04..02bfe8757 100644 --- a/src/bun.js/node/types.zig +++ b/src/bun.js/node/types.zig @@ -1861,14 +1861,17 @@ pub const Path = struct { var insert_separator = true; if (path_object.getTruthy(globalThis, "dir")) |prop| { prop.toZigString(&dir, globalThis); - insert_separator = !dir.isEmpty(); - } else if (path_object.getTruthy(globalThis, "root")) |prop| { - prop.toZigString(&dir, globalThis); + } + if (dir.isEmpty()) { + if (path_object.getTruthy(globalThis, "root")) |prop| { + prop.toZigString(&dir, globalThis); + } } if (path_object.getTruthy(globalThis, "base")) |prop| { prop.toZigString(&name_with_ext, globalThis); - } else { + } + if (name_with_ext.isEmpty()) { var had_ext = false; if (path_object.getTruthy(globalThis, "ext")) |prop| { prop.toZigString(&ext, globalThis); @@ -1897,6 +1900,16 @@ pub const Path = struct { defer allocator.free(out); return JSC.ZigString.init(out).withEncoding().toValueGC(globalThis); + } else { + if (!isWindows) { + if (dir.eqlComptime("/")) { + insert_separator = false; + } + } else { + if (dir.eqlComptime("\\")) { + insert_separator = false; + } + } } if (insert_separator) { diff --git a/test/js/node/path/path.test.js b/test/js/node/path/path.test.js index 47d16fee0..3c8a04d72 100644 --- a/test/js/node/path/path.test.js +++ b/test/js/node/path/path.test.js @@ -627,71 +627,204 @@ it("path.resolve", () => { strictEqual(failures.length, 0, failures.join("\n")); }); -it("path.parse", () => { - expect(path.parse("/tmp/test.txt")).toStrictEqual({ - root: "/", - dir: "/tmp", - base: "test.txt", - ext: ".txt", - name: "test", - }); - - expect(path.parse("/tmp/test/file.txt")).toStrictEqual({ - root: "/", - dir: "/tmp/test", - base: "file.txt", - ext: ".txt", - name: "file", - }); - - expect(path.parse("/tmp/test/dir")).toStrictEqual({ root: "/", dir: "/tmp/test", base: "dir", ext: "", name: "dir" }); - expect(path.parse("/tmp/test/dir/")).toStrictEqual({ - root: "/", - dir: "/tmp/test", - base: "dir", - ext: "", - name: "dir", - }); - - expect(path.parse(".")).toStrictEqual({ root: "", dir: "", base: ".", ext: "", name: "." }); - expect(path.parse("./")).toStrictEqual({ root: "", dir: "", base: ".", ext: "", name: "." }); - expect(path.parse("/.")).toStrictEqual({ root: "/", dir: "/", base: ".", ext: "", name: "." }); - expect(path.parse("/../")).toStrictEqual({ root: "/", dir: "/", base: "..", ext: ".", name: "." }); - - expect(path.parse("./file.txt")).toStrictEqual({ root: "", dir: ".", base: "file.txt", ext: ".txt", name: "file" }); - expect(path.parse("../file.txt")).toStrictEqual({ root: "", dir: "..", base: "file.txt", ext: ".txt", name: "file" }); - expect(path.parse("../test/file.txt")).toStrictEqual({ - root: "", - dir: "../test", - base: "file.txt", - ext: ".txt", - name: "file", - }); - expect(path.parse("test/file.txt")).toStrictEqual({ - root: "", - dir: "test", - base: "file.txt", - ext: ".txt", - name: "file", - }); +describe("path.parse and path.format", () => { + const testCases = [ + { + input: "/tmp/test.txt", + expected: { + root: "/", + dir: "/tmp", + base: "test.txt", + ext: ".txt", + name: "test", + }, + }, + { + input: "/tmp/test/file.txt", + expected: { + root: "/", + dir: "/tmp/test", + base: "file.txt", + ext: ".txt", + name: "file", + }, + }, + { + input: "/tmp/test/dir", + expected: { + root: "/", + dir: "/tmp/test", + base: "dir", + ext: "", + name: "dir", + }, + }, + { + input: "/tmp/test/dir/", + expected: { + root: "/", + dir: "/tmp/test", + base: "dir", + ext: "", + name: "dir", + }, + }, + { + input: ".", + expected: { + root: "", + dir: "", + base: ".", + ext: "", + name: ".", + }, + }, + { + input: "./", + expected: { + root: "", + dir: "", + base: ".", + ext: "", + name: ".", + }, + }, + { + input: "/.", + expected: { + root: "/", + dir: "/", + base: ".", + ext: "", + name: ".", + }, + }, + { + input: "/../", + expected: { + root: "/", + dir: "/", + base: "..", + ext: ".", + name: ".", + }, + }, + { + input: "./file.txt", + expected: { + root: "", + dir: ".", + base: "file.txt", + ext: ".txt", + name: "file", + }, + }, + { + input: "../file.txt", + expected: { + root: "", + dir: "..", + base: "file.txt", + ext: ".txt", + name: "file", + }, + }, + { + input: "../test/file.txt", + expected: { + root: "", + dir: "../test", + base: "file.txt", + ext: ".txt", + name: "file", + }, + }, + { + input: "test/file.txt", + expected: { + root: "", + dir: "test", + base: "file.txt", + ext: ".txt", + name: "file", + }, + }, + { + input: "test/dir", + expected: { + root: "", + dir: "test", + base: "dir", + ext: "", + name: "dir", + }, + }, + { + input: "test/dir/another_dir", + expected: { + root: "", + dir: "test/dir", + base: "another_dir", + ext: "", + name: "another_dir", + }, + }, + { + input: "./dir", + expected: { + root: "", + dir: ".", + base: "dir", + ext: "", + name: "dir", + }, + }, + { + input: "../dir", + expected: { + root: "", + dir: "..", + base: "dir", + ext: "", + name: "dir", + }, + }, + { + input: "../dir/another_dir", + expected: { + root: "", + dir: "../dir", + base: "another_dir", + ext: "", + name: "another_dir", + }, + }, + ]; + testCases.forEach(({ input, expected }) => { + it(`case ${input}`, () => { + const parsed = path.parse(input); + expect(parsed).toStrictEqual(expected); - expect(path.parse("test/dir")).toStrictEqual({ root: "", dir: "test", base: "dir", ext: "", name: "dir" }); - expect(path.parse("test/dir/another_dir")).toStrictEqual({ - root: "", - dir: "test/dir", - base: "another_dir", - ext: "", - name: "another_dir", + const formatted = path.format(parsed); + expect(formatted).toStrictEqual(input.slice(-1) === "/" ? input.slice(0, -1) : input); + }); }); - - expect(path.parse("./dir")).toStrictEqual({ root: "", dir: ".", base: "dir", ext: "", name: "dir" }); - expect(path.parse("../dir")).toStrictEqual({ root: "", dir: "..", base: "dir", ext: "", name: "dir" }); - expect(path.parse("../dir/another_dir")).toStrictEqual({ - root: "", - dir: "../dir", - base: "another_dir", - ext: "", - name: "another_dir", + it("empty string arguments, issue #4005", () => { + expect( + path.format({ + root: "", + dir: "", + base: "", + name: "foo", + ext: ".ts", + }), + ).toStrictEqual("foo.ts"); + expect( + path.format({ + name: "foo", + ext: ".ts", + }), + ).toStrictEqual("foo.ts"); }); }); |