2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-01-13 15:51:32 +08:00
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
import { assert, assertEquals, assertThrows, delay } from "./test_util.ts";
|
2020-02-21 13:21:51 -05:00
|
|
|
|
|
|
|
// TODO(ry) Add more tests to specify format.
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test({ permissions: { read: false } }, function watchFsPermissions() {
|
2020-06-25 06:57:08 +08:00
|
|
|
assertThrows(() => {
|
2020-04-24 23:40:29 +02:00
|
|
|
Deno.watchFs(".");
|
2020-06-25 06:57:08 +08:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2020-02-21 13:21:51 -05:00
|
|
|
});
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test({ permissions: { read: true } }, function watchFsInvalidPath() {
|
2020-06-25 06:57:08 +08:00
|
|
|
if (Deno.build.os === "windows") {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2023-06-26 15:10:27 +02:00
|
|
|
Deno.watchFs("non-existent.file");
|
2020-06-25 06:57:08 +08:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"Input watch path is neither a file nor a directory",
|
2020-06-25 06:57:08 +08:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
assertThrows(() => {
|
2023-06-26 15:10:27 +02:00
|
|
|
Deno.watchFs("non-existent.file");
|
2023-11-01 08:54:27 -07:00
|
|
|
}, Deno.errors.NotFound);
|
2020-03-24 20:50:51 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-02-21 13:21:51 -05:00
|
|
|
async function getTwoEvents(
|
2021-06-01 15:35:06 +09:00
|
|
|
iter: Deno.FsWatcher,
|
2020-02-21 13:21:51 -05:00
|
|
|
): Promise<Deno.FsEvent[]> {
|
|
|
|
const events = [];
|
|
|
|
for await (const event of iter) {
|
|
|
|
events.push(event);
|
2023-11-01 08:54:27 -07:00
|
|
|
if (events.length > 2) break;
|
2020-02-21 13:21:51 -05:00
|
|
|
}
|
|
|
|
return events;
|
|
|
|
}
|
|
|
|
|
2021-10-19 12:50:13 +01:00
|
|
|
async function makeTempDir(): Promise<string> {
|
|
|
|
const testDir = await Deno.makeTempDir();
|
|
|
|
// The watcher sometimes witnesses the creation of it's own root
|
|
|
|
// directory. Delay a bit.
|
|
|
|
await delay(100);
|
|
|
|
return testDir;
|
|
|
|
}
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 13:08:58 +02:00
|
|
|
async function watchFsBasic() {
|
2021-10-19 12:50:13 +01:00
|
|
|
const testDir = await makeTempDir();
|
2020-04-24 23:40:29 +02:00
|
|
|
const iter = Deno.watchFs(testDir);
|
2020-02-21 13:21:51 -05:00
|
|
|
|
2023-06-26 15:10:27 +02:00
|
|
|
// Asynchronously capture two fs events.
|
2020-03-04 17:31:14 +01:00
|
|
|
const eventsPromise = getTwoEvents(iter);
|
2020-02-21 13:21:51 -05:00
|
|
|
|
2020-03-04 17:31:14 +01:00
|
|
|
// Make some random file system activity.
|
|
|
|
const file1 = testDir + "/file1.txt";
|
|
|
|
const file2 = testDir + "/file2.txt";
|
|
|
|
Deno.writeFileSync(file1, new Uint8Array([0, 1, 2]));
|
|
|
|
Deno.writeFileSync(file2, new Uint8Array([0, 1, 2]));
|
2020-02-21 13:21:51 -05:00
|
|
|
|
2020-03-04 17:31:14 +01:00
|
|
|
// We should have gotten two fs events.
|
|
|
|
const events = await eventsPromise;
|
|
|
|
assert(events.length >= 2);
|
|
|
|
assert(events[0].kind == "create");
|
|
|
|
assert(events[0].paths[0].includes(testDir));
|
|
|
|
assert(events[1].kind == "create" || events[1].kind == "modify");
|
|
|
|
assert(events[1].paths[0].includes(testDir));
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 17:31:14 +01:00
|
|
|
);
|
2020-07-24 02:33:52 +01:00
|
|
|
|
2021-06-01 15:35:06 +09:00
|
|
|
// TODO(kt3k): This test is for the backward compatibility of `.return` method.
|
|
|
|
// This should be removed at 2.0
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 13:08:58 +02:00
|
|
|
async function watchFsReturn() {
|
2021-10-19 12:50:13 +01:00
|
|
|
const testDir = await makeTempDir();
|
2020-07-24 02:33:52 +01:00
|
|
|
const iter = Deno.watchFs(testDir);
|
|
|
|
|
|
|
|
// Asynchronously loop events.
|
|
|
|
const eventsPromise = getTwoEvents(iter);
|
|
|
|
|
|
|
|
// Close the watcher.
|
|
|
|
await iter.return!();
|
|
|
|
|
|
|
|
// Expect zero events.
|
|
|
|
const events = await eventsPromise;
|
|
|
|
assertEquals(events, []);
|
|
|
|
},
|
|
|
|
);
|
2021-06-01 15:35:06 +09:00
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 13:08:58 +02:00
|
|
|
async function watchFsClose() {
|
2021-10-19 12:50:13 +01:00
|
|
|
const testDir = await makeTempDir();
|
2021-06-01 15:35:06 +09:00
|
|
|
const iter = Deno.watchFs(testDir);
|
|
|
|
|
|
|
|
// Asynchronously loop events.
|
|
|
|
const eventsPromise = getTwoEvents(iter);
|
|
|
|
|
|
|
|
// Close the watcher.
|
2021-10-19 12:50:13 +01:00
|
|
|
iter.close();
|
2021-06-01 15:35:06 +09:00
|
|
|
|
|
|
|
// Expect zero events.
|
|
|
|
const events = await eventsPromise;
|
|
|
|
assertEquals(events, []);
|
|
|
|
},
|
|
|
|
);
|
2023-11-01 20:26:12 +01:00
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, write: true } },
|
|
|
|
async function watchFsExplicitResourceManagement() {
|
|
|
|
let res;
|
|
|
|
{
|
|
|
|
const testDir = await makeTempDir();
|
|
|
|
using iter = Deno.watchFs(testDir);
|
|
|
|
|
|
|
|
res = iter[Symbol.asyncIterator]().next();
|
|
|
|
}
|
|
|
|
|
|
|
|
const { done } = await res;
|
|
|
|
assert(done);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, write: true } },
|
|
|
|
async function watchFsExplicitResourceManagementManualClose() {
|
|
|
|
const testDir = await makeTempDir();
|
|
|
|
using iter = Deno.watchFs(testDir);
|
|
|
|
|
|
|
|
const res = iter[Symbol.asyncIterator]().next();
|
|
|
|
|
|
|
|
iter.close();
|
|
|
|
const { done } = await res;
|
|
|
|
assert(done);
|
|
|
|
},
|
|
|
|
);
|