diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b4462976..1e3356a39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ This changelog format is based on [Keep a Changelog](https://keepachangelog.com/ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased](https://github.com/eth-brownie/brownie) +- commented out pragma statements in contracts is now ignored upon compile ([#1552](https://github.com/eth-brownie/brownie/pull/1552)) ## [1.19.0](https://github.com/eth-brownie/brownie/tree/v1.19.0) - 2022-05-29 ### Added diff --git a/brownie/project/sources.py b/brownie/project/sources.py index 3377c72dd..ea53fd888 100644 --- a/brownie/project/sources.py +++ b/brownie/project/sources.py @@ -213,11 +213,13 @@ def get_pragma_spec(source: str, path: Optional[str] = None) -> NpmSpec: Returns: NpmSpec object """ - pragma_match = next(re.finditer(r"pragma +solidity([^;]*);", source), None) - if pragma_match is not None: - pragma_string = pragma_match.groups()[0] - pragma_string = " ".join(pragma_string.split()) - return NpmSpec(pragma_string) + matches = re.findall(r"(.*)pragma +solidity([^;]*);", source) + if len(matches) != 0 or matches is not None: + for match in matches: + if match[0].strip() != "": + continue + pragma_match = " ".join(match[1].split()) + return NpmSpec(pragma_match) if path: raise PragmaError(f"No version pragma in '{path}'") raise PragmaError("String does not contain a version pragma") diff --git a/tests/project/test_sources.py b/tests/project/test_sources.py index 42f4b9366..d0cee74bc 100644 --- a/tests/project/test_sources.py +++ b/tests/project/test_sources.py @@ -4,7 +4,7 @@ from semantic_version import NpmSpec from brownie import compile_source -from brownie.exceptions import NamespaceCollision +from brownie.exceptions import NamespaceCollision, PragmaError from brownie.project import sources MESSY_SOURCE = """ @@ -15,6 +15,37 @@ Foo{ enum E {a, b} struct S {bool b; }} library Bar2{}""" +GET_PRAGMA_SPEC_SOURCE__OneVersion = """ +// pragma solidity ^0.6.0; +// pragma solidity ^1.6.0; +pragma solidity ^4.2.0; +contract Foo{} interface Bar + {} enum Struct { Contract } +abstract contract Baz is Foo {} struct Interface { uint256 Abstract; +} library Potato{} pragma solidity ^0.6.0; contract Foo2 is +Foo{ enum E {a, b} struct S {bool b; +}} library Bar2{}""" + +GET_PRAGMA_SPEC_SOURCE__BetweenVersions = """ +// pragma solidity ^0.6.0; +// pragma solidity ^1.6.0; +pragma solidity >=0.4.22 <0.7.0; +contract Foo{} interface Bar + {} enum Struct { Contract } +abstract contract Baz is Foo {} struct Interface { uint256 Abstract; +} library Potato{} pragma solidity ^0.6.0; contract Foo2 is +Foo{ enum E {a, b} struct S {bool b; +}} library Bar2{}""" + +GET_PRAGMA_SPEC_SOURCE__NoVersion = """ +// No Pragma version +contract Foo{} interface Bar + {} enum Struct { Contract } +abstract contract Baz is Foo {} struct Interface { uint256 Abstract; +} library Potato{} pragma solidity ^0.6.0; contract Foo2 is +Foo{ enum E {a, b} struct S {bool b; +}} library Bar2{}""" + @pytest.fixture(scope="module") def sourceobj(solc5source): @@ -77,8 +108,19 @@ def test_load_messy_project(): assert list(project.keys()) == ["Bar2", "Foo", "Foo2", "Potato"] -def test_get_pragma_spec(): - assert sources.get_pragma_spec(MESSY_SOURCE) == NpmSpec(">=0.4.22 <0.7.0") +def test_get_pragma_spec__one_version(): + assert sources.get_pragma_spec(GET_PRAGMA_SPEC_SOURCE__OneVersion) == NpmSpec("^4.2.0") + + +def test_get_pragma_spec__between_versions(): + assert sources.get_pragma_spec(GET_PRAGMA_SPEC_SOURCE__BetweenVersions) == NpmSpec( + ">=0.4.22 <0.7.0" + ) + + +def test_get_pragma_spec__no_version(): + with pytest.raises(PragmaError): + sources.get_pragma_spec(GET_PRAGMA_SPEC_SOURCE__NoVersion) @pytest.mark.parametrize(