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

feat: adding wasm coverage #1233

Draft
wants to merge 41 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
8ebaf17
feat: adding wasm coveage
jrmncos Aug 3, 2024
2fe5f31
fix: renaming coverage job
jrmncos Aug 3, 2024
1fedaeb
fix: renaming
jrmncos Aug 3, 2024
470581b
fixing build
jrmncos Aug 3, 2024
8c9ac5c
adding some rust-src components
jrmncos Aug 3, 2024
c036b7e
removing macOS config
jrmncos Aug 3, 2024
5f4500a
using macOS config
jrmncos Aug 3, 2024
c3c181b
using help suggestion
jrmncos Aug 3, 2024
a1144c9
using default toolchain nightly
jrmncos Aug 3, 2024
0eb5575
minor fix
jrmncos Aug 3, 2024
a6682fe
using the same toolchain to rust-src component
jrmncos Aug 3, 2024
818f988
adding aarch64-apple
jrmncos Aug 3, 2024
6b2295e
using nightly latest
jrmncos Aug 3, 2024
f13648a
adding wasmcov config to main directories
jrmncos Aug 3, 2024
28c43e2
removing wasmcov from near-sys
jrmncos Aug 3, 2024
5efc293
testing with cargo wasmcov
jrmncos Aug 3, 2024
efb5789
getting llvm version
jrmncos Aug 3, 2024
f25452b
installing llvm
jrmncos Aug 3, 2024
41bbef2
adding llvm to path
jrmncos Aug 3, 2024
3a43c21
rollbacking sdk files
jrmncos Aug 4, 2024
82feaae
minor fix
jrmncos Aug 4, 2024
7d3f7f7
creating script to run and build /examples with wasmcov
jrmncos Aug 5, 2024
1ce9e69
minor fix
jrmncos Aug 5, 2024
dbc6d9a
fixing formatting
jrmncos Aug 5, 2024
00b7d6d
sending toolchain version to script
jrmncos Aug 5, 2024
eb94596
using same cargo version as github actions in script
jrmncos Aug 5, 2024
78f4e18
minor fix
jrmncos Aug 5, 2024
715b70e
minor change
jrmncos Aug 5, 2024
0d4e22e
using wasmcov test in script
jrmncos Aug 5, 2024
a5ca548
testing with older nightly version
jrmncos Aug 5, 2024
ac230f0
using nightly-aarch64-apple-darwin as main version
jrmncos Aug 5, 2024
0d4b12a
fixing test with wasmcov
jrmncos Aug 7, 2024
be6ddd5
Merge branch 'master' into feat/adding-wasmcov
jrmncos Aug 15, 2024
dbdeef1
using base example from wasmcov to all examples directories
jrmncos Aug 16, 2024
ab6c619
adding main script to build and generate lcov files
jrmncos Aug 18, 2024
232e9c8
changing coverage-example name
jrmncos Aug 18, 2024
fe013da
adding platform
jrmncos Aug 18, 2024
ca96ca0
updating time
jrmncos Aug 18, 2024
6db679f
updating nightly version
jrmncos Aug 18, 2024
15d6ee4
adding cargo tree to verify time version
jrmncos Aug 18, 2024
9937bf1
fixing merge_profdata.sh
jrmncos Aug 19, 2024
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
67 changes: 67 additions & 0 deletions .github/workflows/coverage-examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Coverage examples
on:
push:
branches:
- master
pull_request:
env:
RUSTFLAGS: "-D warnings"

jobs:
coverage:
runs-on: ${{ matrix.platform }}
name: "${{ matrix.example }} - ${{ matrix.platform }}"
strategy:
matrix:
platform: [macos-latest]
toolchain: [nightly-2024-05-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: time version
run: cargo tree | grep time
- name: Build examples
env:
RUSTFLAGS: '-C link-arg=-s'
run: cd ./examples && ./build_wasm_test_all.sh
- name: Merge profdata files
env:
RUSTFLAGS: '-C link-arg=-s'
run: cd ./examples && ./merge_profdata.sh
4 changes: 4 additions & 0 deletions examples/adder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../near-sdk" }
wasmcov = { version = "0.2", optional = true }

[dev-dependencies]
near-workspaces = "0.11.1"
Expand All @@ -18,6 +19,9 @@ 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.
Expand Down
15 changes: 11 additions & 4 deletions examples/adder/build.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#!/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)"

cargo build --target wasm32-unknown-unknown --release
$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
#wasm-opt -Oz --output ./res/status_message.wasm ./res/status_message.wasm
5 changes: 4 additions & 1 deletion examples/adder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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);

Expand Down Expand Up @@ -88,4 +91,4 @@ mod tests {

Ok(())
}
}
}
19 changes: 19 additions & 0 deletions examples/build_wasm_test_all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

for d in "status-message" $(ls -d */ | grep -v -e "status-message\/$"); do
if [ -d "$d" ]; then
echo "Processing directory $d"
echo "Building"
cd $d;
pwd
./build.sh
echo "Testing"
cargo wasmcov test --near=1.40.0
cd ..
echo "End of processing of directory $d"
fi
done




4 changes: 4 additions & 0 deletions examples/callback-results/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../near-sdk" }
wasmcov = { version = "0.2", optional = true }

[dev-dependencies]
near-workspaces = "0.11.1"
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.
Expand Down
14 changes: 11 additions & 3 deletions examples/callback-results/build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#!/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)"
cargo build --target wasm32-unknown-unknown --release
cp $TARGET/wasm32-unknown-unknown/release/callback_results.wasm ./res/
$BUILD_COMMAND --target wasm32-unknown-unknown --release
cp $TARGET/wasm32-unknown-unknown/release/callback_results.wasm ./res/
5 changes: 4 additions & 1 deletion examples/callback-results/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -122,4 +125,4 @@ mod tests {

Ok(())
}
}
}
6 changes: 6 additions & 0 deletions examples/cross-contract-calls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ version = "0.1.0"
authors = ["Near Inc <[email protected]>"]
edition = "2021"

[dependencies]
wasmcov = { version = "0.2", optional = true }

[dev-dependencies]
anyhow = "1.0"
near-sdk = { path = "../../near-sdk", features = ["default", "unit-testing"] }
Expand All @@ -22,5 +25,8 @@ lto = true
debug = false
panic = "abort"

[features]
wasmcov = ["dep:wasmcov"]

[workspace]
members = ["high-level", "low-level"]
15 changes: 11 additions & 4 deletions examples/cross-contract-calls/build.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#!/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)"

cargo build --all --target wasm32-unknown-unknown --release
$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/
cp $TARGET/wasm32-unknown-unknown/release/cross_contract_low_level.wasm ./res/
4 changes: 4 additions & 0 deletions examples/cross-contract-calls/high-level/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../../near-sdk" , features = ["default", "unit-testing"] }
wasmcov = { version = "0.2", optional = true }

[features]
wasmcov = ["dep:wasmcov"]
3 changes: 3 additions & 0 deletions examples/cross-contract-calls/high-level/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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 {}
Expand Down
4 changes: 4 additions & 0 deletions examples/cross-contract-calls/low-level/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../../near-sdk" , features = ["default", "unit-testing"] }
wasmcov = { version = "0.2", optional = true }

[features]
wasmcov = ["dep:wasmcov"]
3 changes: 3 additions & 0 deletions examples/cross-contract-calls/low-level/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
6 changes: 6 additions & 0 deletions examples/factory-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ version = "0.1.0"
authors = ["Near Inc <[email protected]>"]
edition = "2021"

[dependencies]
wasmcov = { version = "0.2", optional = true }

[dev-dependencies]
anyhow = "1.0"
test-case = "2.0"
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.
Expand Down
14 changes: 11 additions & 3 deletions examples/factory-contract/build.sh
Original file line number Diff line number Diff line change
@@ -1,8 +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)"

cargo build --all --target wasm32-unknown-unknown --release
$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/
cp $TARGET/wasm32-unknown-unknown/release/factory_contract_low_level.wasm ./res/
4 changes: 4 additions & 0 deletions examples/factory-contract/high-level/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../../near-sdk" }
wasmcov = { version = "0.2", optional = true }

[dev-dependencies]
near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] }

[features]
wasmcov = ["dep:wasmcov"]
3 changes: 3 additions & 0 deletions examples/factory-contract/high-level/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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 {}
Expand Down
4 changes: 4 additions & 0 deletions examples/factory-contract/low-level/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../../near-sdk" }
wasmcov = { version = "0.2", optional = true }

[dev-dependencies]
near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] }

[features]
wasmcov = ["dep:wasmcov"]
3 changes: 3 additions & 0 deletions examples/factory-contract/low-level/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 6 additions & 0 deletions examples/fungible-token/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ version = "0.0.2"
authors = ["Near Inc <[email protected]>"]
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.
Expand Down
14 changes: 11 additions & 3 deletions examples/fungible-token/build.sh
Original file line number Diff line number Diff line change
@@ -1,8 +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)"

cargo build --all --target wasm32-unknown-unknown --release
$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/
cp $TARGET/wasm32-unknown-unknown/release/fungible_token.wasm ./res/
Loading
Loading