Skip to content

Commit

Permalink
added compact format and e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dbale-altoros committed Jul 24, 2023
1 parent af1c996 commit 121fbe7
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Linter for Solidity programming language
Options:
-V, --version output the version number
-f, --formatter [name] report formatter name (stylish, table, tap, unix, json)
-f, --formatter [name] report formatter name (stylish, table, tap, unix, json, compact)
-w, --max-warnings [maxWarningsNumber] number of allowed warnings
-c, --config [file_name] file to use as your .solhint.json
-q, --quiet report errors only - default: false
Expand Down
3 changes: 3 additions & 0 deletions e2e/06-formatters/.solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "solhint:all"
}
11 changes: 11 additions & 0 deletions e2e/06-formatters/contracts/Foo.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0;

contract Foo {
uint256 public constant test1 = 1;
uint256 TEST2;

constructor() {

}
}
10 changes: 10 additions & 0 deletions e2e/06-formatters/contracts/Foo2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.8;

contract Foo {
uint256 public constant test1 = 1;

constructor() {

}
}
11 changes: 11 additions & 0 deletions e2e/06-formatters/contracts/Foo3.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.8;

contract Foo {
uint256 public constant TEST1 = 1;
uint256 public value;

function _goodContract() private {
value = TEST1;
}
}
99 changes: 99 additions & 0 deletions e2e/06-formatters/helpers/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const foo1Output = [
{
line: 2,
column: 1,
severity: 2,
message: 'Compiler version >=0.6.0 does not satisfy the ^0.5.8 semver requirement',
ruleId: 'compiler-version',
fix: null,
filePath: 'contracts/Foo.sol',
},
{
line: 5,
column: 5,
severity: 3,
message: 'Constant name must be in capitalized SNAKE_CASE',
ruleId: 'const-name-snakecase',
fix: null,
filePath: 'contracts/Foo.sol',
},
{
line: 6,
column: 5,
severity: 3,
message: 'Explicitly mark visibility of state',
ruleId: 'state-visibility',
fix: null,
filePath: 'contracts/Foo.sol',
},
{
line: 6,
column: 5,
severity: 3,
message: "'TEST2' should start with _",
ruleId: 'private-vars-leading-underscore',
fix: null,
filePath: 'contracts/Foo.sol',
},
{
line: 6,
column: 5,
severity: 3,
message: 'Variable name must be in mixedCase',
ruleId: 'var-name-mixedcase',
fix: null,
filePath: 'contracts/Foo.sol',
},
{
line: 8,
column: 5,
severity: 3,
message:
'Explicitly mark visibility in function (Set ignoreConstructors to true if using solidity >=0.7.0)',
ruleId: 'func-visibility',
fix: null,
filePath: 'contracts/Foo.sol',
},
{
line: 8,
column: 19,
severity: 3,
message: 'Code contains empty blocks',
ruleId: 'no-empty-blocks',
fix: null,
filePath: 'contracts/Foo.sol',
},
]

const foo2Output = [
{
line: 5,
column: 5,
severity: 3,
message: 'Constant name must be in capitalized SNAKE_CASE',
ruleId: 'const-name-snakecase',
fix: null,
filePath: 'contracts/Foo2.sol',
},
{
line: 7,
column: 5,
severity: 3,
message:
'Explicitly mark visibility in function (Set ignoreConstructors to true if using solidity >=0.7.0)',
ruleId: 'func-visibility',
fix: null,
filePath: 'contracts/Foo2.sol',
},
{
line: 7,
column: 19,
severity: 3,
message: 'Code contains empty blocks',
ruleId: 'no-empty-blocks',
fix: null,
filePath: 'contracts/Foo2.sol',
},
]

module.exports = { foo1Output, foo2Output }
1 change: 1 addition & 0 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"license": "MIT",
"devDependencies": {
"chai": "^4.3.7",
"chai-spies": "^1.0.0",
"fs-extra": "^11.1.0",
"get-stream": "^6.0.0",
"mocha": "^10.2.0",
Expand Down
181 changes: 177 additions & 4 deletions e2e/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { expect } = require('chai')
const chai = require('chai')
const { expect } = chai
const cp = require('child_process')
const fs = require('fs-extra')
const getStream = require('get-stream')
Expand Down Expand Up @@ -113,7 +114,7 @@ describe('e2e', function () {
})
})

describe.only('--max-warnings parameter tests', function () {
describe('--max-warnings parameter tests', function () {
// Foo contract has 6 warnings
// Foo2 contract has 1 error and 14 warnings
useFixture('05-max-warnings')
Expand All @@ -128,13 +129,13 @@ describe('e2e', function () {
})

it('should display [warnings exceeded] for max 3 warnings and exit error 1', function () {
const { code, stdout, } = shell.exec('solhint contracts/Foo.sol --max-warnings 3')
const { code, stdout } = shell.exec('solhint contracts/Foo.sol --max-warnings 3')
expect(code).to.equal(1)
expect(stdout.trim()).to.contain(warningExceededMsg)
})

it('should return error for Compiler version rule, ignoring 3 --max-warnings', function () {
const { code, stdout } = shell.exec('solhint contracts/Foo2.sol --max-warnings 3')
const { code, stdout } = shell.exec('solhint contracts/Foo2.sol --max-warnings 3')
expect(code).to.equal(1)
expect(stdout.trim()).to.contain(errorFound)
})
Expand All @@ -145,4 +146,176 @@ describe('e2e', function () {
expect(stdout.trim()).to.not.contain(errorFound)
})
})

describe('formatters tests', function () {
// Foo contract has 1 error and 6 warnings
// Foo2 contract has 3 warnings
useFixture('06-formatters')

it('should display COMPACT format', function () {
const { code, stdout } = shell.exec('solhint contracts/Foo.sol -f compact')
expect(code).to.equal(0)
expect(stdout.trim()).to.not.contain(warningExceededMsg)
})

it('should display JSON format', function () {
const { code, stdout } = shell.exec('solhint contracts/Foo.sol -f json')
expect(code).to.equal(0)
expect(stdout.trim()).to.contain(warningExceededMsg)
})

it('should display STYLISH format', function () {
const { code, stdout } = shell.exec('solhint contracts/Foo2.sol -f stylish')
expect(code).to.equal(0)
expect(stdout.trim()).to.contain(errorFound)
})

it('should display TABLE format', function () {
const { code, stdout } = shell.exec('solhint contracts/Foo2.sol -f table')
expect(code).to.equal(0)
expect(stdout.trim()).to.not.contain(errorFound)
})

it('should display TAP format', function () {
const { code, stdout } = shell.exec('solhint contracts/Foo2.sol -f tap')
expect(code).to.equal(0)
expect(stdout.trim()).to.not.contain(errorFound)
})

it('should display UNIX format', function () {
const { code, stdout } = shell.exec('solhint contracts/Foo2.sol -f unix')
expect(code).to.equal(0)
expect(stdout.trim()).to.not.contain(errorFound)
})
})

describe.only('formatter tests', () => {
const { foo1Output, foo2Output } = require('./06-formatters/helpers/helpers.js')

// Foo contract has 1 error and 6 warnings
// Foo2 contract has 3 warnings
useFixture('06-formatters')

it('should fail when wrong formatter is specify', () => {
const formatterType = 'wrongOne'
const { code } = shell.exec(`solhint contracts/Foo2.sol -f ${formatterType}`)
expect(code).to.equal(1)
})

describe('unix formatter tests', () => {
const formatterType = 'unix'

it('should return nothing when file does not exist and unix is the formatter', () => {
const { code, stdout } = shell.exec(`solhint contracts/Foo1.sol -f ${formatterType}`)
expect(code).to.equal(0)
expect(stdout.trim()).to.be.empty
})
it('should return nothing when file exists and there is no error/warning', () => {
const { code, stdout } = shell.exec(`solhint contracts/Foo3.sol -f ${formatterType}`)
expect(code).to.equal(0)
expect(stdout.trim()).to.be.empty
})
it('should make the output report with unix formatter for Foo2', () => {
const { code, stdout } = shell.exec(`solhint contracts/Foo2.sol -f ${formatterType}`)

const reportLines = stdout.trim().split('\n')
let expectedLine
let msgType

for (let i = 0; i < reportLines.length - 3; i++) {
msgType = foo2Output[i].severity === 2 ? 'Error' : 'Warning'
expectedLine = `${foo2Output[i].filePath}:${foo2Output[i].line}:${foo2Output[i].column}: ${foo2Output[i].message} [${msgType}/${foo2Output[i].ruleId}]`
expect(reportLines[i]).to.equal(expectedLine)
}
expect(code).to.equal(0)

const finalLine = '3 problem/s (3 warning/s) '
expect(reportLines[reportLines.length - 2]).to.equal(finalLine)
})
it('should make the output report with unix formatter for Foo and Foo2', () => {
const { code, stdout } = shell.exec(`solhint contracts/Foo.sol contracts/Foo2.sol -f ${formatterType}`)

const reportLines = stdout.trim().split('\n')

console.log('reportLines :>> ', reportLines);
console.log('reportLines length :>> ', reportLines.length);

const joinedFoo = foo1Output.concat(foo2Output)
console.log('-----------------------------------------------------------');
console.log('joinedFoo :>> ', joinedFoo)
console.log('===========================================================');
let expectedLine
let msgType

for (let i = 0; i < reportLines.length - 3; i++) {
console.log('i :>> ', i)
console.log('joinedFoo[i] :>> ', joinedFoo[i])
msgType = joinedFoo[i].severity === 2 ? 'Error' : 'Warning'
expectedLine = `${joinedFoo[i].filePath}:${joinedFoo[i].line}:${joinedFoo[i].column}: ${joinedFoo[i].message} [${msgType}/${joinedFoo[i].ruleId}]`
expect(reportLines[i]).to.equal(expectedLine)
}
// because there's an error
expect(code).to.equal(1)

const finalLine = '10 problem/s (1 error/s, 9 warning/s)'
expect(reportLines[reportLines.length - 2]).to.contain(finalLine)
})
})
describe('json formatter tests', () => {
const formatterType = 'json'

it('should return nothing when file does not exist and json is the formatter', () => {
const { code, stdout } = shell.exec(`solhint contracts/Foo1.sol -f ${formatterType}`)
expect(code).to.equal(0)
expect(stdout.trim()).to.be.empty
})
it('should return nothing when file exists and there is no error/warning', () => {
const { code, stdout } = shell.exec(`solhint contracts/Foo3.sol -f ${formatterType}`)
expect(code).to.equal(0)
expect(stdout.trim()).to.be.empty
})
it('should make the output report with json formatter', () => {
const { code, stdout } = shell.exec(`solhint contracts/Foo2.sol -f ${formatterType}`)

const reportLines = stdout.trim().split('\n')
console.log('reportLines :>> ', reportLines);
console.log('reportLines length :>> ', reportLines.length);

// let expectedLine
// let msgType


// for (let i = 0; i < reportLines.length - 3; i++) {
// msgType = foo2Output[i].severity === 2 ? 'Error' : 'Warning'
// expectedLine = `${foo2Output[i].filePath}:${foo2Output[i].line}:${foo2Output[i].column}: ${foo2Output[i].message} [${msgType}/${foo2Output[i].ruleId}]`
// expect(reportLines[i]).to.equal(expectedLine)
// }
expect(code).to.equal(0)

// const finalLine = '3 problem/s (3 warning/s) '
// expect(reportLines[reportLines.length - 2]).to.equal(finalLine)
})
it('should make the output report with json formatter', () => {
const { code, stdout } = shell.exec(`solhint contracts/Foo.sol contracts/Foo2.sol -f ${formatterType}`)

const reportLines = stdout.trim().split('\n')
console.log('reportLines :>> ', reportLines);
console.log('reportLines length :>> ', reportLines.length);
// const joinedFoo = foo1Output.concat(foo2Output)
// let expectedLine
// let msgType


// for (let i = 0; i < reportLines.length - 3; i++) {
// msgType = foo2Output[i].severity === 2 ? 'Error' : 'Warning'
// expectedLine = `${foo2Output[i].filePath}:${foo2Output[i].line}:${foo2Output[i].column}: ${foo2Output[i].message} [${msgType}/${foo2Output[i].ruleId}]`
// expect(reportLines[i]).to.equal(expectedLine)
// }
expect(code).to.equal(1)

// const finalLine = '3 problem/s (3 warning/s) '
// expect(reportLines[reportLines.length - 2]).to.equal(finalLine)
})
})
})
})
12 changes: 7 additions & 5 deletions lib/formatters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

files in this directory are pulled from eslint repository:

- table.js: eslint v5.6.0 Gajus Kuizinas <[email protected]>
- unix.js: eslint v8.32.0 oshi-shinobu
- tap.js: eslint v8.32.0 Jonathan Kingston
- stylish.js: eslint v8.32.0 by Sindre Sorhus
- json.js: eslint v8.32.0 by Artur Lukianov & Diego Bale
- table.js: eslint - Gajus Kuizinas
- unix.js: eslint - oshi-shinobu
- tap.js: eslint - Jonathan Kingston
- stylish.js: eslint - Sindre Sorhus
- json.js: eslint - Artur Lukianov & Diego Bale
- compact.js: eslint - Nicholas C. Zakas

Loading

0 comments on commit 121fbe7

Please sign in to comment.