mirror of
https://github.com/denoland/deno.git
synced 2024-12-12 18:42:18 -05:00
f5e46c9bf2
This looks like a massive PR, but it's only a move from cli/tests -> tests, and updates of relative paths for files. This is the first step towards aggregate all of the integration test files under tests/, which will lead to a set of integration tests that can run without the CLI binary being built. While we could leave these tests under `cli`, it would require us to keep a more complex directory structure for the various test runners. In addition, we have a lot of complexity to ignore various test files in the `cli` project itself (cargo publish exclusion rules, autotests = false, etc). And finally, the `tests/` folder will eventually house the `test_ffi`, `test_napi` and other testing code, reducing the size of the root repo directory. For easier review, the extremely large and noisy "move" is in the first commit (with no changes -- just a move), while the remainder of the changes to actual files is in the second commit.
218 lines
7 KiB
TypeScript
218 lines
7 KiB
TypeScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import {
|
|
assert,
|
|
assertEquals,
|
|
assertRejects,
|
|
assertThrows,
|
|
} from "./test_util.ts";
|
|
|
|
Deno.test(
|
|
{ permissions: { read: true, write: true } },
|
|
function writeTextFileSyncSuccess() {
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
Deno.writeTextFileSync(filename, "Hello");
|
|
const dataRead = Deno.readTextFileSync(filename);
|
|
assertEquals(dataRead, "Hello");
|
|
},
|
|
);
|
|
|
|
Deno.test(
|
|
{ permissions: { read: true, write: true } },
|
|
function writeTextFileSyncByUrl() {
|
|
const tempDir = Deno.makeTempDirSync();
|
|
const fileUrl = new URL(
|
|
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/test.txt`,
|
|
);
|
|
Deno.writeTextFileSync(fileUrl, "Hello");
|
|
const dataRead = Deno.readTextFileSync(fileUrl);
|
|
assertEquals(dataRead, "Hello");
|
|
|
|
Deno.removeSync(fileUrl, { recursive: true });
|
|
},
|
|
);
|
|
|
|
Deno.test({ permissions: { write: true } }, function writeTextFileSyncFail() {
|
|
const filename = "/baddir/test.txt";
|
|
// The following should fail because /baddir doesn't exist (hopefully).
|
|
assertThrows(() => {
|
|
Deno.writeTextFileSync(filename, "hello");
|
|
}, Deno.errors.NotFound);
|
|
});
|
|
|
|
Deno.test({ permissions: { write: false } }, function writeTextFileSyncPerm() {
|
|
const filename = "/baddir/test.txt";
|
|
// The following should fail due to no write permission
|
|
assertThrows(() => {
|
|
Deno.writeTextFileSync(filename, "Hello");
|
|
}, Deno.errors.PermissionDenied);
|
|
});
|
|
|
|
Deno.test(
|
|
{ permissions: { read: true, write: true } },
|
|
function writeTextFileSyncUpdateMode() {
|
|
if (Deno.build.os !== "windows") {
|
|
const data = "Hello";
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
Deno.writeTextFileSync(filename, data, { mode: 0o755 });
|
|
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o755);
|
|
Deno.writeTextFileSync(filename, data, { mode: 0o666 });
|
|
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o666);
|
|
}
|
|
},
|
|
);
|
|
|
|
Deno.test(
|
|
{ permissions: { read: true, write: true } },
|
|
function writeTextFileSyncCreate() {
|
|
const data = "Hello";
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
let caughtError = false;
|
|
// if create turned off, the file won't be created
|
|
try {
|
|
Deno.writeTextFileSync(filename, data, { create: false });
|
|
} catch (e) {
|
|
caughtError = true;
|
|
assert(e instanceof Deno.errors.NotFound);
|
|
}
|
|
assert(caughtError);
|
|
|
|
// Turn on create, should have no error
|
|
Deno.writeTextFileSync(filename, data, { create: true });
|
|
Deno.writeTextFileSync(filename, data, { create: false });
|
|
assertEquals(Deno.readTextFileSync(filename), "Hello");
|
|
},
|
|
);
|
|
|
|
Deno.test(
|
|
{ permissions: { read: true, write: true } },
|
|
function writeTextFileSyncAppend() {
|
|
const data = "Hello";
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
Deno.writeTextFileSync(filename, data);
|
|
Deno.writeTextFileSync(filename, data, { append: true });
|
|
assertEquals(Deno.readTextFileSync(filename), "HelloHello");
|
|
// Now attempt overwrite
|
|
Deno.writeTextFileSync(filename, data, { append: false });
|
|
assertEquals(Deno.readTextFileSync(filename), "Hello");
|
|
// append not set should also overwrite
|
|
Deno.writeTextFileSync(filename, data);
|
|
assertEquals(Deno.readTextFileSync(filename), "Hello");
|
|
},
|
|
);
|
|
|
|
Deno.test(
|
|
{ permissions: { read: true, write: true } },
|
|
async function writeTextFileSuccess() {
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
await Deno.writeTextFile(filename, "Hello");
|
|
const dataRead = Deno.readTextFileSync(filename);
|
|
assertEquals(dataRead, "Hello");
|
|
},
|
|
);
|
|
|
|
Deno.test(
|
|
{ permissions: { read: true, write: true } },
|
|
async function writeTextFileByUrl() {
|
|
const tempDir = Deno.makeTempDirSync();
|
|
const fileUrl = new URL(
|
|
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/test.txt`,
|
|
);
|
|
await Deno.writeTextFile(fileUrl, "Hello");
|
|
const dataRead = Deno.readTextFileSync(fileUrl);
|
|
assertEquals(dataRead, "Hello");
|
|
|
|
Deno.removeSync(fileUrl, { recursive: true });
|
|
},
|
|
);
|
|
|
|
Deno.test(
|
|
{ permissions: { read: true, write: true } },
|
|
async function writeTextFileNotFound() {
|
|
const filename = "/baddir/test.txt";
|
|
// The following should fail because /baddir doesn't exist (hopefully).
|
|
await assertRejects(async () => {
|
|
await Deno.writeTextFile(filename, "Hello");
|
|
}, Deno.errors.NotFound);
|
|
},
|
|
);
|
|
|
|
Deno.test(
|
|
{ permissions: { write: false } },
|
|
async function writeTextFilePerm() {
|
|
const filename = "/baddir/test.txt";
|
|
// The following should fail due to no write permission
|
|
await assertRejects(async () => {
|
|
await Deno.writeTextFile(filename, "Hello");
|
|
}, Deno.errors.PermissionDenied);
|
|
},
|
|
);
|
|
|
|
Deno.test(
|
|
{ permissions: { read: true, write: true } },
|
|
async function writeTextFileUpdateMode() {
|
|
if (Deno.build.os !== "windows") {
|
|
const data = "Hello";
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
await Deno.writeTextFile(filename, data, { mode: 0o755 });
|
|
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o755);
|
|
await Deno.writeTextFile(filename, data, { mode: 0o666 });
|
|
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o666);
|
|
}
|
|
},
|
|
);
|
|
|
|
Deno.test(
|
|
{ permissions: { read: true, write: true } },
|
|
async function writeTextFileCreate() {
|
|
const data = "Hello";
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
let caughtError = false;
|
|
// if create turned off, the file won't be created
|
|
try {
|
|
await Deno.writeTextFile(filename, data, { create: false });
|
|
} catch (e) {
|
|
caughtError = true;
|
|
assert(e instanceof Deno.errors.NotFound);
|
|
}
|
|
assert(caughtError);
|
|
|
|
// Turn on create, should have no error
|
|
await Deno.writeTextFile(filename, data, { create: true });
|
|
await Deno.writeTextFile(filename, data, { create: false });
|
|
assertEquals(Deno.readTextFileSync(filename), "Hello");
|
|
},
|
|
);
|
|
|
|
Deno.test(
|
|
{ permissions: { read: true, write: true } },
|
|
async function writeTextFileAppend() {
|
|
const data = "Hello";
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
await Deno.writeTextFile(filename, data);
|
|
await Deno.writeTextFile(filename, data, { append: true });
|
|
assertEquals(Deno.readTextFileSync(filename), "HelloHello");
|
|
// Now attempt overwrite
|
|
await Deno.writeTextFile(filename, data, { append: false });
|
|
assertEquals(Deno.readTextFileSync(filename), "Hello");
|
|
// append not set should also overwrite
|
|
await Deno.writeTextFile(filename, data);
|
|
assertEquals(Deno.readTextFileSync(filename), "Hello");
|
|
},
|
|
);
|
|
|
|
Deno.test(
|
|
{ permissions: { read: true, write: true } },
|
|
async function writeTextFileStream() {
|
|
const stream = new ReadableStream({
|
|
pull(controller) {
|
|
controller.enqueue("Hello");
|
|
controller.enqueue("World");
|
|
controller.close();
|
|
},
|
|
});
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
await Deno.writeTextFile(filename, stream);
|
|
assertEquals(Deno.readTextFileSync(filename), "HelloWorld");
|
|
},
|
|
);
|