Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed May 4, 2021
1 parent 0390fa0 commit cfce188
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 23 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,18 @@ For more information on Regular Expressions, see [this useful guide](https://dev

## Install

First, ensure [Node.js](https://nodejs.org/en/) v14 or above is installed.

To install renamer globally as a part of your regular command-line tool kit:

```
$ npm install --global renamer
```

To install renamer as a development dependency of your project:

```
$ npm install -g renamer
$ npm install --save-dev renamer
```

* * *
Expand Down
4 changes: 3 additions & 1 deletion example/make-sandbox.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const someFiles = [
'sandbox/data2 (checked by Lloyd).csv',
'sandbox/data3.xls',
'sandbox/dates/20180716_180000.jpg',
'sandbox/dates/20180717_210000.jpg'
'sandbox/dates/20180717_210000.jpg',
'sandbox/alphanumeric/10002ASmith.pdf',
'sandbox/alphanumeric/10230CJones.pdf'
]
someFiles.forEach(createFixture)
25 changes: 8 additions & 17 deletions lib/cli-app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CliApp {
const { cliArgs, allOptionDefinitions } = await this.getPluginArgs(optionDefinitions, startOptions.argv)

/* set view */
const view = cliArgs.view || this.view
let view = cliArgs.view || this.view
if (view) {
if (view === 'long') {
this.view = new LongView()
Expand All @@ -42,16 +42,15 @@ class CliApp {
this.view = new OneLineView()
} else {
if (t.isString(view)) {
// TODO: load plugin
const ViewClass = await loadModule(view, { paths: process.cwd() })
this.view = new ViewClass()
} else {
view = new ViewClass()
}
if (view.write && typeof view.write === 'function') {
this.view = view
} else {
throw new Error('View must define a `write` method')
}
}
if (!(this.view.write && typeof this.view.write === 'function')) {
throw new Error('View must define a `write` method')
}
}

this.options = cliArgs
Expand Down Expand Up @@ -115,19 +114,11 @@ class CliApp {

async printUsage (allOptionDefinitions = [], plugins = []) {
const sections = await usageSections(allOptionDefinitions, plugins)
if (this.view.write && typeof this.view.write === 'function') {
this.view.write('usage', commandLineUsage(sections), this.options)
} else {
console.error(commandLineUsage(sections))
}
this.view.write('usage', commandLineUsage(sections), this.options)
}

halt (err) {
if (this.view.write && typeof this.view.write === 'function') {
this.view.write('error', err, this.options)
} else {
console.error(chalk.red(err.stack))
}
this.view.write('error', err, this.options)
process.exitCode = 1
}

Expand Down
4 changes: 2 additions & 2 deletions lib/view/one-line.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from 'chalk'
import DefaultView from './default.mjs'

class OneLine extends DefaultView {
class OneLineView extends DefaultView {
logComplete (stats, options) {
const dryRun = options.dryRun ? chalk.bold.underline('Dry run.') + ' ' : ''
this.log(`${dryRun}Rename complete: ${stats.renamed} of ${stats.total} files renamed.`)
Expand All @@ -20,4 +20,4 @@ class OneLine extends DefaultView {
}
}

export default OneLine
export default OneLineView
34 changes: 34 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"c8": "^7.7.1",
"coveralls": "^3.1.0",
"mkdirp2": "^1.0.4",
"renamer-case": "^2.0.1",
"rimraf": "^3.0.2",
"test-runner": "^0.9.2"
}
Expand Down
26 changes: 26 additions & 0 deletions test/cli-bad-input.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ const tom = new TestRunner.Tom()
const testRoot = `tmp/${path.basename(import.meta.url)}`
rimraf.sync(testRoot)

class TestView extends DefaultView {
log (...args) {
this.logs = this.logs || []
this.logs.push(args)
}

logError (err) { // eslint-disable-line
super.logError(err)
/* Uncomment to print handled errors */
// console.error('ERROR', err)
}
}

tom.test('invalid option: exit code set to 1, usage guide displayed, no file renamed', async function () {
const fixturePath = createFixture(`${testRoot}/${this.index}/one`)
const origCode = process.exitCode
Expand All @@ -33,4 +46,17 @@ tom.test('invalid option: exit code set to 1, usage guide displayed, no file ren
a.equal(/For detailed instructions/.test(logs[1]), true)
})

tom.test('--view: broken plugin', async function () {
const origCode = process.exitCode
const fixturePath = createFixture(`${testRoot}/${this.index}/one`)
const view = new TestView()
const cliApp = new CliApp({ view })
a.deepEqual(fs.existsSync(fixturePath), true)
await cliApp.start({ argv: ['-s', '--find', 'one', '--replace', 'yeah', fixturePath, '--view', './test/lib/broken-view.mjs'] }),
a.equal(cliApp.view.constructor.name, 'TestView')
a.ok(/View must define a `write` method/.test(view.logs[0][0]))
a.equal(process.exitCode, 1)
process.exitCode = origCode
})

export default tom
44 changes: 42 additions & 2 deletions test/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ tom.test('failed replace', async function () {
process.exitCode = 0
})

tom.test('simple, diff view', async function () {
tom.test('simple, pass custom view to constructor', async function () {
class TestDiffView extends DiffView {
log (...args) {
this.logs = this.logs || []
Expand All @@ -177,11 +177,42 @@ tom.test('simple, diff view', async function () {
a.deepEqual(fs.existsSync(`${testRoot}/${this.index}/yeah`), true)
})

tom.test('simple, long view', async function () {
tom.test('--view long', async function () {
const fixturePath = createFixture(`${testRoot}/${this.index}/one`)
const cliApp = new CliApp()
a.deepEqual(fs.existsSync(fixturePath), true)
await cliApp.start({ argv: ['-s', '--find', 'one', '--replace', 'yeah', fixturePath, '--view', 'long'] })
a.equal(cliApp.view.constructor.name, 'LongView')
a.deepEqual(fs.existsSync(fixturePath), false)
a.deepEqual(fs.existsSync(`${testRoot}/${this.index}/yeah`), true)
})

tom.test('--view diff', async function () {
const fixturePath = createFixture(`${testRoot}/${this.index}/one`)
const cliApp = new CliApp()
a.deepEqual(fs.existsSync(fixturePath), true)
await cliApp.start({ argv: ['-s', '--find', 'one', '--replace', 'yeah', fixturePath, '--view', 'diff'] })
a.equal(cliApp.view.constructor.name, 'DiffView')
a.deepEqual(fs.existsSync(fixturePath), false)
a.deepEqual(fs.existsSync(`${testRoot}/${this.index}/yeah`), true)
})

tom.test('--view one-line', async function () {
const fixturePath = createFixture(`${testRoot}/${this.index}/one`)
const cliApp = new CliApp()
a.deepEqual(fs.existsSync(fixturePath), true)
await cliApp.start({ argv: ['-s', '--find', 'one', '--replace', 'yeah', fixturePath, '--view', 'one-line'] })
a.equal(cliApp.view.constructor.name, 'OneLineView')
a.deepEqual(fs.existsSync(fixturePath), false)
a.deepEqual(fs.existsSync(`${testRoot}/${this.index}/yeah`), true)
})

tom.test('--view: load plugin', async function () {
const fixturePath = createFixture(`${testRoot}/${this.index}/one`)
const cliApp = new CliApp()
a.deepEqual(fs.existsSync(fixturePath), true)
await cliApp.start({ argv: ['-s', '--find', 'one', '--replace', 'yeah', fixturePath, '--view', './test/lib/dummy-view.mjs'] })
a.equal(cliApp.view.constructor.name, 'DummyView')
a.deepEqual(fs.existsSync(fixturePath), false)
a.deepEqual(fs.existsSync(`${testRoot}/${this.index}/yeah`), true)
})
Expand Down Expand Up @@ -213,6 +244,15 @@ tom.test('--chain built-in local --help', async function () {
a.ok(/DummyPlugin/.test(cliApp.view.logs[0][0]))
})

tom.test('--chain: local plugin package', async function () {
const fixturePath = createFixture(`${testRoot}/${this.index}/one-file`)
const cliApp = new CliApp()
a.deepEqual(fs.existsSync(fixturePath), true)
await cliApp.start({ argv: ['-s', '--chain', 'renamer-case', '--case', 'camel', fixturePath] })
a.deepEqual(fs.existsSync(fixturePath), false)
a.deepEqual(fs.existsSync(`${testRoot}/${this.index}/oneFile`), true)
})

tom.test('--silent, view logging is not invoked', async function () {
const view = new TestView()
const fixturePath = createFixture(`${testRoot}/${this.index}/one`)
Expand Down
4 changes: 4 additions & 0 deletions test/lib/broken-view.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class BrokenView {
}

export default BrokenView
7 changes: 7 additions & 0 deletions test/lib/dummy-view.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class DummyView {
write (key, value, options) {
return 'ok'
}
}

export default DummyView

0 comments on commit cfce188

Please sign in to comment.