Skip to content

Commit

Permalink
Merge pull request #29 from ASSERT-KTH/28-add-custom-reporter-for-tes…
Browse files Browse the repository at this point in the history
…t-results

Add custom mocha reporter
  • Loading branch information
Mokita-J authored Sep 24, 2024
2 parents c8fda1e + 6342e29 commit f2e5fbb
Show file tree
Hide file tree
Showing 5 changed files with 739 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ contract Ethraffle_v4bAttacker {

function chooseWinner() public returns (uint) {

// console.log("coinbase", block.coinbase);
// console.log("difficulty: %d", block.difficulty);

address seed1 = contestants[uint(block.coinbase) % totalTickets];
address seed2 = contestants[uint(address(this)) % totalTickets];
Expand Down
8 changes: 7 additions & 1 deletion smartbugs-curated/0.4.x/hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,11 @@ module.exports = {
initialDate: "2018-12-31 11:59:00 PM",
hardfork: "shanghai",
}
}
},
mocha: {
reporter: './scripts/CustomReporter.js',
reporterOptions: {
json: false, // Export test results to JSON
}
},
};
98 changes: 98 additions & 0 deletions smartbugs-curated/0.4.x/scripts/CustomReporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
const Mocha = require('mocha');
const fs = require('fs');
const path = require('path');
const { fail } = require('assert');

// Extend the Spec reporter
const Spec = Mocha.reporters.Spec;
const Base = Mocha.reporters.Base; // For styling and symbols

class CustomReporter extends Spec {
constructor(runner, options) {
// Call the parent constructor (Spec reporter)
super(runner);

// Initialize variables to track the current file and passing file count
let passingFiles = 0;
let currentFile = null;
let allTestsPassed = true;
let allFiles = 0;
const testResults = [];

const exportOptions = options.reporterOptions || {};
const exportToJson = exportOptions.json || false;

// When a new suite (test file) starts
runner.on('suite', (suite) => {
if (suite.file) {
if (currentFile !== suite.file) {
// New test file started
currentFile = suite.file;
allTestsPassed = true; // Assume all tests will pass initially
allFiles += 1;
}
}
});

// If any test fails
runner.on('fail', () => {
// Mark the current test file as having failed tests
allTestsPassed = false;
});

// When a test ends, store its result
runner.on('test end', (test) => {
// only get the string after 'test' in the title
// filename = currentFile.split('/');
const fileName = currentFile.split('/test/')[1];
const contractFile = fileName.replace('_test.js', '.sol');
// console.log(contract_file);
testResults.push({
title: test.title,
file: fileName,
contractFile: contractFile,
state: test.state,
});
});

// When the suite (test file) ends
runner.on('suite end', (suite) => {
if (suite.file && currentFile === suite.file && allTestsPassed) {
passingFiles += 1;
}
});

// At the end of all tests, log the number of passing test files in the same style as passing tests
runner.on('end', () => {
const { tests, passes, failures, pending, duration } = runner.stats;

const failedFiles = allFiles - passingFiles;

const formattedMessage = Base.color('green', `Total passing test files: ${passingFiles}/${allFiles}`);
const formattedMessage2 = Base.color('fail', `Total failed files: ${failedFiles}/${allFiles}`);
// // Log the formatted message
console.log(`${formattedMessage}`);
console.log(`${formattedMessage2}`);

if (exportToJson) {
// Prepare the data to be exported to JSON
const results = {
totalTests: tests,
passingTests: passes,
failingTests: failures,
totalFiles: allFiles,
passingFiles: passingFiles,
failingFiles: failedFiles,
testResults: testResults,
};

// Write to JSON file
const jsonPath = path.join(__dirname, 'test-results.json');
fs.writeFileSync(jsonPath, JSON.stringify(results, null, 2));
console.log(`\nTest results written to ${jsonPath}`);
}
});
}
}

module.exports = CustomReporter;
Loading

0 comments on commit f2e5fbb

Please sign in to comment.