Skip to content

Commit

Permalink
Rework - Move conftest const in their file and adapt import in test f…
Browse files Browse the repository at this point in the history
…iles (#197)

* rework: move conftest const in their file amd adapt import in test files

    - Moving constants inside their proper file under tests folder -> avoid import `tests.conftest` and keep it as fixture file
    - Adapt import from `tests.conftest` to `tests.constants` with constant uppercase naming -> adapted
    - Fix `moccasin.toml` explorer_uri for live testing

* feat: update lock file

---------

Co-authored-by: Patrick Collins <[email protected]>
  • Loading branch information
s3bc40 and PatrickAlphaC authored Feb 2, 2025
1 parent f8990bd commit 302407d
Show file tree
Hide file tree
Showing 16 changed files with 747 additions and 675 deletions.
5 changes: 1 addition & 4 deletions tests/cli/deployments/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import pytest

from moccasin.config import Config, _set_global_config

DEPLOYMENTS_PROJECT_PATH = Path(__file__).parent.parent.parent.joinpath(
"data/deployments_project/"
)
from tests.constants import DEPLOYMENTS_PROJECT_PATH


# ------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/test_cli_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import subprocess
from pathlib import Path

from tests.conftest import (
from tests.constants import (
ANVIL1_KEYSTORE_NAME,
ANVIL1_KEYSTORE_PASSWORD,
ANVIL1_PRIVATE_KEY,
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/test_cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import subprocess
from pathlib import Path

from tests.conftest import COMPLEX_PROJECT_PATH
from tests.constants import COMPLEX_PROJECT_PATH

EXPECTED_HELP_TEXT = "Runs pytest"

Expand Down
2 changes: 1 addition & 1 deletion tests/cli/test_cli_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import subprocess
from pathlib import Path

from tests.conftest import COMPLEX_PROJECT_PATH
from tests.constants import COMPLEX_PROJECT_PATH


def test_run_help(mox_path):
Expand Down
64 changes: 15 additions & 49 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,20 @@
from moccasin.commands.wallet import save_to_keystores
from moccasin.config import Config, _set_global_config
from moccasin.constants.vars import DEPENDENCIES_FOLDER
from tests.utils.anvil import ANVIL_URL, AnvilProcess

COMPLEX_PROJECT_PATH = Path(__file__).parent.joinpath("data/complex_project/")
INSTALL_PROJECT_PATH = Path(__file__).parent.joinpath("data/installation_project/")
PURGE_PROJECT_PATH = Path(__file__).parent.joinpath("data/purge_project/")
ZKSYNC_PROJECT_PATH = Path(__file__).parent.joinpath("data/zksync_project/")
NO_CONFIG_PROJECT_PATH = Path(__file__).parent.joinpath("data/no_config_project/")
TESTS_CONFIG_PROJECT_PATH = Path(__file__).parent.joinpath("data/tests_project/")
ANVIL1_PRIVATE_KEY = (
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
)
ANVIL1_KEYSTORE_NAME = "anvil1"
ANVIL1_KEYSTORE_PASSWORD = "password"
ANVIL_STORED_STATE_PATH = Path(__file__).parent.joinpath("data/anvil_data/state.json")
ANVIL_STORED_KEYSTORE_PATH = Path(__file__).parent.joinpath(
"data/anvil_data/anvil1.json"
from tests.constants import (
ANVIL1_KEYSTORE_NAME,
ANVIL1_KEYSTORE_PASSWORD,
ANVIL1_PRIVATE_KEY,
ANVIL_STORED_STATE_PATH,
COMPLEX_PROJECT_PATH,
INSTALL_PROJECT_PATH,
INSTALLATION_STARTING_TOML,
NO_CONFIG_PROJECT_PATH,
PURGE_PROJECT_PATH,
PURGE_STARTING_TOML,
TESTS_CONFIG_PROJECT_PATH,
)

INSTALLATION_STARTING_TOML = """[project]
dependencies = ["snekmate", "moccasin"]
# PRESERVE COMMENTS
[networks.sepolia]
url = "https://ethereum-sepolia-rpc.publicnode.com"
chain_id = 11155111
save_to_db = false
"""

PURGE_STARTING_TOML = """[project]
dependencies = ["snekmate", "patrickalphac/test_repo"]
# PRESERVE COMMENTS
[networks.sepolia]
url = "https://ethereum-sepolia-rpc.publicnode.com"
chain_id = 11155111
"""

pip_package_name = "snekmate"
org_name = "pcaversaccio"
github_package_name = f"{org_name}/{pip_package_name}"
version = "0.1.0"
new_version = "0.0.5"
comment_content = "PRESERVE COMMENTS"
patrick_org_name = "patrickalphac"
patrick_repo_name = "test_repo"
patrick_package_name = f"{patrick_org_name}/{patrick_repo_name}"
from tests.utils.anvil import ANVIL_URL, AnvilProcess


# ------------------------------------------------------------------
Expand Down Expand Up @@ -238,7 +204,7 @@ def purge_reset_dependencies(purge_temp_path):
# NO CONFIG
# ------------------------------------------------------------------
@pytest.fixture(scope="module")
def no_config_temp_path():
def no_config_temp_path() -> Generator[Path, None, None]:
with tempfile.TemporaryDirectory() as temp_dir:
shutil.copytree(
NO_CONFIG_PROJECT_PATH, os.path.join(temp_dir), dirs_exist_ok=True
Expand All @@ -258,7 +224,7 @@ def no_config_config(no_config_temp_path) -> Config:
# TEST TEST
# ------------------------------------------------------------------
@pytest.fixture(scope="module")
def test_config_temp_path():
def test_config_temp_path() -> Generator[Path, None, None]:
with tempfile.TemporaryDirectory() as temp_dir:
shutil.copytree(
TESTS_CONFIG_PROJECT_PATH, os.path.join(temp_dir), dirs_exist_ok=True
Expand Down
64 changes: 64 additions & 0 deletions tests/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from pathlib import Path

# ------------------------------------------------------------------
# TEST PROJECT PATH
# ------------------------------------------------------------------
COMPLEX_PROJECT_PATH = Path(__file__).parent.joinpath("data/complex_project/")
DEPLOYMENTS_PROJECT_PATH = Path(__file__).parent.joinpath("data/deployments_project/")
INSTALL_PROJECT_PATH = Path(__file__).parent.joinpath("data/installation_project/")
NO_CONFIG_PROJECT_PATH = Path(__file__).parent.joinpath("data/no_config_project/")
PURGE_PROJECT_PATH = Path(__file__).parent.joinpath("data/purge_project/")
TESTS_CONFIG_PROJECT_PATH = Path(__file__).parent.joinpath("data/tests_project/")
ZKSYNC_PROJECT_PATH = Path(__file__).parent.joinpath("data/zksync_project/")

# ------------------------------------------------------------------
# TEST ANVIL
# ------------------------------------------------------------------
ANVIL1_PRIVATE_KEY = (
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
)
ANVIL1_KEYSTORE_NAME = "anvil1"
ANVIL1_KEYSTORE_PASSWORD = "password"
ANVIL_STORED_STATE_PATH = Path(__file__).parent.joinpath("data/anvil_data/state.json")
ANVIL_STORED_KEYSTORE_PATH = Path(__file__).parent.joinpath(
"data/anvil_data/anvil1.json"
)


# ------------------------------------------------------------------
# TEST TOML
# ------------------------------------------------------------------
INSTALLATION_STARTING_TOML = """[project]
dependencies = ["snekmate", "moccasin"]
# PRESERVE COMMENTS
[networks.sepolia]
url = "https://ethereum-sepolia-rpc.publicnode.com"
chain_id = 11155111
save_to_db = false
"""

PURGE_STARTING_TOML = """[project]
dependencies = ["snekmate", "patrickalphac/test_repo"]
# PRESERVE COMMENTS
[networks.sepolia]
url = "https://ethereum-sepolia-rpc.publicnode.com"
chain_id = 11155111
"""


# ------------------------------------------------------------------
# TEST GITHUB
# ------------------------------------------------------------------
PIP_PACKAGE_NAME = "snekmate"
ORG_NAME = "pcaversaccio"
GITHUB_PACKAGE_NAME = f"{ORG_NAME}/{PIP_PACKAGE_NAME}"
VERSION = "0.1.0"
NEW_VERSION = "0.0.5"
COMMENT_CONTENT = "PRESERVE COMMENTS"
PATRICK_ORG_NAME = "patrickalphac"
PATRICK_REPO_NAME = "test_repo"
PATRICK_PACKAGE_NAME = f"{PATRICK_ORG_NAME}/{PATRICK_REPO_NAME}"
2 changes: 2 additions & 0 deletions tests/data/complex_project/moccasin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ prompt_live = false
is_zksync = true
default_account_name = "smalltestnet"
unsafe_password_file = "~/.moccasin/unsafe-passwords/smalltestnet"
explorer_uri = 'https://explorer.sepolia.era.zksync.dev'
explorer_type = 'zksyncexplorer'

[networks.fake_chain]
url = "${FAKE_CHAIN_RPC_URL}"
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_integration_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path

from moccasin.commands.explorer import boa_get_abi_from_explorer
from tests.conftest import COMPLEX_PROJECT_PATH
from tests.constants import COMPLEX_PROJECT_PATH

CURVE_ADDRESS_ETH_MAINNET = "0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E"
LINK_ADDRESS_OPT_MAINNET = "0x350a791Bfc2C21F9Ed5d10980Dad2e2638ffa7f6"
Expand Down
60 changes: 30 additions & 30 deletions tests/integration/test_integration_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from moccasin.commands.install import GITHUB, PYPI
from moccasin.config import Config
from moccasin.constants.vars import DEPENDENCIES_FOLDER
from tests.conftest import (
comment_content,
github_package_name,
new_version,
org_name,
patrick_package_name,
pip_package_name,
version,
from tests.constants import (
COMMENT_CONTENT,
GITHUB_PACKAGE_NAME,
NEW_VERSION,
ORG_NAME,
PATRICK_PACKAGE_NAME,
PIP_PACKAGE_NAME,
VERSION,
)


Expand All @@ -27,10 +27,10 @@ def test_install_without_parameters_installs_packages_in_toml(
)
finally:
os.chdir(current_dir)
assert f"+ {pip_package_name}=={version}" in result.stderr
assert f"+ {PIP_PACKAGE_NAME}=={VERSION}" in result.stderr
assert (
Path(installation_temp_path)
.joinpath(f"{DEPENDENCIES_FOLDER}/{PYPI}/{pip_package_name}")
.joinpath(f"{DEPENDENCIES_FOLDER}/{PYPI}/{PIP_PACKAGE_NAME}")
.exists()
)

Expand All @@ -42,13 +42,13 @@ def test_double_install_snekmate(
try:
os.chdir(installation_temp_path)
result_one = subprocess.run(
[mox_path, "install", github_package_name],
[mox_path, "install", GITHUB_PACKAGE_NAME],
check=True,
capture_output=True,
text=True,
)
result_two = subprocess.run(
[mox_path, "install", github_package_name],
[mox_path, "install", GITHUB_PACKAGE_NAME],
check=True,
capture_output=True,
text=True,
Expand All @@ -59,7 +59,7 @@ def test_double_install_snekmate(
assert result_two.returncode == 0
assert (
Path(installation_temp_path)
.joinpath(f"{DEPENDENCIES_FOLDER}/{GITHUB}/{org_name}")
.joinpath(f"{DEPENDENCIES_FOLDER}/{GITHUB}/{ORG_NAME}")
.exists()
)

Expand All @@ -70,15 +70,15 @@ def test_write_to_config_after_install(
project_root: Path = Config.find_project_root(Path(installation_temp_path))
config = Config(project_root)
starting_dependencies = config.dependencies
assert github_package_name not in config.dependencies
assert GITHUB_PACKAGE_NAME not in config.dependencies

# Arrange
current_dir = Path.cwd()
# Act
try:
os.chdir(installation_temp_path)
subprocess.run(
[mox_path, "install", github_package_name],
[mox_path, "install", GITHUB_PACKAGE_NAME],
check=True,
capture_output=True,
text=True,
Expand All @@ -88,10 +88,10 @@ def test_write_to_config_after_install(
# Assert
project_root: Path = Config.find_project_root(Path(installation_temp_path))
config = Config(project_root)
assert github_package_name in config.dependencies
assert GITHUB_PACKAGE_NAME in config.dependencies
for dep in starting_dependencies:
assert dep in config.dependencies
assert comment_content in config.read_configs_preserve_comments().as_string()
assert COMMENT_CONTENT in config.read_configs_preserve_comments().as_string()


def test_can_install_with_version(
Expand All @@ -101,20 +101,20 @@ def test_can_install_with_version(
try:
os.chdir(installation_temp_path)
result = subprocess.run(
[mox_path, "install", f"{github_package_name}@{version}"],
[mox_path, "install", f"{GITHUB_PACKAGE_NAME}@{VERSION}"],
check=True,
capture_output=True,
text=True,
)
finally:
os.chdir(current_dir)
assert f"Installed {github_package_name}" in result.stderr
assert f"Installed {GITHUB_PACKAGE_NAME}" in result.stderr
project_root: Path = Config.find_project_root(Path(installation_temp_path))
config = Config(project_root)
assert f"{github_package_name}@{version}" in config.dependencies
assert f"{GITHUB_PACKAGE_NAME}@{VERSION}" in config.dependencies
assert (
Path(installation_temp_path)
.joinpath(f"{DEPENDENCIES_FOLDER}/{GITHUB}/{org_name}")
.joinpath(f"{DEPENDENCIES_FOLDER}/{GITHUB}/{ORG_NAME}")
.exists()
)

Expand All @@ -126,27 +126,27 @@ def test_can_change_versions(
try:
os.chdir(installation_temp_path)
result_one = subprocess.run(
[mox_path, "install", f"{github_package_name}@{version}"],
[mox_path, "install", f"{GITHUB_PACKAGE_NAME}@{VERSION}"],
check=True,
capture_output=True,
text=True,
)
result_two = subprocess.run(
[mox_path, "install", f"{github_package_name}@{new_version}"],
[mox_path, "install", f"{GITHUB_PACKAGE_NAME}@{NEW_VERSION}"],
check=True,
capture_output=True,
text=True,
)
finally:
os.chdir(current_dir)
assert f"Installed {github_package_name}" in result_one.stderr
assert f"Updating {github_package_name}" in result_two.stderr
assert f"Installed {GITHUB_PACKAGE_NAME}" in result_one.stderr
assert f"Updating {GITHUB_PACKAGE_NAME}" in result_two.stderr
project_root: Path = Config.find_project_root(Path(installation_temp_path))
config = Config(project_root)
assert f"{github_package_name}@{new_version}" in config.dependencies
assert f"{GITHUB_PACKAGE_NAME}@{NEW_VERSION}" in config.dependencies
assert (
Path(installation_temp_path)
.joinpath(f"{DEPENDENCIES_FOLDER}/{GITHUB}/{org_name}")
.joinpath(f"{DEPENDENCIES_FOLDER}/{GITHUB}/{ORG_NAME}")
.exists()
)

Expand All @@ -158,7 +158,7 @@ def test_can_compile_with_github_search_path(
try:
os.chdir(installation_temp_path)
result_install = subprocess.run(
[mox_path, "install", patrick_package_name],
[mox_path, "install", PATRICK_PACKAGE_NAME],
check=True,
capture_output=True,
text=True,
Expand All @@ -180,7 +180,7 @@ def test_no_moccasin_toml_saves_dependencies_to_pyproject(
try:
os.chdir(no_config_temp_path)
result_install = subprocess.run(
[mox_path, "install", patrick_package_name],
[mox_path, "install", PATRICK_PACKAGE_NAME],
check=True,
capture_output=True,
text=True,
Expand All @@ -206,4 +206,4 @@ def test_no_moccasin_toml_saves_dependencies_to_pyproject(

project_root: Path = Config.find_project_root(Path(no_config_temp_path))
config = Config(project_root)
assert patrick_package_name in config.dependencies
assert PATRICK_PACKAGE_NAME in config.dependencies
Loading

0 comments on commit 302407d

Please sign in to comment.