|
| 1 | +// Copyright 2018-2025 the Deno authors. MIT license. |
| 2 | + |
| 3 | +import { assertEquals, assertRejects, assertThrows } from "@std/assert"; |
| 4 | +import { truncate, truncateSync } from "./unstable_truncate.ts"; |
| 5 | +import { NotFound } from "./unstable_errors.js"; |
| 6 | +import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; |
| 7 | +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 8 | +import { join, resolve } from "node:path"; |
| 9 | +import { tmpdir } from "node:os"; |
| 10 | + |
| 11 | +Deno.test("truncate() succeeds in truncating file sizes", async () => { |
| 12 | + const tempDataDir = await mkdtemp(resolve(tmpdir(), "truncate_")); |
| 13 | + const testFile = join(tempDataDir, "truncFile.txt"); |
| 14 | + await writeFile(testFile, "Hello, Standard Library"); |
| 15 | + |
| 16 | + await truncate(testFile, 30); |
| 17 | + assertEquals((await readFile(testFile)).length, 30); |
| 18 | + await truncate(testFile, 10); |
| 19 | + assertEquals((await readFile(testFile)).length, 10); |
| 20 | + await truncate(testFile, -5); |
| 21 | + assertEquals((await readFile(testFile)).length, 0); |
| 22 | + |
| 23 | + await rm(tempDataDir, { recursive: true, force: true }); |
| 24 | +}); |
| 25 | + |
| 26 | +Deno.test("truncate() truncates the file to zero when 'len' is not provided", async () => { |
| 27 | + const tempDataDir = await mkdtemp(resolve(tmpdir(), "truncate_")); |
| 28 | + const testFile = join(tempDataDir, "truncFile.txt"); |
| 29 | + await writeFile(testFile, "Hello, Standard Library"); |
| 30 | + |
| 31 | + await truncate(testFile); |
| 32 | + assertEquals((await readFile(testFile)).length, 0); |
| 33 | + |
| 34 | + await rm(tempDataDir, { recursive: true, force: true }); |
| 35 | +}); |
| 36 | + |
| 37 | +Deno.test("truncate() rejects with Error when passing a non-regular file", async () => { |
| 38 | + const tempDataDir = await mkdtemp(resolve(tmpdir(), "truncate_")); |
| 39 | + |
| 40 | + await assertRejects(async () => { |
| 41 | + await truncate(tempDataDir); |
| 42 | + }, Error); |
| 43 | + |
| 44 | + await rm(tempDataDir, { recursive: true, force: true }); |
| 45 | +}); |
| 46 | + |
| 47 | +Deno.test("truncate() rejects with NotFound with a non-existent file", async () => { |
| 48 | + await assertRejects(async () => { |
| 49 | + await truncate("non-existent-file.txt"); |
| 50 | + }, NotFound); |
| 51 | +}); |
| 52 | + |
| 53 | +Deno.test("truncateSync() succeeds in truncating file sizes", () => { |
| 54 | + const tempDataDir = mkdtempSync(resolve(tmpdir(), "truncateSync_")); |
| 55 | + const testFile = join(tempDataDir, "truncFile.txt"); |
| 56 | + writeFileSync(testFile, "Hello, Standard Library"); |
| 57 | + |
| 58 | + truncateSync(testFile, 30); |
| 59 | + assertEquals((readFileSync(testFile)).length, 30); |
| 60 | + truncateSync(testFile, 10); |
| 61 | + assertEquals((readFileSync(testFile)).length, 10); |
| 62 | + truncateSync(testFile, -5); |
| 63 | + assertEquals((readFileSync(testFile)).length, 0); |
| 64 | + |
| 65 | + rmSync(tempDataDir, { recursive: true, force: true }); |
| 66 | +}); |
| 67 | + |
| 68 | +Deno.test("truncateSync() truncates the file to zero when 'len' is not provided", () => { |
| 69 | + const tempDataDir = mkdtempSync(resolve(tmpdir(), "truncateSync_")); |
| 70 | + const testFile = join(tempDataDir, "truncFile.txt"); |
| 71 | + writeFileSync(testFile, "Hello, Standard Library"); |
| 72 | + |
| 73 | + truncateSync(testFile); |
| 74 | + assertEquals((readFileSync(testFile)).length, 0); |
| 75 | + |
| 76 | + rmSync(tempDataDir, { recursive: true, force: true }); |
| 77 | +}); |
| 78 | + |
| 79 | +Deno.test("truncateSync() throws with Error with a non-regular file", () => { |
| 80 | + const tempDataDir = mkdtempSync(resolve(tmpdir(), "truncateSync_")); |
| 81 | + |
| 82 | + assertThrows(() => { |
| 83 | + truncateSync(tempDataDir); |
| 84 | + }, Error); |
| 85 | + |
| 86 | + rmSync(tempDataDir, { recursive: true, force: true }); |
| 87 | +}); |
| 88 | + |
| 89 | +Deno.test("truncateSync() throws with NotFound with a non-existent file", () => { |
| 90 | + assertThrows(() => { |
| 91 | + truncateSync("non-existent-file.txt"); |
| 92 | + }, NotFound); |
| 93 | +}); |
0 commit comments