diff --git a/.github/workflows/mypy.yml b/.github/workflows/mypy.yml index 3f2fedb..a068079 100644 --- a/.github/workflows/mypy.yml +++ b/.github/workflows/mypy.yml @@ -8,7 +8,7 @@ defaults: on: pull_request: - branches: [master, dev] + branches: [main, dev] concurrency: group: ${{ github.workflow }}-${{ github.ref }} diff --git a/pyproject.toml b/pyproject.toml index 0a3e97c..233315d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 diff --git a/test_generator/main.py b/test_generator/main.py index 3a807f5..326916e 100644 --- a/test_generator/main.py +++ b/test_generator/main.py @@ -34,10 +34,11 @@ 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: @@ -45,7 +46,7 @@ def _get_target_contract(self) -> Contract: 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""" @@ -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." ) @@ -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..." diff --git a/test_generator/utils/encoding.py b/test_generator/utils/encoding.py index b086894..13cfef1 100644 --- a/test_generator/utils/encoding.py +++ b/test_generator/utils/encoding.py @@ -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() diff --git a/tests/conftest.py b/tests/conftest.py index 0ec54e9..a954400 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,3 +1,4 @@ +""" Globally available fixtures""" import os import pytest from slither import Slither @@ -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) @@ -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 @@ -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" @@ -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" @@ -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" @@ -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" diff --git a/tests/test_types_echidna.py b/tests/test_types_echidna.py index 6723903..f47e16a 100644 --- a/tests/test_types_echidna.py +++ b/tests/test_types_echidna.py @@ -1,4 +1,4 @@ -""" BasicTypes tests""" +""" Tests for generating compilable test files from an Echidna corpus""" from pathlib import Path import os import re @@ -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") @@ -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") @@ -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") @@ -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") diff --git a/tests/test_types_medusa.py b/tests/test_types_medusa.py index 5126716..9c5af9f 100644 --- a/tests/test_types_medusa.py +++ b/tests/test_types_medusa.py @@ -1,4 +1,4 @@ -""" BasicTypes tests""" +""" Tests for generating compilable test files from an Medusa corpus""" from pathlib import Path import os import re @@ -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") @@ -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") @@ -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") @@ -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")