Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Commit

Permalink
Add test case and make minor test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nbanmp committed May 20, 2019
1 parent a581cf2 commit 46c53fd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
5 changes: 2 additions & 3 deletions helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ async function analyze(config) {
// User specified contracts; only analyze those
await Promise.all(selectedContracts.map(async selectedContract => {
const [contractFile, contractName] = selectedContract.split(':');
const fullPath = path.resolve(contractFile);
const fullPath = await path.resolve(contractFile);

const buildObj = buildObjForSourcePath(allBuildObjs, fullPath)
if(!buildObj) {
Expand Down Expand Up @@ -754,7 +754,6 @@ function compareMessLCRange(mess1, mess2) {
* @returns {ESListIssue[]}
*/
const groupEslintIssuesByBasename = issues => {
const path = require('path');
const mappedIssues = issues.reduce((accum, issue) => {
const {
errorCount,
Expand All @@ -765,7 +764,7 @@ const groupEslintIssuesByBasename = issues => {
messages,
} = issue;

const basename = path.basename(filePath);
const basename = filePath;
if (!accum[basename]) {
accum[basename] = {
errorCount: 0,
Expand Down
68 changes: 63 additions & 5 deletions test/test_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const assert = require('assert');
const proxyquire = require('proxyquire');
const rewire = require('rewire');
const fs = require('fs');
const path = require('path');
const armlet = require('armlet');
const sinon = require('sinon');
const trufstuf = require('../lib/trufstuf');
Expand Down Expand Up @@ -89,12 +90,12 @@ describe('helpers.js', function() {
let ghettoReportStub;
let getIssuesStub;
let loginStub;
let pathStub;


beforeEach(() => {
getTruffleBuildJsonFilesStub = sinon.stub(trufstuf, 'getTruffleBuildJsonFiles');
parseBuildJsonStub = sinon.stub(trufstuf, 'parseBuildJson');
contractsCompileStub = sinon.stub();
doReportStub = sinon.stub();
doAnalysisStub = sinon.stub();
loggerStub = sinon.stub();
Expand All @@ -103,6 +104,11 @@ describe('helpers.js', function() {
getUserInfoStub = sinon.stub(armlet.Client.prototype, 'getUserInfo');
getIssuesStub = sinon.stub(armlet.Client.prototype, 'getIssues');
loginStub = sinon.stub(armlet.Client.prototype, 'login');
contractsCompileStub = sinon.stub();
pathStub = {
resolve: sinon.stub(),
join: path.join
}

config = {
contracts_directory: '/contracts',
Expand All @@ -117,6 +123,7 @@ describe('helpers.js', function() {
};

helpers = rewire('../helpers');
helpers.__set__('path', pathStub);
helpers.__set__('contractsCompile', contractsCompileStub);
helpers.__set__('doAnalysis', doAnalysisStub);
helpers.__set__('doReport', doReportStub);
Expand Down Expand Up @@ -145,8 +152,59 @@ describe('helpers.js', function() {

/* TODO: Logged in messaging tests */

/* TODO: Test for the different types of inputs: contract.sol:Contract,
contract.sol, and nothing, and ensure the correct build files are selected. */
it('should find and analyze the correct build object', async () => {
config._ = ["verify", "contract.sol:Contract1"];
const fakeBuildJson = {
"compiler": { "name": "", "version": "" },
"updatedAt": "",
"sources": {
"/build/contracts/mythx/contracts/contract.sol": {
"contracts": [
{
"contractName": "Contract1",
"bytecode": "0x",
"deployedBytecode": "0x",
"sourceMap": "",
"deployedSourceMap": ""
},
{
"contractName": "Contract2",
"bytecode": "0x",
"deployedBytecode": "0x",
"sourceMap": "",
"deployedSourceMap": ""
}
],
"ast": {},
"legacyAST": {},
"id": 0,
"source": ""
}
}
}

pathStub.resolve.resolves("/build/contracts/mythx/contracts/contract.sol");

doAnalysisStub.resolves({ objects: 1, errors: 3 });
getUserInfoStub.resolves({
total: 1,
users: [
{ id: '000000000000000000000001',
roles: ['regular_user'],
}
]
});
getTruffleBuildJsonFilesStub.resolves(['contract.json']);
parseBuildJsonStub.resolves(fakeBuildJson);

await helpers.analyze(config);
assert.ok(pathStub.resolve.calledWith('contract.sol'));
assert.ok(getTruffleBuildJsonFilesStub.calledWith('/build/contracts/mythx/contracts'));
assert.ok(getTruffleBuildJsonFilesStub.calledWith('/build/contracts/mythx/contracts'));
assert.ok(doAnalysisStub.calledWith(sinon.match.any, config, [ { contractName: "Contract1", contract: sinon.match.any} ], helpers.defaultAnalyzeRateLimit));
assert.ok(doReportStub.calledWith(config, 1, 3));
});


it('should call doAnalysis and report issues', async () => {
const fakeBuildJson = {
Expand Down Expand Up @@ -299,7 +357,7 @@ describe('helpers.js', function() {
warningCount: 1,
fixableErrorCount: 0,
fixableWarningCount: 0,
filePath: 'contract.sol',
filePath: '/tmp/test_dir/contract.sol',
messages: [
'message 1',
'message 2',
Expand Down Expand Up @@ -330,7 +388,7 @@ describe('helpers.js', function() {
warningCount: 2,
fixableErrorCount: 0,
fixableWarningCount: 0,
filePath: 'contract.sol',
filePath: '/tmp/test_dir/contract.sol',
messages: [
'message 1',
'message 2',
Expand Down

0 comments on commit 46c53fd

Please sign in to comment.