From f4e561e8353eaa4b22b263c6379385460f17c304 Mon Sep 17 00:00:00 2001 From: PatrickAlphaC <54278053+PatrickAlphaC@users.noreply.github.com> Date: Wed, 18 Sep 2024 15:38:05 -0400 Subject: [PATCH] feat: made .env location customizeable --- moccasin/config.py | 12 +++++++++++- moccasin/constants/file_data.py | 8 ++++++-- moccasin/constants/vars.py | 1 + tests/data/complex_project/.hello | 1 + tests/data/complex_project/moccasin.toml | 1 + tests/unit/test_unit_config.py | 8 ++++++++ 6 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 tests/data/complex_project/.hello create mode 100644 tests/unit/test_unit_config.py diff --git a/moccasin/config.py b/moccasin/config.py index da1b2d0..84e5077 100644 --- a/moccasin/config.py +++ b/moccasin/config.py @@ -12,6 +12,7 @@ INSTALLER, DEFAULT_INSTALLER, SAVE_ABI_PATH, + DOT_ENV_KEY, ) import tomllib from dotenv import load_dotenv @@ -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) @@ -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) @@ -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: diff --git a/moccasin/constants/file_data.py b/moccasin/constants/file_data.py index 2806e55..241dc3d 100644 --- a/moccasin/constants/file_data.py +++ b/moccasin/constants/file_data.py @@ -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] @@ -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" @@ -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 = """{ diff --git a/moccasin/constants/vars.py b/moccasin/constants/vars.py index 8fce69a..f622505 100644 --- a/moccasin/constants/vars.py +++ b/moccasin/constants/vars.py @@ -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" diff --git a/tests/data/complex_project/.hello b/tests/data/complex_project/.hello new file mode 100644 index 0000000..f6768fc --- /dev/null +++ b/tests/data/complex_project/.hello @@ -0,0 +1 @@ +HELLO_THERE="HI" \ No newline at end of file diff --git a/tests/data/complex_project/moccasin.toml b/tests/data/complex_project/moccasin.toml index 4ef1271..fe6d5f4 100644 --- a/tests/data/complex_project/moccasin.toml +++ b/tests/data/complex_project/moccasin.toml @@ -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 } diff --git a/tests/unit/test_unit_config.py b/tests/unit/test_unit_config.py new file mode 100644 index 0000000..0a6f793 --- /dev/null +++ b/tests/unit/test_unit_config.py @@ -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"