Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix get_pragma_spec() to exclude commented out pragma statements on compile #1552

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions brownie/project/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
48 changes: 45 additions & 3 deletions tests/project/test_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand All @@ -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):
Expand Down Expand Up @@ -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(
Expand Down