Skip to content

Commit

Permalink
Create good end to end tests (NomicFoundation#4620)
Browse files Browse the repository at this point in the history
Add end-to-end tests
  • Loading branch information
ChristopherDedominici committed Dec 6, 2023
1 parent acbda54 commit 61e63df
Show file tree
Hide file tree
Showing 54 changed files with 707 additions and 11 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: E2E tests

on:
push:
branches:
- "**"

jobs:
run-e2e:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
name: Run E2E tests on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: pnpm/action-setup@v2
with:
version: 8
- uses: actions/setup-node@v2
with:
node-version: 18
- name: Run fixture-projects script
run: |
cd e2e
chmod +x run-fixture-projects.sh
./run-fixture-projects.sh
shell: bash
- name: Run test-project-initialization script
run: |
cd e2e
chmod +x test-project-initialization.sh
./test-project-initialization.sh
shell: bash
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ $ npx hardhat init
? What do you want to do? …
❯ Create a JavaScript project
Create a TypeScript project
Create a TypeScript project (with Viem)
Create an empty hardhat.config.js
Quit
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Welcome to Hardhat v{HARDHAT_VERSION}
? What do you want to do? …
▸ Create a JavaScript project
Create a TypeScript project
Create a TypeScript project (with Viem)
Create an empty hardhat.config.js
Quit
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Welcome to Hardhat v{HARDHAT_VERSION}
? What do you want to do? …
▸ Create a JavaScript project
Create a TypeScript project
Create a TypeScript project (with Viem)
Create an empty hardhat.config.js
Quit
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ $ npx hardhat init
? What do you want to do? …
Create a JavaScript project
Create a TypeScript project
Create a TypeScript project (with Viem)
❯ Create an empty hardhat.config.js
Quit
```
Expand Down
3 changes: 3 additions & 0 deletions e2e/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# All e2e test temporary folders
projects-initialization-tests-*
fixture-projects-run-*
23 changes: 23 additions & 0 deletions e2e/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
### Hardhat initialization tests

To run the "hardhat init" tests, use the command:

```
./test-project-initialization.sh
```

### All other Hardhat scenario tests

To run all the fixture tests, use the command:

```
./run-fixture-projects.sh
```

You can run a single project's tests by passing the project folder name as a parameter.

Example: to run only the tests inside the `vars` folder, use:

```
./run-fixture-projects.sh vars
```
3 changes: 3 additions & 0 deletions e2e/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#! /usr/bin/env bash
rm -fr fixture-projects-run-*
rm -fr projects-initialization-tests-*
34 changes: 34 additions & 0 deletions e2e/fixture-projects/clean/contracts/Lock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;

// Uncomment this line to use console.log
// import "hardhat/console.sol";

contract Lock {
uint public unlockTime;
address payable public owner;

event Withdrawal(uint amount, uint when);

constructor(uint _unlockTime) payable {
require(
block.timestamp < _unlockTime,
"Unlock time should be in the future"
);

unlockTime = _unlockTime;
owner = payable(msg.sender);
}

function withdraw() public {
// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal
// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);

require(block.timestamp >= unlockTime, "You can't withdraw yet");
require(msg.sender == owner, "You aren't the owner");

emit Withdrawal(address(this).balance, block.timestamp);

owner.transfer(address(this).balance);
}
}
3 changes: 3 additions & 0 deletions e2e/fixture-projects/clean/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
solidity: "0.8.19",
};
3 changes: 3 additions & 0 deletions e2e/fixture-projects/clean/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "clean"
}
25 changes: 25 additions & 0 deletions e2e/fixture-projects/clean/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#! /usr/bin/env sh

# fail if any commands fails
set -e

# import helpers functions
. ../../helpers.sh

echo "Running tests: $(basename "$(pwd)")"

run_test_and_handle_failure "npx hardhat compile" 0

echo "assert that the artifacts and cache directory exist and are not empty"
assert_directory_exists "artifacts"
assert_directory_not_empty "artifacts"
assert_directory_exists "cache"
assert_directory_not_empty "cache"

echo "it should run the command clean successfully"
run_test_and_handle_failure "npx hardhat clean" 0

echo "it should have deleted the artifacts directory and emptied the cache directory"
assert_directory_doesnt_exist "artifacts"
assert_directory_exists "cache"
assert_directory_empty "cache"
11 changes: 11 additions & 0 deletions e2e/fixture-projects/compile-fail/contracts/Foo.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Foo {
uint public x;

function inc() public {
x++;
error
}
}
3 changes: 3 additions & 0 deletions e2e/fixture-projects/compile-fail/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
solidity: "0.8.20"
}
3 changes: 3 additions & 0 deletions e2e/fixture-projects/compile-fail/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "compile-fail"
}
12 changes: 12 additions & 0 deletions e2e/fixture-projects/compile-fail/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#! /usr/bin/env sh

# fail if any commands fails
set -e

# import helpers functions
. ../../helpers.sh

echo "Running tests: $(basename "$(pwd)")"

echo "it should fail the compilation"
run_test_and_handle_failure "npx hardhat compile" 1
10 changes: 10 additions & 0 deletions e2e/fixture-projects/compile/contracts/Foo.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Foo {
uint public x;

function inc() public {
x++;
}
}
3 changes: 3 additions & 0 deletions e2e/fixture-projects/compile/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
solidity: "0.8.20"
}
3 changes: 3 additions & 0 deletions e2e/fixture-projects/compile/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "compile"
}
12 changes: 12 additions & 0 deletions e2e/fixture-projects/compile/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#! /usr/bin/env sh

# fail if any commands fails
set -e

# import helpers functions
. ../../helpers.sh

echo "Running tests: $(basename "$(pwd)")"

echo "it should compile the contracts in the default project folder"
run_test_and_handle_failure "npx hardhat compile" 0
18 changes: 18 additions & 0 deletions e2e/fixture-projects/flatten/contracts/A.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* SPDX-License-Identifier: MIT */

pragma abicoder v2;
pragma solidity ^0.8.19;

import "./B.sol";

contract A {
uint256 private storedData;

function set(uint256 value) public {
storedData = value;
}

function get() public view returns (uint256) {
return storedData;
}
}
13 changes: 13 additions & 0 deletions e2e/fixture-projects/flatten/contracts/B.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MPL-2.0

pragma solidity ^0.8.19;

import "./C.sol";

contract B {
uint public x;

function inc() public {
x++;
}
}
3 changes: 3 additions & 0 deletions e2e/fixture-projects/flatten/contracts/C.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pragma solidity ^0.8.19;

contract C {}
34 changes: 34 additions & 0 deletions e2e/fixture-projects/flatten/contracts/Lock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;

// Uncomment this line to use console.log
// import "hardhat/console.sol";

contract Lock {
uint public unlockTime;
address payable public owner;

event Withdrawal(uint amount, uint when);

constructor(uint _unlockTime) payable {
require(
block.timestamp < _unlockTime,
"Unlock time should be in the future"
);

unlockTime = _unlockTime;
owner = payable(msg.sender);
}

function withdraw() public {
// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal
// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);

require(block.timestamp >= unlockTime, "You can't withdraw yet");
require(msg.sender == owner, "You aren't the owner");

emit Withdrawal(address(this).balance, block.timestamp);

owner.transfer(address(this).balance);
}
}
3 changes: 3 additions & 0 deletions e2e/fixture-projects/flatten/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
solidity: "0.8.19",
};
3 changes: 3 additions & 0 deletions e2e/fixture-projects/flatten/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "flatten"
}
15 changes: 15 additions & 0 deletions e2e/fixture-projects/flatten/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#! /usr/bin/env sh

# fail if any commands fails
set -e

# import helpers functions
. ../../helpers.sh

echo "Running tests: $(basename "$(pwd)")"

echo "it should flatten all the files in the folder because no file names are passed"
run_test_and_handle_failure "npx hardhat flatten" 0

echo "it should flatten only the 'Lock.sol' file because it is passed as an argument"
run_test_and_handle_failure "npx hardhat flatten contracts/Lock.sol" 0
10 changes: 10 additions & 0 deletions e2e/fixture-projects/gas-reporter/contracts/Foo.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Foo {
uint public x;

function inc() public {
x++;
}
}
5 changes: 5 additions & 0 deletions e2e/fixture-projects/gas-reporter/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require("@nomicfoundation/hardhat-toolbox");

module.exports = {
solidity: "0.8.20",
};
6 changes: 6 additions & 0 deletions e2e/fixture-projects/gas-reporter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "gas-reporter",
"devDependencies": {
"@nomicfoundation/hardhat-toolbox": "^4.0.0"
}
}
13 changes: 13 additions & 0 deletions e2e/fixture-projects/gas-reporter/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#! /usr/bin/env sh

# fail if any commands fails
set -e

# import helpers functions
. ../../helpers.sh

echo "Running tests: $(basename "$(pwd)")"

echo "it should run the gas reporter while running the tests"
export "REPORT_GAS='true'"
run_test_and_handle_failure "npx hardhat test" 0
3 changes: 3 additions & 0 deletions e2e/fixture-projects/gas-reporter/test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it("provider should be available", async function () {
await hre.network.provider.send("eth_accounts");
});
3 changes: 3 additions & 0 deletions e2e/fixture-projects/help/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
solidity: "0.8.19",
};
3 changes: 3 additions & 0 deletions e2e/fixture-projects/help/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "help"
}
15 changes: 15 additions & 0 deletions e2e/fixture-projects/help/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#! /usr/bin/env sh

# fail if any commands fails
set -e

# import helpers functions
. ../../helpers.sh

echo "Running tests: $(basename "$(pwd)")"

echo "it should show the help information when no argument is passed"
run_test_and_handle_failure "npx hardhat" 0

echo "it should show the help information when the 'help' argument is passed"
run_test_and_handle_failure "npx hardhat help" 0
Loading

0 comments on commit 61e63df

Please sign in to comment.