2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assert, assertEquals, assertRejects, assertThrows } from "@std/assert";
|
|
|
|
import * as path from "@std/path";
|
2019-05-16 16:19:17 +00:00
|
|
|
import { copy, copySync } from "./copy.ts";
|
2022-08-24 04:28:54 +00:00
|
|
|
import { existsSync } from "./exists.ts";
|
2019-05-16 16:19:17 +00:00
|
|
|
import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
|
|
|
|
import { ensureFile, ensureFileSync } from "./ensure_file.ts";
|
|
|
|
import { ensureSymlink, ensureSymlinkSync } from "./ensure_symlink.ts";
|
|
|
|
|
2020-09-08 09:43:43 +00:00
|
|
|
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
|
|
|
|
const testdataDir = path.resolve(moduleDir, "testdata");
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2020-07-06 02:21:03 +00:00
|
|
|
function testCopy(
|
|
|
|
name: string,
|
|
|
|
cb: (tempDir: string) => Promise<void>,
|
2020-07-14 19:24:17 +00:00
|
|
|
ignore = false,
|
2022-08-24 01:21:57 +00:00
|
|
|
) {
|
2020-02-11 16:24:27 +00:00
|
|
|
Deno.test({
|
2019-05-16 16:19:17 +00:00
|
|
|
name,
|
2021-04-05 11:49:05 +00:00
|
|
|
async fn() {
|
2019-05-16 16:19:17 +00:00
|
|
|
const tempDir = await Deno.makeTempDir({
|
2020-03-28 17:03:49 +00:00
|
|
|
prefix: "deno_std_copy_async_test_",
|
2019-05-16 16:19:17 +00:00
|
|
|
});
|
|
|
|
await cb(tempDir);
|
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
2020-03-28 17:03:49 +00:00
|
|
|
},
|
2020-07-06 02:21:03 +00:00
|
|
|
ignore,
|
2019-05-16 16:19:17 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-24 01:21:57 +00:00
|
|
|
function testCopySync(name: string, cb: (tempDir: string) => void) {
|
2020-02-11 16:24:27 +00:00
|
|
|
Deno.test({
|
2019-05-16 16:19:17 +00:00
|
|
|
name,
|
2022-08-24 01:21:57 +00:00
|
|
|
fn: () => {
|
2019-05-16 16:19:17 +00:00
|
|
|
const tempDir = Deno.makeTempDirSync({
|
2020-03-28 17:03:49 +00:00
|
|
|
prefix: "deno_std_copy_sync_test_",
|
2019-05-16 16:19:17 +00:00
|
|
|
});
|
|
|
|
cb(tempDir);
|
|
|
|
Deno.removeSync(tempDir, { recursive: true });
|
2020-03-28 17:03:49 +00:00
|
|
|
},
|
2019-05-16 16:19:17 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
testCopy(
|
2023-11-23 06:59:59 +00:00
|
|
|
"copy() rejects if src does not exist",
|
2021-04-05 11:49:05 +00:00
|
|
|
async (tempDir: string) => {
|
2019-05-16 16:19:17 +00:00
|
|
|
const srcFile = path.join(testdataDir, "copy_file_not_exists.txt");
|
|
|
|
const destFile = path.join(tempDir, "copy_file_not_exists_1.txt");
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(
|
2021-04-05 11:49:05 +00:00
|
|
|
async () => {
|
2019-05-16 16:19:17 +00:00
|
|
|
await copy(srcFile, destFile);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopy(
|
2023-11-23 06:59:59 +00:00
|
|
|
"copy() rejects if src and dest are the same paths",
|
2021-04-05 11:49:05 +00:00
|
|
|
async (tempDir: string) => {
|
2019-05-16 16:19:17 +00:00
|
|
|
const srcFile = path.join(tempDir, "copy_file_same.txt");
|
|
|
|
const destFile = path.join(tempDir, "copy_file_same.txt");
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(
|
2021-04-05 11:49:05 +00:00
|
|
|
async () => {
|
2019-05-16 16:19:17 +00:00
|
|
|
await copy(srcFile, destFile);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-26 04:31:34 +00:00
|
|
|
"Source and destination cannot be the same",
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopy(
|
2023-11-23 06:59:59 +00:00
|
|
|
"copy() copies file to new destination",
|
2021-04-05 11:49:05 +00:00
|
|
|
async (tempDir: string) => {
|
2019-05-16 16:19:17 +00:00
|
|
|
const srcFile = path.join(testdataDir, "copy_file.txt");
|
|
|
|
const destFile = path.join(tempDir, "copy_file_copy.txt");
|
|
|
|
|
2023-11-03 04:41:18 +00:00
|
|
|
const srcContent = await Deno.readTextFile(srcFile);
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2022-08-24 04:28:54 +00:00
|
|
|
assert(await Deno.lstat(srcFile), "source should exist before copy");
|
|
|
|
await assertRejects(
|
|
|
|
async () => await Deno.lstat(destFile),
|
2020-07-14 19:24:17 +00:00
|
|
|
"destination should not exist before copy",
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await copy(srcFile, destFile);
|
|
|
|
|
2022-08-24 04:28:54 +00:00
|
|
|
assert(await Deno.lstat(srcFile), "source should exist after copy");
|
|
|
|
assert(await Deno.lstat(destFile), "destination should exist after copy");
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2023-11-03 04:41:18 +00:00
|
|
|
const destContent = await Deno.readTextFile(destFile);
|
2019-05-16 16:19:17 +00:00
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
srcContent,
|
|
|
|
destContent,
|
2020-07-14 19:24:17 +00:00
|
|
|
"source and destination should have the same content",
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Copy again and it should throw an error.
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(
|
2021-04-05 11:49:05 +00:00
|
|
|
async () => {
|
2019-05-16 16:19:17 +00:00
|
|
|
await copy(srcFile, destFile);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-26 04:31:34 +00:00
|
|
|
`'${destFile}' already exists`,
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Modify destination file.
|
|
|
|
|
2023-11-03 04:41:18 +00:00
|
|
|
await Deno.writeTextFile(destFile, "txt copy");
|
|
|
|
|
|
|
|
assertEquals(await Deno.readTextFile(destFile), "txt copy");
|
2019-05-16 16:19:17 +00:00
|
|
|
|
|
|
|
// Copy again with overwrite option.
|
|
|
|
await copy(srcFile, destFile, { overwrite: true });
|
|
|
|
|
|
|
|
// Make sure the file has been overwritten.
|
2023-11-03 04:41:18 +00:00
|
|
|
assertEquals(await Deno.readTextFile(destFile), "txt");
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
2020-09-01 14:03:07 +00:00
|
|
|
testCopy(
|
2023-11-23 06:59:59 +00:00
|
|
|
"copy() copies with preserve timestamps",
|
2021-04-05 11:49:05 +00:00
|
|
|
async (tempDir: string) => {
|
2024-06-20 09:14:34 +00:00
|
|
|
{
|
|
|
|
const srcFile = path.join(testdataDir, "copy_file.txt");
|
|
|
|
const destFile = path.join(tempDir, "copy_file_copy.txt");
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2024-06-20 09:14:34 +00:00
|
|
|
const srcStatInfo = await Deno.stat(srcFile);
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2024-06-20 09:14:34 +00:00
|
|
|
assert(srcStatInfo.atime instanceof Date);
|
|
|
|
assert(srcStatInfo.mtime instanceof Date);
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2024-06-20 09:14:34 +00:00
|
|
|
// Copy with overwrite and preserve timestamps options.
|
|
|
|
await copy(srcFile, destFile, {
|
|
|
|
overwrite: true,
|
|
|
|
preserveTimestamps: true,
|
|
|
|
});
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2024-06-20 09:14:34 +00:00
|
|
|
const destStatInfo = await Deno.stat(destFile);
|
|
|
|
|
|
|
|
assert(destStatInfo.atime instanceof Date);
|
|
|
|
assert(destStatInfo.mtime instanceof Date);
|
|
|
|
assertEquals(destStatInfo.atime, srcStatInfo.atime);
|
|
|
|
assertEquals(destStatInfo.mtime, srcStatInfo.mtime);
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy dir with preserve timestamps
|
|
|
|
{
|
|
|
|
const srcDir = path.join(testdataDir, "copy_dir");
|
|
|
|
const destDir = path.join(tempDir, "copy_dir");
|
|
|
|
const srcFile = path.join(srcDir, "0.txt");
|
|
|
|
const destFile = path.join(destDir, "0.txt");
|
|
|
|
const srcNestFile = path.join(srcDir, "nest", "0.txt");
|
|
|
|
const destNestFile = path.join(destDir, "nest", "0.txt");
|
|
|
|
|
|
|
|
await copy(srcDir, destDir, { preserveTimestamps: true });
|
|
|
|
|
|
|
|
const srcDirInfo = await Deno.stat(srcFile);
|
|
|
|
const destDirInfo = await Deno.stat(destFile);
|
|
|
|
const srcFileInfo = await Deno.stat(srcFile);
|
|
|
|
const destFileInfo = await Deno.stat(destFile);
|
|
|
|
const srcNestFileInfo = await Deno.stat(srcNestFile);
|
|
|
|
const destNestFileInfo = await Deno.stat(destNestFile);
|
|
|
|
|
|
|
|
assertEquals(srcDirInfo.atime, destDirInfo.atime);
|
|
|
|
assertEquals(srcDirInfo.mtime, destDirInfo.mtime);
|
|
|
|
assertEquals(srcFileInfo.atime, destFileInfo.atime);
|
|
|
|
assertEquals(srcFileInfo.mtime, destFileInfo.mtime);
|
|
|
|
assertEquals(srcNestFileInfo.atime, destNestFileInfo.atime);
|
|
|
|
assertEquals(srcNestFileInfo.mtime, destNestFileInfo.mtime);
|
|
|
|
}
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopy(
|
2023-11-23 06:59:59 +00:00
|
|
|
"copy() rejects if destination is its own subdirectory",
|
2021-04-05 11:49:05 +00:00
|
|
|
async (tempDir: string) => {
|
2019-05-16 16:19:17 +00:00
|
|
|
const srcDir = path.join(tempDir, "parent");
|
|
|
|
const destDir = path.join(srcDir, "child");
|
|
|
|
|
|
|
|
await ensureDir(srcDir);
|
|
|
|
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(
|
2021-04-05 11:49:05 +00:00
|
|
|
async () => {
|
2019-05-16 16:19:17 +00:00
|
|
|
await copy(srcDir, destDir);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-26 04:31:34 +00:00
|
|
|
`Cannot copy '${srcDir}' to a subdirectory of itself: '${destDir}'`,
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopy(
|
2023-11-23 06:59:59 +00:00
|
|
|
"copy() rejects when copying a directory to an existent destination that is not a directory",
|
2021-04-05 11:49:05 +00:00
|
|
|
async (tempDir: string) => {
|
2019-05-16 16:19:17 +00:00
|
|
|
const srcDir = path.join(tempDir, "parent");
|
|
|
|
const destDir = path.join(tempDir, "child.txt");
|
|
|
|
|
|
|
|
await ensureDir(srcDir);
|
|
|
|
await ensureFile(destDir);
|
|
|
|
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(
|
2021-04-05 11:49:05 +00:00
|
|
|
async () => {
|
2019-05-16 16:19:17 +00:00
|
|
|
await copy(srcDir, destDir);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-26 04:31:34 +00:00
|
|
|
`Cannot overwrite non-directory '${destDir}' with directory '${srcDir}'`,
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopy(
|
2023-11-23 06:59:59 +00:00
|
|
|
"copy() copies a directory",
|
2021-04-05 11:49:05 +00:00
|
|
|
async (tempDir: string) => {
|
2019-05-16 16:19:17 +00:00
|
|
|
const srcDir = path.join(testdataDir, "copy_dir");
|
|
|
|
const destDir = path.join(tempDir, "copy_dir");
|
|
|
|
const srcFile = path.join(srcDir, "0.txt");
|
|
|
|
const destFile = path.join(destDir, "0.txt");
|
|
|
|
const srcNestFile = path.join(srcDir, "nest", "0.txt");
|
|
|
|
const destNestFile = path.join(destDir, "nest", "0.txt");
|
|
|
|
|
|
|
|
await copy(srcDir, destDir);
|
|
|
|
|
2022-08-24 04:28:54 +00:00
|
|
|
assert(await Deno.lstat(destFile));
|
|
|
|
assert(await Deno.lstat(destNestFile));
|
2019-05-16 16:19:17 +00:00
|
|
|
|
|
|
|
// After copy. The source and destination should have the same content.
|
|
|
|
assertEquals(
|
2023-11-03 04:41:18 +00:00
|
|
|
await Deno.readTextFile(srcFile),
|
|
|
|
await Deno.readTextFile(destFile),
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
assertEquals(
|
2023-11-03 04:41:18 +00:00
|
|
|
await Deno.readTextFile(srcNestFile),
|
|
|
|
await Deno.readTextFile(destNestFile),
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Copy again without overwrite option and it should throw an error.
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(
|
2021-04-05 11:49:05 +00:00
|
|
|
async () => {
|
2019-05-16 16:19:17 +00:00
|
|
|
await copy(srcDir, destDir);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-26 04:31:34 +00:00
|
|
|
`'${destDir}' already exists`,
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Modify the file in the destination directory.
|
2023-11-03 04:41:18 +00:00
|
|
|
await Deno.writeTextFile(destNestFile, "nest copy");
|
|
|
|
assertEquals(await Deno.readTextFile(destNestFile), "nest copy");
|
2019-05-16 16:19:17 +00:00
|
|
|
|
|
|
|
// Copy again with overwrite option.
|
|
|
|
await copy(srcDir, destDir, { overwrite: true });
|
|
|
|
|
|
|
|
// Make sure the file has been overwritten.
|
2023-11-03 04:41:18 +00:00
|
|
|
assertEquals(await Deno.readTextFile(destNestFile), "nest");
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopy(
|
2023-11-23 06:59:59 +00:00
|
|
|
"copy() copies a symlink file",
|
2021-04-05 11:49:05 +00:00
|
|
|
async (tempDir: string) => {
|
2019-05-16 16:19:17 +00:00
|
|
|
const dir = path.join(testdataDir, "copy_dir_link_file");
|
|
|
|
const srcLink = path.join(dir, "0.txt");
|
|
|
|
const destLink = path.join(tempDir, "0_copy.txt");
|
|
|
|
|
|
|
|
assert(
|
2020-04-16 05:40:30 +00:00
|
|
|
(await Deno.lstat(srcLink)).isSymlink,
|
2020-07-14 19:24:17 +00:00
|
|
|
`'${srcLink}' should be symlink type`,
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await copy(srcLink, destLink);
|
|
|
|
|
|
|
|
const statInfo = await Deno.lstat(destLink);
|
|
|
|
|
2020-04-16 05:40:30 +00:00
|
|
|
assert(statInfo.isSymlink, `'${destLink}' should be symlink type`);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopy(
|
2023-11-23 06:59:59 +00:00
|
|
|
"copy() copies a symlink directory",
|
2021-04-05 11:49:05 +00:00
|
|
|
async (tempDir: string) => {
|
2019-05-16 16:19:17 +00:00
|
|
|
const srcDir = path.join(testdataDir, "copy_dir");
|
|
|
|
const srcLink = path.join(tempDir, "copy_dir_link");
|
|
|
|
const destLink = path.join(tempDir, "copy_dir_link_copy");
|
|
|
|
|
|
|
|
await ensureSymlink(srcDir, srcLink);
|
|
|
|
|
|
|
|
assert(
|
2020-04-16 05:40:30 +00:00
|
|
|
(await Deno.lstat(srcLink)).isSymlink,
|
2020-07-14 19:24:17 +00:00
|
|
|
`'${srcLink}' should be symlink type`,
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await copy(srcLink, destLink);
|
|
|
|
|
|
|
|
const statInfo = await Deno.lstat(destLink);
|
|
|
|
|
2020-04-16 05:40:30 +00:00
|
|
|
assert(statInfo.isSymlink);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopySync(
|
2023-11-23 06:59:59 +00:00
|
|
|
"copySync() throws if src does not exist",
|
2022-08-24 01:21:57 +00:00
|
|
|
(tempDir: string) => {
|
2019-05-16 16:19:17 +00:00
|
|
|
const srcFile = path.join(testdataDir, "copy_file_not_exists_sync.txt");
|
|
|
|
const destFile = path.join(tempDir, "copy_file_not_exists_1_sync.txt");
|
2022-08-24 01:21:57 +00:00
|
|
|
assertThrows(() => {
|
2019-11-13 18:42:34 +00:00
|
|
|
copySync(srcFile, destFile);
|
|
|
|
});
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopySync(
|
2023-11-23 06:59:59 +00:00
|
|
|
"copySync() copies with preserve timestamps",
|
2022-08-24 01:21:57 +00:00
|
|
|
(tempDir: string) => {
|
2024-06-20 09:14:34 +00:00
|
|
|
{
|
|
|
|
const srcFile = path.join(testdataDir, "copy_file.txt");
|
|
|
|
const destFile = path.join(tempDir, "copy_file_copy.txt");
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2024-06-20 09:14:34 +00:00
|
|
|
const srcStatInfo = Deno.statSync(srcFile);
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2024-06-20 09:14:34 +00:00
|
|
|
assert(srcStatInfo.atime instanceof Date);
|
|
|
|
assert(srcStatInfo.mtime instanceof Date);
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2024-06-20 09:14:34 +00:00
|
|
|
// Copy with overwrite and preserve timestamps options.
|
|
|
|
copySync(srcFile, destFile, {
|
|
|
|
overwrite: true,
|
|
|
|
preserveTimestamps: true,
|
|
|
|
});
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2024-06-20 09:14:34 +00:00
|
|
|
const destStatInfo = Deno.statSync(destFile);
|
|
|
|
|
|
|
|
assert(destStatInfo.atime instanceof Date);
|
|
|
|
assert(destStatInfo.mtime instanceof Date);
|
|
|
|
assertEquals(destStatInfo.atime, srcStatInfo.atime);
|
|
|
|
assertEquals(destStatInfo.mtime, srcStatInfo.mtime);
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy dir with preserve timestamps
|
|
|
|
{
|
|
|
|
const srcDir = path.join(testdataDir, "copy_dir");
|
|
|
|
const destDir = path.join(tempDir, "copy_dir");
|
|
|
|
const srcFile = path.join(srcDir, "0.txt");
|
|
|
|
const destFile = path.join(destDir, "0.txt");
|
|
|
|
const srcNestFile = path.join(srcDir, "nest", "0.txt");
|
|
|
|
const destNestFile = path.join(destDir, "nest", "0.txt");
|
|
|
|
|
|
|
|
copySync(srcDir, destDir, { preserveTimestamps: true });
|
|
|
|
|
|
|
|
const srcDirInfo = Deno.statSync(srcFile);
|
|
|
|
const destDirInfo = Deno.statSync(destFile);
|
|
|
|
const srcFileInfo = Deno.statSync(srcFile);
|
|
|
|
const destFileInfo = Deno.statSync(destFile);
|
|
|
|
const srcNestFileInfo = Deno.statSync(srcNestFile);
|
|
|
|
const destNestFileInfo = Deno.statSync(destNestFile);
|
|
|
|
|
|
|
|
assertEquals(srcDirInfo.atime, destDirInfo.atime);
|
|
|
|
assertEquals(srcDirInfo.mtime, destDirInfo.mtime);
|
|
|
|
assertEquals(srcFileInfo.atime, destFileInfo.atime);
|
|
|
|
assertEquals(srcFileInfo.mtime, destFileInfo.mtime);
|
|
|
|
assertEquals(srcNestFileInfo.atime, destNestFileInfo.atime);
|
|
|
|
assertEquals(srcNestFileInfo.mtime, destNestFileInfo.mtime);
|
|
|
|
}
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopySync(
|
2023-11-23 06:59:59 +00:00
|
|
|
"copySync() throws if src and dest are the same paths",
|
2022-08-24 01:21:57 +00:00
|
|
|
() => {
|
2019-05-16 16:19:17 +00:00
|
|
|
const srcFile = path.join(testdataDir, "copy_file_same_sync.txt");
|
|
|
|
assertThrows(
|
2022-08-24 01:21:57 +00:00
|
|
|
() => {
|
2019-05-16 16:19:17 +00:00
|
|
|
copySync(srcFile, srcFile);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-26 04:31:34 +00:00
|
|
|
"Source and destination cannot be the same",
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
testCopySync("copySync() copies file to new destination", (tempDir: string) => {
|
2019-11-13 18:42:34 +00:00
|
|
|
const srcFile = path.join(testdataDir, "copy_file.txt");
|
|
|
|
const destFile = path.join(tempDir, "copy_file_copy_sync.txt");
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2019-11-13 18:42:34 +00:00
|
|
|
const srcContent = new TextDecoder().decode(Deno.readFileSync(srcFile));
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2019-11-13 18:42:34 +00:00
|
|
|
assertEquals(existsSync(srcFile), true);
|
|
|
|
assertEquals(existsSync(destFile), false);
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2019-11-13 18:42:34 +00:00
|
|
|
copySync(srcFile, destFile);
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2019-11-13 18:42:34 +00:00
|
|
|
assertEquals(existsSync(srcFile), true);
|
|
|
|
assertEquals(existsSync(destFile), true);
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2019-11-13 18:42:34 +00:00
|
|
|
const destContent = new TextDecoder().decode(Deno.readFileSync(destFile));
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2019-11-13 18:42:34 +00:00
|
|
|
assertEquals(srcContent, destContent);
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2019-11-13 18:42:34 +00:00
|
|
|
// Copy again without overwrite option and it should throw an error.
|
|
|
|
assertThrows(
|
2022-08-24 01:21:57 +00:00
|
|
|
() => {
|
2019-11-13 18:42:34 +00:00
|
|
|
copySync(srcFile, destFile);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-26 04:31:34 +00:00
|
|
|
`'${destFile}' already exists`,
|
2019-11-13 18:42:34 +00:00
|
|
|
);
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2019-11-13 18:42:34 +00:00
|
|
|
// Modify destination file.
|
|
|
|
Deno.writeFileSync(destFile, new TextEncoder().encode("txt copy"));
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2019-11-13 18:42:34 +00:00
|
|
|
assertEquals(
|
|
|
|
new TextDecoder().decode(Deno.readFileSync(destFile)),
|
2020-07-14 19:24:17 +00:00
|
|
|
"txt copy",
|
2019-11-13 18:42:34 +00:00
|
|
|
);
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2019-11-13 18:42:34 +00:00
|
|
|
// Copy again with overwrite option.
|
|
|
|
copySync(srcFile, destFile, { overwrite: true });
|
2019-05-16 16:19:17 +00:00
|
|
|
|
2019-11-13 18:42:34 +00:00
|
|
|
// Make sure the file has been overwritten.
|
|
|
|
assertEquals(new TextDecoder().decode(Deno.readFileSync(destFile)), "txt");
|
|
|
|
});
|
2019-05-16 16:19:17 +00:00
|
|
|
|
|
|
|
testCopySync(
|
2023-11-23 06:59:59 +00:00
|
|
|
"copySync() throws if destination is its own subdirectory",
|
2022-08-24 01:21:57 +00:00
|
|
|
(tempDir: string) => {
|
2019-05-16 16:19:17 +00:00
|
|
|
const srcDir = path.join(tempDir, "parent");
|
|
|
|
const destDir = path.join(srcDir, "child");
|
|
|
|
|
|
|
|
ensureDirSync(srcDir);
|
|
|
|
|
|
|
|
assertThrows(
|
2022-08-24 01:21:57 +00:00
|
|
|
() => {
|
2019-05-16 16:19:17 +00:00
|
|
|
copySync(srcDir, destDir);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-26 04:31:34 +00:00
|
|
|
`Cannot copy '${srcDir}' to a subdirectory of itself: '${destDir}'`,
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopySync(
|
2023-11-23 06:59:59 +00:00
|
|
|
"copySync() throws when copying a directory to an existent destination that is not a directory",
|
2022-08-24 01:21:57 +00:00
|
|
|
(tempDir: string) => {
|
2019-05-16 16:19:17 +00:00
|
|
|
const srcDir = path.join(tempDir, "parent_sync");
|
|
|
|
const destDir = path.join(tempDir, "child.txt");
|
|
|
|
|
|
|
|
ensureDirSync(srcDir);
|
|
|
|
ensureFileSync(destDir);
|
|
|
|
|
|
|
|
assertThrows(
|
2022-08-24 01:21:57 +00:00
|
|
|
() => {
|
2019-05-16 16:19:17 +00:00
|
|
|
copySync(srcDir, destDir);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-26 04:31:34 +00:00
|
|
|
`Cannot overwrite non-directory '${destDir}' with directory '${srcDir}'`,
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
testCopySync("copySync() copies a directory", (tempDir: string) => {
|
2019-11-13 18:42:34 +00:00
|
|
|
const srcDir = path.join(testdataDir, "copy_dir");
|
|
|
|
const destDir = path.join(tempDir, "copy_dir_copy_sync");
|
|
|
|
const srcFile = path.join(srcDir, "0.txt");
|
|
|
|
const destFile = path.join(destDir, "0.txt");
|
|
|
|
const srcNestFile = path.join(srcDir, "nest", "0.txt");
|
|
|
|
const destNestFile = path.join(destDir, "nest", "0.txt");
|
|
|
|
|
|
|
|
copySync(srcDir, destDir);
|
|
|
|
|
|
|
|
assertEquals(existsSync(destFile), true);
|
|
|
|
assertEquals(existsSync(destNestFile), true);
|
|
|
|
|
|
|
|
// After copy. The source and destination should have the same content.
|
|
|
|
assertEquals(
|
|
|
|
new TextDecoder().decode(Deno.readFileSync(srcFile)),
|
2020-07-14 19:24:17 +00:00
|
|
|
new TextDecoder().decode(Deno.readFileSync(destFile)),
|
2019-11-13 18:42:34 +00:00
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
new TextDecoder().decode(Deno.readFileSync(srcNestFile)),
|
2020-07-14 19:24:17 +00:00
|
|
|
new TextDecoder().decode(Deno.readFileSync(destNestFile)),
|
2019-11-13 18:42:34 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Copy again without overwrite option and it should throw an error.
|
|
|
|
assertThrows(
|
2022-08-24 01:21:57 +00:00
|
|
|
() => {
|
2019-11-13 18:42:34 +00:00
|
|
|
copySync(srcDir, destDir);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-26 04:31:34 +00:00
|
|
|
`'${destDir}' already exists`,
|
2019-11-13 18:42:34 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Modify the file in the destination directory.
|
|
|
|
Deno.writeFileSync(destNestFile, new TextEncoder().encode("nest copy"));
|
|
|
|
assertEquals(
|
|
|
|
new TextDecoder().decode(Deno.readFileSync(destNestFile)),
|
2020-07-14 19:24:17 +00:00
|
|
|
"nest copy",
|
2019-11-13 18:42:34 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Copy again with overwrite option.
|
|
|
|
copySync(srcDir, destDir, { overwrite: true });
|
|
|
|
|
|
|
|
// Make sure the file has been overwritten.
|
|
|
|
assertEquals(
|
|
|
|
new TextDecoder().decode(Deno.readFileSync(destNestFile)),
|
2020-07-14 19:24:17 +00:00
|
|
|
"nest",
|
2019-11-13 18:42:34 +00:00
|
|
|
);
|
|
|
|
});
|
2019-05-16 16:19:17 +00:00
|
|
|
|
|
|
|
testCopySync(
|
2023-11-23 06:59:59 +00:00
|
|
|
"copySync() copies symlink file",
|
2022-08-24 01:21:57 +00:00
|
|
|
(tempDir: string) => {
|
2019-05-16 16:19:17 +00:00
|
|
|
const dir = path.join(testdataDir, "copy_dir_link_file");
|
|
|
|
const srcLink = path.join(dir, "0.txt");
|
|
|
|
const destLink = path.join(tempDir, "0_copy.txt");
|
|
|
|
|
|
|
|
assert(
|
2020-04-16 05:40:30 +00:00
|
|
|
Deno.lstatSync(srcLink).isSymlink,
|
2020-07-14 19:24:17 +00:00
|
|
|
`'${srcLink}' should be symlink type`,
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
copySync(srcLink, destLink);
|
|
|
|
|
|
|
|
const statInfo = Deno.lstatSync(destLink);
|
|
|
|
|
2020-04-16 05:40:30 +00:00
|
|
|
assert(statInfo.isSymlink, `'${destLink}' should be symlink type`);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopySync(
|
2023-11-23 06:59:59 +00:00
|
|
|
"copySync() copies symlink directory",
|
2022-08-24 01:21:57 +00:00
|
|
|
(tempDir: string) => {
|
2019-05-16 16:19:17 +00:00
|
|
|
const originDir = path.join(testdataDir, "copy_dir");
|
|
|
|
const srcLink = path.join(tempDir, "copy_dir_link");
|
|
|
|
const destLink = path.join(tempDir, "copy_dir_link_copy");
|
|
|
|
|
|
|
|
ensureSymlinkSync(originDir, srcLink);
|
|
|
|
|
|
|
|
assert(
|
2020-04-16 05:40:30 +00:00
|
|
|
Deno.lstatSync(srcLink).isSymlink,
|
2020-07-14 19:24:17 +00:00
|
|
|
`'${srcLink}' should be symlink type`,
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
copySync(srcLink, destLink);
|
|
|
|
|
|
|
|
const statInfo = Deno.lstatSync(destLink);
|
|
|
|
|
2020-04-16 05:40:30 +00:00
|
|
|
assert(statInfo.isSymlink);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-05-16 16:19:17 +00:00
|
|
|
);
|