From f771467f27f03c8d20b8032bac64b3ab60436d3c Mon Sep 17 00:00:00 2001 From: Andrew Fleming Date: Mon, 2 May 2022 22:39:17 -0400 Subject: [PATCH] automate account flag (#112) --- README.md | 4 ++-- src/nile/core/compile.py | 2 +- tests/commands/test_compile.py | 36 ++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 750c35f2..046d6325 100644 --- a/README.md +++ b/README.md @@ -64,10 +64,10 @@ nile compile contracts/MyContract.cairo # compiles single contract nile compile contracts/MyContract.cairo --disable-hint-validation # compiles single contract with unwhitelisted hints ``` -As of cairo-lang v0.8.0, account contracts (contracts with the `__execute__` method) must be compiled with the `--account_contract` flag. +As of cairo-lang v0.8.0, account contracts (contracts with the `__execute__` method) must be compiled with the `--account_contract` flag. Nile automatically inserts the flag if the contract's name ends with `Account` i.e. Account.cairo, EthAccount.cairo. Otherwise, the flag must be included by the user. ```sh -nile compile contracts/MyAccount.cairo --account_contract # compiles account contract +nile compile contracts/NewAccountType.cairo --account_contract # compiles account contract ``` Example output: diff --git a/src/nile/core/compile.py b/src/nile/core/compile.py index 9d4ba73b..348d85cc 100644 --- a/src/nile/core/compile.py +++ b/src/nile/core/compile.py @@ -66,7 +66,7 @@ def _compile_contract( --abi {ABIS_DIRECTORY}/{filename}.json """ - if account_contract: + if account_contract or filename.endswith("Account"): cmd = cmd + "--account_contract" if disable_hint_validation: diff --git a/tests/commands/test_compile.py b/tests/commands/test_compile.py index f4e6def7..a4816db1 100644 --- a/tests/commands/test_compile.py +++ b/tests/commands/test_compile.py @@ -116,6 +116,42 @@ def test__compile_account_contract(mock_subprocess): mock_process.communicate.assert_called_once() +@pytest.mark.parametrize( + "contract_name, flag", + [ + ("Account", "--account_contract"), + ("mock_Account", "--account_contract"), + ("Account_Manager", False), + ], +) +def test__compile_auto_account_flag(mock_subprocess, contract_name, flag): + path = f"path/to/{contract_name}.cairo" + + mock_process = Mock() + mock_subprocess.Popen.return_value = mock_process + + _compile_contract(path) + + returned_subprocess = [ + "starknet-compile", + path, + f"--cairo_path={CONTRACTS_DIRECTORY}", + "--output", + f"artifacts/{contract_name}.json", + "--abi", + f"artifacts/abis/{contract_name}.json", + ] + + if flag is not False: + returned_subprocess.append(flag) + + mock_subprocess.Popen.assert_called_once_with( + returned_subprocess, + stdout=mock_subprocess.PIPE, + ) + mock_process.communicate.assert_called_once() + + def test__compile_contract_without_hint_validation(mock_subprocess): contract_name_root = "contract_with_unwhitelisted_hints" path = f"path/to/{contract_name_root}.cairo"