From 54f1def217b772fb8e50f73cf831f74406e339ef Mon Sep 17 00:00:00 2001 From: Joey Guerra Date: Sat, 31 Aug 2024 11:44:26 -0500 Subject: [PATCH 1/2] testing --- bin/Hubot.mjs | 12 +++++++++++- test/Hubot_test.mjs | 24 ++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/bin/Hubot.mjs b/bin/Hubot.mjs index 01f9965f1..0cf29662c 100755 --- a/bin/Hubot.mjs +++ b/bin/Hubot.mjs @@ -16,7 +16,8 @@ const switches = [ ['-n', '--name HUBOT_NAME', 'The name of the robot in chat'], ['-r', '--require PATH', 'Alternative scripts path'], ['-t', '--config-check', "Test hubot's config to make sure it won't fail at startup"], - ['-v', '--version', 'Displays the version of hubot installed'] + ['-v', '--version', 'Displays the version of hubot installed'], + ['-e', '--execute', 'Runs the command as if it were a hubot command'], ] const options = { @@ -47,6 +48,7 @@ Parser.on('create', function (opt, value) { }) Parser.on('disable-httpd', opt => { + console.log('disable-httpd', opt) options.enableHttpd = false }) @@ -66,6 +68,10 @@ Parser.on('name', (opt, value) => { options.name = value }) +Parser.on('execute', (opt, value) => { + options.execute = value +}) + Parser.on('require', (opt, value) => { options.scripts.push(value) }) @@ -144,4 +150,8 @@ async function loadExternalScripts () { robot.adapter.once('connected', loadScripts) await robot.run() + + if (options.execute) { + await robot.receive(new Hubot.TextMessage(new Hubot.User('shell', { room: '#shell' }), options.execute)) + } })() diff --git a/test/Hubot_test.mjs b/test/Hubot_test.mjs index 8f711b3a0..fe133d4d3 100644 --- a/test/Hubot_test.mjs +++ b/test/Hubot_test.mjs @@ -12,7 +12,7 @@ const __dirname = path.dirname(__filename) const root = __dirname.replace(/test$/, '') describe('Running bin/Hubot.mjs', () => { - it('should load adapter from HUBOT_FILE environment variable', async () => { + it.skip('should load adapter from HUBOT_FILE environment variable', async () => { process.env.HUBOT_HTTPD = 'false' process.env.HUBOT_FILE = path.resolve(root, 'test', 'fixtures', 'MockAdapter.mjs') const hubot = (await import('../bin/Hubot.mjs')).default @@ -35,7 +35,7 @@ describe('Running bin/Hubot.mjs', () => { } }) - it('should output a help message when run with --help', (t, done) => { + it.skip('should output a help message when run with --help', (t, done) => { const hubot = process.platform === 'win32' ? spawn('node', ['./bin/Hubot.mjs', '--help']) : spawn('./bin/hubot', ['--help']) const expected = `Usage: hubot [options] -a, --adapter HUBOT_ADAPTER @@ -61,4 +61,24 @@ describe('Running bin/Hubot.mjs', () => { done() }) }) + it('should execute the command when run with --execute or -e', (t, done) => { + let expected = `HELO World! I'm Hubot.` + let commandText = 'helo' + let env = Object.assign({}, process.env, { HUBOT_LOG_LEVEL: 'error' }); + let hubot = process.platform === 'win32' + ? spawn('node', ['./bin/Hubot.mjs', '-d', '--execute', commandText, '-r', 'test/scripts/Xample.mjs'], { env }) + : spawn('./bin/hubot', ['-d', '--execute', commandText, '-r', 'test/scripts/Xample.mjs'], { env }) + let actual = '' + hubot.stdout.on('data', (data) => { + actual += data.toString() + console.log(actual) + }) + hubot.stderr.on('data', (data) => { + actual += data.toString() + }) + hubot.on('close', (code) => { + assert.deepEqual(actual, expected) + done() + }) + }) }) From f239af1086adb579d42e2f66e75d297272c4b241 Mon Sep 17 00:00:00 2001 From: Joey Guerra Date: Sat, 31 Aug 2024 13:52:10 -0500 Subject: [PATCH 2/2] feat: Execute commands in scripts on the CLI via hubot -e --- bin/Hubot.mjs | 16 ++++++++-------- test/Hubot_test.mjs | 20 ++++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/bin/Hubot.mjs b/bin/Hubot.mjs index 0cf29662c..5032cbf46 100755 --- a/bin/Hubot.mjs +++ b/bin/Hubot.mjs @@ -17,7 +17,7 @@ const switches = [ ['-r', '--require PATH', 'Alternative scripts path'], ['-t', '--config-check', "Test hubot's config to make sure it won't fail at startup"], ['-v', '--version', 'Displays the version of hubot installed'], - ['-e', '--execute', 'Runs the command as if it were a hubot command'], + ['-e', '--execute', 'Runs the command as if it were a hubot command'] ] const options = { @@ -48,7 +48,6 @@ Parser.on('create', function (opt, value) { }) Parser.on('disable-httpd', opt => { - console.log('disable-httpd', opt) options.enableHttpd = false }) @@ -147,11 +146,12 @@ async function loadExternalScripts () { process.exit(0) } - robot.adapter.once('connected', loadScripts) - + robot.adapter.once('connected', async () => { + await loadScripts() + if (options.execute) { + await robot.receive(new Hubot.TextMessage(new Hubot.User('shell', { room: '#shell' }), `@${robot.name} ${options.execute.trim()}`)) + robot.shutdown() + } + }) await robot.run() - - if (options.execute) { - await robot.receive(new Hubot.TextMessage(new Hubot.User('shell', { room: '#shell' }), options.execute)) - } })() diff --git a/test/Hubot_test.mjs b/test/Hubot_test.mjs index edea02860..c476cec7c 100644 --- a/test/Hubot_test.mjs +++ b/test/Hubot_test.mjs @@ -12,7 +12,7 @@ const __dirname = path.dirname(__filename) const root = __dirname.replace(/test$/, '') describe('Running bin/Hubot.mjs', () => { - it.skip('should load adapter from HUBOT_FILE environment variable', async () => { + it('should load adapter from HUBOT_FILE environment variable', async () => { process.env.HUBOT_HTTPD = 'false' process.env.HUBOT_FILE = path.resolve(root, 'test', 'fixtures', 'MockAdapter.mjs') const hubot = (await import('../bin/Hubot.mjs')).default @@ -35,7 +35,7 @@ describe('Running bin/Hubot.mjs', () => { } }) - it.skip('should output a help message when run with --help', (t, done) => { + it('should output a help message when run with --help', (t, done) => { const hubot = process.platform === 'win32' ? spawn('node', ['./bin/Hubot.mjs', '--help']) : spawn('./bin/hubot', ['--help']) const expected = `Usage: hubot [options] -a, --adapter HUBOT_ADAPTER @@ -48,6 +48,7 @@ describe('Running bin/Hubot.mjs', () => { -r, --require PATH -t, --config-check -v, --version + -e, --execute ` let actual = '' hubot.stdout.on('data', (data) => { @@ -62,22 +63,21 @@ describe('Running bin/Hubot.mjs', () => { }) }) it('should execute the command when run with --execute or -e', (t, done) => { - let expected = `HELO World! I'm Hubot.` - let commandText = 'helo' - let env = Object.assign({}, process.env, { HUBOT_LOG_LEVEL: 'error' }); - let hubot = process.platform === 'win32' - ? spawn('node', ['./bin/Hubot.mjs', '-d', '--execute', commandText, '-r', 'test/scripts/Xample.mjs'], { env }) - : spawn('./bin/hubot', ['-d', '--execute', commandText, '-r', 'test/scripts/Xample.mjs'], { env }) + const expected = "HELO World! I'm Hubot." + const commandText = 'helo' + const env = Object.assign({}, process.env, { NOLOG: 'off' }) + const hubot = process.platform === 'win32' + ? spawn('node', ['./bin/Hubot.mjs', '-d', '--execute', commandText, '-r', 'test/scripts'], { env }) + : spawn('./bin/hubot', ['-d', '--execute', commandText, '-r', 'test/scripts'], { env }) let actual = '' hubot.stdout.on('data', (data) => { actual += data.toString() - console.log(actual) }) hubot.stderr.on('data', (data) => { actual += data.toString() }) hubot.on('close', (code) => { - assert.deepEqual(actual, expected) + assert.ok(actual.includes(expected)) done() }) })