Skip to content

Commit

Permalink
fix: #1732 Passing -d should disable the http service" (#1733)
Browse files Browse the repository at this point in the history
* fix: #1732 Passing -d should disable the http service"

* Fix standards issues
  • Loading branch information
joeyguerra authored Aug 31, 2024
1 parent 49d35e4 commit 3647698
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/OptParse.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions test/Hubot_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
2 changes: 1 addition & 1 deletion test/OptParse-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 3647698

Please sign in to comment.