Skip to content

Commit

Permalink
Merge pull request #5 from vim-denops/stream
Browse files Browse the repository at this point in the history
👍 Use Streams API instead
  • Loading branch information
lambdalisue authored May 14, 2023
2 parents a09b064 + 9c8143c commit 3d98e30
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 41 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,9 @@ jobs:
DENOPS_TEST_VIM_EXECUTABLE: ${{ steps.vim.outputs.executable_path }}
DENOPS_TEST_NVIM_EXECUTABLE: ${{ steps.nvim.outputs.executable_path }}
working-directory: ./repo
- run: |
deno coverage --unstable .coverage --lcov > coverage.lcov
working-directory: ./repo
- uses: codecov/codecov-action@v1
with:
file: ./repo/coverage.lcov
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
deno.lock
.coverage
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![Test](https://github.com/vim-denops/deno-denops-test/actions/workflows/test.yml/badge.svg)](https://github.com/vim-denops/deno-denops-test/actions/workflows/test.yml)
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/denops_test/mod.ts)
[![deno land](http://img.shields.io/badge/available%20on-deno.land/x/denops__test-lightgrey.svg?logo=deno)](https://deno.land/x/denops_test)
[![codecov](https://codecov.io/github/vim-denops/deno-denops-test/branch/main/graph/badge.svg?token=X9O5XB4O1S)](https://codecov.io/github/vim-denops/deno-denops-test)

[Deno][deno] module for testing [denops.vim][denops.vim]. This module is assumed
to be used in unittests of denops plugin.
Expand Down
7 changes: 4 additions & 3 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"lock": false,
"tasks": {
"test": "deno test --no-lock --unstable -A --doc --parallel --shuffle",
"check": "deno check --no-lock --unstable $(find . -name '*.ts')",
"upgrade": "deno run --no-lock -A https://deno.land/x/udd/main.ts $(find . -name '*.ts')"
"test": "deno test --unstable -A --doc --parallel --shuffle --coverage=.coverage",
"check": "deno check --unstable $(find . -name '*.ts')",
"coverage": "deno coverage --unstable .coverage",
"upgrade": "deno run -A https://deno.land/x/udd/main.ts $(find . -name '*.ts')"
}
}
1 change: 1 addition & 0 deletions denops_test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![Test](https://github.com/vim-denops/deno-denops-test/actions/workflows/test.yml/badge.svg)](https://github.com/vim-denops/deno-denops-test/actions/workflows/test.yml)
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/denops_test/mod.ts)
[![deno land](http://img.shields.io/badge/available%20on-deno.land/x/denops__test-lightgrey.svg?logo=deno)](https://deno.land/x/denops_test)
[![codecov](https://codecov.io/github/vim-denops/deno-denops-test/branch/main/graph/badge.svg?token=X9O5XB4O1S)](https://codecov.io/github/vim-denops/deno-denops-test)

[Deno][deno] module for testing [denops.vim][denops.vim]. This module is assumed
to be used in unittests of denops plugin.
Expand Down
2 changes: 1 addition & 1 deletion denops_test/conf.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as path from "https://deno.land/std@0.186.0/path/mod.ts";
import * as path from "https://deno.land/std@0.187.0/path/mod.ts";
import { orElse } from "./or_else.ts";

function get(name: string): string | undefined {
Expand Down
15 changes: 7 additions & 8 deletions denops_test/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { copy } from "https://deno.land/[email protected]/streams/copy.ts";
import {
WorkerReader,
WorkerWriter,
} from "https://deno.land/x/workerio@v1.4.4/mod.ts#^";
readableStreamFromWorker,
writableStreamFromWorker,
} from "https://deno.land/x/workerio@v3.1.0/mod.ts#^";
import { orElse } from "./or_else.ts";

const worker = self as unknown as Worker;
const reader = new WorkerReader(worker);
const writer = new WorkerWriter(worker);
const reader = readableStreamFromWorker(worker);
const writer = writableStreamFromWorker(worker);

const addr = JSON.parse(orElse(Deno.env.get("DENOPS_TEST_ADDRESS"), () => {
throw new Error("Environment variable `DENOPS_TEST_ADDRESS` is required");
}));
const conn = await Deno.connect(addr);

await Promise.race([
copy(conn, writer).finally(() => conn.close()),
copy(reader, conn),
reader.pipeTo(conn.writable),
conn.readable.pipeTo(writer),
]);
2 changes: 1 addition & 1 deletion denops_test/tester.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sample } from "https://deno.land/std@0.186.0/collections/sample.ts";
import { sample } from "https://deno.land/std@0.187.0/collections/sample.ts";
import type { Denops } from "https://deno.land/x/[email protected]/mod.ts";
import type { RunMode } from "./runner.ts";
import { withDenops } from "./with.ts";
Expand Down
2 changes: 1 addition & 1 deletion denops_test/tester_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
assert,
assertEquals,
assertFalse,
} from "https://deno.land/std@0.186.0/testing/asserts.ts";
} from "https://deno.land/std@0.187.0/testing/asserts.ts";
import { test } from "./tester.ts";

test({
Expand Down
66 changes: 40 additions & 26 deletions denops_test/with.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as path from "https://deno.land/[email protected]/path/mod.ts";
import { Session } from "https://deno.land/x/[email protected]/mod.ts#^";
import { using } from "https://deno.land/x/[email protected]/mod.ts#^";
import * as path from "https://deno.land/[email protected]/path/mod.ts";
import {
Client,
Session,
} from "https://deno.land/x/[email protected]/mod.ts#^";
import type {
Denops,
Meta,
Expand Down Expand Up @@ -78,39 +80,37 @@ export async function withDenops(
});
const conn = await listener.accept();
try {
await using(
new Session(conn, conn, {}, {
errorCallback(e) {
if (e.name === "Interrupted") {
return;
}
console.error("Unexpected error occurred", e);
},
}),
async (session) => {
const meta = await session.call(
"call",
"denops#_internal#meta#get",
) as Meta;
const denops = await newDenopsImpl(meta, session, pluginName);
// Workaround for unexpected "leaking async ops"
// https://github.com/denoland/deno/issues/15425#issuecomment-1368245954
await new Promise((resolve) => setTimeout(resolve, 0));
await main(denops);
},
);
const session = new Session(conn.readable, conn.writable);
session.onInvalidMessage = (message) => {
console.error("Unexpected message:", message);
};
session.onMessageError = (error, message) => {
console.error(`Unexpected error occured for message ${message}:`, error);
};
session.start();
const client = new Client(session);
const meta = await client.call(
"call",
"denops#_internal#meta#get",
) as Meta;
const denops = await newDenopsImpl(meta, session, client, pluginName);
// Workaround for unexpected "leaking async ops"
// https://github.com/denoland/deno/issues/15425#issuecomment-1368245954
await new Promise((resolve) => setTimeout(resolve, 0));
await main(denops);
await session.shutdown();
} finally {
proc.stdin.close();
proc.kill();
await proc.status;
conn.close();
listener.close();
}
}

async function newDenopsImpl(
meta: Meta,
session: Session,
client: Client,
pluginName: string,
): Promise<Denops> {
const url = path.toFileUrl(path.join(
Expand All @@ -120,5 +120,19 @@ async function newDenopsImpl(
"impl.ts",
));
const { DenopsImpl } = await import(url.href);
return new DenopsImpl(pluginName, meta, session);
return new DenopsImpl(pluginName, meta, {
get dispatcher() {
return session.dispatcher;
},
set dispatcher(dispatcher) {
session.dispatcher = dispatcher;
},
call(method: string, ...params: unknown[]): Promise<unknown> {
return client.call(method, ...params);
},
notify(method: string, ...params: unknown[]): Promise<void> {
client.notify(method, ...params);
return Promise.resolve();
},
});
}
2 changes: 1 addition & 1 deletion denops_test/with_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
assert,
assertFalse,
} from "https://deno.land/std@0.186.0/testing/asserts.ts";
} from "https://deno.land/std@0.187.0/testing/asserts.ts";
import { withDenops } from "./with.ts";

Deno.test(
Expand Down

0 comments on commit 3d98e30

Please sign in to comment.