forked from wallpants/bunvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dlants/tests
Tests
- Loading branch information
Showing
7 changed files
with
117 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { describe, expect, it } from "bun:test"; | ||
import { withNvimClient } from "../tests/preamble"; | ||
|
||
describe("src/attach.test.ts", () => { | ||
it("nvim_buf_set_lines with a large file", async () => { | ||
await withNvimClient(async (nvim) => { | ||
const lines: string[] = []; | ||
for (let line = 0; line < 500; line += 1) { | ||
lines.push("x".repeat(100)); | ||
} | ||
await nvim.call("nvim_buf_set_lines", [0, 0, -1, false, lines]); | ||
const roundtripLines = await nvim.call("nvim_buf_get_lines", [0, 0, -1, false]); | ||
expect(roundtripLines).toEqual(lines); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { attach, type Nvim } from "../src/index.ts"; | ||
import { unlink } from "node:fs/promises"; | ||
import { spawn } from "child_process"; | ||
import path from "path"; | ||
|
||
const SOCK = `/tmp/bunvim-test.sock`; | ||
export async function withNvimProcess(fn: (sock: string) => Promise<void>) { | ||
try { | ||
await unlink(SOCK); | ||
} catch (e) { | ||
if ((e as { code: string }).code !== "ENOENT") { | ||
console.error(e); | ||
} | ||
} | ||
|
||
const nvimProcess = spawn( | ||
"nvim", | ||
["--headless", "-n", "--clean", "--listen", SOCK], | ||
{ | ||
// root dir relative to this file | ||
cwd: path.resolve(path.dirname(__filename), "../"), | ||
}, | ||
); | ||
|
||
if (!nvimProcess.pid) { | ||
throw new Error("Failed to start nvim process"); | ||
} | ||
|
||
try { | ||
nvimProcess.on("error", (err) => { | ||
throw err; | ||
}); | ||
|
||
nvimProcess.on("exit", (code, signal) => { | ||
if (code !== 1) { | ||
throw new Error( | ||
`Nvim process exited with code ${code} and signal ${signal}`, | ||
); | ||
} | ||
}); | ||
|
||
// give enough time for socket to be created | ||
// TODO: poll for socket instead | ||
await new Promise((resolve) => setTimeout(resolve, 500)); | ||
|
||
await fn(SOCK); | ||
} finally { | ||
const res = nvimProcess.kill(); | ||
console.log(`Killed process ${nvimProcess.pid} with result ${res}`); | ||
} | ||
} | ||
|
||
export async function withNvimClient(fn: (nvim: Nvim) => Promise<void>) { | ||
return await withNvimProcess(async (sock) => { | ||
const nvim = await attach({ | ||
socket: sock, | ||
client: { name: "test" }, | ||
logging: { level: "debug" }, | ||
}); | ||
|
||
try { | ||
await fn(nvim); | ||
} finally { | ||
nvim.detach(); | ||
} | ||
}); | ||
} | ||
|
||
process.on("uncaughtException", (err) => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters