-
Notifications
You must be signed in to change notification settings - Fork 640
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node): add truncate and truncateSync (#765)
Co-authored-by: Yoshiya Hinosawa <[email protected]>
- Loading branch information
1 parent
3b0dd8a
commit 85fdc6d
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
import { CallbackWithError } from "./_fs_common.ts"; | ||
import { fromFileUrl } from "../path.ts"; | ||
|
||
export function truncate( | ||
path: string | URL, | ||
lenOrCallback: number | CallbackWithError, | ||
maybeCallback?: CallbackWithError, | ||
) { | ||
path = path instanceof URL ? fromFileUrl(path) : path; | ||
const len: number | undefined = typeof lenOrCallback === "number" | ||
? lenOrCallback | ||
: undefined; | ||
const callback: CallbackWithError = typeof lenOrCallback === "function" | ||
? lenOrCallback | ||
: maybeCallback as CallbackWithError; | ||
|
||
if (!callback) throw new Error("No callback function supplied"); | ||
|
||
Deno.truncate(path, len).then(() => callback(null), callback); | ||
} | ||
|
||
export function truncateSync(path: string | URL, len?: number) { | ||
path = path instanceof URL ? fromFileUrl(path) : path; | ||
|
||
Deno.truncateSync(path, len); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
import { assertEquals, assertThrows, fail } from "../../testing/asserts.ts"; | ||
import { truncate, truncateSync } from "./_fs_truncate.ts"; | ||
|
||
Deno.test({ | ||
name: "ASYNC: no callback function results in Error", | ||
fn() { | ||
assertThrows( | ||
() => { | ||
truncate("some/path", 0); | ||
}, | ||
Error, | ||
"No callback function supplied", | ||
); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "ASYNC: truncate entire file contents", | ||
async fn() { | ||
const file: string = Deno.makeTempFileSync(); | ||
await Deno.writeFile(file, new TextEncoder().encode("hello world")); | ||
|
||
await new Promise<void>((resolve, reject) => { | ||
truncate(file, (err: Error | null) => { | ||
if (err !== null) reject(); | ||
else resolve(); | ||
}); | ||
}) | ||
.then( | ||
() => { | ||
const fileInfo: Deno.FileInfo = Deno.lstatSync(file); | ||
assertEquals(fileInfo.size, 0); | ||
}, | ||
() => { | ||
fail("No error expected"); | ||
}, | ||
) | ||
.finally(() => Deno.removeSync(file)); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "ASYNC: truncate file to a size of precisely len bytes", | ||
async fn() { | ||
const file: string = Deno.makeTempFileSync(); | ||
await Deno.writeFile(file, new TextEncoder().encode("hello world")); | ||
|
||
await new Promise<void>((resolve, reject) => { | ||
truncate(file, 3, (err: Error | null) => { | ||
if (err !== null) reject(); | ||
else resolve(); | ||
}); | ||
}) | ||
.then( | ||
() => { | ||
const fileInfo: Deno.FileInfo = Deno.lstatSync(file); | ||
assertEquals(fileInfo.size, 3); | ||
}, | ||
() => { | ||
fail("No error expected"); | ||
}, | ||
) | ||
.finally(() => Deno.removeSync(file)); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "SYNC: truncate entire file contents", | ||
fn() { | ||
const file: string = Deno.makeTempFileSync(); | ||
try { | ||
truncateSync(file); | ||
const fileInfo: Deno.FileInfo = Deno.lstatSync(file); | ||
assertEquals(fileInfo.size, 0); | ||
} finally { | ||
Deno.removeSync(file); | ||
} | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "SYNC: truncate file to a size of precisely len bytes", | ||
fn() { | ||
const file: string = Deno.makeTempFileSync(); | ||
try { | ||
truncateSync(file, 3); | ||
const fileInfo: Deno.FileInfo = Deno.lstatSync(file); | ||
assertEquals(fileInfo.size, 3); | ||
} finally { | ||
Deno.removeSync(file); | ||
} | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters