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

feat: made .env location customizeable #56

Merged
merged 1 commit into from
Sep 18, 2024
Merged
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
12 changes: 11 additions & 1 deletion moccasin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
INSTALLER,
DEFAULT_INSTALLER,
SAVE_ABI_PATH,
DOT_ENV_KEY,
)
import tomllib
from dotenv import load_dotenv
Expand Down Expand Up @@ -490,6 +491,11 @@ def __init__(self, root_path: Path):

def _load_config(self, config_path: Path):
toml_data: dict = self.read_moccasin_config(config_path)
# Need to get the .env file before expanding env vars
self.project = {}
self.project[DOT_ENV_KEY] = toml_data.get("project", {}).get(
DOT_ENV_KEY, DOT_ENV_FILE
)
self._load_env_file()
toml_data = self.expand_env_vars(toml_data)
self.networks = _Networks(toml_data)
Expand All @@ -502,7 +508,7 @@ def _load_config(self, config_path: Path):
self.extra_data = toml_data.get("extra_data", {})

def _load_env_file(self):
load_dotenv(dotenv_path=self.project_root.joinpath(DOT_ENV_FILE))
load_dotenv(dotenv_path=self.project_root.joinpath(self.dot_env))

def read_moccasin_config(self, config_path: Path = None) -> dict:
config_path = self._validate_config_path(config_path)
Expand Down Expand Up @@ -608,6 +614,10 @@ def src_folder(self) -> str:
def cov_config(self) -> str | None:
return self.project.get("cov_config", None)

@property
def dot_env(self) -> str:
return self.project.get(DOT_ENV_KEY, DOT_ENV_FILE)

# Tests must be in "tests" folder
@property
def test_folder(self) -> str:
Expand Down
8 changes: 6 additions & 2 deletions moccasin/constants/file_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ def counter_contract():

```bash
mox init
mox compile
mox run deploy
```

_For documentation, please run `mox --help` or visit [the Moccasin documentation](https://github.com/cyfrin/moccasin)_
_For documentation, please run `mox --help` or visit [the Moccasin documentation](https://cyfrin.github.io/moccasin)_
"""

MOCCASIN_DEFAULT_CONFIG = """[project]
Expand All @@ -247,6 +247,7 @@ def counter_contract():
script = "script"
lib = "lib"
installer = "uv"
dot_env = ".env"

[networks.sepolia]
url = "https://ethereum-sepolia-rpc.publicnode.com"
Expand All @@ -256,6 +257,9 @@ def counter_contract():
url = "https://sepolia.era.zksync.dev"
chain_id = 300
is_zksync = true
prompt_live = true

# You can view all configuration options at https://cyfrin.github.io/moccasin/all_moccasin_toml_parameters.html
"""

VSCODE_SETTINGS_DEFAULT = """{
Expand Down
1 change: 1 addition & 0 deletions moccasin/constants/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
DEFAULT_KEYSTORES_PATH = DEFAULT_MOCCASIN_FOLDER.joinpath("keystores/")
FOUNDRTY_KEYSTORES_PATH = Path.home().joinpath(".foundry/keystores")
DOT_ENV_FILE = ".env"
DOT_ENV_KEY = "dot_env"
CONSOLE_HISTORY_FILE = "moccasin_history"
DEFAULT_API_KEY_ENV_VAR = "EXPLORER_API_KEY"

Expand Down
1 change: 1 addition & 0 deletions tests/data/complex_project/.hello
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
HELLO_THERE="HI"
1 change: 1 addition & 0 deletions tests/data/complex_project/moccasin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ out = "build"
explorer_api_key = "${ETHERSCAN_API_KEY}"
save_abi_path = "abis"
cov_config = ".coveragerc"
dot_env = ".hello"

[networks.contracts]
price_feed = { abi_from_file_path = "mocks/MockV3Aggregator.vy", force_deploy = false, deployer_script = "mock_deployer/deploy_feed", fixture = false }
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_unit_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from moccasin.config import get_config
import os


def test_set_dot_env(complex_project_config):
config = get_config()
assert config.dot_env == ".hello"
assert os.getenv("HELLO_THERE") == "HI"
Loading