mirror of
https://github.com/oven-sh/bun
synced 2026-02-11 11:29:02 +00:00
1. Check if the argument is an empty string in path.format. (#4064)
2. Avoid duplicating '/' at the beginning of the path. Close: #4005
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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",
|
||||
});
|
||||
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("/tmp/test/file.txt")).toStrictEqual({
|
||||
root: "/",
|
||||
dir: "/tmp/test",
|
||||
base: "file.txt",
|
||||
ext: ".txt",
|
||||
name: "file",
|
||||
const formatted = path.format(parsed);
|
||||
expect(formatted).toStrictEqual(input.slice(-1) === "/" ? input.slice(0, -1) : input);
|
||||
});
|
||||
});
|
||||
|
||||
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",
|
||||
});
|
||||
|
||||
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",
|
||||
});
|
||||
|
||||
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");
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user