Add tests for the Revive WASM version (#147)

This commit is contained in:
Sebastian Miasojed
2025-01-10 09:12:43 +01:00
committed by GitHub
parent f49d145e9a
commit d7d60da6f1
7 changed files with 199 additions and 18 deletions
+32
View File
@@ -0,0 +1,32 @@
const soljson = require('solc/soljson');
const createRevive = require('./resolc.js');
async function compile(standardJsonInput) {
if (!standardJsonInput) {
throw new Error('Input JSON for the Solidity compiler is required.');
}
// Initialize the compiler
const compiler = createRevive();
compiler.soljson = soljson;
// Provide input to the compiler
compiler.writeToStdin(JSON.stringify(standardJsonInput));
// Run the compiler
compiler.callMain(['--standard-json']);
// Collect output
const stdout = compiler.readFromStdout();
const stderr = compiler.readFromStderr();
// Check for errors and throw if stderr exists
if (stderr) {
throw new Error(`Compilation failed: ${stderr}`);
}
// Return the output if no errors
return stdout;
}
module.exports = { compile };
+3 -12
View File
@@ -1,5 +1,4 @@
const soljson = require('solc/soljson');
const createRevive = require('./resolc.js');
const { compile } = require('./revive.js');
const compilerStandardJsonInput = {
language: 'Solidity',
@@ -30,16 +29,8 @@ const compilerStandardJsonInput = {
};
async function runCompiler() {
const m = createRevive();
m.soljson = soljson;
// Set input data for stdin
m.writeToStdin(JSON.stringify(compilerStandardJsonInput));
// Compile the Solidity source code
let x = m.callMain(['--standard-json']);
console.log("Stdout: " + m.readFromStdout());
console.error("Stderr: " + m.readFromStderr());
let output = await compile(compilerStandardJsonInput)
console.log("Output: " + output);
}
runCompiler().catch(err => {
+7 -2
View File
@@ -7,9 +7,14 @@
"scripts": {
"fetch:soljson": "wget https://binaries.soliditylang.org/wasm/soljson-v0.8.28+commit.7893614a.js -O ./examples/web/soljson.js",
"example:web": "npm run fetch:soljson && http-server ./examples/web/",
"example:node": "node ./examples/node/run_revive.js"
"example:node": "node ./examples/node/run_revive.js",
"test:node": "mocha --timeout 10000 ./tests",
"test:bun": "bun test",
"test:all": "npm run test:node && npm run test:bun"
},
"devDependencies": {
"http-server": "^14.1.1"
"chai": "^5.1.2",
"http-server": "^14.1.1",
"mocha": "^11.0.1"
}
}
+102
View File
@@ -0,0 +1,102 @@
import { expect } from 'chai';
import { compile } from '../examples/node/revive.js';
const validCompilerInput = {
language: 'Solidity',
sources: {
'MyContract.sol': {
content: `
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
contract MyContract {
function greet() public pure returns (string memory) {
return "Hello";
}
}
`,
},
},
settings: {
optimizer: {
enabled: true,
runs: 200,
},
outputSelection: {
'*': {
'*': ['abi', 'evm.bytecode'],
},
},
},
};
describe('Compile Function Tests', function () {
it('should successfully compile valid Solidity code', async function () {
const result = await compile(validCompilerInput);
// Ensure result contains compiled contract
expect(result).to.be.a('string');
const output = JSON.parse(result);
expect(output).to.have.property('contracts');
expect(output.contracts['MyContract.sol']).to.have.property('MyContract');
expect(output.contracts['MyContract.sol'].MyContract).to.have.property('abi');
expect(output.contracts['MyContract.sol'].MyContract).to.have.property('evm');
expect(output.contracts['MyContract.sol'].MyContract.evm).to.have.property('bytecode');
});
it('should throw an error for invalid Solidity code', async function () {
const invalidCompilerInput = {
...validCompilerInput,
sources: {
'MyContract.sol': {
content: `
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "nonexistent/console.sol";
contract MyContract {
function greet() public pure returns (string memory) {
return "Hello" // Missing semicolon
}
}
`,
},
},
};
const result = await compile(invalidCompilerInput);
expect(result).to.be.a('string');
const output = JSON.parse(result);
expect(output).to.have.property('errors');
expect(output.errors).to.be.an('array');
expect(output.errors.length).to.be.greaterThan(0);
expect(output.errors[0].type).to.exist;
expect(output.errors[0].type).to.contain("ParserError");
});
it('should return not found error for missing imports', async function () {
const compilerInputWithImport = {
...validCompilerInput,
sources: {
'MyContract.sol': {
content: `
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "nonexistent/console.sol";
contract MyContract {
function greet() public pure returns (string memory) {
return "Hello";
}
}
`,
},
},
};
let result = await compile(compilerInputWithImport);
const output = JSON.parse(result);
expect(output).to.have.property('errors');
expect(output.errors).to.be.an('array');
expect(output.errors.length).to.be.greaterThan(0);
expect(output.errors[0].message).to.exist;
expect(output.errors[0].message).to.include('Source "nonexistent/console.sol" not found');
});
});