Skip to content

Commit

Permalink
add macos tests (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonkaliski authored May 7, 2024
1 parent dd3c831 commit 4116c43
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ jobs:
run: ls -R .
shell: bash
- name: Test bindings
run: bun test:darwin
run: npm run test:darwin

test-linux-binding:
name: Test on ${{ matrix.settings.target }}
Expand Down
Binary file modified bun.lockb
Binary file not shown.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
"license": "MIT",
"devDependencies": {
"@napi-rs/cli": "^2.18.2",
"@types/node": "^20.4.1",
"@types/jest": "^29.5.11",
"@types/node": "^20.4.1",
"jest": "^29.7.0",
"prettier": "^3.2.4"
},
"scripts": {
Expand All @@ -35,11 +36,11 @@
"build:debug": "napi build --platform",
"prepublishOnly": "napi prepublish -t npm",
"test:linux": "bun test tests/linux.test.ts",
"test:darwin": "bun test tests/darwin.test.ts",
"test:darwin": "npx jest tests/darwin.test.js",
"universal": "napi universal",
"version": "napi version",
"release": "npm publish --access public",
"format": "npx prettier *.ts --write"
"format": "npx prettier *.ts,*.js --write"
},
"optionalDependencies": {
"@replit/ruspty-darwin-x64": "1.0.0-alpha.1",
Expand Down
214 changes: 214 additions & 0 deletions tests/darwin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
const { Pty } = require('../index');
const fs = require('fs');

const EOT = '\x04';

describe('PTY', () => {
test('spawns and exits', (done) => {
const message = 'hello from a pty';
let buffer = '';

const pty = new Pty({
command: '/bin/echo',
args: [message],
onExit: (err, exitCode) => {
expect(err).toBeNull();
expect(exitCode).toBe(0);
expect(buffer).toBe(message + '\r\n');
pty.close();

done();
},
});

setImmediate(() => {
const fd = pty.fd();
const readStream = fs.createReadStream('', { fd });

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

test('captures an exit code', (done) => {
const pty = new Pty({
command: '/bin/sh',
args: ['-c', 'exit 17'],
onExit: (err, exitCode) => {
expect(err).toBeNull();
expect(exitCode).toBe(17);
pty.close();

done();
},
});
});

test('can be written to', (done) => {
// The message should end in newline so that the EOT can signal that the input has ended and not
// just the line.
const message = 'hello cat\n';
let buffer = '';

// We have local echo enabled, so we'll read the message twice.
// `cat` on darwin also logs `^D`
const result = 'hello cat\r\n^D\b\bhello cat\r\n';

const pty = new Pty({
command: '/bin/cat',
onExit: () => {
expect(buffer).toBe(result);

pty.close();

done();
},
});

setImmediate(() => {
const writeFd = pty.fd();
const writeStream = fs.createWriteStream('', { fd: writeFd });

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

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

writeStream.write(message);
writeStream.end(EOT);
writeStream.on('error', (err) => {
if (err.code && err.code.indexOf('EIO') !== -1) {
return;
}
throw err;
});
});
});

// TODO: fix this test on macOS
test.skip('can be resized', (done) => {
let buffer = '';

const pty = new Pty({
command: '/bin/sh',
size: { rows: 24, cols: 80 },
onExit: () => {
pty.close();

done();
},
});

setImmediate(() => {
const writeFd = pty.fd();
const writeStream = fs.createWriteStream('', { fd: writeFd });

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

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

console.log('buffer');
console.log(buffer);
console.log('-----');

if (buffer.includes('done1\r\n')) {
expect(buffer).toContain('24 80');
pty.resize({ rows: 60, cols: 100 });
buffer = '';

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

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

writeStream.write("stty size; echo 'done1'\n");
writeStream.on('error', (err) => {
if (err.code && err.code.indexOf('EIO') !== -1) {
return;
}
throw err;
});
});
});

// TODO: fix this test on macOS
test.skip('respects working directory', (done) => {
const cwd = process.cwd();
let buffer = '';

const pty = new Pty({
command: '/bin/pwd',
dir: cwd,
onExit: (err, exitCode) => {
expect(err).toBeNull();
expect(exitCode).toBe(0);
expect(buffer).toBe(`${cwd}\r\n`);
pty.close();

done();
},
});

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

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

// TODO: fix this test on macOS
test.skip('respects env', (done) => {
const message = 'hello from env';
let buffer;

const pty = new Pty({
command: '/bin/sh',
args: ['-c', 'echo $ENV_VARIABLE && exit'],
envs: {
ENV_VARIABLE: message,
},
onExit: (err, exitCode) => {
expect(err).toBeNull();
expect(exitCode).toBe(0);
assert(buffer);
expect(Buffer.compare(buffer, Buffer.from(message + '\r\n'))).toBe(0);
pty.close();

done();
},
});

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

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

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

done();
}
});
});
3 changes: 0 additions & 3 deletions tests/darwin.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tests/linux.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ describe('PTY', () => {
const message = 'hello cat\n';
let buffer = '';

// We have local echo enabled, so we'll read the message twice.
const result = 'hello cat\r\nhello cat\r\n';

const pty = new Pty({
command: 'cat',
onExit: () => {
// We have local echo enabled, so we'll read the message twice.
expect(buffer).toBe(result);

pty.close();
Expand Down

0 comments on commit 4116c43

Please sign in to comment.