From dbdeef1dca3f73db5f0722622710db4511173251 Mon Sep 17 00:00:00 2001 From: jrmncos Date: Fri, 16 Aug 2024 02:06:55 -0300 Subject: [PATCH] using base example from wasmcov to all examples directories --- .github/workflows/coverage-examples.yml | 60 +++++++++++++++++++ examples/adder/Cargo.toml | 7 ++- examples/adder/build.sh | 21 ++++--- examples/adder/src/lib.rs | 8 +-- examples/callback-results/Cargo.toml | 7 ++- examples/callback-results/build.sh | 22 +++---- examples/callback-results/src/lib.rs | 8 +-- examples/cross-contract-calls/Cargo.toml | 8 ++- examples/cross-contract-calls/build.sh | 25 ++++---- .../high-level/Cargo.toml | 5 +- .../high-level/src/lib.rs | 6 +- .../cross-contract-calls/low-level/Cargo.toml | 5 +- .../cross-contract-calls/low-level/src/lib.rs | 6 +- examples/factory-contract/Cargo.toml | 8 ++- examples/factory-contract/build.sh | 24 ++++---- .../factory-contract/high-level/Cargo.toml | 5 +- .../factory-contract/high-level/src/lib.rs | 6 +- .../factory-contract/low-level/Cargo.toml | 5 +- .../factory-contract/low-level/src/lib.rs | 6 +- examples/fungible-token/Cargo.toml | 8 ++- examples/fungible-token/build.sh | 25 ++++---- examples/fungible-token/ft/Cargo.toml | 7 ++- examples/fungible-token/ft/src/lib.rs | 6 +- examples/fungible-token/res/.gitignore | 5 ++ .../test-contract-defi/Cargo.toml | 7 ++- .../test-contract-defi/src/lib.rs | 6 +- examples/fungible-token/tests/workspaces.rs | 9 ++- examples/lockable-fungible-token/Cargo.toml | 7 ++- examples/lockable-fungible-token/build.sh | 23 ++++--- examples/lockable-fungible-token/src/lib.rs | 6 +- examples/mission-control/Cargo.toml | 7 ++- examples/mission-control/build.sh | 24 ++++---- examples/mission-control/src/lib.rs | 2 +- examples/non-fungible-token/Cargo.toml | 8 ++- examples/non-fungible-token/build.sh | 28 ++++----- examples/non-fungible-token/nft/Cargo.toml | 7 ++- examples/non-fungible-token/nft/src/lib.rs | 6 +- .../test-approval-receiver/Cargo.toml | 5 +- .../test-approval-receiver/src/lib.rs | 6 +- .../test-token-receiver/Cargo.toml | 7 ++- .../test-token-receiver/src/lib.rs | 6 +- examples/status-message/Cargo.toml | 7 ++- examples/status-message/build.sh | 24 ++++---- examples/status-message/src/lib.rs | 6 +- examples/test-contract/Cargo.toml | 9 ++- examples/test-contract/build.sh | 24 ++++---- examples/test-contract/src/lib.rs | 6 +- examples/versioned/Cargo.toml | 9 ++- examples/versioned/build.sh | 22 +++---- examples/versioned/src/lib.rs | 6 +- 50 files changed, 350 insertions(+), 220 deletions(-) create mode 100644 .github/workflows/coverage-examples.yml create mode 100644 examples/fungible-token/res/.gitignore diff --git a/.github/workflows/coverage-examples.yml b/.github/workflows/coverage-examples.yml new file mode 100644 index 000000000..7133e4d8a --- /dev/null +++ b/.github/workflows/coverage-examples.yml @@ -0,0 +1,60 @@ +name: Coverage +on: + push: + branches: + - master + pull_request: +env: + RUSTFLAGS: "-D warnings" + +jobs: + coverage: + runs-on: ${{ matrix.platform }} + name: "${{ matrix.example }} - ${{ matrix.platform }}" + strategy: + matrix: + toolchain: [nightly-2024-07-01-aarch64-apple-darwin] + steps: + - uses: actions/checkout@v3 + - name: Install Homebrew + run: | + /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" + - name: Install LLVM + run: brew install llvm + - name: Add LLVM to PATH + run: echo "/opt/homebrew/opt/llvm/bin" >> $GITHUB_PATH + - name: Llvm version + run: llvm-config --version + - name: Clang version + run: clang --version + - name: "${{ matrix.toolchain }} with rustfmt, and wasm32" + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: ${{ matrix.toolchain }} + target: wasm32-unknown-unknown + - name: Set Default Toolchain + run: rustup override set ${{ matrix.toolchain }} + - name: Rust version + run: rustc --version --verbose + - name: Verify Rust Toolchain + run: rustup show + - name: Add rust-src component + run: rustup component add rust-src --toolchain ${{ matrix.toolchain }} + - name: Install wasmcov + run: cargo +${{ matrix.toolchain }} install wasmcov + - name: Create wasmcov directory + run: mkdir -p ${{ github.workspace }}/wasmcov + - name: Set WASMCOV_DIR environment variable + run: echo "WASMCOV_DIR=${{ github.workspace }}/wasmcov" >> $GITHUB_ENV + - name: Set up cache for near_sandbox directory + uses: actions/cache@v3 + with: + path: ${{ github.workspace }}/wasmcov/near_sandbox + key: ${{ runner.os }}-near-sandbox-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-near-sandbox-${{ github.sha }} + - name: Build examples + env: + RUSTFLAGS: '-C link-arg=-s' + run: bash ./examples/build_wasm_test_all.sh ${{ matrix.toolchain }} \ No newline at end of file diff --git a/examples/adder/Cargo.toml b/examples/adder/Cargo.toml index 28760799c..d64044e70 100644 --- a/examples/adder/Cargo.toml +++ b/examples/adder/Cargo.toml @@ -9,7 +9,7 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../near-sdk" } -wasmcov = "0.2" +wasmcov = { version = "0.2", optional = true } [dev-dependencies] near-workspaces = "0.11.1" @@ -19,10 +19,13 @@ near-abi = "0.4.0" zstd = "0.13" near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } +[features] +wasmcov = ["dep:wasmcov"] + [profile.release] codegen-units = 1 # Tell `rustc` to optimize for small code size. opt-level = "z" -lto = false +lto = true debug = false panic = "abort" diff --git a/examples/adder/build.sh b/examples/adder/build.sh index ef84e52b8..e3a4e35d1 100755 --- a/examples/adder/build.sh +++ b/examples/adder/build.sh @@ -1,16 +1,15 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" -set -e -cd "$(dirname $0)" - -INCLUDE_COVERAGE="${1:-false}" -if [ "$INCLUDE_COVERAGE" = true ]; then - cargo-wasmcov build --wasmcov-dir /Users/jrmncos/forks/near-sdk-rs/examples/wasmcov -- --all --target wasm32-unknown-unknown --release - cp ../wasmcov/target/wasm32-unknown-unknown/release/adder.wasm ./res/ +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" else - cargo build --target wasm32-unknown-unknown --release - cp $TARGET/wasm32-unknown-unknown/release/adder.wasm ./res/ - #wasm-opt -Oz --output ./res/status_message.wasm ./res/status_message.wasm + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" fi +set -e +cd "$(dirname $0)" +$BUILD_COMMAND --target wasm32-unknown-unknown --release +cp $TARGET/wasm32-unknown-unknown/release/adder.wasm ./res/ +#wasm-opt -Oz --output ./res/status_message.wasm ./res/status_message.wasm \ No newline at end of file diff --git a/examples/adder/src/lib.rs b/examples/adder/src/lib.rs index 425f8370a..ba98141a9 100644 --- a/examples/adder/src/lib.rs +++ b/examples/adder/src/lib.rs @@ -1,5 +1,8 @@ use near_sdk::near; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + #[near(serializers=[borsh, json])] pub struct Pair(u32, u32); @@ -88,7 +91,4 @@ mod tests { Ok(()) } -} - -#[cfg(target_family = "wasm")] -wasmcov::near::add_coverage!(); \ No newline at end of file +} \ No newline at end of file diff --git a/examples/callback-results/Cargo.toml b/examples/callback-results/Cargo.toml index 7c92e6ff2..87a01e2cd 100644 --- a/examples/callback-results/Cargo.toml +++ b/examples/callback-results/Cargo.toml @@ -9,7 +9,7 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../near-sdk" } -wasmcov = "0.2" +wasmcov = { version = "0.2", optional = true } [dev-dependencies] near-workspaces = "0.11.1" @@ -17,10 +17,13 @@ tokio = { version = "1.14", features = ["full"] } anyhow = "1.0" near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } +[features] +wasmcov = ["dep:wasmcov"] + [profile.release] codegen-units = 1 # Tell `rustc` to optimize for small code size. opt-level = "z" -lto = false +lto = true debug = false panic = "abort" diff --git a/examples/callback-results/build.sh b/examples/callback-results/build.sh index 695b72590..a5b5f9bc3 100755 --- a/examples/callback-results/build.sh +++ b/examples/callback-results/build.sh @@ -1,14 +1,14 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" -set -e -cd "$(dirname $0)" - -INCLUDE_COVERAGE="${1:-false}" -if [ "$INCLUDE_COVERAGE" = "true" ]; then - cargo-wasmcov build --wasmcov-dir /Users/jrmncos/forks/near-sdk-rs/examples/wasmcov -- --all --target wasm32-unknown-unknown --release - cp ../wasmcov/target/wasm32-unknown-unknown/release/callback_results.wasm ./res/ +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" else - cargo build --target wasm32-unknown-unknown --release - cp $TARGET/wasm32-unknown-unknown/release/callback_results.wasm ./res/ -fi \ No newline at end of file + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + +set -e +cd "$(dirname $0)" +$BUILD_COMMAND --target wasm32-unknown-unknown --release +cp $TARGET/wasm32-unknown-unknown/release/callback_results.wasm ./res/ \ No newline at end of file diff --git a/examples/callback-results/src/lib.rs b/examples/callback-results/src/lib.rs index be25d5abc..8b12056f1 100644 --- a/examples/callback-results/src/lib.rs +++ b/examples/callback-results/src/lib.rs @@ -1,6 +1,9 @@ use near_sdk::require; use near_sdk::{env, near, Promise, PromiseError}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + const A_VALUE: u8 = 8; #[near(contract_state)] @@ -122,7 +125,4 @@ mod tests { Ok(()) } -} - -#[cfg(target_family = "wasm")] -wasmcov::near::add_coverage!(); \ No newline at end of file +} \ No newline at end of file diff --git a/examples/cross-contract-calls/Cargo.toml b/examples/cross-contract-calls/Cargo.toml index f609b18d1..372270001 100644 --- a/examples/cross-contract-calls/Cargo.toml +++ b/examples/cross-contract-calls/Cargo.toml @@ -4,6 +4,9 @@ version = "0.1.0" authors = ["Near Inc "] edition = "2021" +[dependencies] +wasmcov = { version = "0.2", optional = true } + [dev-dependencies] anyhow = "1.0" near-sdk = { path = "../../near-sdk", features = ["default", "unit-testing"] } @@ -18,9 +21,12 @@ cross-contract-low-level = { path = "./low-level" } codegen-units = 1 # Tell `rustc` to optimize for small code size. opt-level = "z" -lto = false +lto = true debug = false panic = "abort" +[features] +wasmcov = ["dep:wasmcov"] + [workspace] members = ["high-level", "low-level"] diff --git a/examples/cross-contract-calls/build.sh b/examples/cross-contract-calls/build.sh index 118c8b829..f892efd56 100755 --- a/examples/cross-contract-calls/build.sh +++ b/examples/cross-contract-calls/build.sh @@ -1,16 +1,15 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" -set -e -cd "$(dirname $0)" - -INCLUDE_COVERAGE="${1:-false}" -if [ "$INCLUDE_COVERAGE" = "true" ]; then - cargo-wasmcov build --wasmcov-dir /Users/jrmncos/forks/near-sdk-rs/examples/wasmcov -- --all --target wasm32-unknown-unknown --release - cp ../wasmcov/target/wasm32-unknown-unknown/release/cross_contract_high_level.wasm ./res/ - cp ../wasmcov/target/wasm32-unknown-unknown/release/cross_contract_low_level.wasm ./res/ +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" else - cargo build --all --target wasm32-unknown-unknown --release - cp $TARGET/wasm32-unknown-unknown/release/cross_contract_high_level.wasm ./res/ - cp $TARGET/wasm32-unknown-unknown/release/cross_contract_low_level.wasm ./res/ -fi \ No newline at end of file + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + +set -e +cd "$(dirname $0)" +$BUILD_COMMAND --all --target wasm32-unknown-unknown --release +cp $TARGET/wasm32-unknown-unknown/release/cross_contract_high_level.wasm ./res/ +cp $TARGET/wasm32-unknown-unknown/release/cross_contract_low_level.wasm ./res/ \ No newline at end of file diff --git a/examples/cross-contract-calls/high-level/Cargo.toml b/examples/cross-contract-calls/high-level/Cargo.toml index 2da8e1d85..9ac9b7abd 100644 --- a/examples/cross-contract-calls/high-level/Cargo.toml +++ b/examples/cross-contract-calls/high-level/Cargo.toml @@ -9,4 +9,7 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../../near-sdk" , features = ["default", "unit-testing"] } -wasmcov = "0.2" \ No newline at end of file +wasmcov = { version = "0.2", optional = true } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/cross-contract-calls/high-level/src/lib.rs b/examples/cross-contract-calls/high-level/src/lib.rs index 2e3933aa8..caaf1734d 100644 --- a/examples/cross-contract-calls/high-level/src/lib.rs +++ b/examples/cross-contract-calls/high-level/src/lib.rs @@ -1,6 +1,9 @@ use near_sdk::env; use near_sdk::{log, near, PromiseOrValue}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + #[derive(Default)] #[near(contract_state)] pub struct CrossContract {} @@ -30,6 +33,3 @@ impl CrossContract { result } } - -#[cfg(target_family = "wasm")] -wasmcov::near::add_coverage!(); diff --git a/examples/cross-contract-calls/low-level/Cargo.toml b/examples/cross-contract-calls/low-level/Cargo.toml index b13416dd2..c3df46277 100644 --- a/examples/cross-contract-calls/low-level/Cargo.toml +++ b/examples/cross-contract-calls/low-level/Cargo.toml @@ -9,4 +9,7 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../../near-sdk" , features = ["default", "unit-testing"] } -wasmcov = "0.2" \ No newline at end of file +wasmcov = { version = "0.2", optional = true } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/cross-contract-calls/low-level/src/lib.rs b/examples/cross-contract-calls/low-level/src/lib.rs index 3d06b9452..a70c2da5a 100644 --- a/examples/cross-contract-calls/low-level/src/lib.rs +++ b/examples/cross-contract-calls/low-level/src/lib.rs @@ -1,6 +1,9 @@ use near_sdk::serde_json; use near_sdk::{env, near, require, Gas, NearToken, PromiseResult}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + // Prepaid gas for a single (not inclusive of recursion) `factorial` call. const FACTORIAL_CALL_GAS: Gas = Gas::from_tgas(20); @@ -50,6 +53,3 @@ impl CrossContract { env::value_return(&serde_json::to_vec(&(cur * n)).unwrap()); } } - -#[cfg(target_family = "wasm")] -wasmcov::near::add_coverage!(); diff --git a/examples/factory-contract/Cargo.toml b/examples/factory-contract/Cargo.toml index 2985aec43..e771a7d13 100644 --- a/examples/factory-contract/Cargo.toml +++ b/examples/factory-contract/Cargo.toml @@ -4,6 +4,9 @@ version = "0.1.0" authors = ["Near Inc "] edition = "2021" +[dependencies] +wasmcov = { version = "0.2", optional = true } + [dev-dependencies] anyhow = "1.0" test-case = "2.0" @@ -11,11 +14,14 @@ tokio = { version = "1.14", features = ["full"] } near-workspaces = "0.11.1" near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } +[features] +wasmcov = ["dep:wasmcov"] + [profile.release] codegen-units = 1 # Tell `rustc` to optimize for small code size. opt-level = "z" -lto = false +lto = true debug = false panic = "abort" diff --git a/examples/factory-contract/build.sh b/examples/factory-contract/build.sh index 416fa0ed0..cd1940492 100755 --- a/examples/factory-contract/build.sh +++ b/examples/factory-contract/build.sh @@ -1,16 +1,16 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" + +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" +else + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + set -e cd "$(dirname $0)" -INCLUDE_COVERAGE="${1:-false}" - -if [ "$INCLUDE_COVERAGE" = "true" ]; then - cargo-wasmcov build --wasmcov-dir /Users/jrmncos/forks/near-sdk-rs/examples/wasmcov -- --all --target wasm32-unknown-unknown --release - cp ../wasmcov/target/wasm32-unknown-unknown/release/factory_contract_high_level.wasm ./res/ - cp ../wasmcov/target/wasm32-unknown-unknown/release/factory_contract_low_level.wasm ./res/ -else - cargo build --all --target wasm32-unknown-unknown --release - cp $TARGET/wasm32-unknown-unknown/release/factory_contract_high_level.wasm ./res/ - cp $TARGET/wasm32-unknown-unknown/release/factory_contract_low_level.wasm ./res/ -fi \ No newline at end of file +$BUILD_COMMAND --all --target wasm32-unknown-unknown --release +cp $TARGET/wasm32-unknown-unknown/release/factory_contract_high_level.wasm ./res/ +cp $TARGET/wasm32-unknown-unknown/release/factory_contract_low_level.wasm ./res/ \ No newline at end of file diff --git a/examples/factory-contract/high-level/Cargo.toml b/examples/factory-contract/high-level/Cargo.toml index 481d9868f..57bae0682 100644 --- a/examples/factory-contract/high-level/Cargo.toml +++ b/examples/factory-contract/high-level/Cargo.toml @@ -9,7 +9,10 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../../near-sdk" } -wasmcov = "0.2" +wasmcov = { version = "0.2", optional = true } [dev-dependencies] near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } + +[features] +wasmcov = ["dep:wasmcov"] diff --git a/examples/factory-contract/high-level/src/lib.rs b/examples/factory-contract/high-level/src/lib.rs index 0524000ce..30dc56dcd 100644 --- a/examples/factory-contract/high-level/src/lib.rs +++ b/examples/factory-contract/high-level/src/lib.rs @@ -1,6 +1,9 @@ use near_sdk::PromiseError; use near_sdk::{env, ext_contract, near, AccountId, NearToken, Promise}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + #[derive(Default)] #[near(contract_state)] pub struct FactoryContract {} @@ -50,6 +53,3 @@ impl FactoryContract { } } } - -#[cfg(target_family = "wasm")] -wasmcov::near::add_coverage!(); diff --git a/examples/factory-contract/low-level/Cargo.toml b/examples/factory-contract/low-level/Cargo.toml index 6c48f29ac..5e538a4b1 100644 --- a/examples/factory-contract/low-level/Cargo.toml +++ b/examples/factory-contract/low-level/Cargo.toml @@ -9,7 +9,10 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../../near-sdk" } -wasmcov = "0.2" +wasmcov = { version = "0.2", optional = true } [dev-dependencies] near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/factory-contract/low-level/src/lib.rs b/examples/factory-contract/low-level/src/lib.rs index 05c1f53fe..a5b8dd37c 100644 --- a/examples/factory-contract/low-level/src/lib.rs +++ b/examples/factory-contract/low-level/src/lib.rs @@ -2,6 +2,9 @@ use near_sdk::json_types::U128; use near_sdk::serde_json; use near_sdk::{env, near, AccountId, Gas, NearToken, PromiseResult}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + // Prepaid gas for making a single simple call. const SINGLE_CALL_GAS: Gas = Gas::from_tgas(20); @@ -72,6 +75,3 @@ impl FactoryContract { }; } } - -#[cfg(target_family = "wasm")] -wasmcov::near::add_coverage!(); diff --git a/examples/fungible-token/Cargo.toml b/examples/fungible-token/Cargo.toml index c09611a8a..50e2d2851 100644 --- a/examples/fungible-token/Cargo.toml +++ b/examples/fungible-token/Cargo.toml @@ -4,17 +4,23 @@ version = "0.0.2" authors = ["Near Inc "] edition = "2021" +[dependencies] +wasmcov = { version = "0.2", optional = true } + [dev-dependencies] anyhow = "1.0" near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } tokio = { version = "1.14", features = ["full"] } near-workspaces = "0.11.1" +[features] +wasmcov = ["dep:wasmcov"] + [profile.release] codegen-units = 1 # Tell `rustc` to optimize for small code size. opt-level = "z" -lto = false +lto = true debug = false panic = "abort" overflow-checks = true diff --git a/examples/fungible-token/build.sh b/examples/fungible-token/build.sh index 5f27c5c3f..7dd6172e3 100755 --- a/examples/fungible-token/build.sh +++ b/examples/fungible-token/build.sh @@ -1,17 +1,16 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" -set -e -cd "$(dirname $0)" -INCLUDE_COVERAGE="${1:-false}" +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" +else + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi -if [ "$INCLUDE_COVERAGE" = "true" ]; then - cargo-wasmcov build --wasmcov-dir /Users/jrmncos/forks/near-sdk-rs/examples/wasmcov -- --all --target wasm32-unknown-unknown --release - cp ../wasmcov/target/wasm32-unknown-unknown/release/defi.wasm ./res/ - cp ../wasmcov/target/wasm32-unknown-unknown/release/fungible_token.wasm ./res/ +set -e +cd "$(dirname $0)" -else - cargo build --all --target wasm32-unknown-unknown --release - cp $TARGET/wasm32-unknown-unknown/release/defi.wasm ./res/ - cp $TARGET/wasm32-unknown-unknown/release/fungible_token.wasm ./res/ -fi \ No newline at end of file +$BUILD_COMMAND --all --target wasm32-unknown-unknown --release +cp $TARGET/wasm32-unknown-unknown/release/defi.wasm ./res/ +cp $TARGET/wasm32-unknown-unknown/release/fungible_token.wasm ./res/ \ No newline at end of file diff --git a/examples/fungible-token/ft/Cargo.toml b/examples/fungible-token/ft/Cargo.toml index 2a07ecd0e..b18166e0a 100644 --- a/examples/fungible-token/ft/Cargo.toml +++ b/examples/fungible-token/ft/Cargo.toml @@ -10,7 +10,10 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../../near-sdk" } near-contract-standards = { path = "../../../near-contract-standards" } -wasmcov = "0.2" +wasmcov = { version = "0.2", optional = true } [dev-dependencies] -near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } \ No newline at end of file +near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/fungible-token/ft/src/lib.rs b/examples/fungible-token/ft/src/lib.rs index 5415f3748..cc55c4598 100644 --- a/examples/fungible-token/ft/src/lib.rs +++ b/examples/fungible-token/ft/src/lib.rs @@ -31,6 +31,9 @@ use near_sdk::{ env, log, near, require, AccountId, BorshStorageKey, NearToken, PanicOnDefault, PromiseOrValue, }; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + #[derive(PanicOnDefault)] #[near(contract_state)] pub struct Contract { @@ -248,6 +251,3 @@ mod tests { assert_eq!(contract.ft_balance_of(accounts(1)).0, transfer_amount); } } - -#[cfg(target_family = "wasm")] -wasmcov::near::add_coverage!(); diff --git a/examples/fungible-token/res/.gitignore b/examples/fungible-token/res/.gitignore new file mode 100644 index 000000000..fae4549b6 --- /dev/null +++ b/examples/fungible-token/res/.gitignore @@ -0,0 +1,5 @@ +# Ignore all files +* + +# Except this to keep the directory +!.gitignore \ No newline at end of file diff --git a/examples/fungible-token/test-contract-defi/Cargo.toml b/examples/fungible-token/test-contract-defi/Cargo.toml index b9a5d90bc..4ae23344d 100644 --- a/examples/fungible-token/test-contract-defi/Cargo.toml +++ b/examples/fungible-token/test-contract-defi/Cargo.toml @@ -10,7 +10,10 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../../near-sdk" } near-contract-standards = { path = "../../../near-contract-standards" } -wasmcov = "0.2" +wasmcov = { version = "0.2", optional = true } [dev-dependencies] -near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } \ No newline at end of file +near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/fungible-token/test-contract-defi/src/lib.rs b/examples/fungible-token/test-contract-defi/src/lib.rs index 59d347fdc..8aaced549 100644 --- a/examples/fungible-token/test-contract-defi/src/lib.rs +++ b/examples/fungible-token/test-contract-defi/src/lib.rs @@ -5,6 +5,9 @@ use near_contract_standards::fungible_token::{receiver::FungibleTokenReceiver, B use near_sdk::json_types::U128; use near_sdk::{env, log, near, require, AccountId, Gas, PanicOnDefault, PromiseOrValue}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + const BASE_GAS: u64 = 5_000_000_000_000; const PROMISE_CALL: u64 = 5_000_000_000_000; const GAS_FOR_FT_ON_TRANSFER: Gas = Gas::from_gas(BASE_GAS + PROMISE_CALL); @@ -69,6 +72,3 @@ impl ValueReturnTrait for DeFi { PromiseOrValue::Value(amount.into()) } } - -#[cfg(target_family = "wasm")] -wasmcov::near::add_coverage!(); diff --git a/examples/fungible-token/tests/workspaces.rs b/examples/fungible-token/tests/workspaces.rs index 2652a3cf2..2b364faa7 100644 --- a/examples/fungible-token/tests/workspaces.rs +++ b/examples/fungible-token/tests/workspaces.rs @@ -107,7 +107,7 @@ async fn test_storage_deposit_not_enough_deposit() -> anyhow::Result<()> { contract.view_account().await?.balance.saturating_sub(contract_balance_before_deposit); // contract receives a gas rewards for the function call, so it should gain some NEAR assert!(contract_balance_diff > NearToken::from_near(0)); - assert!(contract_balance_diff < NearToken::from_yoctonear(30_000_000_000_000_000_000)); + assert!(contract_balance_diff < NearToken::from_yoctonear(100_000_000_000_000_000_000)); Ok(()) } @@ -144,7 +144,7 @@ async fn test_storage_deposit_minimal_deposit() -> anyhow::Result<()> { // new_account is charged the transaction fee, so it should loose a bit more than minimal_deposit assert!(new_account_balance_diff > minimal_deposit); assert!( - new_account_balance_diff < minimal_deposit.saturating_add(NearToken::from_millinear(1)) + new_account_balance_diff < minimal_deposit.saturating_add(NearToken::from_yoctonear(2_000_000_000_000_000_000_000)) ); let contract_balance_diff = @@ -154,9 +154,8 @@ async fn test_storage_deposit_minimal_deposit() -> anyhow::Result<()> { // adjust the upper limit of the assertion to be more flexible for small variations in the gas reward received assert!( contract_balance_diff - < minimal_deposit.saturating_add(NearToken::from_yoctonear(50_000_000_000_000_000_000)) + < minimal_deposit.saturating_add(NearToken::from_yoctonear(2_000_000_000_000_000_000_000)) ); - Ok(()) } @@ -246,7 +245,7 @@ async fn test_storage_deposit_refunds_excessive_deposit() -> anyhow::Result<()> assert!(contract_balance_diff > minimal_deposit); assert!( contract_balance_diff - < minimal_deposit.saturating_add(NearToken::from_yoctonear(50_000_000_000_000_000_000)) + < minimal_deposit.saturating_add(NearToken::from_yoctonear(150_000_000_000_000_000_000)) ); Ok(()) diff --git a/examples/lockable-fungible-token/Cargo.toml b/examples/lockable-fungible-token/Cargo.toml index 80fdf4151..cc38038f7 100644 --- a/examples/lockable-fungible-token/Cargo.toml +++ b/examples/lockable-fungible-token/Cargo.toml @@ -9,7 +9,7 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../near-sdk", features = ["legacy"] } -wasmcov = "0.2" +wasmcov = { version = "0.2", optional = true } [dev-dependencies] anyhow = "1.0" @@ -17,10 +17,13 @@ tokio = { version = "1.14", features = ["full"] } near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } near-workspaces = "0.11.1" +[features] +wasmcov = ["dep:wasmcov"] + [profile.release] codegen-units = 1 # Tell `rustc` to optimize for small code size. opt-level = "z" -lto = false +lto = true debug = false panic = "abort" diff --git a/examples/lockable-fungible-token/build.sh b/examples/lockable-fungible-token/build.sh index acf94a9c6..b719942fc 100755 --- a/examples/lockable-fungible-token/build.sh +++ b/examples/lockable-fungible-token/build.sh @@ -1,15 +1,14 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" -set -e -cd "$(dirname $0)" - -INCLUDE_COVERAGE="${1:-false}" - -if [ "$INCLUDE_COVERAGE" = "true" ]; then - cargo-wasmcov build --wasmcov-dir /Users/jrmncos/forks/near-sdk-rs/examples/wasmcov -- --all --target wasm32-unknown-unknown --release - cp ../wasmcov/target/wasm32-unknown-unknown/release/lockable_fungible_token.wasm ./res/ +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" else - cargo build --target wasm32-unknown-unknown --release - cp $TARGET/wasm32-unknown-unknown/release/lockable_fungible_token.wasm ./res/ -fi \ No newline at end of file + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + +set -e +cd "$(dirname $0)" +$BUILD_COMMAND --target wasm32-unknown-unknown --release +cp $TARGET/wasm32-unknown-unknown/release/lockable_fungible_token.wasm ./res/ \ No newline at end of file diff --git a/examples/lockable-fungible-token/src/lib.rs b/examples/lockable-fungible-token/src/lib.rs index 8f37be68b..c0df4fd2a 100644 --- a/examples/lockable-fungible-token/src/lib.rs +++ b/examples/lockable-fungible-token/src/lib.rs @@ -2,6 +2,9 @@ use near_sdk::collections::UnorderedMap; use near_sdk::{env, json_types::U128, near, AccountId, PanicOnDefault}; use std::collections::HashMap; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + type Balance = u128; #[derive(Default)] @@ -480,6 +483,3 @@ mod tests { contract.lock(carol(), U128(lock_amount)).unwrap_err(); } } - -#[cfg(target_family = "wasm")] -wasmcov::near::add_coverage!(); diff --git a/examples/mission-control/Cargo.toml b/examples/mission-control/Cargo.toml index c52b9ac9a..dd68982c8 100644 --- a/examples/mission-control/Cargo.toml +++ b/examples/mission-control/Cargo.toml @@ -9,15 +9,18 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../near-sdk" } -wasmcov = "0.2" +wasmcov = { version = "0.2", optional = true } [dev-dependencies] near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } +[features] +wasmcov = ["dep:wasmcov"] + [profile.release] codegen-units = 1 # Tell `rustc` to optimize for small code size. opt-level = "z" -lto = false +lto = true debug = false panic = "abort" diff --git a/examples/mission-control/build.sh b/examples/mission-control/build.sh index 86b12ff77..6ae795aae 100755 --- a/examples/mission-control/build.sh +++ b/examples/mission-control/build.sh @@ -1,15 +1,15 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" -set -e -cd "$(dirname $0)" - -INCLUDE_COVERAGE="${1:-false}" -if [ "$INCLUDE_COVERAGE" = "true" ]; then - cargo-wasmcov build --wasmcov-dir /Users/jrmncos/forks/near-sdk-rs/examples/wasmcov -- --all --target wasm32-unknown-unknown --release - cp ../wasmcov/target/wasm32-unknown-unknown/release/mission_control.wasm ./res/ +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" else - cargo build --target wasm32-unknown-unknown --release - cp $TARGET/wasm32-unknown-unknown/release/mission_control.wasm ./res/ - #wasm-opt -Oz --output ./res/mission_control.wasm ./res/mission_control.wasm -fi \ No newline at end of file + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + +set -e +cd "$(dirname $0)" +$BUILD_COMMAND --target wasm32-unknown-unknown --release +cp $TARGET/wasm32-unknown-unknown/release/mission_control.wasm ./res/ +#wasm-opt -Oz --output ./res/mission_control.wasm ./res/mission_control.wasm \ No newline at end of file diff --git a/examples/mission-control/src/lib.rs b/examples/mission-control/src/lib.rs index cbf0092d5..e07efc09f 100644 --- a/examples/mission-control/src/lib.rs +++ b/examples/mission-control/src/lib.rs @@ -6,5 +6,5 @@ mod macros; mod mission_control; mod rate; -#[cfg(target_family = "wasm")] +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] wasmcov::near::add_coverage!(); diff --git a/examples/non-fungible-token/Cargo.toml b/examples/non-fungible-token/Cargo.toml index b98019365..acc4f35f8 100644 --- a/examples/non-fungible-token/Cargo.toml +++ b/examples/non-fungible-token/Cargo.toml @@ -4,6 +4,9 @@ version = "0.0.2" authors = ["Near Inc "] edition = "2021" +[dependencies] +wasmcov = { version = "0.2", optional = true } + [dev-dependencies] anyhow = "1.0" near-contract-standards = { path = "../../near-contract-standards" } @@ -15,7 +18,7 @@ near-workspaces = "0.11.1" codegen-units = 1 # Tell `rustc` to optimize for small code size. opt-level = "z" -lto = false +lto = true debug = false panic = "abort" overflow-checks = true @@ -23,3 +26,6 @@ overflow-checks = true [workspace] # remember to include a member for each contract members = ["nft", "test-approval-receiver", "test-token-receiver"] + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/non-fungible-token/build.sh b/examples/non-fungible-token/build.sh index 7b7d51f26..b339c3845 100755 --- a/examples/non-fungible-token/build.sh +++ b/examples/non-fungible-token/build.sh @@ -1,18 +1,16 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" -set -e -cd "$(dirname $0)" - -INCLUDE_COVERAGE="${1:-false}" -if [ "$INCLUDE_COVERAGE" = "true" ]; then - cargo-wasmcov build --wasmcov-dir /Users/jrmncos/forks/near-sdk-rs/examples/wasmcov -- --all --target wasm32-unknown-unknown --release - cp ../wasmcov/target/wasm32-unknown-unknown/release/approval_receiver.wasm ./res/ - cp ../wasmcov/target/wasm32-unknown-unknown/release/non_fungible_token.wasm ./res/ - cp ../wasmcov/target/wasm32-unknown-unknown/release/token_receiver.wasm ./res/ +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" else - cargo build --all --target wasm32-unknown-unknown --release - cp $TARGET/wasm32-unknown-unknown/release/approval_receiver.wasm ./res/ - cp $TARGET/wasm32-unknown-unknown/release/non_fungible_token.wasm ./res/ - cp $TARGET/wasm32-unknown-unknown/release/token_receiver.wasm ./res/ -fi \ No newline at end of file + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + +set -e +cd "$(dirname $0)" +$BUILD_COMMAND --all --target wasm32-unknown-unknown --release +cp $TARGET/wasm32-unknown-unknown/release/approval_receiver.wasm ./res/ +cp $TARGET/wasm32-unknown-unknown/release/non_fungible_token.wasm ./res/ +cp $TARGET/wasm32-unknown-unknown/release/token_receiver.wasm ./res/ \ No newline at end of file diff --git a/examples/non-fungible-token/nft/Cargo.toml b/examples/non-fungible-token/nft/Cargo.toml index 72ac5d4b9..489456eb5 100644 --- a/examples/non-fungible-token/nft/Cargo.toml +++ b/examples/non-fungible-token/nft/Cargo.toml @@ -10,7 +10,10 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../../near-sdk" } near-contract-standards = { path = "../../../near-contract-standards" } -wasmcov = "0.2" +wasmcov = { version = "0.2", optional = true } [dev-dependencies] -near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } \ No newline at end of file +near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/non-fungible-token/nft/src/lib.rs b/examples/non-fungible-token/nft/src/lib.rs index 8acd1841c..88d6dbab8 100644 --- a/examples/non-fungible-token/nft/src/lib.rs +++ b/examples/non-fungible-token/nft/src/lib.rs @@ -32,6 +32,9 @@ use near_sdk::{ }; use std::collections::HashMap; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + #[derive(PanicOnDefault)] #[near(contract_state)] pub struct Contract { @@ -436,6 +439,3 @@ mod tests { assert!(!contract.nft_is_approved(token_id.clone(), accounts(1), Some(1))); } } - -#[cfg(target_family = "wasm")] -wasmcov::near::add_coverage!(); diff --git a/examples/non-fungible-token/test-approval-receiver/Cargo.toml b/examples/non-fungible-token/test-approval-receiver/Cargo.toml index 1642d1dee..288be0ffe 100644 --- a/examples/non-fungible-token/test-approval-receiver/Cargo.toml +++ b/examples/non-fungible-token/test-approval-receiver/Cargo.toml @@ -10,7 +10,10 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../../near-sdk" } near-contract-standards = { path = "../../../near-contract-standards" } -wasmcov = "0.2" +wasmcov = { version = "0.2", optional = true } [dev-dependencies] near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/non-fungible-token/test-approval-receiver/src/lib.rs b/examples/non-fungible-token/test-approval-receiver/src/lib.rs index 57404dbda..a5ef46bfd 100644 --- a/examples/non-fungible-token/test-approval-receiver/src/lib.rs +++ b/examples/non-fungible-token/test-approval-receiver/src/lib.rs @@ -5,6 +5,9 @@ use near_contract_standards::non_fungible_token::approval::NonFungibleTokenAppro use near_contract_standards::non_fungible_token::TokenId; use near_sdk::{env, log, near, require, AccountId, Gas, PanicOnDefault, PromiseOrValue}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + /// It is estimated that we need to attach 5 TGas for the code execution and 5 TGas for cross-contract call const GAS_FOR_NFT_ON_APPROVE: Gas = Gas::from_tgas(10); @@ -75,6 +78,3 @@ impl ValueReturnTrait for ApprovalReceiver { PromiseOrValue::Value(msg) } } - -#[cfg(target_family = "wasm")] -wasmcov::near::add_coverage!(); diff --git a/examples/non-fungible-token/test-token-receiver/Cargo.toml b/examples/non-fungible-token/test-token-receiver/Cargo.toml index 345f4e78a..a85fd48e3 100644 --- a/examples/non-fungible-token/test-token-receiver/Cargo.toml +++ b/examples/non-fungible-token/test-token-receiver/Cargo.toml @@ -10,7 +10,10 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../../near-sdk" } near-contract-standards = { path = "../../../near-contract-standards" } -wasmcov = "0.2" +wasmcov = { version = "0.2", optional = true } [dev-dependencies] -near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } \ No newline at end of file +near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/non-fungible-token/test-token-receiver/src/lib.rs b/examples/non-fungible-token/test-token-receiver/src/lib.rs index 1a159d3b2..96cb07b35 100644 --- a/examples/non-fungible-token/test-token-receiver/src/lib.rs +++ b/examples/non-fungible-token/test-token-receiver/src/lib.rs @@ -5,6 +5,9 @@ use near_contract_standards::non_fungible_token::core::NonFungibleTokenReceiver; use near_contract_standards::non_fungible_token::TokenId; use near_sdk::{env, log, near, require, AccountId, Gas, PanicOnDefault, PromiseOrValue}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + /// It is estimated that we need to attach 5 TGas for the code execution and 5 TGas for cross-contract call const GAS_FOR_NFT_ON_TRANSFER: Gas = Gas::from_tgas(10); @@ -86,6 +89,3 @@ impl ValueReturnTrait for TokenReceiver { PromiseOrValue::Value(return_it) } } - -#[cfg(target_family = "wasm")] -wasmcov::near::add_coverage!(); diff --git a/examples/status-message/Cargo.toml b/examples/status-message/Cargo.toml index 2d7211974..b95f3e525 100644 --- a/examples/status-message/Cargo.toml +++ b/examples/status-message/Cargo.toml @@ -9,15 +9,18 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../near-sdk" } -wasmcov = "0.2" +wasmcov = { version = "0.2", optional = true } [dev-dependencies] near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } +[features] +wasmcov = ["dep:wasmcov"] + [profile.release] codegen-units = 1 # Tell `rustc` to optimize for small code size. opt-level = "z" -lto = false +lto = true debug = false panic = "abort" diff --git a/examples/status-message/build.sh b/examples/status-message/build.sh index ddf663c2b..f5679b9f7 100755 --- a/examples/status-message/build.sh +++ b/examples/status-message/build.sh @@ -1,15 +1,15 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" -set -e -cd "$(dirname $0)" - -INCLUDE_COVERAGE="${1:-false}" -if [ "$INCLUDE_COVERAGE" = "true" ]; then - cargo-wasmcov build --wasmcov-dir /Users/jrmncos/forks/near-sdk-rs/examples/wasmcov -- --all --target wasm32-unknown-unknown --release - cp ../wasmcov/target/wasm32-unknown-unknown/release/status_message.wasm ./res/ +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" else - cargo build --target wasm32-unknown-unknown --release - cp $TARGET/wasm32-unknown-unknown/release/status_message.wasm ./res/ - #wasm-opt -Oz --output ./res/status_message.wasm ./res/status_message.wasm -fi \ No newline at end of file + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + +set -e +cd "$(dirname $0)" +$BUILD_COMMAND --target wasm32-unknown-unknown --release +cp $TARGET/wasm32-unknown-unknown/release/status_message.wasm ./res/ +#wasm-opt -Oz --output ./res/status_message.wasm ./res/status_message.wasm \ No newline at end of file diff --git a/examples/status-message/src/lib.rs b/examples/status-message/src/lib.rs index 1c8c90044..ac8e7cd7e 100644 --- a/examples/status-message/src/lib.rs +++ b/examples/status-message/src/lib.rs @@ -1,6 +1,9 @@ use near_sdk::store::LookupMap; use near_sdk::{env, log, near, AccountId, BorshStorageKey}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + #[derive(BorshStorageKey)] #[near] struct RecordsKey; @@ -71,6 +74,3 @@ mod tests { assert_eq!(get_logs(), vec!["get_status for account_id francis.near"]) } } - -#[cfg(target_family = "wasm")] -wasmcov::near::add_coverage!(); diff --git a/examples/test-contract/Cargo.toml b/examples/test-contract/Cargo.toml index afd25d2aa..d9cd38c78 100644 --- a/examples/test-contract/Cargo.toml +++ b/examples/test-contract/Cargo.toml @@ -9,15 +9,18 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../near-sdk" } -wasmcov = "0.2" +wasmcov = { version = "0.2", optional = true } [profile.release] codegen-units = 1 # Tell `rustc` to optimize for small code size. opt-level = "z" -lto = false +lto = true debug = false panic = "abort" [dev-dependencies] -near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } +near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/test-contract/build.sh b/examples/test-contract/build.sh index 7cb89ee94..c535116c5 100755 --- a/examples/test-contract/build.sh +++ b/examples/test-contract/build.sh @@ -1,15 +1,15 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" -set -e -cd "$(dirname $0)" - -INCLUDE_COVERAGE="${1:-false}" -if [ "$INCLUDE_COVERAGE" = "true" ]; then - cargo-wasmcov build --wasmcov-dir /Users/jrmncos/forks/near-sdk-rs/examples/wasmcov -- --all --target wasm32-unknown-unknown --release - cp ../wasmcov/target/wasm32-unknown-unknown/release/test_contract.wasm ./res/ +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" else - cargo build --target wasm32-unknown-unknown --release - cp $TARGET/wasm32-unknown-unknown/release/test_contract.wasm ./res/ - #wasm-opt -Oz --output ./res/test_contract.wasm ./res/test_contract.wasm -fi \ No newline at end of file + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + +set -e +cd "$(dirname $0)" +$BUILD_COMMAND --target wasm32-unknown-unknown --release +cp $TARGET/wasm32-unknown-unknown/release/test_contract.wasm ./res/ +#wasm-opt -Oz --output ./res/test_contract.wasm ./res/test_contract.wasm \ No newline at end of file diff --git a/examples/test-contract/src/lib.rs b/examples/test-contract/src/lib.rs index a0522c5d0..036c6f322 100644 --- a/examples/test-contract/src/lib.rs +++ b/examples/test-contract/src/lib.rs @@ -1,5 +1,8 @@ use near_sdk::{env, near}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + #[near(contract_state)] pub struct TestContract {} @@ -45,6 +48,3 @@ mod tests { contract.test_panic_macro(); } } - -#[cfg(target_family = "wasm")] -wasmcov::near::add_coverage!(); diff --git a/examples/versioned/Cargo.toml b/examples/versioned/Cargo.toml index ec3ecf694..6e8dfbcff 100644 --- a/examples/versioned/Cargo.toml +++ b/examples/versioned/Cargo.toml @@ -9,15 +9,18 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../near-sdk", features = ["unstable", "unit-testing"] } -wasmcov = "0.2" +wasmcov = { version = "0.2", optional = true } [profile.release] codegen-units = 1 # Tell `rustc` to optimize for small code size. opt-level = "z" -lto = false +lto = true debug = false panic = "abort" [dev-dependencies] -near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } \ No newline at end of file +near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/versioned/build.sh b/examples/versioned/build.sh index 5945b1e14..f9d3626d1 100755 --- a/examples/versioned/build.sh +++ b/examples/versioned/build.sh @@ -1,14 +1,14 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" -set -e -cd "$(dirname $0)" - -INCLUDE_COVERAGE="${1:-false}" -if [ "$INCLUDE_COVERAGE" = "true" ]; then - cargo-wasmcov build --wasmcov-dir /Users/jrmncos/forks/near-sdk-rs/examples/wasmcov -- --all --target wasm32-unknown-unknown --release - cp ../wasmcov/target/wasm32-unknown-unknown/release/versioned.wasm ./res/ +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" else - cargo build --target wasm32-unknown-unknown --release - cp $TARGET/wasm32-unknown-unknown/release/versioned.wasm ./res/ -fi \ No newline at end of file + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + +set -e +cd "$(dirname $0)" +$BUILD_COMMAND --target wasm32-unknown-unknown --release +cp $TARGET/wasm32-unknown-unknown/release/versioned.wasm ./res/ \ No newline at end of file diff --git a/examples/versioned/src/lib.rs b/examples/versioned/src/lib.rs index d1fa5baa8..85e45e3a0 100644 --- a/examples/versioned/src/lib.rs +++ b/examples/versioned/src/lib.rs @@ -1,6 +1,9 @@ use near_sdk::store::IterableMap; use near_sdk::{env, log, near, AccountId, NearToken}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + /// An example of a versioned contract. This is a simple contract that tracks how much /// each account deposits into the contract. In v1, a nonce is added to state which increments /// after each successful deposit. @@ -144,6 +147,3 @@ mod tests { assert_eq!(contract.get_deposit(&bob()), Some(&NearToken::from_yoctonear(8))); } } - -#[cfg(target_family = "wasm")] -wasmcov::near::add_coverage!();