Skip to content

Commit

Permalink
error out explicitly when extra config file passed via -c isnt found
Browse files Browse the repository at this point in the history
code is duplicated in main action and listRules, but didn't see a really
good way modularize it yet. Perhaps when taking care of #90 or #133 it'll be
more evident?
  • Loading branch information
juanpcapurro committed Jan 29, 2024
1 parent b5ede46 commit 9d6ae6d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
28 changes: 24 additions & 4 deletions e2e/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ describe('main executable tests', function () {
let code
let stderr
let stdout

describe('GIVEN a non-existing extra config file passed with -c WHEN linting', function () {
beforeEach(function () {
;({ code, stderr, stdout } = shell.exec('solhint -c nothere.json Foo.sol', {
silent: true,
}))
})

it('THEN linter exits with error 255 for bad options', function () {
expect(code).to.equal(255)
})
it('AND stdout is empty', function () {
expect(stdout.trim()).to.eq('')
})
it('AND stderr logs the file wasnt found', function () {
expect(stderr.trim()).to.include('Extra config file "nothere.json" couldnt be found')
})
})
describe('GIVEN a config file with invalid syntax, WHEN linting', function () {
beforeEach(function () {
;({ code, stderr, stdout } = shell.exec('solhint -c broken-json-syntax.json Foo.sol', {
Expand Down Expand Up @@ -394,11 +412,13 @@ describe('main executable tests', function () {
{ silent: true }
))
})
it('THEN it returns error code 1', function () {
expect(code).to.equal(1)
it('THEN it returns error code 255 for bad options', function () {
expect(code).to.equal(255)
})
it('AND it reports the problem to stderr', function () {
expect(stderr).to.contain("Failed to load a solhint's config file")
it('AND stderr logs the file wasnt found', function () {
expect(stderr.trim()).to.include(
'Extra config file "config-file-with-weird-name.json" couldnt be found'
)
})
it('AND it does NOT list disabled rules', function () {
expect(stdout).to.eq('')
Expand Down
15 changes: 14 additions & 1 deletion solhint.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ function execMainAction() {
console.error(ex.message)
process.exit(EXIT_CODES.BAD_OPTIONS)
}

const customConfig = rootCommand.opts().config
if (customConfig && !fs.existsSync(customConfig)) {
console.error(`Extra config file "${customConfig}" couldnt be found.`)
process.exit(EXIT_CODES.BAD_OPTIONS)
}

let reports
try {
const reportLists = rootCommand.args.filter(_.isString).map(processPath)
Expand Down Expand Up @@ -216,7 +223,13 @@ function consumeReport(reports, formatterFn) {
}

function listRules() {
const { config } = loadFullConfigurationForPath('.', rootCommand.opts().config)
const customConfig = rootCommand.opts().config
if (customConfig && !fs.existsSync(customConfig)) {
console.error(`Extra config file "${customConfig}" couldnt be found.`)
process.exit(EXIT_CODES.BAD_OPTIONS)
}

const { config } = loadFullConfigurationForPath('.', customConfig)
const rulesObject = config.rules

console.log('\nRules: \n')
Expand Down

0 comments on commit 9d6ae6d

Please sign in to comment.