Skip to content

Commit

Permalink
Add compilation restrictions for tests (#12)
Browse files Browse the repository at this point in the history
* fix a few bugs with doc generation

* add additional compiler profiles to template
  • Loading branch information
gretzke authored Nov 22, 2024
1 parent 7e5ff8c commit e5eff0c
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/Increment counter number.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
26304
26263
2 changes: 1 addition & 1 deletion .forge-snapshots/Set counter number.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
26394
26337
2 changes: 1 addition & 1 deletion docs/autogen/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ title = ""
no-section-label = true
additional-js = ["solidity.min.js"]
additional-css = ["book.css"]
git-repository-url = "https://github.com/gretzke/foundry-template"
git-repository-url = "https://github.com/Uniswap/foundry-template"

[output.html.fold]
enable = true
4 changes: 2 additions & 2 deletions docs/autogen/src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Summary
- [Home](README.md)
# src
- [interface](src/interface/README.md)
- [ICounter](src/interface/ICounter.sol/interface.ICounter.md)
- [interfaces](src/interfaces/README.md)
- [ICounter](src/interfaces/ICounter.sol/interface.ICounter.md)
- [Counter](src/Counter.sol/contract.Counter.md)
4 changes: 2 additions & 2 deletions docs/autogen/src/src/Counter.sol/contract.Counter.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Counter
[Git Source](https://github.com/Uniswap/foundry-template/blob/6ed2d53f10b4739f84426a12bef01482d7a2e669/src/Counter.sol)
[Git Source](https://github.com/Uniswap/foundry-template/blob/0864bd3fda3ae2b97362ae80691cd59ca9cf5090/src/Counter.sol)

**Inherits:**
[ICounter](/src/interface/ICounter.sol/interface.ICounter.md)
[ICounter](/src/interfaces/ICounter.sol/interface.ICounter.md)


## State Variables
Expand Down
2 changes: 1 addition & 1 deletion docs/autogen/src/src/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@


# Contents
- [interface](/src/interface)
- [interfaces](/src/interfaces)
- [Counter](Counter.sol/contract.Counter.md)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# ICounter
[Git Source](https://github.com/Uniswap/foundry-template/blob/6ed2d53f10b4739f84426a12bef01482d7a2e669/src/interface/ICounter.sol)
[Git Source](https://github.com/Uniswap/foundry-template/blob/0864bd3fda3ae2b97362ae80691cd59ca9cf5090/src/interfaces/ICounter.sol)


## Functions
Expand Down
File renamed without changes.
10 changes: 9 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ out = "out"
libs = ["lib"]
optimizer = true
optimizer_runs = 999999
via_ir = false
via_ir = true
solc = "0.8.26"
verbosity = 2
ffi = true
Expand All @@ -19,6 +19,14 @@ remappings = [
"@openzeppelin/contracts-upgradeable=lib/openzeppelin-contracts-upgradeable/contracts"
]

additional_compiler_profiles = [
{ name = "test", via_ir = false }
]

compilation_restrictions = [
{ paths = "test/**", via_ir = false }
]

[profile.default.fuzz]
runs = 1000

Expand Down
9 changes: 6 additions & 3 deletions script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ pragma solidity 0.8.26;

import 'forge-std/Script.sol';

import {Counter} from 'src/Counter.sol';
import {ICounter} from '../src/interfaces/ICounter.sol';

contract Deploy is Script {
using stdJson for string;

function run() public returns (Counter) {
function run() public returns (ICounter counter) {
uint256 initialNumber = 5;
return new Counter(initialNumber);
bytes memory bytecode = abi.encodePacked(vm.getCode('Counter.sol:Counter'), abi.encode(initialNumber));
assembly {
counter := create(0, add(bytecode, 0x20), mload(bytecode))
}
}
}
25 changes: 17 additions & 8 deletions script/util/doc_gen.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

set -e
forge build
rm -rf docs/autogen
# generate docs
forge doc -b -o docs/autogen

Expand All @@ -9,12 +11,19 @@ files=$(git diff --name-only -- 'docs/autogen/*')

# Loop over each file
for file in $files; do
# Get the diff for the file, only lines that start with - or +
diff=$(git diff $file | grep '^[+-][^+-]')
# Check if there are any other changes in the diff besides the commit hash (in that case the file has more than 1 line that changed, one minus one plus)
if [[ $(echo "$diff" | wc -l) -eq 2 ]]; then
# If there are no other changes, discard the changes for the file
git reset HEAD $file
git checkout -- $file
# Check if the file exists
if [[ -f $file ]]; then
# Get the diff for the file, strip metadata and only keep lines that start with - or +
diff=$(git diff $file | sed '/^diff --git/d; /^index /d; /^--- /d; /^\+\+\+ /d; /^@@ /d' | grep '^[+-]')

# Filter lines that start with -[Git Source] or +[Git Source]
filtered_diff=$(echo "$diff" | grep '^\-\[Git Source\]\|^\+\[Git Source\]' || true)

# Compare the original diff with the filtered diff
if [[ "$diff" == "$filtered_diff" ]]; then
# If they are equal, discard the changes for the file
git reset HEAD $file
git checkout -- $file
fi
fi
done
2 changes: 1 addition & 1 deletion src/Counter.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.26;

import {ICounter} from './interface/ICounter.sol';
import {ICounter} from './interfaces/ICounter.sol';

contract Counter is ICounter {
uint256 public number;
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions test/Counter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ pragma solidity 0.8.26;
import {GasSnapshot} from 'forge-gas-snapshot/GasSnapshot.sol';
import 'forge-std/Test.sol';

import {CounterDeployer, ICounter} from './deployers/CounterDeployer.sol';
import {Deploy} from 'script/Deploy.s.sol';
import {Counter} from 'src/Counter.sol';

abstract contract Deployed is Test {
Counter counter;
abstract contract Deployed is CounterDeployer {
ICounter counter;

function setUp() public virtual {
uint256 initialNumber = 10;
counter = new Counter(initialNumber);
counter = deploy(initialNumber);
}
}

Expand Down Expand Up @@ -40,7 +40,7 @@ contract CounterTest_Deployed is Deployed, GasSnapshot {
}

contract DeploymentTest is Test {
Counter counter;
ICounter counter;

function setUp() public virtual {
counter = new Deploy().run();
Expand Down
14 changes: 14 additions & 0 deletions test/deployers/CounterDeployer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {ICounter} from '../../src/interfaces/ICounter.sol';
import {Test} from 'forge-std/Test.sol';

contract CounterDeployer is Test {
function deploy(uint256 initialNumber) internal returns (ICounter counter) {
bytes memory bytecode = abi.encodePacked(vm.getCode('Counter.sol:Counter'), abi.encode(initialNumber));
assembly {
counter := create(0, add(bytecode, 0x20), mload(bytecode))
}
}
}

0 comments on commit e5eff0c

Please sign in to comment.