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

Add custom mocha reporter #29

Merged
merged 1 commit into from
Sep 24, 2024
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
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
Loading