Skip to content

Commit

Permalink
bytes not strings
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonkaliski committed May 6, 2024
1 parent 46a8b25 commit e76d0a7
Showing 1 changed file with 40 additions and 41 deletions.
81 changes: 40 additions & 41 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs';
import os from 'node:os';
import { readdir, readlink } from 'node:fs/promises';
import { Pty } from './index';
import assert from 'assert';

const EOT = '\x04';
const procSelfFd = '/proc/self/fd/';
Expand Down Expand Up @@ -99,15 +100,17 @@ describe('PTY', () => {
// 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 = '';
let buffer: Buffer | undefined;

const result = Buffer.from([
104, 101, 108, 108, 111, 32, 99, 97, 116, 13, 10, 104, 101, 108, 108, 111,
32, 99, 97, 116, 13, 10, 94, 68, 8, 8,
]);

const pty = new Pty('/bin/cat', [], {}, CWD, { rows: 24, cols: 80 }, () => {
// We have local echo enabled, so we'll read the message twice.
//
// We're using `.toStartWith` instead of `.toBe` here because macOS
// output has additional bytes that linux output does not coming from
// differences in `cat` implementation.
expect(buffer).toStartWith('hello cat\r\nhello cat\r\n');
assert(buffer);
expect(Buffer.compare(buffer, result)).toBe(0);
pty.close();

done();
Expand All @@ -117,7 +120,8 @@ describe('PTY', () => {
const writeStream = fs.createWriteStream('', { fd: pty.fd() });

readStream.on('data', (chunk) => {
buffer += chunk.toString();
assert(Buffer.isBuffer(chunk));
buffer = chunk;
});
readStream.on('error', (err: any) => {
if (err.code && err.code.indexOf('EIO') !== -1) {
Expand Down Expand Up @@ -246,45 +250,40 @@ describe('PTY', () => {
});
});

// TODO: Not sure why this is failing in Darwin.
(os.type() !== 'Darwin' ? test : test.skip)(
'works with Bun.read & Bun.write',
(done) => {
const message = 'hello bun\n';
let buffer = '';
test('works with Bun.read & Bun.write', (done) => {
const message = 'hello bun\n';
let buffer: Uint8Array | undefined;

const pty = new Pty(
'/bin/cat',
[],
{},
CWD,
{ rows: 24, cols: 80 },
() => {
// We have local echo enabled, so we'll read the message twice. Furthermore, the newline
// is converted to `\r\n` in this method.
expect(buffer).toBe('hello bun\r\nhello bun\r\n');
pty.close();

done();
},
);
const result = new Uint8Array([
104, 101, 108, 108, 111, 32, 98, 117, 110, 13, 10, 94, 68, 8, 8, 94, 68,
8, 8, 104, 101, 108, 108, 111, 32, 98, 117, 110, 13, 10,
]);

const file = Bun.file(pty.fd());
const pty = new Pty('/bin/cat', [], {}, CWD, { rows: 24, cols: 80 }, () => {
// We have local echo enabled, so we'll read the message twice. Furthermore, the newline
// is converted to `\r\n` in this method.
assert(buffer !== undefined);
expect(Buffer.compare(buffer, result)).toBe(0);
pty.close();

async function read() {
const stream = file.stream();
for await (const chunk of stream) {
buffer += Buffer.from(chunk).toString();
// TODO: For some reason, Bun's stream will raise the EIO somewhere where we cannot catch
// it, and make the test fail no matter how many try / catch blocks we add.
break;
}
done();
});

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

async function read() {
const stream = file.stream();
for await (const chunk of stream) {
buffer = chunk;
// TODO: For some reason, Bun's stream will raise the EIO somewhere where we cannot catch
// it, and make the test fail no matter how many try / catch blocks we add.
break;
}
}

read();
Bun.write(pty.fd(), message + EOT + EOT);
},
);
read();
Bun.write(pty.fd(), message + EOT + EOT);
});

test("doesn't break when executing non-existing binary", (done) => {
try {
Expand Down

0 comments on commit e76d0a7

Please sign in to comment.