Skip to content

Commit

Permalink
fix linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tuturu-tech committed Jan 11, 2024
1 parent 1cae612 commit 7e9d568
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defaults:

on:
pull_request:
branches: [master, dev]
branches: [main, dev]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ logging-fstring-interpolation,
logging-not-lazy,
duplicate-code,
import-error,
unsubscriptable-object
unsubscriptable-object,
too-many-arguments
"""
[tool.mypy]
warn_incomplete_stub = true
Expand Down
10 changes: 6 additions & 4 deletions test_generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@ def __init__(
self.corpus_path = corpus_path
self.test_dir = test_dir
self.slither = slither
self.target = self._get_target_contract()
self.target = self.get_target_contract()
self.fuzzer = fuzzer

def _get_target_contract(self) -> Contract:
def get_target_contract(self) -> Contract:
""" Gets the Slither Contract object for the specified contract file"""
contracts = self.slither.get_contract_from_name(self.target_name)
# Loop in case slither fetches multiple contracts for some reason (e.g., similar names?)
for contract in contracts:
if contract.name == self.target_name:
return contract

# TODO throw error if no contract found
exit(-1)
sys.exit(-1)

def create_poc(self) -> str:
"""Takes in a directory path to the echidna reproducers and generates a test file"""
Expand Down Expand Up @@ -85,6 +86,7 @@ def create_poc(self) -> str:


def main() -> None:
""" The main entry point """
parser = argparse.ArgumentParser(
prog="test-generator", description="Generate test harnesses for Echidna failed properties."
)
Expand Down Expand Up @@ -134,7 +136,7 @@ def main() -> None:
fuzzer = Medusa(target_contract, corpus_dir, slither)
case _:
# TODO create a descriptive error
exit(-1)
sys.exit(-1)

CryticPrint().print_information(
f"Generating Foundry unit tests based on the {fuzzer.name} reproducers..."
Expand Down
1 change: 1 addition & 0 deletions test_generator/utils/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ def octal_to_byte(match):


def parse_medusa_byte_string(s: str) -> str:
""" Decode bytes* or string type from Medusa format to Solidity hex literal"""
return s.encode("utf-8").hex()
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
""" Globally available fixtures"""
import os
import pytest
from slither import Slither
Expand All @@ -7,6 +8,7 @@


class TestGenerator:
""" Helper class for testing all fuzzers with the tool"""
def __init__(self, target, target_path, corpus_dir):
slither = Slither(target_path)
echidna = Echidna(target, f"echidna-corpora/{corpus_dir}", slither)
Expand All @@ -19,14 +21,17 @@ def __init__(self, target, target_path, corpus_dir):
)

def echidna_generate_tests(self):
""" Runs the test-generator tool for an Echidna corpus"""
self.echidna_generator.create_poc()

def medusa_generate_tests(self):
""" Runs the test-generator tool for a Medusa corpus"""
self.medusa_generator.create_poc()


@pytest.fixture(autouse=True)
def change_test_dir(request, monkeypatch):
""" Helper fixture to change the working directory"""
# Directory of the test file
test_dir = request.fspath.dirname

Expand All @@ -39,6 +44,7 @@ def change_test_dir(request, monkeypatch):

@pytest.fixture
def basic_types():
""" Fixture for the BasicTypes test contract"""
target = "BasicTypes"
target_path = "./src/BasicTypes.sol"
corpus_dir = "corpus-basic"
Expand All @@ -48,6 +54,7 @@ def basic_types():

@pytest.fixture
def fixed_size_arrays():
""" Fixture for the FixedArrays test contract"""
target = "FixedArrays"
target_path = "./src/FixedArrays.sol"
corpus_dir = "corpus-fixed-arr"
Expand All @@ -57,6 +64,7 @@ def fixed_size_arrays():

@pytest.fixture
def dynamic_arrays():
""" Fixture for the DynamicArrays test contract"""
target = "DynamicArrays"
target_path = "./src/DynamicArrays.sol"
corpus_dir = "corpus-dyn-arr"
Expand All @@ -66,6 +74,7 @@ def dynamic_arrays():

@pytest.fixture
def structs_and_enums():
""" Fixture for the TupleTypes test contract"""
target = "TupleTypes"
target_path = "./src/TupleTypes.sol"
corpus_dir = "corpus-struct"
Expand Down
6 changes: 5 additions & 1 deletion tests/test_types_echidna.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" BasicTypes tests"""
""" Tests for generating compilable test files from an Echidna corpus"""
from pathlib import Path
import os
import re
Expand All @@ -9,6 +9,7 @@


def test_echidna_basic_types(basic_types):
""" Tests the BasicTypes contract with an Echidna corpus"""
basic_types.echidna_generate_tests()
# Ensure the file was created
path = os.path.join(os.getcwd(), "test", "BasicTypes_Echidna_Test.t.sol")
Expand Down Expand Up @@ -40,6 +41,7 @@ def test_echidna_basic_types(basic_types):


def test_echidna_fixed_array_types(fixed_size_arrays):
""" Tests the FixedArrays contract with an Echidna corpus"""
fixed_size_arrays.echidna_generate_tests()
# Ensure the file was created
path = os.path.join(os.getcwd(), "test", "FixedArrays_Echidna_Test.t.sol")
Expand Down Expand Up @@ -71,6 +73,7 @@ def test_echidna_fixed_array_types(fixed_size_arrays):


def test_echidna_dynamic_array_types(dynamic_arrays):
""" Tests the DynamicArrays contract with an Echidna corpus"""
dynamic_arrays.echidna_generate_tests()
# Ensure the file was created
path = os.path.join(os.getcwd(), "test", "DynamicArrays_Echidna_Test.t.sol")
Expand Down Expand Up @@ -102,6 +105,7 @@ def test_echidna_dynamic_array_types(dynamic_arrays):


def test_echidna_structs_and_enums(structs_and_enums):
""" Tests the TupleTypes contract with an Echidna corpus"""
structs_and_enums.echidna_generate_tests()
# Ensure the file was created
path = os.path.join(os.getcwd(), "test", "TupleTypes_Echidna_Test.t.sol")
Expand Down
6 changes: 5 additions & 1 deletion tests/test_types_medusa.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" BasicTypes tests"""
""" Tests for generating compilable test files from an Medusa corpus"""
from pathlib import Path
import os
import re
Expand All @@ -9,6 +9,7 @@


def test_medusa_basic_types(basic_types):
""" Tests the BasicTypes contract with a Medusa corpus"""
basic_types.medusa_generate_tests()
# Ensure the file was created
path = os.path.join(os.getcwd(), "test", "BasicTypes_Medusa_Test.t.sol")
Expand Down Expand Up @@ -40,6 +41,7 @@ def test_medusa_basic_types(basic_types):


def test_medusa_fixed_array_types(fixed_size_arrays):
""" Tests the FixedArrays contract with a Medusa corpus"""
fixed_size_arrays.medusa_generate_tests()
# Ensure the file was created
path = os.path.join(os.getcwd(), "test", "FixedArrays_Medusa_Test.t.sol")
Expand Down Expand Up @@ -71,6 +73,7 @@ def test_medusa_fixed_array_types(fixed_size_arrays):


def test_medusa_dynamic_array_types(dynamic_arrays):
""" Tests the DynamicArrays contract with a Medusa corpus"""
dynamic_arrays.medusa_generate_tests()
# Ensure the file was created
path = os.path.join(os.getcwd(), "test", "DynamicArrays_Medusa_Test.t.sol")
Expand Down Expand Up @@ -102,6 +105,7 @@ def test_medusa_dynamic_array_types(dynamic_arrays):


def test_medusa_structs_and_enums(structs_and_enums):
""" Tests the TupleTypes contract with a Medusa corpus"""
structs_and_enums.medusa_generate_tests()
# Ensure the file was created
path = os.path.join(os.getcwd(), "test", "TupleTypes_Medusa_Test.t.sol")
Expand Down

0 comments on commit 7e9d568

Please sign in to comment.