Skip to content

Commit

Permalink
new test & prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonkaliski committed Jan 30, 2024
1 parent 1846e20 commit 25a0071
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 50 deletions.
10 changes: 10 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"quoteProps": "as-needed",
"trailingComma": "all",
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"bracketSpacing": true,
"useTabs": false,
"arrowParens": "always"
}
Binary file modified bun.lockb
Binary file not shown.
15 changes: 11 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@
/* auto-generated by NAPI-RS */

export class Pty {
fd: number
pid: number
constructor(command: string, args: Array<string>, envs: Record<string, string>, dir: string, size: [cols: number, rows: number], onExit: (err: null | Error, exitCode: number) => void)
resize(size: [cols: number, rows: number]): void
fd: number;
pid: number;
constructor(
command: string,
args: Array<string>,
envs: Record<string, string>,
dir: string,
size: [cols: number, rows: number],
onExit: (err: null | Error, exitCode: number) => void,
);
resize(size: [cols: number, rows: number]): void;
}
98 changes: 54 additions & 44 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import fs from "fs";
import { Pty } from "./index";
import fs from 'fs';
import { Pty } from './index';

describe("PTY", () => {
describe('PTY', () => {
const CWD = process.cwd();

test("spawns and exits", (done) => {
const message = "hello from a pty";
test('spawns and exits', (done) => {
const message = 'hello from a pty';

const pty = new Pty(
"/bin/echo",
'/bin/echo',
[message],
{},
CWD,
Expand All @@ -20,17 +20,17 @@ describe("PTY", () => {
},
);

const readStream = fs.createReadStream("", { fd: pty.fd });
const readStream = fs.createReadStream('', { fd: pty.fd });

readStream.on("data", (chunk) => {
expect(chunk.toString()).toBe(message + "\r\n");
readStream.on('data', (chunk) => {
expect(chunk.toString()).toBe(message + '\r\n');
});
});

test("captures an exit code", (done) => {
test('captures an exit code', (done) => {
new Pty(
"/bin/sh",
["-c", "exit 17"],
'/bin/sh',
['-c', 'exit 17'],
{},
CWD,
[80, 24],
Expand All @@ -42,70 +42,70 @@ describe("PTY", () => {
);
});

test("can be written to", (done) => {
const message = "hello cat";
test('can be written to', (done) => {
const message = 'hello cat';

const pty = new Pty("/bin/cat", [], {}, CWD, [80, 24], () => {});
const pty = new Pty('/bin/cat', [], {}, CWD, [80, 24], () => {});

const readStream = fs.createReadStream("", { fd: pty.fd });
const writeStream = fs.createWriteStream("", { fd: pty.fd });
const readStream = fs.createReadStream('', { fd: pty.fd });
const writeStream = fs.createWriteStream('', { fd: pty.fd });

readStream.on("data", (chunk) => {
readStream.on('data', (chunk) => {
expect(chunk.toString()).toBe(message);
done();
});

writeStream.write(message);
});

test("can be resized", (done) => {
const pty = new Pty("/bin/sh", [], {}, CWD, [80, 24], () => {});
test('can be resized', (done) => {
const pty = new Pty('/bin/sh', [], {}, CWD, [80, 24], () => {});

const readStream = fs.createReadStream("", { fd: pty.fd });
const writeStream = fs.createWriteStream("", { fd: pty.fd });
const readStream = fs.createReadStream('', { fd: pty.fd });
const writeStream = fs.createWriteStream('', { fd: pty.fd });

let buffer = "";
let buffer = '';

readStream.on("data", (chunk) => {
readStream.on('data', (chunk) => {
buffer += chunk.toString();

if (buffer.includes("done1\r\n")) {
expect(buffer).toContain("24 80");
if (buffer.includes('done1\r\n')) {
expect(buffer).toContain('24 80');
pty.resize([100, 60]);
buffer = "";
buffer = '';
writeStream.write("stty size; echo 'done2'\n");
}

if (buffer.includes("done2\r\n")) {
expect(buffer).toContain("60 100");
if (buffer.includes('done2\r\n')) {
expect(buffer).toContain('60 100');
done();
}
});

writeStream.write("stty size; echo 'done1'\n");
});

test("respects working directory", (done) => {
const pty = new Pty("/bin/pwd", [], {}, CWD, [80, 24], (err, exitCode) => {
test('respects working directory', (done) => {
const pty = new Pty('/bin/pwd', [], {}, CWD, [80, 24], (err, exitCode) => {
expect(err).toBeNull();
expect(exitCode).toBe(0);
done();
});

const readStream = fs.createReadStream("", { fd: pty.fd });
const readStream = fs.createReadStream('', { fd: pty.fd });

readStream.on("data", (chunk) => {
readStream.on('data', (chunk) => {
expect(chunk.toString()).toBe(`${CWD}\r\n`);
});
});

test.skip("respects env", (done) => {
const message = "hello from env";
let buffer = "";
test.skip('respects env', (done) => {
const message = 'hello from env';
let buffer = '';

const pty = new Pty(
"/bin/sh",
["-c", "sleep 0.1s && echo $ENV_VARIABLE && exit"],
'/bin/sh',
['-c', 'sleep 0.1s && echo $ENV_VARIABLE && exit'],
{
ENV_VARIABLE: message,
},
Expand All @@ -114,23 +114,23 @@ describe("PTY", () => {
(err, exitCode) => {
expect(err).toBeNull();
expect(exitCode).toBe(0);
expect(buffer).toBe(message + "\r\n");
expect(buffer).toBe(message + '\r\n');

done();
},
);

const readStream = fs.createReadStream("", { fd: pty.fd });
const readStream = fs.createReadStream('', { fd: pty.fd });

readStream.on("data", (chunk) => {
readStream.on('data', (chunk) => {
buffer += chunk.toString();
});
});

test("works with Bun.read & Bun.write", (done) => {
const message = "hello bun";
test('works with Bun.read & Bun.write', (done) => {
const message = 'hello bun';

const pty = new Pty("/bin/cat", [], {}, CWD, [80, 24], () => {});
const pty = new Pty('/bin/cat', [], {}, CWD, [80, 24], () => {});

const file = Bun.file(pty.fd);

Expand All @@ -147,4 +147,14 @@ describe("PTY", () => {

Bun.write(pty.fd, message);
});

test("doesn't break when executing non-existing binary", (done) => {
try {
new Pty('/bin/this-does-not-exist', [], {}, CWD, [80, 24], () => {});
} catch (e) {
expect(e.message).toContain('No such file or directory');

done();
}
});
});
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"devDependencies": {
"@napi-rs/cli": "^2.17.0",
"@types/node": "^20.4.1",
"@types/jest": "^29.5.11"
"@types/jest": "^29.5.11",
"prettier": "^3.2.4"
},
"scripts": {
"artifacts": "napi artifacts",
Expand All @@ -28,7 +29,8 @@
"test": "bun test",
"universal": "napi universal",
"version": "napi version",
"release": "npm publish --access public"
"release": "npm publish --access public",
"format": "npx prettier *.ts --write"
},
"optionalDependencies": {
"@replit/ruspty-darwin-x64": "1.0.0-alpha.1",
Expand Down

0 comments on commit 25a0071

Please sign in to comment.