2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 20:48:46 -05:00
|
|
|
import { testPerm, assertEquals } from "./test_util.ts";
|
2018-09-30 15:06:20 -04:00
|
|
|
|
|
|
|
function readDataSync(name: string): string {
|
2019-02-12 10:08:56 -05:00
|
|
|
const data = Deno.readFileSync(name);
|
2018-09-30 15:06:20 -04:00
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
const text = decoder.decode(data);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function readData(name: string): Promise<string> {
|
2019-02-12 10:08:56 -05:00
|
|
|
const data = await Deno.readFile(name);
|
2018-09-30 15:06:20 -04:00
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
const text = decoder.decode(data);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ read: true, write: true }, function truncateSyncSuccess(): void {
|
2018-09-30 15:06:20 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const d = enc.encode("Hello");
|
2019-02-12 10:08:56 -05:00
|
|
|
const filename = Deno.makeTempDirSync() + "/test_truncateSync.txt";
|
|
|
|
Deno.writeFileSync(filename, d);
|
|
|
|
Deno.truncateSync(filename, 20);
|
2018-09-30 15:06:20 -04:00
|
|
|
let data = readDataSync(filename);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(data.length, 20);
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.truncateSync(filename, 5);
|
2018-09-30 15:06:20 -04:00
|
|
|
data = readDataSync(filename);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(data.length, 5);
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.truncateSync(filename, -5);
|
2018-09-30 15:06:20 -04:00
|
|
|
data = readDataSync(filename);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(data.length, 0);
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.removeSync(filename);
|
2018-09-30 15:06:20 -04:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ read: true, write: true }, async function truncateSuccess(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2018-09-30 15:06:20 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const d = enc.encode("Hello");
|
2019-02-12 10:08:56 -05:00
|
|
|
const filename = Deno.makeTempDirSync() + "/test_truncate.txt";
|
|
|
|
await Deno.writeFile(filename, d);
|
|
|
|
await Deno.truncate(filename, 20);
|
2018-09-30 15:06:20 -04:00
|
|
|
let data = await readData(filename);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(data.length, 20);
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.truncate(filename, 5);
|
2018-09-30 15:06:20 -04:00
|
|
|
data = await readData(filename);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(data.length, 5);
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.truncate(filename, -5);
|
2018-09-30 15:06:20 -04:00
|
|
|
data = await readData(filename);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(data.length, 0);
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.remove(filename);
|
2018-09-30 15:06:20 -04:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ write: false }, function truncateSyncPerm(): void {
|
2018-09-30 15:06:20 -04:00
|
|
|
let err;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.mkdirSync("/test_truncateSyncPermission.txt");
|
2018-09-30 15:06:20 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.kind, Deno.ErrorKind.PermissionDenied);
|
|
|
|
assertEquals(err.name, "PermissionDenied");
|
2018-09-30 15:06:20 -04:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ write: false }, async function truncatePerm(): Promise<void> {
|
2018-09-30 15:06:20 -04:00
|
|
|
let err;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.mkdir("/test_truncatePermission.txt");
|
2018-09-30 15:06:20 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.kind, Deno.ErrorKind.PermissionDenied);
|
|
|
|
assertEquals(err.name, "PermissionDenied");
|
2018-09-30 15:06:20 -04:00
|
|
|
});
|