Skip to content

Commit

Permalink
chore: Added ABI tests for SDK-generated methods [contract_source_met…
Browse files Browse the repository at this point in the history
…adata] (#1136)
  • Loading branch information
Yasir Shariff authored Feb 4, 2024
1 parent b427b2f commit a12da50
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
1 change: 1 addition & 0 deletions near-sdk-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ serde_json = "1"
insta = { version = "1.31.0", features = ["yaml"] }
prettyplease = { version = "0.2.15" }


[features]
abi = []
__abi-embed = ["abi"]
Expand Down
9 changes: 0 additions & 9 deletions near-sdk-macros/src/core_impl/utils/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ pub fn pretty_print_syn_str(input: &TokenStream) -> syn::Result<String> {
Ok(prettyplease::unparse(&syn_file))
}

// macro_rules! local_insta_assert_debug_snapshot {
// ($value:expr) => {{

// insta::with_settings!({prepend_module_to_snapshot => false}, {
// insta::assert_debug_snapshot!($value);
// });
// }};
// }

macro_rules! local_insta_assert_snapshot {
($value:expr) => {{

Expand Down
2 changes: 2 additions & 0 deletions near-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ hex = { version = "0.4.3", features = ["serde"] }
getrandom = { version = "0.2", features = ["js"] }
rand_chacha = "0.3.1"
near-rng = "0.1.1"
near-abi = { version = "0.4.0", features = ["__chunked-entries"] }
symbolic-debuginfo = "8.8"

[features]
default = ["wee_alloc", "unit-testing", "legacy", "abi"]
Expand Down
64 changes: 64 additions & 0 deletions near-sdk/tests/abi_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::{collections::HashSet, env, fs, path::Path, process::Command};

// These methods are prepended to the contract internally, update this test list if they change
const PREPENDED_METHODS: [&str; 1] = ["contract_source_metadata"];

#[test]
fn ensure_abi_for_prepended_functions() {
const NEAR_SDK_DIR: &str = env!("CARGO_MANIFEST_DIR");

// using the adder example as a test case
let target = Path::new(NEAR_SDK_DIR).join("../examples/adder/target");
let project_manifest = Path::new(NEAR_SDK_DIR).join("../examples/adder/Cargo.toml");

let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
let res = Command::new(cargo)
.arg("build")
.args(["--manifest-path", &project_manifest.to_string_lossy()])
.args(["--features", "near-sdk/__abi-generate"])
.env("CARGO_TARGET_DIR", &target)
.env("RUSTFLAGS", "-Awarnings")
.output()
.unwrap();

assert!(
res.status.success(),
"failed to compile contract abi: {}",
String::from_utf8_lossy(&res.stderr)
);

let dylib_file = target.join(format!("debug/libadder.{}", dylib_extension()));
assert!(dylib_file.exists(), "Build file should exist");

let dylib_file_contents = fs::read(dylib_file).expect("unable to read build file");

let near_abi_symbols = symbolic_debuginfo::Object::parse(&dylib_file_contents)
.expect("unable to parse dylib")
.symbols()
.flat_map(|sym| sym.name)
.filter(|sym_name| sym_name.starts_with("__near_abi_"))
.collect::<HashSet<_>>();

// ensure methods are prepended
PREPENDED_METHODS.map(|method| {
assert!(
near_abi_symbols.contains(format!("__near_abi_{}", method).as_str()),
"ABI should contain prepended method {}",
method
);
});
}

const fn dylib_extension() -> &'static str {
#[cfg(target_os = "linux")]
return "so";

#[cfg(target_os = "macos")]
return "dylib";

#[cfg(target_os = "windows")]
return "dll";

#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
compile_error!("Unsupported platform");
}

0 comments on commit a12da50

Please sign in to comment.