Skip to content

Commit

Permalink
fix(ext/node): cp into non-existent parent directory (#23469)
Browse files Browse the repository at this point in the history
Fixes #20604
  • Loading branch information
littledivy authored Apr 20, 2024
1 parent e0554ac commit 04c6785
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ext/fs/std_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,12 @@ fn cp(from: &Path, to: &Path) -> FsResult<()> {
);
}
}

// Ensure parent destination directory exists
if let Some(parent) = to.parent() {
fs::create_dir_all(parent)?;
}

copy_file(from, to)
}

Expand Down
12 changes: 11 additions & 1 deletion tests/unit_node/_fs/_fs_copy_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as path from "@std/path/mod.ts";
import { assert } from "@std/assert/mod.ts";
import { assertCallbackErrorUncaught } from "../_test_utils.ts";
import { copyFile, copyFileSync, existsSync } from "node:fs";
import { copyFile, copyFileSync, cpSync, existsSync } from "node:fs";

const destFile = "./destination.txt";

Expand Down Expand Up @@ -50,3 +50,13 @@ Deno.test("[std/node/fs] copyFile callback isn't called twice if error is thrown
},
});
});

Deno.test("[std/node/fs] cp creates destination directory", async () => {
const tempDir = await Deno.makeTempDir();
const tempFile1 = path.join(tempDir, "file1.txt");
const tempFile2 = path.join(tempDir, "dir", "file2.txt");
await Deno.writeTextFile(tempFile1, "hello world");
cpSync(tempFile1, tempFile2);
assert(existsSync(tempFile2));
await Deno.remove(tempDir, { recursive: true });
});

0 comments on commit 04c6785

Please sign in to comment.