Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #1732 Passing -d should disable the http service" #1733

Merged
merged 2 commits into from
Aug 31, 2024
Merged
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
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