Skip to content

Commit

Permalink
pytest: fix nayduck looking for contracts that are long long gone
Browse files Browse the repository at this point in the history
Storing build artifacts in the source directory shouldn't be happening.
Although not an ideal, this is introducing a near-test-contracts
binary that can output the contracts upon request. A good solution would
be for pytest to either maintain their own contracts or link to
`near-test-contracts` proper and grab the contracts by calling a
function, just like is done in the rest of the test suite.
  • Loading branch information
nagisa committed Jan 16, 2025
1 parent 8c0e9b9 commit c4b50dd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
17 changes: 6 additions & 11 deletions pytest/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from configured_logger import logger
import key
import transaction
from branches import _REPO_DIR


class TxContext:
Expand Down Expand Up @@ -126,7 +127,7 @@ def count(self, pattern: str) -> int:

class MetricsTracker:
"""Helper class to collect prometheus metrics from the node.
Usage:
tracker = MetricsTracker(node)
assert tracker.get_int_metric_value("near-connections") == 2
Expand Down Expand Up @@ -243,16 +244,10 @@ def load_binary_file(filepath):

def load_test_contract(
filename: str = 'backwards_compatible_rs_contract.wasm') -> bytearray:
"""Loads a WASM file from near-test-contracts package.
This is just a convenience function around load_binary_file which loads
files from ../runtime/near-test-contracts/res directory. By default
test_contract_rs.wasm is loaded.
"""
repo_dir = pathlib.Path(__file__).resolve().parents[2]
path = repo_dir / 'runtime/near-test-contracts/res' / filename
return load_binary_file(path)

"""Loads a WASM file from near-test-contracts package."""
output = subprocess.check_output(['cargo', 'run', '-p', 'near-test-contracts', filename],
cwd=_REPO_DIR)
return output.stdout

def user_name():
username = os.getlogin()
Expand Down
30 changes: 30 additions & 0 deletions runtime/near-test-contracts/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::io::Write as _;

use near_test_contracts::*;

fn main() {
let contract_name = std::env::args()
.nth(1)
.expect("expecting one argument with the name of the contract to return");
let Some((name, extension)) = contract_name.rsplit_once(".") else {
panic!("argument expected in `filename.ext` form");
};
let code = match &*name {
"trivial" => trivial_contract(),
"rs_contract" => rs_contract(),
"backwards_compatible_rs_contract" => backwards_compatible_rs_contract(),
"nightly_rs_contract" => nightly_rs_contract(),
"ts_contract" => ts_contract(),
"fuzzing_contract" => fuzzing_contract(),
"ft_contract" => ft_contract(),
"smallest_rs_contract" => smallest_rs_contract(),
"estimator_contract" => estimator_contract(),
"congestion_control_test_contract" => congestion_control_test_contract(),
_ => panic!("unknown contract {name}"),
};
if extension == "wasm" {
std::io::stdout().write_all(&code).expect("while writing code to stdout");
} else {
panic!("unsupported filetype `{extension}`");
}
}

0 comments on commit c4b50dd

Please sign in to comment.