Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ jobs:
run: npm run build

- name: Run Tests (production)
if: github.event_name != 'pull_request'
# if: github.event_name != 'pull_request'
run: |
echo 'METACALL_AUTH_EMAIL="${{ secrets.METACALL_AUTH_EMAIL }}"' > .env
echo 'METACALL_AUTH_PASSWORD="${{ secrets.METACALL_AUTH_PASSWORD }}"' >> .env
Expand Down
96 changes: 29 additions & 67 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,19 @@
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@types/archiver": "^3.1.1",
"@types/concat-stream": "^2.0.0",
"@types/cross-spawn": "^6.0.2",
"@types/ini": "^1.3.30",
"@types/inquirer": "^8.2.12",
"@types/mocha": "^8.2.2",
"@types/node": "^14.14.7",
"@types/node": "^18.19.130",
"@typescript-eslint/eslint-plugin": "^5.11.0",
"@typescript-eslint/parser": "^5.11.0",
"concat-stream": "^2.0.0",
"cross-spawn": "^7.0.3",
"eslint": "^7.13.0",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-tsdoc": "^0.2.7",
"mocha": "^9.2.0",
"node-pty": "^1.1.0",
"nyc": "^15.1.0",
"prettier": "^2.1.2",
"typescript": "^4.3.2"
Expand Down
91 changes: 28 additions & 63 deletions src/test/cli.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import API from '@metacall/protocol/protocol';
import { fail } from 'assert';
import concat from 'concat-stream';
import spawn from 'cross-spawn';
import * as dotenv from 'dotenv';
import { existsSync } from 'fs';
import fs from 'fs/promises';
import inspector from 'inspector';
import * as pty from 'node-pty';
import os from 'os';
import { join } from 'path';
import { stripVTControlCharacters } from 'util';
import args from '../cli/args';
import { configFilePath } from '../config';
import { startup } from '../startup';
Expand All @@ -19,9 +19,6 @@ dotenv.config();
process.env.NODE_ENV = 'testing';
process.env.METACALL_DEPLOY_INTERACTIVE = 'true';

const PATH = process.env.PATH;
const HOME = process.env.HOME;

export const isInDebugMode = () => inspector.url() !== undefined;

export const run = (
Expand All @@ -36,20 +33,18 @@ export const run = (
// TODO: Implement this properly for better debugging
/* const debugArgs = isInDebugMode() ? ['--inspect-brk=0'] : []; */

const child = spawn('node', [/*...debugArgs,*/ path, ...args], {
env: Object.assign(
{
NODE_ENV: 'test',
PATH,
HOME
},
env
),
stdio: [null, null, null, 'ipc']
const child = pty.spawn('node', [/*...debugArgs,*/ path, ...args], {
name: 'xterm-color',
cols: 80,
rows: 30,
cwd: process.cwd(),
env: {
...process.env,
NODE_ENV: 'test',
...env
}
});

child.stdin?.setDefaultEncoding('utf-8');

return child;
};

Expand All @@ -60,59 +55,29 @@ export const runWithInput = (
env: Record<string, string> = {}
) => {
const child = run(path, args, env);
let childTimeout: NodeJS.Timeout, killTimeout: NodeJS.Timeout;

const loop = (inputs: string[]) => {
if (killTimeout) {
clearTimeout(killTimeout);
}

if (
inputs.length === 0 ||
(inputs.length > 0 && inputs[0] === keys.kill)
) {
child.stdin?.end();

killTimeout = setTimeout(() => {
child.kill(os.constants.signals.SIGTERM);
}, 3000);

return;
}

childTimeout = setTimeout(() => {
child.stdin?.cork();
child.stdin?.write(inputs.shift());
child.stdin?.uncork();
loop(inputs);
}, 3000);
};

return {
promise: new Promise((resolve, reject) => {
child.stderr?.once('data', err => {
child.stdin?.end();
let output = '';

if (childTimeout) {
clearTimeout(childTimeout);
inputs = [];
}
reject(String(err));
child.onData(data => {
output += stripVTControlCharacters(data);
});

child.on('error', reject);

loop(inputs);

child.stdout?.pipe(
concat(result => {
if (killTimeout) {
clearTimeout(killTimeout);
}
child.onExit(({ exitCode }) => {
if (exitCode === 0) {
resolve(output);
} else {
reject({
output,
exitCode
});
}
});

resolve(result.toString());
})
);
for (const input of inputs) {
child.write(input);
}
}),
child
};
Expand Down
Loading