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

test: switch to forge-std for JS integration tests #770

Merged
merged 2 commits into from
Jan 17, 2025
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
@@ -1,13 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "./test.sol";
import "./Vm.sol";
import {Test} from "forge-std/src/Test.sol";

// Test that the contract environment related config values are passed on correctly
contract ContractEnvironmentTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);

contract ContractEnvironmentTest is Test {
function chainId() internal view returns (uint256 id) {
assembly {
id := chainid()
Expand All @@ -20,8 +17,4 @@ contract ContractEnvironmentTest is DSTest {
assertEq(block.number, 23, "block number is incorrect");
assertEq(block.timestamp, 45, "timestamp is incorrect");
}

function testContextIsTest() public {
assertEq(vm.isContext(Vm.ExecutionContext.Test), true);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this test case, because the execution context doesn't seem to be exposed from forge-std and it's unnecessary anyway: there is an identical Rust integration test.

7 changes: 2 additions & 5 deletions js/integration-tests/solidity-tests/contracts/EnvVar.t.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "./test.sol";
import "./Vm.sol";

contract EnvVarTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);
import {Test} from "forge-std/src/Test.sol";

contract EnvVarTest is Test {
function testGetEnv() public {
string memory key = "_EDR_SOLIDITY_TESTS_GET_ENV_TEST_KEY";
string memory val = "_edrSolidityTestsGetEnvTestVal";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "./test.sol";
import "./Vm.sol";
import {Test} from "forge-std/src/Test.sol";

// Test that the fork cheatcode works correctly
contract ForkCheatcodeTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);
contract ForkCheatcodeTest is Test {
uint256 fork;

function setUp() public {
fork = vm.createSelectFork("alchemyMainnet", 20_000_000);
}

function testBlockNumber() public {
assertEq(fork, vm.activeFork());
assertEq(block.number, 20_000_000);
Expand Down
13 changes: 5 additions & 8 deletions js/integration-tests/solidity-tests/contracts/FuzzFixture.t.sol
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "./test.sol";
import "./Vm.sol";
import {Test} from "forge-std/src/Test.sol";

// Contract to be tested with overflow vulnerability
contract IdentityContract {
function identity(uint256 amount) pure public returns(uint256) {
function identity(uint256 amount) pure public returns (uint256) {
require(amount != 7191815684697958081204101901807852913954269296144377099693178655035380638910, "Got value from fixture");
return amount;
}
}

// Test that fuzz fixtures specified in Solidity are not supported.
contract FuzzFixtureTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);

contract FuzzFixtureTest is Test {
IdentityContract testDummy;

uint256[] public fixtureAmount = [
// This is a random value
7191815684697958081204101901807852913954269296144377099693178655035380638910
// This is a random value
7191815684697958081204101901807852913954269296144377099693178655035380638910
];

function setUp() public {
Expand Down
5 changes: 2 additions & 3 deletions js/integration-tests/solidity-tests/contracts/Invariant.t.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "./test.sol";
import "./Vm.sol";
import {Test} from "forge-std/src/Test.sol";

contract StochasticWrongContract {
uint256 public a;
Expand All @@ -25,7 +24,7 @@ contract StochasticWrongContract {
}

// Test that the invariant testing works correctly by catching a bug in the contract.
contract FailingInvariantTest is DSTest {
contract FailingInvariantTest is Test {
StochasticWrongContract wrongContract;

function setUp() external {
Expand Down
29 changes: 14 additions & 15 deletions js/integration-tests/solidity-tests/contracts/IsolateTest.sol
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.24;

import "./test.sol";
import "./Vm.sol";
import {Test} from "forge-std/src/Test.sol";

contract TransientStorageStore {
uint value;
uint value;

function tstore(uint key, uint val) public {
assembly {
tstore(key, val)
function tstore(uint key, uint val) public {
assembly {
tstore(key, val)
}
}
}

function tload(uint key) public view returns (uint) {
uint val;
assembly {
val := tload(key)
function tload(uint key) public view returns (uint) {
uint val;
assembly {
val := tload(key)
}
return val;
}
return val;
}
}

contract IsolateTest is DSTest {
contract IsolateTest is Test {
TransientStorageStore transientStorageStore;

function setUp() public {
transientStorageStore = new TransientStorageStore();
transientStorageStore = new TransientStorageStore();
}

function testIsolateTest() public {
Expand Down
7 changes: 2 additions & 5 deletions js/integration-tests/solidity-tests/contracts/Overflow.t.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "./test.sol";
import "./Vm.sol";
import {Test} from "forge-std/src/Test.sol";

// Contract to be tested with overflow vulnerability
contract MyContract {
Expand All @@ -12,9 +11,7 @@ contract MyContract {
}

// Test that the fuzzing catches overflows
contract OverflowTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);

contract OverflowTest is Test {
MyContract public myContract;

function setUp() public {
Expand Down
31 changes: 4 additions & 27 deletions js/integration-tests/solidity-tests/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
import { task } from "hardhat/config";
import fs from "node:fs";
import path from "node:path";

const RUST_INTEGRATION_TEST_DATA_PATH =
"../../../crates/edr_solidity_tests/tests/testdata";

// HACK: copy the libraries Rust integration tests into the Hardhat project source
task("copyLibraries", "Run pre-test script", async (taskArgs, hre) => {
await fs.promises.copyFile(
path.join(RUST_INTEGRATION_TEST_DATA_PATH, "lib/ds-test/src/test.sol"),
path.join(hre.config.paths.sources, "test.sol")
);
await fs.promises.copyFile(
path.join(RUST_INTEGRATION_TEST_DATA_PATH, "cheats/Vm.sol"),
path.join(hre.config.paths.sources, "Vm.sol")
);
});

task("test").setAction(async (taskArgs, hre, runSuper) => {
// Run the pretest task before tests
await hre.run("copyLibraries");
// Run the actual tests
await runSuper();
});

export default {
solidity: { version: "0.8.24", settings: { evmVersion: "cancun" } },
solidity: {
version: "0.8.24",
settings: { evmVersion: "cancun" },
},
};
1 change: 1 addition & 0 deletions js/integration-tests/solidity-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@types/mocha": ">=9.1.0",
"@types/node": "^20.0.0",
"chai": "^4.3.6",
"forge-std": "github:foundry-rs/forge-std#v1.9.5",
"hardhat": "2.22.17",
"prettier": "^3.2.5",
"ts-node": "^10.8.0",
Expand Down
2 changes: 1 addition & 1 deletion js/integration-tests/solidity-tests/test/unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("Unit tests", () => {
);

assert.equal(failedTests, 0);
assert.equal(totalTests, 2);
assert.equal(totalTests, 1);
});

describe("IsolateMode", function () {
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading