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

feat(exit): non-zero exit code for getUnusedRules #34

Merged
merged 1 commit into from
Apr 14, 2016
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,20 @@ The intended usage is as an npm script:
{
...
"scripts": {
"eslint-find-option-rules": "eslint-find-rules [option] <file>"
"eslint-find-option-rules": "eslint-find-rules [option] <file> [flag]"
}
...
}
```

```
available options are -c|--current, -a|--all-available, -p|--plugin, -u|--unused
available flag is -n|--no-error
```

By default it will error out only for `-u|--unused`,
however if you do not want the `process` to `exit` with a `non-zero` exit code, use the `-n|--no-error` along with `-u|--unused`

Then run it with: `$ npm run eslint-find-option-rules -s` (the `-s` is to silence npm output).

### Specify a file
Expand Down
11 changes: 9 additions & 2 deletions src/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,32 @@ var options = {
getPluginRules: ['plugin', 'p'],
getAllAvailableRules: ['all-available', 'a'],
getUnusedRules: ['unused', 'u'],
n: ['no-error'],
}

var argv = require('yargs')
.boolean(Object.keys(options))
.alias(options)
.argv

var processExitCode = argv.u && !argv.n ? 1 : 0
var getRuleFinder = require('./rule-finder')
var specifiedFile = argv._[0]

var ruleFinder = getRuleFinder(specifiedFile)
Object.keys(options).forEach(function findRules(option) {
var rules
if (argv[option]) {
rules = ruleFinder[option]()
var ruleFinderMethod = ruleFinder[option]
if (argv[option] && ruleFinderMethod) {
rules = ruleFinderMethod()
/* istanbul ignore next */
if (rules.length) {
console.log('\n' + options[option][0], 'rules\n') // eslint-disable-line no-console
console.log(rules.join(', ')) // eslint-disable-line no-console
}
}
})

if (processExitCode) {
process.exit(processExitCode)
}
16 changes: 16 additions & 0 deletions test/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var proxyquire = require('proxyquire')
var sinon = require('sinon')

var consoleLog = console.log // eslint-disable-line no-console
var processExit = process.exit

var getCurrentRules = sinon.stub().returns(['current'])
var getPluginRules = sinon.stub().returns(['plugin'])
Expand All @@ -28,11 +29,13 @@ describe('bin', function() {
}
consoleLog.apply(null, arguments)
}
process.exit = function noop() {}
process.argv = process.argv.slice(0, 2)
})

afterEach(function() {
console.log = consoleLog // eslint-disable-line no-console
process.exit = processExit
// purge yargs cache
delete require.cache[require.resolve('yargs')]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't proxyquire take care of the cache?

From its docs:

// ensure we don't get any module from the cache, but to load it fresh every time 
var proxyquire = require('proxyquire').noPreserveCache();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Respectively, extend the stub with

  'yargs': {
    '@global': true
  }

?

At the end of the day, if it works (and it does), I'm fine with it. Just asked for clarification ...

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you suggesting something like below?

var assert = require('assert')
-var proxyquire = require('proxyquire')
+var proxyquire = require('proxyquire').noPreserveCache();
 var sinon = require('sinon')

 var consoleLog = console.log // eslint-disable-line no-console
@@ -19,6 +19,9 @@ var stub = {
       getUnusedRules: getUnusedRules,
     }
   },
+  'yargs': {
+    '@global': true
+  }
 }

 describe('bin', function() {
@@ -36,8 +39,6 @@ describe('bin', function() {
   afterEach(function() {
     console.log = consoleLog // eslint-disable-line no-console
     process.exit = processExit
-    // purge yargs cache
-    delete require.cache[require.resolve('yargs')]
   })

   it('option -c|--current', function() {

could not get it working, looks like yargs is cached after the first require. Let me know if I have mis interpreted.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that is exactly what I thought of. I just remembered, that I had an issue with the caching behavior in another case ... Let's forget this. The explicit deletion from require.cache seems to work, so let's not overcomplicate this.

(I have to dig into this deeper some time, there has to be a way to accomplish this with proxyquire's own means... if not, it has to be a bug.)

})
Expand All @@ -56,7 +59,20 @@ describe('bin', function() {
})

it('option -u|--unused', function() {
process.exit = function(status) {
assert.equal(status, 1)
}
process.argv[2] = '-u'
proxyquire('../src/bin', stub)
assert.ok(getUnusedRules.called)
})

it('option -u|--unused along with -n|no-error', function() {
process.exit = function(status) {
assert.equal(status, 0)
}
process.argv[2] = '-u'
process.argv[3] = '-n'
proxyquire('../src/bin', stub)
assert.ok(getUnusedRules.called)
})
Expand Down