From afbb202ce8745bb1fa3af83d2985f9212ce8fa7e Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Thu, 5 Sep 2024 14:09:23 +0200 Subject: [PATCH 1/8] Add `output_format` kwarg and `detect_vyper_version_from_source` function - Also deletes the temporary file after compilation --- .gitignore | 3 +++ vvm/__init__.py | 2 +- vvm/main.py | 62 ++++++++++++++++++++++++++++++++++++------------- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index b6e4761..bd6ad26 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,6 @@ dmypy.json # Pyre type checker .pyre/ + +# PyCharm +.idea/ diff --git a/vvm/__init__.py b/vvm/__init__.py index 101c423..0c4ddd4 100644 --- a/vvm/__init__.py +++ b/vvm/__init__.py @@ -5,4 +5,4 @@ install_vyper, set_vyper_version, ) -from vvm.main import compile_files, compile_source, compile_standard, get_vyper_version +from vvm.main import compile_files, compile_source, compile_standard, get_vyper_version, detect_vyper_version_from_source diff --git a/vvm/main.py b/vvm/main.py index 8e6ea50..3ef7205 100644 --- a/vvm/main.py +++ b/vvm/main.py @@ -1,4 +1,5 @@ import json +import re import tempfile from pathlib import Path from typing import Any, Dict, List, Optional, Union @@ -9,6 +10,9 @@ from vvm.exceptions import VyperError from vvm.install import get_executable +# TODO: this currently doesn't work with version ranges or release candidates +VERSION_RE = re.compile(r"\s*#\s*(pragma\s+version|@version)\s+(\d+\.\d+\.\d+)") + def get_vyper_version() -> Version: """ @@ -23,13 +27,22 @@ def get_vyper_version() -> Version: return wrapper._get_vyper_version(vyper_binary) +def detect_vyper_version_from_source(source_code: str): + res = VERSION_RE.findall(source_code) + if len(res) < 1: + return None + # TODO: handle len(res) > 1 + return res[0][1] + + def compile_source( source: str, base_path: Union[Path, str] = None, evm_version: str = None, vyper_binary: Union[str, Path] = None, vyper_version: Version = None, -) -> Dict: + output_format: str = None, +) -> Dict | str: """ Compile a Vyper contract. @@ -51,25 +64,33 @@ def compile_source( vyper_version: Version, optional `vyper` version to use. If not given, the currently active version is used. Ignored if `vyper_binary` is also given. + output_format: str, optional + Output format of the compiler. See `vyper --help` for more information. Returns ------- Dict Compiler output. The source file name is given as ``. """ - source_path = tempfile.mkstemp(suffix=".vy", prefix="vyper-", text=True)[1] - with open(source_path, "w") as fp: - fp.write(source) + if vyper_version is None: + vyper_version = detect_vyper_version_from_source(source) - compiler_data = _compile( - vyper_binary=vyper_binary, - vyper_version=vyper_version, - source_files=[source_path], - base_path=base_path, - evm_version=evm_version, - ) + with tempfile.NamedTemporaryFile(suffix=".vy", prefix="vyper-") as source_file: + source_file.write(source.encode()) + source_file.flush() - return {"": list(compiler_data.values())[0]} + compiler_data = _compile( + vyper_binary=vyper_binary, + vyper_version=vyper_version, + source_files=[source_file.name], + base_path=base_path, + evm_version=evm_version, + output_format=output_format, + ) + + if output_format in ("combined_json", None): + return {"": list(compiler_data.values())[0]} + return compiler_data def compile_files( @@ -78,7 +99,8 @@ def compile_files( evm_version: str = None, vyper_binary: Union[str, Path] = None, vyper_version: Version = None, -) -> Dict: + output_format: str = None, +) -> Dict | str: """ Compile one or more Vyper source files. @@ -100,6 +122,8 @@ def compile_files( vyper_version: Version, optional `vyper` version to use. If not given, the currently active version is used. Ignored if `vyper_binary` is also given. + output_format: str, optional + Output format of the compiler. See `vyper --help` for more information. Returns ------- @@ -112,6 +136,7 @@ def compile_files( source_files=source_files, base_path=base_path, evm_version=evm_version, + output_format=output_format, ) @@ -119,17 +144,22 @@ def _compile( base_path: Union[str, Path, None], vyper_binary: Union[str, Path, None], vyper_version: Optional[Version], + output_format: Optional[str], **kwargs: Any, -) -> Dict: +) -> Dict | str: if vyper_binary is None: vyper_binary = get_executable(vyper_version) + if output_format is None: + output_format = "combined_json" stdoutdata, stderrdata, command, proc = wrapper.vyper_wrapper( - vyper_binary=vyper_binary, f="combined_json", p=base_path, **kwargs + vyper_binary=vyper_binary, f=output_format, p=base_path, **kwargs ) - return json.loads(stdoutdata) + if output_format in ("combined_json", "standard_json", "metadata"): + return json.loads(stdoutdata) + return stdoutdata def compile_standard( From faffd9b09858ff303b9f4d53006c090be835e097 Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Thu, 5 Sep 2024 16:16:18 +0200 Subject: [PATCH 2/8] Update tests & support RCs Add test for `output_format` and `detect_vyper_version_from_source` Fix tests for vyper 0.4 --- .github/workflows/main.yaml | 44 ++++++++++++++-------------- tests/conftest.py | 6 +++- tests/test_compile_source.py | 15 ++++++++++ tests/test_detect_vyper_version.py | 25 ++++++++++++++++ vvm/__init__.py | 8 ++++- vvm/main.py | 47 ++++++++++++++++++++---------- 6 files changed, 106 insertions(+), 39 deletions(-) create mode 100644 tests/test_detect_vyper_version.py diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index d4624c0..766f752 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -28,22 +28,22 @@ jobs: - name: Run Tox run: tox -e lint - py36: + py37: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: Setup Python 3.6 + - name: Setup Python 3.7 uses: actions/setup-python@v2 with: - python-version: 3.6 + python-version: 3.7 - name: Install Tox run: pip install tox wheel - name: Run Tox - run: tox -e py36 + run: tox -e py37 - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 @@ -51,22 +51,22 @@ jobs: file: ./coverage.xml fail_ci_if_error: true - py37: + py38: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: Setup Python 3.7 + - name: Setup Python 3.8 uses: actions/setup-python@v2 with: - python-version: 3.7 + python-version: 3.8 - name: Install Tox run: pip install tox wheel - name: Run Tox - run: tox -e py37 + run: tox -e py38 - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 @@ -74,22 +74,26 @@ jobs: file: ./coverage.xml fail_ci_if_error: true - py38: - runs-on: ubuntu-latest + py39: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [macos-latest, ubuntu-latest, windows-latest] steps: - uses: actions/checkout@v2 - - name: Setup Python 3.8 + - name: Setup Python 3.9 uses: actions/setup-python@v2 with: - python-version: 3.8 + python-version: 3.9 - name: Install Tox run: pip install tox wheel - name: Run Tox - run: tox -e py38 + run: tox -e py39 - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 @@ -97,26 +101,22 @@ jobs: file: ./coverage.xml fail_ci_if_error: true - py39: - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [macos-latest, ubuntu-latest, windows-latest] + py312: + runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: Setup Python 3.9 + - name: Setup Python 3.12 uses: actions/setup-python@v2 with: - python-version: 3.9 + python-version: 3.12 - name: Install Tox run: pip install tox wheel - name: Run Tox - run: tox -e py39 + run: tox -e py312 - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 diff --git a/tests/conftest.py b/tests/conftest.py index 098eec0..eb879ff 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -67,8 +67,12 @@ def all_versions(request): @pytest.fixture def foo_source(all_versions): visibility = "external" if all_versions >= Version("0.2.0") else "public" + interface = "IERC20" if all_versions >= Version("0.4.0a") else "ERC20" + import_path = "ethereum.ercs" if all_versions >= Version("0.4.0a") else "vyper.interfaces" + pragma_version = "pragma version" if all_versions >= Version("0.3.10") else "@version" yield f""" -from vyper.interfaces import ERC20 +#{pragma_version} ^{all_versions} +from {import_path} import {interface} @{visibility} def foo() -> int128: diff --git a/tests/test_compile_source.py b/tests/test_compile_source.py index c8f0cb5..c355a49 100644 --- a/tests/test_compile_source.py +++ b/tests/test_compile_source.py @@ -1,4 +1,5 @@ import pytest +from packaging.version import Version import vvm @@ -30,3 +31,17 @@ def foo() -> int128: return 42 """ vvm.compile_source(source, vyper_version=version_str) + + +def test_compile_metadata(foo_source, all_versions): + if all_versions <= Version("0.3.1"): + raise pytest.skip("metadata output not supported in vyper < 0.3.2") + output = vvm.compile_source(foo_source, output_format="metadata") + assert "function_info" in output + + +def test_compile_metadata_from_file(foo_path, all_versions): + if all_versions <= Version("0.3.1"): + raise pytest.skip("metadata output not supported in vyper < 0.3.2") + output = vvm.compile_files([foo_path], output_format="metadata") + assert "function_info" in output diff --git a/tests/test_detect_vyper_version.py b/tests/test_detect_vyper_version.py new file mode 100644 index 0000000..b60230a --- /dev/null +++ b/tests/test_detect_vyper_version.py @@ -0,0 +1,25 @@ +import pytest + +from vvm import detect_vyper_version_from_source + + +def test_detect_vyper_version_from_source(foo_source, all_versions): + assert detect_vyper_version_from_source(foo_source) == str(all_versions) + +@pytest.mark.parametrize( + "version_str,decorator", + [ + ("0.1.0b17", "public"), + ("0.3.0-beta17", "external"), + ("0.4.0rc6", "external"), + ], +) +def test_detect_vyper_version_beta(version_str, decorator): + source = f""" +# @version {version_str} + +@{decorator} +def foo() -> int128: + return 42 + """ + assert detect_vyper_version_from_source(source) == version_str diff --git a/vvm/__init__.py b/vvm/__init__.py index 0c4ddd4..a8d3d10 100644 --- a/vvm/__init__.py +++ b/vvm/__init__.py @@ -5,4 +5,10 @@ install_vyper, set_vyper_version, ) -from vvm.main import compile_files, compile_source, compile_standard, get_vyper_version, detect_vyper_version_from_source +from vvm.main import ( + compile_files, + compile_source, + compile_standard, + get_vyper_version, + detect_vyper_version_from_source, +) diff --git a/vvm/main.py b/vvm/main.py index 3ef7205..1ed5fb2 100644 --- a/vvm/main.py +++ b/vvm/main.py @@ -4,14 +4,13 @@ from pathlib import Path from typing import Any, Dict, List, Optional, Union -from packaging.version import Version +from packaging.version import Version, InvalidVersion from vvm import wrapper from vvm.exceptions import VyperError from vvm.install import get_executable -# TODO: this currently doesn't work with version ranges or release candidates -VERSION_RE = re.compile(r"\s*#\s*(pragma\s+version|@version)\s+(\d+\.\d+\.\d+)") +VERSION_RE = re.compile(r"\s*#\s*(?:pragma\s+|@)version\s+[=><^]?(\d+\.\d+\.\d+\S*)") def get_vyper_version() -> Version: @@ -27,12 +26,28 @@ def get_vyper_version() -> Version: return wrapper._get_vyper_version(vyper_binary) -def detect_vyper_version_from_source(source_code: str): - res = VERSION_RE.findall(source_code) - if len(res) < 1: +def detect_vyper_version_from_source(source_code: str) -> Optional[str]: + """ + Detect the version given by the pragma version in the source code. + TODO: when the user has a range, we should compare to the installed versions + + Arguments + --------- + source_code : str + Source code to detect the version from. + + Returns + ------- + str + vyper version, or None if no version could be detected. + """ + try: + finditer = VERSION_RE.finditer(source_code) + version_str = next(finditer).group(1) + Version(version_str) # validate the version + return version_str + except (StopIteration, InvalidVersion): return None - # TODO: handle len(res) > 1 - return res[0][1] def compile_source( @@ -42,7 +57,7 @@ def compile_source( vyper_binary: Union[str, Path] = None, vyper_version: Version = None, output_format: str = None, -) -> Dict | str: +) -> Any: """ Compile a Vyper contract. @@ -69,8 +84,9 @@ def compile_source( Returns ------- - Dict - Compiler output. The source file name is given as ``. + Any + Compiler output (depends on `output_format`). + For JSON output the return type is a dictionary, otherwise it is a string. """ if vyper_version is None: vyper_version = detect_vyper_version_from_source(source) @@ -100,7 +116,7 @@ def compile_files( vyper_binary: Union[str, Path] = None, vyper_version: Version = None, output_format: str = None, -) -> Dict | str: +) -> Any: """ Compile one or more Vyper source files. @@ -127,8 +143,9 @@ def compile_files( Returns ------- - Dict - Compiler output + Any + Compiler output (depends on `output_format`). + For JSON output the return type is a dictionary, otherwise it is a string. """ return _compile( vyper_binary=vyper_binary, @@ -146,7 +163,7 @@ def _compile( vyper_version: Optional[Version], output_format: Optional[str], **kwargs: Any, -) -> Dict | str: +) -> Any: if vyper_binary is None: vyper_binary = get_executable(vyper_version) From 9846c5b2c42bb11f71c0d1659bc748b18dc401f1 Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Thu, 5 Sep 2024 16:19:27 +0200 Subject: [PATCH 3/8] Linting --- tests/conftest.py | 2 +- tests/test_detect_vyper_version.py | 1 + vvm/__init__.py | 2 +- vvm/main.py | 8 ++++---- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index eb879ff..22226e1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,8 +1,8 @@ #!/usr/bin/python3 import pytest -from requests import ConnectionError from packaging.version import Version +from requests import ConnectionError import vvm diff --git a/tests/test_detect_vyper_version.py b/tests/test_detect_vyper_version.py index b60230a..8bb4414 100644 --- a/tests/test_detect_vyper_version.py +++ b/tests/test_detect_vyper_version.py @@ -6,6 +6,7 @@ def test_detect_vyper_version_from_source(foo_source, all_versions): assert detect_vyper_version_from_source(foo_source) == str(all_versions) + @pytest.mark.parametrize( "version_str,decorator", [ diff --git a/vvm/__init__.py b/vvm/__init__.py index a8d3d10..59a037a 100644 --- a/vvm/__init__.py +++ b/vvm/__init__.py @@ -9,6 +9,6 @@ compile_files, compile_source, compile_standard, - get_vyper_version, detect_vyper_version_from_source, + get_vyper_version, ) diff --git a/vvm/main.py b/vvm/main.py index 1ed5fb2..76e15cc 100644 --- a/vvm/main.py +++ b/vvm/main.py @@ -4,7 +4,7 @@ from pathlib import Path from typing import Any, Dict, List, Optional, Union -from packaging.version import Version, InvalidVersion +from packaging.version import InvalidVersion, Version from vvm import wrapper from vvm.exceptions import VyperError @@ -55,7 +55,7 @@ def compile_source( base_path: Union[Path, str] = None, evm_version: str = None, vyper_binary: Union[str, Path] = None, - vyper_version: Version = None, + vyper_version: Union[str, Version, None] = None, output_format: str = None, ) -> Any: """ @@ -114,7 +114,7 @@ def compile_files( base_path: Union[Path, str] = None, evm_version: str = None, vyper_binary: Union[str, Path] = None, - vyper_version: Version = None, + vyper_version: Union[str, Version, None] = None, output_format: str = None, ) -> Any: """ @@ -160,7 +160,7 @@ def compile_files( def _compile( base_path: Union[str, Path, None], vyper_binary: Union[str, Path, None], - vyper_version: Optional[Version], + vyper_version: Union[str, Version, None], output_format: Optional[str], **kwargs: Any, ) -> Any: From bf9336f7f53a8067fbe29fd4677f040e99038061 Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Thu, 5 Sep 2024 16:20:36 +0200 Subject: [PATCH 4/8] Revert workflow changes --- .github/workflows/main.yaml | 44 ++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 766f752..d4624c0 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -28,22 +28,22 @@ jobs: - name: Run Tox run: tox -e lint - py37: + py36: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: Setup Python 3.7 + - name: Setup Python 3.6 uses: actions/setup-python@v2 with: - python-version: 3.7 + python-version: 3.6 - name: Install Tox run: pip install tox wheel - name: Run Tox - run: tox -e py37 + run: tox -e py36 - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 @@ -51,22 +51,22 @@ jobs: file: ./coverage.xml fail_ci_if_error: true - py38: + py37: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: Setup Python 3.8 + - name: Setup Python 3.7 uses: actions/setup-python@v2 with: - python-version: 3.8 + python-version: 3.7 - name: Install Tox run: pip install tox wheel - name: Run Tox - run: tox -e py38 + run: tox -e py37 - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 @@ -74,26 +74,22 @@ jobs: file: ./coverage.xml fail_ci_if_error: true - py39: - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [macos-latest, ubuntu-latest, windows-latest] + py38: + runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: Setup Python 3.9 + - name: Setup Python 3.8 uses: actions/setup-python@v2 with: - python-version: 3.9 + python-version: 3.8 - name: Install Tox run: pip install tox wheel - name: Run Tox - run: tox -e py39 + run: tox -e py38 - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 @@ -101,22 +97,26 @@ jobs: file: ./coverage.xml fail_ci_if_error: true - py312: - runs-on: ubuntu-latest + py39: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [macos-latest, ubuntu-latest, windows-latest] steps: - uses: actions/checkout@v2 - - name: Setup Python 3.12 + - name: Setup Python 3.9 uses: actions/setup-python@v2 with: - python-version: 3.12 + python-version: 3.9 - name: Install Tox run: pip install tox wheel - name: Run Tox - run: tox -e py312 + run: tox -e py39 - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 From 9c24ef97d53f030c9ee869ed2241266e548570e0 Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Fri, 6 Sep 2024 14:44:33 +0200 Subject: [PATCH 5/8] Remove detect_vyper_version_from_source --- tests/test_detect_vyper_version.py | 26 -------------------------- vvm/__init__.py | 8 +------- vvm/main.py | 26 -------------------------- 3 files changed, 1 insertion(+), 59 deletions(-) delete mode 100644 tests/test_detect_vyper_version.py diff --git a/tests/test_detect_vyper_version.py b/tests/test_detect_vyper_version.py deleted file mode 100644 index 8bb4414..0000000 --- a/tests/test_detect_vyper_version.py +++ /dev/null @@ -1,26 +0,0 @@ -import pytest - -from vvm import detect_vyper_version_from_source - - -def test_detect_vyper_version_from_source(foo_source, all_versions): - assert detect_vyper_version_from_source(foo_source) == str(all_versions) - - -@pytest.mark.parametrize( - "version_str,decorator", - [ - ("0.1.0b17", "public"), - ("0.3.0-beta17", "external"), - ("0.4.0rc6", "external"), - ], -) -def test_detect_vyper_version_beta(version_str, decorator): - source = f""" -# @version {version_str} - -@{decorator} -def foo() -> int128: - return 42 - """ - assert detect_vyper_version_from_source(source) == version_str diff --git a/vvm/__init__.py b/vvm/__init__.py index 59a037a..101c423 100644 --- a/vvm/__init__.py +++ b/vvm/__init__.py @@ -5,10 +5,4 @@ install_vyper, set_vyper_version, ) -from vvm.main import ( - compile_files, - compile_source, - compile_standard, - detect_vyper_version_from_source, - get_vyper_version, -) +from vvm.main import compile_files, compile_source, compile_standard, get_vyper_version diff --git a/vvm/main.py b/vvm/main.py index 76e15cc..d1940c0 100644 --- a/vvm/main.py +++ b/vvm/main.py @@ -26,30 +26,6 @@ def get_vyper_version() -> Version: return wrapper._get_vyper_version(vyper_binary) -def detect_vyper_version_from_source(source_code: str) -> Optional[str]: - """ - Detect the version given by the pragma version in the source code. - TODO: when the user has a range, we should compare to the installed versions - - Arguments - --------- - source_code : str - Source code to detect the version from. - - Returns - ------- - str - vyper version, or None if no version could be detected. - """ - try: - finditer = VERSION_RE.finditer(source_code) - version_str = next(finditer).group(1) - Version(version_str) # validate the version - return version_str - except (StopIteration, InvalidVersion): - return None - - def compile_source( source: str, base_path: Union[Path, str] = None, @@ -88,8 +64,6 @@ def compile_source( Compiler output (depends on `output_format`). For JSON output the return type is a dictionary, otherwise it is a string. """ - if vyper_version is None: - vyper_version = detect_vyper_version_from_source(source) with tempfile.NamedTemporaryFile(suffix=".vy", prefix="vyper-") as source_file: source_file.write(source.encode()) From 0aeff97fc442f6172fb90ea522645dbd41eb3a94 Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Fri, 6 Sep 2024 14:51:54 +0200 Subject: [PATCH 6/8] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c0650f..7371037 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/vyperlang/vvm/) ### Changed - Update contact information in `CONTRIBUTING.md` +- Add `output_format` argument to `compile_source` and `compile_files` ([#21](https://github.com/vyperlang/vvm/pull/21)) ## [0.1.0](https://github.com/vyperlang/vvm/tree/v0.1.0) - 2020-10-07 ### Added From b7e96ac520b98ed3a23d524e8973cb8f3f280a49 Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Fri, 6 Sep 2024 14:53:07 +0200 Subject: [PATCH 7/8] Unused constant --- vvm/main.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/vvm/main.py b/vvm/main.py index d1940c0..29aed4a 100644 --- a/vvm/main.py +++ b/vvm/main.py @@ -1,17 +1,14 @@ import json -import re import tempfile from pathlib import Path from typing import Any, Dict, List, Optional, Union -from packaging.version import InvalidVersion, Version +from packaging.version import Version from vvm import wrapper from vvm.exceptions import VyperError from vvm.install import get_executable -VERSION_RE = re.compile(r"\s*#\s*(?:pragma\s+|@)version\s+[=><^]?(\d+\.\d+\.\d+\S*)") - def get_vyper_version() -> Version: """ From 61f3d9743fe0fa5cb9eda05afe6e06eb01d0af76 Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Fri, 6 Sep 2024 15:09:49 +0200 Subject: [PATCH 8/8] Rename fixture --- tests/conftest.py | 26 +++++++++++++------------- tests/test_compile_source.py | 8 ++++---- tests/test_install.py | 6 +++--- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 22226e1..677ac67 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -43,19 +43,19 @@ def pytest_collection(session): vvm.install_vyper(version) -# auto-parametrize the all_versions fixture with all target vyper versions +# auto-parametrize the vyper_version fixture with all target vyper versions def pytest_generate_tests(metafunc): - if "all_versions" in metafunc.fixturenames: + if "vyper_version" in metafunc.fixturenames: versions = VERSIONS.copy() for marker in metafunc.definition.iter_markers(name="min_vyper"): versions = [i for i in versions if i >= Version(marker.args[0])] for marker in metafunc.definition.iter_markers(name="max_vyper"): versions = [i for i in versions if i <= Version(marker.args[0])] - metafunc.parametrize("all_versions", versions, indirect=True) + metafunc.parametrize("vyper_version", versions, indirect=True) @pytest.fixture -def all_versions(request): +def vyper_version(request): """ Run a test against all vyper versions. """ @@ -65,13 +65,13 @@ def all_versions(request): @pytest.fixture -def foo_source(all_versions): - visibility = "external" if all_versions >= Version("0.2.0") else "public" - interface = "IERC20" if all_versions >= Version("0.4.0a") else "ERC20" - import_path = "ethereum.ercs" if all_versions >= Version("0.4.0a") else "vyper.interfaces" - pragma_version = "pragma version" if all_versions >= Version("0.3.10") else "@version" +def foo_source(vyper_version): + visibility = "external" if vyper_version >= Version("0.2.0") else "public" + interface = "IERC20" if vyper_version >= Version("0.4.0a") else "ERC20" + import_path = "ethereum.ercs" if vyper_version >= Version("0.4.0a") else "vyper.interfaces" + pragma_version = "pragma version" if vyper_version >= Version("0.3.10") else "@version" yield f""" -#{pragma_version} ^{all_versions} +#{pragma_version} ^{vyper_version} from {import_path} import {interface} @{visibility} @@ -81,8 +81,8 @@ def foo() -> int128: @pytest.fixture -def foo_path(tmp_path_factory, foo_source, all_versions): - source = tmp_path_factory.getbasetemp().joinpath(f"Foo-{all_versions}.sol") +def foo_path(tmp_path_factory, foo_source, vyper_version): + source = tmp_path_factory.getbasetemp().joinpath(f"Foo-{vyper_version}.sol") if not source.exists(): with source.open("w") as fp: fp.write(foo_source) @@ -90,7 +90,7 @@ def foo_path(tmp_path_factory, foo_source, all_versions): @pytest.fixture -def input_json(all_versions): +def input_json(vyper_version): json = { "language": "Vyper", "sources": {}, diff --git a/tests/test_compile_source.py b/tests/test_compile_source.py index c355a49..643d42d 100644 --- a/tests/test_compile_source.py +++ b/tests/test_compile_source.py @@ -33,15 +33,15 @@ def foo() -> int128: vvm.compile_source(source, vyper_version=version_str) -def test_compile_metadata(foo_source, all_versions): - if all_versions <= Version("0.3.1"): +def test_compile_metadata(foo_source, vyper_version): + if vyper_version <= Version("0.3.1"): raise pytest.skip("metadata output not supported in vyper < 0.3.2") output = vvm.compile_source(foo_source, output_format="metadata") assert "function_info" in output -def test_compile_metadata_from_file(foo_path, all_versions): - if all_versions <= Version("0.3.1"): +def test_compile_metadata_from_file(foo_path, vyper_version): + if vyper_version <= Version("0.3.1"): raise pytest.skip("metadata output not supported in vyper < 0.3.2") output = vvm.compile_files([foo_path], output_format="metadata") assert "function_info" in output diff --git a/tests/test_install.py b/tests/test_install.py index 21dea44..719104a 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -1,6 +1,6 @@ import vvm -def test_get_installed_vyper_versions(all_versions): - assert "exe" not in str(all_versions) - assert all_versions in vvm.install.get_installed_vyper_versions() +def test_get_installed_vyper_versions(vyper_version): + assert "exe" not in str(vyper_version) + assert vyper_version in vvm.install.get_installed_vyper_versions()