Skip to content

Commit

Permalink
add a test that runs multiple ptys
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonkaliski committed Feb 29, 2024
1 parent d920a63 commit 9acf7ba
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
Binary file modified bun.lockb
Binary file not shown.
5 changes: 3 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";

outputs = { self, nixpkgs }:
let
outputs = { self, nixpkgs }:
let
mkDevShell = system:
let
pkgs = nixpkgs.legacyPackages.${system};
Expand All @@ -20,5 +20,6 @@
{
devShells.aarch64-darwin.default = mkDevShell "aarch64-darwin";
devShells.x86_64-darwin.default = mkDevShell "x86_64-darwin";
devShells.x86_64-linux.default = mkDevShell "x86_64-linux";
};
}
61 changes: 32 additions & 29 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('PTY', () => {
const message = 'hello from a pty';

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

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

test('can be started many times', (done) => {
const num = 100;
let exited = Array.from({ length: num }).map(() => false);

for (let i = 0; i < num; i++) {
new Pty(
'sh',
['-c', `exit 0`],
{},
CWD,
{ rows: 24, cols: 80 },
(err, exitCode) => {
exited[i] = true;
expect(err).toBeNull();
expect(exitCode).toBe(0);

if (!exited.some((x) => x === false)) {
done();
}
},
);
}
});

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

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

const readStream = fs.createReadStream('', { fd: pty.fd });
const writeStream = fs.createWriteStream('', { fd: pty.fd });
Expand All @@ -66,14 +83,7 @@ describe('PTY', () => {
});

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

const readStream = fs.createReadStream('', { fd: pty.fd });
const writeStream = fs.createWriteStream('', { fd: pty.fd });
Expand Down Expand Up @@ -101,7 +111,7 @@ describe('PTY', () => {

test('respects working directory', (done) => {
const pty = new Pty(
'/bin/pwd',
'pwd',
[],
{},
CWD,
Expand All @@ -120,12 +130,12 @@ describe('PTY', () => {
});
});

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

const pty = new Pty(
'/bin/sh',
'sh',
['-c', 'sleep 0.1s && echo $ENV_VARIABLE && exit'],
{
ENV_VARIABLE: message,
Expand All @@ -151,14 +161,7 @@ describe('PTY', () => {
test('works with Bun.read & Bun.write', (done) => {
const message = 'hello bun';

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

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

Expand Down

0 comments on commit 9acf7ba

Please sign in to comment.