summaryrefslogtreecommitdiff
path: root/std/fs
diff options
context:
space:
mode:
Diffstat (limited to 'std/fs')
-rw-r--r--std/fs/README.md32
-rw-r--r--std/fs/copy_test.ts170
-rw-r--r--std/fs/empty_dir_test.ts16
-rw-r--r--std/fs/ensure_dir_test.ts26
-rw-r--r--std/fs/ensure_file_test.ts36
-rw-r--r--std/fs/ensure_link_test.ts8
-rw-r--r--std/fs/ensure_symlink_test.ts26
-rw-r--r--std/fs/glob.ts8
-rw-r--r--std/fs/move_test.ts16
-rw-r--r--std/fs/read_json_test.ts24
-rw-r--r--std/fs/walk.ts12
11 files changed, 210 insertions, 164 deletions
diff --git a/std/fs/README.md b/std/fs/README.md
index b774e061a..56c190ccc 100644
--- a/std/fs/README.md
+++ b/std/fs/README.md
@@ -8,9 +8,9 @@ All the following modules are exposed in `mod.ts`
### emptyDir
-Ensures that a directory is empty. Deletes directory contents if the directory is not empty.
-If the directory does not exist, it is created.
-The directory itself is not deleted.
+Ensures that a directory is empty. Deletes directory contents if the directory
+is not empty. If the directory does not exist, it is created. The directory
+itself is not deleted.
```ts
import { emptyDir, emptyDirSync } from "https://deno.land/std/fs/mod.ts";
@@ -21,8 +21,8 @@ emptyDirSync("./foo"); // void
### ensureDir
-Ensures that the directory exists.
-If the directory structure does not exist, it is created. Like mkdir -p.
+Ensures that the directory exists. If the directory structure does not exist, it
+is created. Like mkdir -p.
```ts
import { ensureDir, ensureDirSync } from "https://deno.land/std/fs/mod.ts";
@@ -33,10 +33,9 @@ ensureDirSync("./ensureDirSync"); // void
### ensureFile
-Ensures that the file exists.
-If the file that is requested to be created is in directories
-that do not exist, these directories are created.
-If the file already exists, it is **NOT MODIFIED**.
+Ensures that the file exists. If the file that is requested to be created is in
+directories that do not exist, these directories are created. If the file
+already exists, it is **NOT MODIFIED**.
```ts
import { ensureFile, ensureFileSync } from "https://deno.land/std/fs/mod.ts";
@@ -47,8 +46,8 @@ ensureFileSync("./folder/targetFile.dat"); // void
### ensureSymlink
-Ensures that the link exists.
-If the directory structure does not exist, it is created.
+Ensures that the link exists. If the directory structure does not exist, it is
+created.
```ts
import {
@@ -102,9 +101,8 @@ existsSync("./foo"); // returns boolean
### globToRegExp
-Generate a regex based on glob pattern and options
-This was meant to be using the the `fs.walk` function
-but can be used anywhere else.
+Generate a regex based on glob pattern and options This was meant to be using
+the the `fs.walk` function but can be used anywhere else.
```ts
import { globToRegExp } from "https://deno.land/std/fs/mod.ts";
@@ -180,8 +178,10 @@ Writes an object to a JSON file.
**WriteJsonOptions**
-- replacer : An array of strings and numbers that acts as a approved list for selecting the object properties that will be stringified.
-- space : Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
+- replacer : An array of strings and numbers that acts as a approved list for
+ selecting the object properties that will be stringified.
+- space : Adds indentation, white space, and line break characters to the
+ return-value JSON text to make it easier to read.
```ts
import { writeJson, writeJsonSync } from "https://deno.land/std/fs/mod.ts";
diff --git a/std/fs/copy_test.ts b/std/fs/copy_test.ts
index 347d2532c..a11838c4b 100644
--- a/std/fs/copy_test.ts
+++ b/std/fs/copy_test.ts
@@ -317,9 +317,11 @@ testCopySync(
(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");
- assertThrows((): void => {
- copySync(srcFile, destFile);
- });
+ assertThrows(
+ (): void => {
+ copySync(srcFile, destFile);
+ }
+ );
}
);
@@ -365,47 +367,50 @@ testCopySync(
}
);
-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");
+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");
- const srcContent = new TextDecoder().decode(Deno.readFileSync(srcFile));
+ const srcContent = new TextDecoder().decode(Deno.readFileSync(srcFile));
- assertEquals(existsSync(srcFile), true);
- assertEquals(existsSync(destFile), false);
+ assertEquals(existsSync(srcFile), true);
+ assertEquals(existsSync(destFile), false);
- copySync(srcFile, destFile);
+ copySync(srcFile, destFile);
- assertEquals(existsSync(srcFile), true);
- assertEquals(existsSync(destFile), true);
+ assertEquals(existsSync(srcFile), true);
+ assertEquals(existsSync(destFile), true);
- const destContent = new TextDecoder().decode(Deno.readFileSync(destFile));
+ const destContent = new TextDecoder().decode(Deno.readFileSync(destFile));
- assertEquals(srcContent, destContent);
+ assertEquals(srcContent, destContent);
- // Copy again without overwrite option and it should throw an error.
- assertThrows(
- (): void => {
- copySync(srcFile, destFile);
- },
- Error,
- `'${destFile}' already exists.`
- );
+ // Copy again without overwrite option and it should throw an error.
+ assertThrows(
+ (): void => {
+ copySync(srcFile, destFile);
+ },
+ Error,
+ `'${destFile}' already exists.`
+ );
- // Modify destination file.
- Deno.writeFileSync(destFile, new TextEncoder().encode("txt copy"));
+ // Modify destination file.
+ Deno.writeFileSync(destFile, new TextEncoder().encode("txt copy"));
- assertEquals(
- new TextDecoder().decode(Deno.readFileSync(destFile)),
- "txt copy"
- );
+ assertEquals(
+ new TextDecoder().decode(Deno.readFileSync(destFile)),
+ "txt copy"
+ );
- // Copy again with overwrite option.
- copySync(srcFile, destFile, { overwrite: true });
+ // Copy again with overwrite option.
+ copySync(srcFile, destFile, { overwrite: true });
- // Make sure the file has been overwritten.
- assertEquals(new TextDecoder().decode(Deno.readFileSync(destFile)), "txt");
-});
+ // Make sure the file has been overwritten.
+ assertEquals(new TextDecoder().decode(Deno.readFileSync(destFile)), "txt");
+ }
+);
testCopySync(
"[fs] copy directory synchronously to its subdirectory",
@@ -445,54 +450,57 @@ testCopySync(
}
);
-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)),
- new TextDecoder().decode(Deno.readFileSync(destFile))
- );
- assertEquals(
- new TextDecoder().decode(Deno.readFileSync(srcNestFile)),
- new TextDecoder().decode(Deno.readFileSync(destNestFile))
- );
-
- // Copy again without overwrite option and it should throw an error.
- assertThrows(
- (): void => {
- copySync(srcDir, destDir);
- },
- Error,
- `'${destDir}' already exists.`
- );
-
- // 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 option.
- copySync(srcDir, destDir, { overwrite: true });
-
- // Make sure the file has been overwritten.
- assertEquals(
- new TextDecoder().decode(Deno.readFileSync(destNestFile)),
- "nest"
- );
-});
+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)),
+ new TextDecoder().decode(Deno.readFileSync(destFile))
+ );
+ assertEquals(
+ new TextDecoder().decode(Deno.readFileSync(srcNestFile)),
+ new TextDecoder().decode(Deno.readFileSync(destNestFile))
+ );
+
+ // Copy again without overwrite option and it should throw an error.
+ assertThrows(
+ (): void => {
+ copySync(srcDir, destDir);
+ },
+ Error,
+ `'${destDir}' already exists.`
+ );
+
+ // 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 option.
+ copySync(srcDir, destDir, { overwrite: true });
+
+ // Make sure the file has been overwritten.
+ assertEquals(
+ new TextDecoder().decode(Deno.readFileSync(destNestFile)),
+ "nest"
+ );
+ }
+);
testCopySync(
"[fs] copy symlink file synchronously",
diff --git a/std/fs/empty_dir_test.ts b/std/fs/empty_dir_test.ts
index 80d3a1789..b44e600d7 100644
--- a/std/fs/empty_dir_test.ts
+++ b/std/fs/empty_dir_test.ts
@@ -110,14 +110,18 @@ test(function emptyDirSyncIfItExist(): void {
assertEquals(stat.isDirectory(), true);
// nest directory have been remove
- assertThrows((): void => {
- Deno.statSync(testNestDir);
- });
+ assertThrows(
+ (): void => {
+ Deno.statSync(testNestDir);
+ }
+ );
// test file have been remove
- assertThrows((): void => {
- Deno.statSync(testDirFile);
- });
+ assertThrows(
+ (): void => {
+ Deno.statSync(testDirFile);
+ }
+ );
} finally {
// remote test dir
Deno.removeSync(testDir, { recursive: true });
diff --git a/std/fs/ensure_dir_test.ts b/std/fs/ensure_dir_test.ts
index affffdbe6..ad34336dc 100644
--- a/std/fs/ensure_dir_test.ts
+++ b/std/fs/ensure_dir_test.ts
@@ -15,9 +15,11 @@ test(async function ensureDirIfItNotExist(): Promise<void> {
await assertThrowsAsync(
async (): Promise<void> => {
- await Deno.stat(testDir).then((): void => {
- throw new Error("test dir should exists.");
- });
+ await Deno.stat(testDir).then(
+ (): void => {
+ throw new Error("test dir should exists.");
+ }
+ );
}
);
@@ -46,9 +48,11 @@ test(async function ensureDirIfItExist(): Promise<void> {
await assertThrowsAsync(
async (): Promise<void> => {
- await Deno.stat(testDir).then((): void => {
- throw new Error("test dir should still exists.");
- });
+ await Deno.stat(testDir).then(
+ (): void => {
+ throw new Error("test dir should still exists.");
+ }
+ );
}
);
@@ -64,10 +68,12 @@ test(function ensureDirSyncIfItExist(): void {
ensureDirSync(testDir);
- assertThrows((): void => {
- Deno.statSync(testDir);
- throw new Error("test dir should still exists.");
- });
+ assertThrows(
+ (): void => {
+ Deno.statSync(testDir);
+ throw new Error("test dir should still exists.");
+ }
+ );
Deno.removeSync(baseDir, { recursive: true });
});
diff --git a/std/fs/ensure_file_test.ts b/std/fs/ensure_file_test.ts
index 56f180786..fa27133ab 100644
--- a/std/fs/ensure_file_test.ts
+++ b/std/fs/ensure_file_test.ts
@@ -14,9 +14,11 @@ test(async function ensureFileIfItNotExist(): Promise<void> {
await assertThrowsAsync(
async (): Promise<void> => {
- await Deno.stat(testFile).then((): void => {
- throw new Error("test file should exists.");
- });
+ await Deno.stat(testFile).then(
+ (): void => {
+ throw new Error("test file should exists.");
+ }
+ );
}
);
@@ -29,10 +31,12 @@ test(function ensureFileSyncIfItNotExist(): void {
ensureFileSync(testFile);
- assertThrows((): void => {
- Deno.statSync(testFile);
- throw new Error("test file should exists.");
- });
+ assertThrows(
+ (): void => {
+ Deno.statSync(testFile);
+ throw new Error("test file should exists.");
+ }
+ );
Deno.removeSync(testDir, { recursive: true });
});
@@ -48,9 +52,11 @@ test(async function ensureFileIfItExist(): Promise<void> {
await assertThrowsAsync(
async (): Promise<void> => {
- await Deno.stat(testFile).then((): void => {
- throw new Error("test file should exists.");
- });
+ await Deno.stat(testFile).then(
+ (): void => {
+ throw new Error("test file should exists.");
+ }
+ );
}
);
@@ -66,10 +72,12 @@ test(function ensureFileSyncIfItExist(): void {
ensureFileSync(testFile);
- assertThrows((): void => {
- Deno.statSync(testFile);
- throw new Error("test file should exists.");
- });
+ assertThrows(
+ (): void => {
+ Deno.statSync(testFile);
+ throw new Error("test file should exists.");
+ }
+ );
Deno.removeSync(testDir, { recursive: true });
});
diff --git a/std/fs/ensure_link_test.ts b/std/fs/ensure_link_test.ts
index 6d5758268..593df5702 100644
--- a/std/fs/ensure_link_test.ts
+++ b/std/fs/ensure_link_test.ts
@@ -31,9 +31,11 @@ test(function ensureLinkSyncIfItNotExist(): void {
const testFile = path.join(testDir, "test.txt");
const linkFile = path.join(testDir, "link.txt");
- assertThrows((): void => {
- ensureLinkSync(testFile, linkFile);
- });
+ assertThrows(
+ (): void => {
+ ensureLinkSync(testFile, linkFile);
+ }
+ );
Deno.removeSync(testDir, { recursive: true });
});
diff --git a/std/fs/ensure_symlink_test.ts b/std/fs/ensure_symlink_test.ts
index 82312b758..4dee56788 100644
--- a/std/fs/ensure_symlink_test.ts
+++ b/std/fs/ensure_symlink_test.ts
@@ -24,9 +24,11 @@ test(async function ensureSymlinkIfItNotExist(): Promise<void> {
assertThrowsAsync(
async (): Promise<void> => {
- await Deno.stat(testFile).then((): void => {
- throw new Error("test file should exists.");
- });
+ await Deno.stat(testFile).then(
+ (): void => {
+ throw new Error("test file should exists.");
+ }
+ );
}
);
});
@@ -35,14 +37,18 @@ test(function ensureSymlinkSyncIfItNotExist(): void {
const testDir = path.join(testdataDir, "link_file_2");
const testFile = path.join(testDir, "test.txt");
- assertThrows((): void => {
- ensureSymlinkSync(testFile, path.join(testDir, "test1.txt"));
- });
+ assertThrows(
+ (): void => {
+ ensureSymlinkSync(testFile, path.join(testDir, "test1.txt"));
+ }
+ );
- assertThrows((): void => {
- Deno.statSync(testFile);
- throw new Error("test file should exists.");
- });
+ assertThrows(
+ (): void => {
+ Deno.statSync(testFile);
+ throw new Error("test file should exists.");
+ }
+ );
});
test(async function ensureSymlinkIfItExist(): Promise<void> {
diff --git a/std/fs/glob.ts b/std/fs/glob.ts
index 86d83b4f8..9be6197fc 100644
--- a/std/fs/glob.ts
+++ b/std/fs/glob.ts
@@ -246,8 +246,8 @@ export async function* expandGlob(
);
}
if (hasTrailingSep) {
- currentMatches = currentMatches.filter(({ info }): boolean =>
- info.isDirectory()
+ currentMatches = currentMatches.filter(
+ ({ info }): boolean => info.isDirectory()
);
}
if (!includeDirs) {
@@ -348,8 +348,8 @@ export function* expandGlobSync(
);
}
if (hasTrailingSep) {
- currentMatches = currentMatches.filter(({ info }): boolean =>
- info.isDirectory()
+ currentMatches = currentMatches.filter(
+ ({ info }): boolean => info.isDirectory()
);
}
if (!includeDirs) {
diff --git a/std/fs/move_test.ts b/std/fs/move_test.ts
index bc73784b2..fae951e1f 100644
--- a/std/fs/move_test.ts
+++ b/std/fs/move_test.ts
@@ -182,9 +182,11 @@ test(function moveSyncDirectoryIfSrcNotExists(): void {
const srcDir = path.join(testdataDir, "move_sync_test_src_1");
const destDir = path.join(testdataDir, "move_sync_test_dest_1");
// if src directory not exist
- assertThrows((): void => {
- moveSync(srcDir, destDir);
- });
+ assertThrows(
+ (): void => {
+ moveSync(srcDir, destDir);
+ }
+ );
});
test(function moveSyncDirectoryIfDestNotExists(): void {
@@ -211,9 +213,11 @@ test(function moveSyncFileIfSrcNotExists(): void {
const destFile = path.join(testdataDir, "move_sync_test_dest_3", "test.txt");
// if src directory not exist
- assertThrows((): void => {
- moveSync(srcFile, destFile);
- });
+ assertThrows(
+ (): void => {
+ moveSync(srcFile, destFile);
+ }
+ );
});
test(function moveSyncFileIfDestExists(): void {
diff --git a/std/fs/read_json_test.ts b/std/fs/read_json_test.ts
index 28f733055..c8aa6dcf1 100644
--- a/std/fs/read_json_test.ts
+++ b/std/fs/read_json_test.ts
@@ -65,25 +65,31 @@ test(async function readValidObjJsonFileWithRelativePath(): Promise<void> {
test(function readJsonFileNotExistsSync(): void {
const emptyJsonFile = path.join(testdataDir, "json_not_exists.json");
- assertThrows((): void => {
- readJsonSync(emptyJsonFile);
- });
+ assertThrows(
+ (): void => {
+ readJsonSync(emptyJsonFile);
+ }
+ );
});
test(function readEmptyJsonFileSync(): void {
const emptyJsonFile = path.join(testdataDir, "json_empty.json");
- assertThrows((): void => {
- readJsonSync(emptyJsonFile);
- });
+ assertThrows(
+ (): void => {
+ readJsonSync(emptyJsonFile);
+ }
+ );
});
test(function readInvalidJsonFile(): void {
const invalidJsonFile = path.join(testdataDir, "json_invalid.json");
- assertThrows((): void => {
- readJsonSync(invalidJsonFile);
- });
+ assertThrows(
+ (): void => {
+ readJsonSync(invalidJsonFile);
+ }
+ );
});
test(function readValidArrayJsonFileSync(): void {
diff --git a/std/fs/walk.ts b/std/fs/walk.ts
index 4808598af..16ccc357e 100644
--- a/std/fs/walk.ts
+++ b/std/fs/walk.ts
@@ -21,11 +21,13 @@ function patternTest(patterns: RegExp[], path: string): boolean {
// Forced to reset last index on regex while iterating for have
// consistent results.
// See: https://stackoverflow.com/a/1520853
- return patterns.some((pattern): boolean => {
- const r = pattern.test(path);
- pattern.lastIndex = 0;
- return r;
- });
+ return patterns.some(
+ (pattern): boolean => {
+ const r = pattern.test(path);
+ pattern.lastIndex = 0;
+ return r;
+ }
+ );
}
function include(filename: string, options: WalkOptions): boolean {