Skip to content
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
13 changes: 13 additions & 0 deletions lib/HelpCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = class {
async todo () {
if (!global.FUZZ) {
console.log(`mr -h | --help # this help message
mr # push current branch to its origin
mr TASK-42 # switch to existing TASK-42 (create if needed)
mr TASK-42 from release # create TASK-42 from release branch
mr TASK-42 to main # merge TASK-42 into main
mr to main # merge current branch into main`)
}
return {todo: []}
}
}
2 changes: 2 additions & 0 deletions lib/MrCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Push = require('./PushCommand')
const Create = require('./CreateCommand')
const Switch = require('./SwitchCommand')
const Merge = require('./MergeCommand')
const Help = require('./HelpCommand')

module.exports = class MrCommand {
static withParsedArgs (parsedArgs) {
Expand All @@ -16,6 +17,7 @@ module.exports = class MrCommand {
Switch,
Create: Switch,
Merge,
Help,
}
let commands = {}
for (let [name, clazz] of Object.entries (name2command)) {
Expand Down
4 changes: 4 additions & 0 deletions lib/ParsedArgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ module.exports = class {
}

value() {
if (this.argv.includes('-h') || this.argv.includes('--help')) {
return {action: 'Help'}
}

const [branch, verb, src_or_dst] = this.argv
const usage = `usage: mr TASK-42 [from|to main]`

Expand Down
2 changes: 1 addition & 1 deletion mr.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const MrCommand = require('./lib/MrCommand')
const RunCommand = require('./lib/RunCommand')

const main = async (argv) => {
// @todo #0:1h add help --version with usage examples
// @todo #0:1h check minimal node version and exit if too old
await new RunCommand (
await MrCommand.withParsedArgs(
new ParsedArgs (
Expand Down
32 changes: 32 additions & 0 deletions tests/HelpCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const {describe, it, mock} = require ('node:test')
const assert = require ('assert')
const MrCommand = require ('../lib/MrCommand')
const ParsedArgs = require ('../lib/ParsedArgs')

describe('HelpCommand', () => {
it ('help command prints help and returns empty todo', async (t) => {
const logCalls = []
mock.method(console, 'log', function(msg) {
logCalls.push(msg)
})

const parsedArgs = new ParsedArgs (['-h'])
const todo = await MrCommand.withParsedArgs (parsedArgs).todo()

assert.deepStrictEqual(todo.todo, [])
assert.ok(logCalls[0].startsWith('mr -h | --help'))
})

it ('--help flag works the same way', async (t) => {
const logCalls = []
mock.method(console, 'log', function(msg) {
logCalls.push(msg)
})

const parsedArgs = new ParsedArgs (['--help'])
const todo = await MrCommand.withParsedArgs (parsedArgs).todo()

assert.deepStrictEqual(todo.todo, [])
assert.ok(logCalls[0].startsWith('mr -h | --help'))
})
})
6 changes: 6 additions & 0 deletions tests/ParsedArgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ describe('ParsedArgs', () => {
it ('deploy current to release', async (t) => {
assert.deepStrictEqual(await new ParsedArgs (toArgv (`to release`)).value (), {src: '__CURRENT_BRANCH__', action: 'Merge', dst: 'release'})
})
it ('help with -h flag', async (t) => {
assert.deepStrictEqual(await new ParsedArgs (['-h']).value (), {action: 'Help'})
})
it ('help with --help flag', async (t) => {
assert.deepStrictEqual(await new ParsedArgs (['--help']).value (), {action: 'Help'})
})
})