Skip to content

Commit

Permalink
create inspect command
Browse files Browse the repository at this point in the history
Registered inspect comman in eoc.js
Made inspect.js command
Made tests
  • Loading branch information
ErnestMatskevich committed Jan 26, 2025
1 parent 5c96cdf commit 3775faf
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 1 deletion.
67 changes: 67 additions & 0 deletions src/commands/inspect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2022-2024 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

const readline = require('readline');

/**
* Command to open inspect mode.
* @param {Hash} opts - All options
*/
module.exports = function(opts) {
console.info('open inspect mode');

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'inspect> '
});

const exitInspectMode = () => {
rl.close();
process.exit(0);
};

const handleInput = (input) => {
if (input.trim().toLowerCase() === 'exit') {
console.info('\nExiting inspect mode...');
exitInspectMode();
} else {
console.info('Sorry, this command is under development 🚧');
rl.prompt();
}
};

const handleSigint = () => {
exitInspectMode();
};

process.on('SIGINT', handleSigint);

rl.prompt();
rl.on('line', handleInput);

rl.on('close', () => {
process.off('SIGINT', handleSigint);
});
};
9 changes: 8 additions & 1 deletion src/eoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ const common = {
lint: require('./commands/lint'),
docs: require('./commands/docs'),
jeo_disassemble: require('./commands/jeo/disassemble'),
jeo_assemble: require('./commands/jeo/assemble')
jeo_assemble: require('./commands/jeo/assemble'),
inspect: require('./commands/inspect')
};
const commands = {
[language.java]: {
Expand Down Expand Up @@ -395,6 +396,12 @@ program.command('jeo:assemble')
coms().jeo_assemble({...program.opts(), ...str});
});

program.command('inspect')
.description('Open the inspect mode. Use this mode to interactively debug EO programs.')
.action((str, opts) => {
coms().inspect(program.opts());
});

try {
program.parse(process.argv);
} catch (e) {
Expand Down
56 changes: 56 additions & 0 deletions test/commands/test_inspect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const {spawn} = require('child_process');
const assert = require('assert');

describe('inspect', function() {
it('should handle the "exit" command', function(done) {
const process = spawn('node', ['src/eoc.js', 'inspect']);

let stdout = '';
process.stdout.on('data', (data) => {
stdout += data.toString();

if (stdout.includes('inspect>')) {
process.stdin.write('exit\n');
}

if (stdout.includes('Exiting inspect mode...')) {
assert(
stdout.includes('Exiting inspect mode...'),
`Expected "Exiting inspect mode..." but got: ${stdout}`
);
process.kill();
done();
}
});

process.on('error', (err) => {
done(err);
});
});

it('should handle unknown commands', function(done) {
const process = spawn('node', ['src/eoc.js', 'inspect']);

let stdout = '';
process.stdout.on('data', (data) => {
stdout += data.toString();

if (stdout.includes('inspect>')) {
process.stdin.write('unknown\n');
}

if (stdout.includes('Sorry, this command is under development 🚧')) {
assert(
stdout.includes('Sorry, this command is under development 🚧'),
`Expected "Sorry, this command is under development 🚧" but got: ${stdout}`
);
process.kill();
done();
}
});

process.on('error', (err) => {
done(err);
});
});
});

0 comments on commit 3775faf

Please sign in to comment.