summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/fs/std_fs.rs6
-rw-r--r--tests/unit_node/_fs/_fs_copy_test.ts12
2 files changed, 17 insertions, 1 deletions
diff --git a/ext/fs/std_fs.rs b/ext/fs/std_fs.rs
index 1176697de..6bc15a72d 100644
--- a/ext/fs/std_fs.rs
+++ b/ext/fs/std_fs.rs
@@ -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)
}
diff --git a/tests/unit_node/_fs/_fs_copy_test.ts b/tests/unit_node/_fs/_fs_copy_test.ts
index 036091303..adc002187 100644
--- a/tests/unit_node/_fs/_fs_copy_test.ts
+++ b/tests/unit_node/_fs/_fs_copy_test.ts
@@ -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";
@@ -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 });
+});