1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00

fix(ext/node): cp into non-existent parent directory (#23469)

Fixes https://github.com/denoland/deno/issues/20604
This commit is contained in:
Divy Srivastava 2024-04-20 17:11:27 +05:30 committed by GitHub
parent e0554ac4a2
commit 04c6785fae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 1 deletions

View file

@ -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) copy_file(from, to)
} }

View file

@ -2,7 +2,7 @@
import * as path from "@std/path/mod.ts"; import * as path from "@std/path/mod.ts";
import { assert } from "@std/assert/mod.ts"; import { assert } from "@std/assert/mod.ts";
import { assertCallbackErrorUncaught } from "../_test_utils.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"; const destFile = "./destination.txt";
@ -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 });
});