From 3647698620e8fcf8ba4d52f03ed9ef47f8ca1f72 Mon Sep 17 00:00:00 2001 From: Joey Guerra Date: Sat, 31 Aug 2024 13:17:56 -0500 Subject: [PATCH] fix: #1732 Passing -d should disable the http service" (#1733) * fix: #1732 Passing -d should disable the http service" * Fix standards issues --- src/OptParse.mjs | 16 ++++++++++------ test/Hubot_test.mjs | 37 +++++++++++++++++++++++++++++++++++++ test/OptParse-test.mjs | 2 +- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/OptParse.mjs b/src/OptParse.mjs index 5c940a34e..e80ded46a 100644 --- a/src/OptParse.mjs +++ b/src/OptParse.mjs @@ -19,17 +19,21 @@ class OptParse extends EventEmitter { for (let i = 0; i < args.length; i++) { const arg = args[i] if (arg.startsWith('-')) { - let key = arg.replace(/^-+/, '') - key = mappings[key] || key - key = key.replace(/-([a-z])/g, g => g[1].toUpperCase()) + const cliArg = arg.replace(/^-+/, '') + let propertyName = mappings[cliArg] + if (!propertyName) { + propertyName = Object.values(mappings).find(value => value === cliArg) + } + const nameToEmit = propertyName + propertyName = propertyName.replace(/-([a-z])/g, g => g[1].toUpperCase()) const nextArg = args[i + 1] if (nextArg && !nextArg.startsWith('-')) { - options[key] = nextArg + options[propertyName] = nextArg i++ } else { - options[key] = true + options[propertyName] = true } - this.emit(key, key, nextArg) + this.emit(nameToEmit, propertyName, nextArg) } } return options diff --git a/test/Hubot_test.mjs b/test/Hubot_test.mjs index 8f711b3a0..cbb6881fe 100644 --- a/test/Hubot_test.mjs +++ b/test/Hubot_test.mjs @@ -62,3 +62,40 @@ describe('Running bin/Hubot.mjs', () => { }) }) }) + +describe('Running hubot with args', () => { + it('should not start web service when --disable-httpd is passed', (t, done) => { + const hubot = process.platform === 'win32' ? spawn('node', ['./bin/Hubot.mjs', '--disable-httpd']) : spawn('./bin/hubot', ['--disable-httpd']) + let actual = {} + const logMessages = [] + hubot.stdout.on('data', (data) => { + console.log(data.toString()) + logMessages.push(data.toString()) + }) + hubot.stderr.on('data', (data) => { + console.log(data.toString()) + logMessages.push(data.toString()) + }) + const interval = setInterval(async () => { + if (logMessages.some(m => m.includes('EADDRINUSE'))) { + clearInterval(interval) + assert.fail('Web service started when --disable-httpd was passed') + done() + } + if (logMessages.some(m => m.includes('No external-scripts.json found. Skipping'))) { + clearInterval(interval) + try { + const response = await fetch('http://localhost:8080') + actual = await response.text() + } catch (e) { + actual = e + } finally { + hubot.kill() + } + assert.ok(actual instanceof TypeError) + assert.deepEqual(actual.message, 'fetch failed') + done() + } + }, 60) + }) +}) diff --git a/test/OptParse-test.mjs b/test/OptParse-test.mjs index d7d1538e5..56c098d42 100644 --- a/test/OptParse-test.mjs +++ b/test/OptParse-test.mjs @@ -32,7 +32,7 @@ describe('CLI Argument Parsing', () => { Parser.on('adapter', (opt, value) => { options.adapter = value }) - Parser.on('disableHttpd', (opt, value) => { + Parser.on('disable-httpd', (opt, value) => { options.enableHttpd = false }) Parser.on('alias', (opt, value) => {