2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-05-16 12:19:17 -04:00
|
|
|
import {
|
|
|
|
assertEquals,
|
|
|
|
assertThrows,
|
|
|
|
assertThrowsAsync,
|
2020-03-28 13:03:49 -04:00
|
|
|
assert,
|
2019-05-16 12:19:17 -04:00
|
|
|
} from "../testing/asserts.ts";
|
2019-10-16 14:39:33 -04:00
|
|
|
import * as path from "../path/mod.ts";
|
2019-05-16 12:19:17 -04:00
|
|
|
import { copy, copySync } from "./copy.ts";
|
|
|
|
import { exists, existsSync } from "./exists.ts";
|
|
|
|
import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
|
|
|
|
import { ensureFile, ensureFileSync } from "./ensure_file.ts";
|
|
|
|
import { ensureSymlink, ensureSymlinkSync } from "./ensure_symlink.ts";
|
|
|
|
|
|
|
|
const testdataDir = path.resolve("fs", "testdata");
|
|
|
|
|
2020-07-05 22:21:03 -04:00
|
|
|
function testCopy(
|
|
|
|
name: string,
|
|
|
|
cb: (tempDir: string) => Promise<void>,
|
2020-07-14 15:24:17 -04:00
|
|
|
ignore = false,
|
2020-07-05 22:21:03 -04:00
|
|
|
): void {
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test({
|
2019-05-16 12:19:17 -04:00
|
|
|
name,
|
|
|
|
async fn(): Promise<void> {
|
|
|
|
const tempDir = await Deno.makeTempDir({
|
2020-03-28 13:03:49 -04:00
|
|
|
prefix: "deno_std_copy_async_test_",
|
2019-05-16 12:19:17 -04:00
|
|
|
});
|
|
|
|
await cb(tempDir);
|
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-07-05 22:21:03 -04:00
|
|
|
ignore,
|
2019-05-16 12:19:17 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-05 22:21:03 -04:00
|
|
|
function testCopyIgnore(
|
|
|
|
name: string,
|
2020-07-14 15:24:17 -04:00
|
|
|
cb: (tempDir: string) => Promise<void>,
|
2020-07-05 22:21:03 -04:00
|
|
|
): void {
|
|
|
|
testCopy(name, cb, true);
|
|
|
|
}
|
|
|
|
|
2019-05-16 12:19:17 -04:00
|
|
|
function testCopySync(name: string, cb: (tempDir: string) => void): void {
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test({
|
2019-05-16 12:19:17 -04:00
|
|
|
name,
|
|
|
|
fn: (): void => {
|
|
|
|
const tempDir = Deno.makeTempDirSync({
|
2020-03-28 13:03:49 -04:00
|
|
|
prefix: "deno_std_copy_sync_test_",
|
2019-05-16 12:19:17 -04:00
|
|
|
});
|
|
|
|
cb(tempDir);
|
|
|
|
Deno.removeSync(tempDir, { recursive: true });
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-05-16 12:19:17 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
testCopy(
|
|
|
|
"[fs] copy file if it does no exist",
|
|
|
|
async (tempDir: string): Promise<void> => {
|
|
|
|
const srcFile = path.join(testdataDir, "copy_file_not_exists.txt");
|
|
|
|
const destFile = path.join(tempDir, "copy_file_not_exists_1.txt");
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await copy(srcFile, destFile);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopy(
|
|
|
|
"[fs] copy if src and dest are the same paths",
|
|
|
|
async (tempDir: string): Promise<void> => {
|
|
|
|
const srcFile = path.join(tempDir, "copy_file_same.txt");
|
|
|
|
const destFile = path.join(tempDir, "copy_file_same.txt");
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await copy(srcFile, destFile);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"Source and destination cannot be the same.",
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopy(
|
|
|
|
"[fs] copy file",
|
|
|
|
async (tempDir: string): Promise<void> => {
|
|
|
|
const srcFile = path.join(testdataDir, "copy_file.txt");
|
|
|
|
const destFile = path.join(tempDir, "copy_file_copy.txt");
|
|
|
|
|
|
|
|
const srcContent = new TextDecoder().decode(await Deno.readFile(srcFile));
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
await exists(srcFile),
|
|
|
|
true,
|
2020-07-14 15:24:17 -04:00
|
|
|
`source should exist before copy`,
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
await exists(destFile),
|
|
|
|
false,
|
2020-07-14 15:24:17 -04:00
|
|
|
"destination should not exist before copy",
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
await copy(srcFile, destFile);
|
|
|
|
|
|
|
|
assertEquals(await exists(srcFile), true, "source should exist after copy");
|
|
|
|
assertEquals(
|
|
|
|
await exists(destFile),
|
|
|
|
true,
|
2020-07-14 15:24:17 -04:00
|
|
|
"destination should exist before copy",
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const destContent = new TextDecoder().decode(await Deno.readFile(destFile));
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
srcContent,
|
|
|
|
destContent,
|
2020-07-14 15:24:17 -04:00
|
|
|
"source and destination should have the same content",
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
// Copy again and it should throw an error.
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await copy(srcFile, destFile);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
`'${destFile}' already exists.`,
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
// Modify destination file.
|
|
|
|
await Deno.writeFile(destFile, new TextEncoder().encode("txt copy"));
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
new TextDecoder().decode(await Deno.readFile(destFile)),
|
2020-07-14 15:24:17 -04:00
|
|
|
"txt copy",
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
// Copy again with overwrite option.
|
|
|
|
await copy(srcFile, destFile, { overwrite: true });
|
|
|
|
|
|
|
|
// Make sure the file has been overwritten.
|
|
|
|
assertEquals(
|
|
|
|
new TextDecoder().decode(await Deno.readFile(destFile)),
|
2020-07-14 15:24:17 -04:00
|
|
|
"txt",
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
2020-07-05 22:21:03 -04:00
|
|
|
// TODO(#6644) This case is ignored because of the issue #5065.
|
|
|
|
testCopyIgnore(
|
2019-05-16 12:19:17 -04:00
|
|
|
"[fs] copy with preserve timestamps",
|
|
|
|
async (tempDir: string): Promise<void> => {
|
|
|
|
const srcFile = path.join(testdataDir, "copy_file.txt");
|
|
|
|
const destFile = path.join(tempDir, "copy_file_copy.txt");
|
|
|
|
|
|
|
|
const srcStatInfo = await Deno.stat(srcFile);
|
|
|
|
|
2020-04-27 14:09:56 -04:00
|
|
|
assert(srcStatInfo.atime instanceof Date);
|
|
|
|
assert(srcStatInfo.mtime instanceof Date);
|
2019-05-16 12:19:17 -04:00
|
|
|
|
|
|
|
// Copy with overwrite and preserve timestamps options.
|
|
|
|
await copy(srcFile, destFile, {
|
|
|
|
overwrite: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
preserveTimestamps: true,
|
2019-05-16 12:19:17 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const destStatInfo = await Deno.stat(destFile);
|
|
|
|
|
2020-04-27 14:09:56 -04:00
|
|
|
assert(destStatInfo.atime instanceof Date);
|
|
|
|
assert(destStatInfo.mtime instanceof Date);
|
|
|
|
assertEquals(destStatInfo.atime, srcStatInfo.atime);
|
|
|
|
assertEquals(destStatInfo.mtime, srcStatInfo.mtime);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopy(
|
|
|
|
"[fs] copy directory to its subdirectory",
|
|
|
|
async (tempDir: string): Promise<void> => {
|
|
|
|
const srcDir = path.join(tempDir, "parent");
|
|
|
|
const destDir = path.join(srcDir, "child");
|
|
|
|
|
|
|
|
await ensureDir(srcDir);
|
|
|
|
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await copy(srcDir, destDir);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
`Cannot copy '${srcDir}' to a subdirectory of itself, '${destDir}'.`,
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopy(
|
|
|
|
"[fs] copy directory and destination exist and not a directory",
|
|
|
|
async (tempDir: string): Promise<void> => {
|
|
|
|
const srcDir = path.join(tempDir, "parent");
|
|
|
|
const destDir = path.join(tempDir, "child.txt");
|
|
|
|
|
|
|
|
await ensureDir(srcDir);
|
|
|
|
await ensureFile(destDir);
|
|
|
|
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await copy(srcDir, destDir);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
`Cannot overwrite non-directory '${destDir}' with directory '${srcDir}'.`,
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopy(
|
|
|
|
"[fs] copy directory",
|
|
|
|
async (tempDir: string): Promise<void> => {
|
|
|
|
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);
|
|
|
|
|
|
|
|
assertEquals(await exists(destFile), true);
|
|
|
|
assertEquals(await exists(destNestFile), true);
|
|
|
|
|
|
|
|
// After copy. The source and destination should have the same content.
|
|
|
|
assertEquals(
|
|
|
|
new TextDecoder().decode(await Deno.readFile(srcFile)),
|
2020-07-14 15:24:17 -04:00
|
|
|
new TextDecoder().decode(await Deno.readFile(destFile)),
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
new TextDecoder().decode(await Deno.readFile(srcNestFile)),
|
2020-07-14 15:24:17 -04:00
|
|
|
new TextDecoder().decode(await Deno.readFile(destNestFile)),
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
// Copy again without overwrite option and it should throw an error.
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await copy(srcDir, destDir);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
`'${destDir}' already exists.`,
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
// Modify the file in the destination directory.
|
|
|
|
await Deno.writeFile(destNestFile, new TextEncoder().encode("nest copy"));
|
|
|
|
assertEquals(
|
|
|
|
new TextDecoder().decode(await Deno.readFile(destNestFile)),
|
2020-07-14 15:24:17 -04:00
|
|
|
"nest copy",
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
// Copy again with overwrite option.
|
|
|
|
await copy(srcDir, destDir, { overwrite: true });
|
|
|
|
|
|
|
|
// Make sure the file has been overwritten.
|
|
|
|
assertEquals(
|
|
|
|
new TextDecoder().decode(await Deno.readFile(destNestFile)),
|
2020-07-14 15:24:17 -04:00
|
|
|
"nest",
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopy(
|
|
|
|
"[fs] copy symlink file",
|
|
|
|
async (tempDir: string): Promise<void> => {
|
|
|
|
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 01:40:30 -04:00
|
|
|
(await Deno.lstat(srcLink)).isSymlink,
|
2020-07-14 15:24:17 -04:00
|
|
|
`'${srcLink}' should be symlink type`,
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
await copy(srcLink, destLink);
|
|
|
|
|
|
|
|
const statInfo = await Deno.lstat(destLink);
|
|
|
|
|
2020-04-16 01:40:30 -04:00
|
|
|
assert(statInfo.isSymlink, `'${destLink}' should be symlink type`);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopy(
|
|
|
|
"[fs] copy symlink directory",
|
|
|
|
async (tempDir: string): Promise<void> => {
|
|
|
|
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 01:40:30 -04:00
|
|
|
(await Deno.lstat(srcLink)).isSymlink,
|
2020-07-14 15:24:17 -04:00
|
|
|
`'${srcLink}' should be symlink type`,
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
await copy(srcLink, destLink);
|
|
|
|
|
|
|
|
const statInfo = await Deno.lstat(destLink);
|
|
|
|
|
2020-04-16 01:40:30 -04:00
|
|
|
assert(statInfo.isSymlink);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopySync(
|
|
|
|
"[fs] copy file synchronously if it does not exist",
|
|
|
|
(tempDir: string): void => {
|
|
|
|
const srcFile = path.join(testdataDir, "copy_file_not_exists_sync.txt");
|
|
|
|
const destFile = path.join(tempDir, "copy_file_not_exists_1_sync.txt");
|
2019-11-13 13:42:34 -05:00
|
|
|
assertThrows((): void => {
|
|
|
|
copySync(srcFile, destFile);
|
|
|
|
});
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopySync(
|
|
|
|
"[fs] copy synchronously with preserve timestamps",
|
|
|
|
(tempDir: string): void => {
|
|
|
|
const srcFile = path.join(testdataDir, "copy_file.txt");
|
|
|
|
const destFile = path.join(tempDir, "copy_file_copy.txt");
|
|
|
|
|
|
|
|
const srcStatInfo = Deno.statSync(srcFile);
|
|
|
|
|
2020-04-27 14:09:56 -04:00
|
|
|
assert(srcStatInfo.atime instanceof Date);
|
|
|
|
assert(srcStatInfo.mtime instanceof Date);
|
2019-05-16 12:19:17 -04:00
|
|
|
|
|
|
|
// Copy with overwrite and preserve timestamps options.
|
|
|
|
copySync(srcFile, destFile, {
|
|
|
|
overwrite: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
preserveTimestamps: true,
|
2019-05-16 12:19:17 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const destStatInfo = Deno.statSync(destFile);
|
|
|
|
|
2020-04-27 14:09:56 -04:00
|
|
|
assert(destStatInfo.atime instanceof Date);
|
|
|
|
assert(destStatInfo.mtime instanceof Date);
|
2019-05-30 09:49:33 -04:00
|
|
|
// TODO: Activate test when https://github.com/denoland/deno/issues/2411
|
|
|
|
// is fixed
|
2020-04-27 14:09:56 -04:00
|
|
|
// assertEquals(destStatInfo.atime, srcStatInfo.atime);
|
|
|
|
// assertEquals(destStatInfo.mtime, srcStatInfo.mtime);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopySync(
|
|
|
|
"[fs] copy synchronously if src and dest are the same paths",
|
|
|
|
(): void => {
|
|
|
|
const srcFile = path.join(testdataDir, "copy_file_same_sync.txt");
|
|
|
|
assertThrows(
|
|
|
|
(): void => {
|
|
|
|
copySync(srcFile, srcFile);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"Source and destination cannot be the same.",
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
testCopySync("[fs] copy file synchronously", (tempDir: string): void => {
|
|
|
|
const srcFile = path.join(testdataDir, "copy_file.txt");
|
|
|
|
const destFile = path.join(tempDir, "copy_file_copy_sync.txt");
|
2019-05-16 12:19:17 -04:00
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
const srcContent = new TextDecoder().decode(Deno.readFileSync(srcFile));
|
2019-05-16 12:19:17 -04:00
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
assertEquals(existsSync(srcFile), true);
|
|
|
|
assertEquals(existsSync(destFile), false);
|
2019-05-16 12:19:17 -04:00
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
copySync(srcFile, destFile);
|
2019-05-16 12:19:17 -04:00
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
assertEquals(existsSync(srcFile), true);
|
|
|
|
assertEquals(existsSync(destFile), true);
|
2019-05-16 12:19:17 -04:00
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
const destContent = new TextDecoder().decode(Deno.readFileSync(destFile));
|
2019-05-16 12:19:17 -04:00
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
assertEquals(srcContent, destContent);
|
2019-05-16 12:19:17 -04:00
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
// Copy again without overwrite option and it should throw an error.
|
|
|
|
assertThrows(
|
|
|
|
(): void => {
|
|
|
|
copySync(srcFile, destFile);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
`'${destFile}' already exists.`,
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
2019-05-16 12:19:17 -04:00
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
// Modify destination file.
|
|
|
|
Deno.writeFileSync(destFile, new TextEncoder().encode("txt copy"));
|
2019-05-16 12:19:17 -04:00
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
assertEquals(
|
|
|
|
new TextDecoder().decode(Deno.readFileSync(destFile)),
|
2020-07-14 15:24:17 -04:00
|
|
|
"txt copy",
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
2019-05-16 12:19:17 -04:00
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
// Copy again with overwrite option.
|
|
|
|
copySync(srcFile, destFile, { overwrite: true });
|
2019-05-16 12:19:17 -04:00
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
// Make sure the file has been overwritten.
|
|
|
|
assertEquals(new TextDecoder().decode(Deno.readFileSync(destFile)), "txt");
|
|
|
|
});
|
2019-05-16 12:19:17 -04:00
|
|
|
|
|
|
|
testCopySync(
|
|
|
|
"[fs] copy directory synchronously to its subdirectory",
|
|
|
|
(tempDir: string): void => {
|
|
|
|
const srcDir = path.join(tempDir, "parent");
|
|
|
|
const destDir = path.join(srcDir, "child");
|
|
|
|
|
|
|
|
ensureDirSync(srcDir);
|
|
|
|
|
|
|
|
assertThrows(
|
|
|
|
(): void => {
|
|
|
|
copySync(srcDir, destDir);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
`Cannot copy '${srcDir}' to a subdirectory of itself, '${destDir}'.`,
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopySync(
|
2019-06-19 00:22:01 -04:00
|
|
|
"[fs] copy directory synchronously, and destination exist and not a " +
|
|
|
|
"directory",
|
2019-05-16 12:19:17 -04:00
|
|
|
(tempDir: string): void => {
|
|
|
|
const srcDir = path.join(tempDir, "parent_sync");
|
|
|
|
const destDir = path.join(tempDir, "child.txt");
|
|
|
|
|
|
|
|
ensureDirSync(srcDir);
|
|
|
|
ensureFileSync(destDir);
|
|
|
|
|
|
|
|
assertThrows(
|
|
|
|
(): void => {
|
|
|
|
copySync(srcDir, destDir);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
`Cannot overwrite non-directory '${destDir}' with directory '${srcDir}'.`,
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
testCopySync("[fs] copy directory synchronously", (tempDir: string): void => {
|
|
|
|
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 15:24:17 -04:00
|
|
|
new TextDecoder().decode(Deno.readFileSync(destFile)),
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
new TextDecoder().decode(Deno.readFileSync(srcNestFile)),
|
2020-07-14 15:24:17 -04:00
|
|
|
new TextDecoder().decode(Deno.readFileSync(destNestFile)),
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// Copy again without overwrite option and it should throw an error.
|
|
|
|
assertThrows(
|
|
|
|
(): void => {
|
|
|
|
copySync(srcDir, destDir);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
`'${destDir}' already exists.`,
|
2019-11-13 13:42:34 -05: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 15:24:17 -04:00
|
|
|
"nest copy",
|
2019-11-13 13:42:34 -05: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 15:24:17 -04:00
|
|
|
"nest",
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
|
|
|
});
|
2019-05-16 12:19:17 -04:00
|
|
|
|
|
|
|
testCopySync(
|
|
|
|
"[fs] copy symlink file synchronously",
|
|
|
|
(tempDir: string): void => {
|
|
|
|
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 01:40:30 -04:00
|
|
|
Deno.lstatSync(srcLink).isSymlink,
|
2020-07-14 15:24:17 -04:00
|
|
|
`'${srcLink}' should be symlink type`,
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
copySync(srcLink, destLink);
|
|
|
|
|
|
|
|
const statInfo = Deno.lstatSync(destLink);
|
|
|
|
|
2020-04-16 01:40:30 -04:00
|
|
|
assert(statInfo.isSymlink, `'${destLink}' should be symlink type`);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
testCopySync(
|
|
|
|
"[fs] copy symlink directory synchronously",
|
|
|
|
(tempDir: string): void => {
|
|
|
|
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 01:40:30 -04:00
|
|
|
Deno.lstatSync(srcLink).isSymlink,
|
2020-07-14 15:24:17 -04:00
|
|
|
`'${srcLink}' should be symlink type`,
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
copySync(srcLink, destLink);
|
|
|
|
|
|
|
|
const statInfo = Deno.lstatSync(destLink);
|
|
|
|
|
2020-04-16 01:40:30 -04:00
|
|
|
assert(statInfo.isSymlink);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-05-16 12:19:17 -04:00
|
|
|
);
|