Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fs): add copy/copySync #278

Merged
merged 30 commits into from
May 16, 2019
Merged
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
449a219
feat: add copy/copySync for fs modules
axetroy Mar 14, 2019
2aa835c
fix: eslint error
axetroy Mar 14, 2019
57af76b
use unimplemented() instead of throw by it self
axetroy Mar 16, 2019
a5231ca
fix: import without ext
axetroy Mar 17, 2019
76bdda3
Merge branch 'master' of https://github.com/denoland/deno_std into copy
axetroy Mar 17, 2019
a06d75e
refactor
axetroy Mar 17, 2019
67acf47
WIP: try to add copyLink
axetroy Mar 18, 2019
5a0b8ad
Merge branch 'master' into copy
axetroy May 13, 2019
b99d5a3
Merge branch 'copy' of https://github.com/axetroy/deno_std into copy
axetroy May 13, 2019
f79991b
fix eslint error
axetroy May 13, 2019
7c9aefc
rename function name
axetroy May 14, 2019
f670a8a
refactor add add preserve timestamps support
axetroy May 15, 2019
6c72aba
Merge branch 'master' into copy
axetroy May 15, 2019
4f6dcb4
fix eslint
axetroy May 15, 2019
fd5aebe
fix eslint
axetroy May 15, 2019
d626976
export copy() for mod.ts
axetroy May 15, 2019
37b6f4b
completed test case
axetroy May 15, 2019
98827b5
fix test in windows
axetroy May 15, 2019
347d755
update test case
axetroy May 15, 2019
ac690d6
try fix in windows
axetroy May 15, 2019
fff9a2d
try fix windows
axetroy May 15, 2019
7d4b1b8
update fs readme
axetroy May 15, 2019
86f723c
format readme
axetroy May 15, 2019
be2737d
update test case. do not modify the origin test folder
axetroy May 16, 2019
26c46f1
wrap 80 col for comments
axetroy May 16, 2019
41e05c0
fix grammar error
axetroy May 16, 2019
184aba4
refactor test case.
axetroy May 16, 2019
ca3c417
update test case
axetroy May 16, 2019
4b71be5
improve error message
axetroy May 16, 2019
94034cb
fix eslint
axetroy May 16, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix grammar error
  • Loading branch information
axetroy committed May 16, 2019
commit 41e05c0e69ec94c44974d7784ba1286bbc7f7f4f
68 changes: 35 additions & 33 deletions fs/copy_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test({
});

test({
name: "[fs] copy if src and dest is same path",
name: "[fs] copy if src and dest are the same paths",
async fn(): Promise<void> {
const destDir = await Deno.makeTempDir({ prefix: tempDirPrefix });
const srcFile = path.join(destDir, "copy_file_same.txt");
Expand All @@ -48,7 +48,7 @@ test({
await copy(srcFile, destFile);
},
Error,
"Source and destination must not be the same."
"Source and destination can not be the same."
);
await Deno.remove(destDir, { recursive: true });
axetroy marked this conversation as resolved.
Show resolved Hide resolved
}
Expand All @@ -66,32 +66,32 @@ test({
assertEquals(
await exists(srcFile),
true,
"src should be exist before copy"
`source should exist before copy`
);
assertEquals(
await exists(destFile),
false,
"dest should not exist before copy"
"destination should not exist before copy"
);

await copy(srcFile, destFile);

assertEquals(await exists(srcFile), true, "src should be exist after copy");
assertEquals(await exists(srcFile), true, "source should exist after copy");
assertEquals(
await exists(destFile),
true,
"dest should be exist before copy"
"destination should exist before copy"
);

const destContent = new TextDecoder().decode(await Deno.readFile(destFile));

assertEquals(
srcContent,
destContent,
"src and dest should have same content"
"source and destination should have the same content"
);

// copy again. it should throw a error
// Copy again and it should throw an error.
await assertThrowsAsync(
async (): Promise<void> => {
await copy(srcFile, destFile);
Expand All @@ -108,10 +108,10 @@ test({
"txt copy"
);

// copy again with overwrite
// Copy again with overwrite option.
await copy(srcFile, destFile, { overwrite: true });

// file have been overwrite
// Make sure the file has been overwritten.
assertEquals(
new TextDecoder().decode(await Deno.readFile(destFile)),
"txt"
Expand All @@ -133,7 +133,7 @@ test({
assert(typeof srcStatInfo.accessed === "number");
assert(typeof srcStatInfo.modified === "number");

// overwrite and preserve timestamps
// Copy with overwrite and preserve timestamps options.
await copy(srcFile, destFile, {
overwrite: true,
preserveTimestamps: true
Expand All @@ -151,7 +151,7 @@ test({
});

test({
name: "[fs] copy directory from parent dir",
name: "[fs] copy directory to its subdirectory",
async fn(): Promise<void> {
const tempDir = await Deno.makeTempDir({ prefix: tempDirPrefix });
const srcDir = path.join(tempDir, "parent");
Expand All @@ -172,7 +172,7 @@ test({
});

test({
name: "[fs] copy directory, and dest exist and not a directory",
name: "[fs] copy directory and destination exist and not a directory",
async fn(): Promise<void> {
const tempDir = await Deno.makeTempDir({ prefix: tempDirPrefix });
const srcDir = path.join(tempDir, "parent");
Expand Down Expand Up @@ -209,7 +209,7 @@ test({
assertEquals(await exists(destFile), true);
assertEquals(await exists(destNestFile), true);

// should have the same content
// After copy. The source and destination should have the same content.
assertEquals(
new TextDecoder().decode(await Deno.readFile(srcFile)),
new TextDecoder().decode(await Deno.readFile(destFile))
Expand All @@ -219,7 +219,7 @@ test({
new TextDecoder().decode(await Deno.readFile(destNestFile))
);

// copy again. it should throw a error
// Copy again without overwrite option and it should throw an error.
await assertThrowsAsync(
async (): Promise<void> => {
await copy(srcDir, destDir);
Expand All @@ -228,17 +228,17 @@ test({
`'${destDir}' already exists`
);

// update nest file
// Modify the file in the destination directory.
await Deno.writeFile(destNestFile, new TextEncoder().encode("nest copy"));
assertEquals(
new TextDecoder().decode(await Deno.readFile(destNestFile)),
"nest copy"
);

// copy again with overwrite
// Copy again with overwrite option.
await copy(srcDir, destDir, { overwrite: true });

// nest file have been overwrite
// Make sure the file has been overwritten.
assertEquals(
new TextDecoder().decode(await Deno.readFile(destNestFile)),
"nest"
Expand Down Expand Up @@ -280,7 +280,7 @@ test({
});

test({
name: "[fs] copy symlink folder",
name: "[fs] copy symlink directory",
async fn(): Promise<void> {
const destDir = await Deno.makeTempDir({ prefix: tempDirPrefix });
const srcDir = path.join(testdataDir, "copy_dir"); // origin dir
Expand Down Expand Up @@ -314,7 +314,7 @@ test({
});

test({
name: "[fs] copy file synchronously if it does no exist",
name: "[fs] copy file synchronously if it does not exist",
fn(): void {
const destDir = Deno.makeTempDirSync({ prefix: tempDirPrefix });
const srcFile = path.join(testdataDir, "copy_file_not_exists_sync.txt");
Expand All @@ -328,7 +328,7 @@ test({
});

test({
name: "[fs] copy with preserve timestamps",
name: "[fs] copy synchronously with preserve timestamps",
fn(): void {
const destDir = Deno.makeTempDirSync({ prefix: tempDirPrefix });
const srcFile = path.join(testdataDir, "copy_file.txt");
Expand All @@ -339,7 +339,7 @@ test({
assert(typeof srcStatInfo.accessed === "number");
assert(typeof srcStatInfo.modified === "number");

// overwrite and preserve timestamps
// Copy with overwrite and preserve timestamps options.
copySync(srcFile, destFile, {
overwrite: true,
preserveTimestamps: true
Expand All @@ -357,7 +357,7 @@ test({
});

test({
name: "[fs] copy synchronously if src and dest is same path",
name: "[fs] copy synchronously if src and dest are the same paths",
fn(): void {
const srcFile = path.join(testdataDir, "copy_file_same_sync.txt");
assertThrows(
Expand Down Expand Up @@ -391,7 +391,7 @@ test({

assertEquals(srcContent, destContent);

// copy again. it should throw a error
// Copy again without overwrite option and it should throw an error.
assertThrows(
(): void => {
copySync(srcFile, destFile);
Expand All @@ -408,18 +408,19 @@ test({
"txt copy"
);

// copy again with overwrite
// Copy again with overwrite option.
copySync(srcFile, destFile, { overwrite: true });

// file have been overwrite
// Make sure the file has been overwritten.
assertEquals(new TextDecoder().decode(Deno.readFileSync(destFile)), "txt");

Deno.removeSync(destDir, { recursive: true });
}
});

test({
name: "[fs] copy directory synchronously from parent dir",
// name: "[fs] copy directory synchronously from parent dir",
name: "[fs] copy directory synchronously to its subdirectory",
fn(): void {
const tempDir = Deno.makeTempDirSync({ prefix: tempDirPrefix });
const srcDir = path.join(tempDir, "parent_sync");
Expand All @@ -440,7 +441,8 @@ test({
});

test({
name: "[fs] copy directory synchronously, and dest exist and not a directory",
name:
"[fs] copy directory synchronously, and destination exist and not a directory",
fn(): void {
const tempDir = Deno.makeTempDirSync({ prefix: tempDirPrefix });
const srcDir = path.join(tempDir, "parent_sync");
Expand Down Expand Up @@ -487,7 +489,7 @@ test({
new TextDecoder().decode(Deno.readFileSync(destNestFile))
);

// copy again. it should throw a error
// Copy again without overwrite option and it should throw an error.
assertThrows(
(): void => {
copySync(srcDir, destDir);
Expand All @@ -496,17 +498,17 @@ test({
`'${destDir}' already exists`
);

// update nest file
// Modify the file in the destination directory.
Deno.writeFileSync(destNestFile, new TextEncoder().encode("nest copy"));
assertEquals(
new TextDecoder().decode(Deno.readFileSync(destNestFile)),
"nest copy"
);

// copy again with overwrite
// Copy again with overwrite option.
copySync(srcDir, destDir, { overwrite: true });

// nest file have been overwrite
// Make sure the file has been overwritten.
assertEquals(
new TextDecoder().decode(Deno.readFileSync(destNestFile)),
"nest"
Expand Down Expand Up @@ -548,7 +550,7 @@ test({
});

test({
name: "[fs] copy symlink folder synchronously",
name: "[fs] copy symlink directory synchronously",
fn(): void {
const destDir = Deno.makeTempDirSync({ prefix: tempDirPrefix });
const originDir = path.join(testdataDir, "copy_dir"); // origin dir
Expand Down