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

chore: adding abi tests #1136

Merged
merged 9 commits into from Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
25 changes: 25 additions & 0 deletions examples/adder/abi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
TARGET="${CARGO_TARGET_DIR:-../../target}"
set -e
cd "$(dirname $0)"

# Build the lib
CARGO_PROFILE_DEV_OPT_LEVEL=0 \
CARGO_PROFILE_DEV_DEBUG=0 \
CARGO_PROFILE_DEV_LTO=off \
RUSTFLAGS="-Awarnings" \
cargo build --features near-sdk/__abi-generate --release

OS="$(uname)"
if [[ "$OS" == "Linux" ]]; then
EXT=".so"
elif [[ "$OS" == "Darwin" ]]; then
EXT=".dylib"
elif [[ "$OS" == MINGW* ]] || [[ "$OS" == CYGWIN* ]] || [[ "$OS" == MSYS* ]]; then
EXT=".dll"
else
echo "Unsupported OS: $OS"
exit 1
fi

cp "$TARGET/release/libadder$EXT" ./res/
2 changes: 2 additions & 0 deletions near-sdk-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"

[dev-dependencies]
near-abi = { version = "0.4.0", features = ["__chunked-entries"] }
insta = { version = "1.31.0", features = ["yaml"] }
prettyplease = { version = "0.2.15" }
symbolic-debuginfo = "8.8"

[features]
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
60 changes: 60 additions & 0 deletions near-sdk-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,3 +643,63 @@ pub fn derive_event_attributes(item: TokenStream) -> TokenStream {
)
}
}

#[cfg(test)]
mod tests {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shariffdev

  1. This place does not make sense for these tests. Let's move them to near-sdk/tests/
  2. I would like to encourage you to consider looking into cargo-near implementation of abi extraction to avoid sh script running, etc: https://github.com/near/cargo-near/blob/48c7b48fa0f7f3b4afda447ba0b04cce3609b0a9/cargo-near/src/commands/abi_command/abi.rs#L31

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

use std::{collections::HashSet, 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"];
const PROJECT_PATH: &str = "../../near-sdk-rs/examples/adder";

#[test]
fn ensure_abi_for_prepended_functions() {
let contract_dir = Path::new(PROJECT_PATH);

let res = Command::new("bash")
.arg(contract_dir.join("abi.sh").to_str().expect("script path is valid"))
.output()
.unwrap();

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

let dylib_file = contract_dir.join(format!("res/libadder.{}", dylib_extension()));
assert!(dylib_file.exists(), "Dylib file should exist");

let dylib_file_contents =
fs::read(contract_dir.join(format!("res/libadder.{}", dylib_extension()))).unwrap();
let object = symbolic_debuginfo::Object::parse(&dylib_file_contents).unwrap();
let near_abi_symbols = object
.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.iter().any(|f| f.eq(&format!("__near_abi_{}", method))),
"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");
}
}
Loading