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: improve dynamic link contracts' tests and add them to github workflow #221

Merged
merged 5 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
113 changes: 112 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,98 @@ jobs:
exit 1
fi

contract_dynamic_callee_contract:
name: dynamic_callee_contract
runs-on: ubuntu-latest
env:
working-directory: ./contracts/dynamic-callee-contract
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: 1.57.0
target: wasm32-unknown-unknown
profile: minimal
override: true
- name: Cache cargo
uses: actions/cache@v2
with:
path: ~/.cargo
key: cargocache-v2-contract_dynamic_callee_contract-rust:1.57.0-${{ hashFiles('contracts/dynamic-callee-contract/Cargo.lock') }}
- name: Version information
run: rustc --version; cargo --version; rustup --version; rustup target list --installed
- name: Add wasm32 target
run: rustup target add wasm32-unknown-unknown && rustup target list --installed
- name: Build wasm binary
working-directory: ${{env.working-directory}}
run: cargo wasm --locked
- name: Unit tests
working-directory: ${{env.working-directory}}
run: cargo unit-test --locked
- name: Integration tests (singlepass backend)
working-directory: ${{env.working-directory}}
run: cargo integration-test --locked --no-default-features
- name: Build and run schema generator
working-directory: ${{env.working-directory}}
run: cargo schema --locked
- name: Ensure schemas are up-to-date
working-directory: ${{env.working-directory}}
run: |
CHANGES_IN_REPO=$(git status --porcelain)
if [[ -n "$CHANGES_IN_REPO" ]]; then
echo "Repository is dirty. Showing 'git status' and 'git --no-pager diff' for debugging now:"
git status && git --no-pager diff
exit 1
fi

contract_dynamic_caller_contract:
name: dynamic_caller_contract
runs-on: ubuntu-latest
env:
working-directory: ./contracts/dynamic-caller-contract
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: 1.57.0
target: wasm32-unknown-unknown
profile: minimal
override: true
- name: Cache cargo
uses: actions/cache@v2
with:
path: ~/.cargo
key: cargocache-v2-contract_dynamic_caller_contract-rust:1.57.0-${{ hashFiles('contracts/dynamic-caller-contract/Cargo.lock') }}
- name: Version information
run: rustc --version; cargo --version; rustup --version; rustup target list --installed
- name: Add wasm32 target
run: rustup target add wasm32-unknown-unknown && rustup target list --installed
- name: Build wasm binary
working-directory: ${{env.working-directory}}
run: cargo wasm --locked
- name: Unit tests
working-directory: ${{env.working-directory}}
run: cargo unit-test --locked
- name: Integration tests (singlepass backend)
working-directory: ${{env.working-directory}}
run: cargo integration-test --locked --no-default-features
- name: Build and run schema generator
working-directory: ${{env.working-directory}}
run: cargo schema --locked
- name: Ensure schemas are up-to-date
working-directory: ${{env.working-directory}}
run: |
CHANGES_IN_REPO=$(git status --porcelain)
if [[ -n "$CHANGES_IN_REPO" ]]; then
echo "Repository is dirty. Showing 'git status' and 'git --no-pager diff' for debugging now:"
git status && git --no-pager diff
exit 1
fi

contract_number:
name: contract_number
runs-on: ubuntu-latest
Expand Down Expand Up @@ -599,6 +691,7 @@ jobs:
git status && git --no-pager diff
exit 1
fi

contract_call_number:
name: contract_call_number
runs-on: ubuntu-latest
Expand Down Expand Up @@ -689,6 +782,12 @@ jobs:
- name: Check formatting of contract staking
working-directory: ${{env.root-directory}}/staking
run: cargo fmt -- --check
- name: Check formatting of contract dynamic callee contract
working-directory: ${{env.root-directory}}/dynamic-callee-contract
run: cargo fmt -- --check
- name: Check formatting of contract dynamic caller contract
working-directory: ${{env.root-directory}}/dynamic-caller-contract
run: cargo fmt -- --check

clippy:
name: clippy
Expand Down Expand Up @@ -790,7 +889,19 @@ jobs:
mkdir -p target/wasm32-unknown-unknown/release
touch target/wasm32-unknown-unknown/release/staking.wasm
cargo clippy --tests -- -D warnings
# These two tests issue errors in 1.51.0. In 1.57.0, they succeeds.
# These four tests issue errors in 1.51.0. In 1.57.0, they succeeds.
# - name: Clippy linting on dynamic-callee-contract
# working-directory: ${{env.contracts}}/dynamic-callee-contract
# run: |
# mkdir -p target/wasm32-unknown-unknown/release
# touch target/wasm32-unknown-unknown/release/dynamic_callee_contract.wasm
# cargo clippy --tests -- -D warnings
# - name: Clippy linting on dynamic-caller-contract
# working-directory: ${{env.contracts}}/dynamic-caller-contract
# run: |
# mkdir -p target/wasm32-unknown-unknown/release
# touch target/wasm32-unknown-unknown/release/dynamic_caller_contract.wasm
# cargo clippy --tests -- -D warnings
# - name: Clippy linting on number
# working-directory: ${{env.contracts}}/number
# run: |
Expand Down
3 changes: 1 addition & 2 deletions contracts/dynamic-callee-contract/examples/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs::create_dir_all;

use cosmwasm_schema::{export_schema, remove_schemas, schema_for};

use dynamic_callee_contract::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use dynamic_callee_contract::msg::{ExecuteMsg, InstantiateMsg};

fn main() {
let mut out_dir = current_dir().unwrap();
Expand All @@ -13,5 +13,4 @@ fn main() {

export_schema(&schema_for!(InstantiateMsg), &out_dir);
export_schema(&schema_for!(ExecuteMsg), &out_dir);
export_schema(&schema_for!(QueryMsg), &out_dir);
}
6 changes: 0 additions & 6 deletions contracts/dynamic-callee-contract/schema/query_msg.json

This file was deleted.

54 changes: 32 additions & 22 deletions contracts/dynamic-callee-contract/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ fn required_exports() -> Vec<(String, FunctionType)> {
String::from("stub_pong_with_tuple"),
([Type::I32], [Type::I32]).into(),
),
(
String::from("stub_pong_with_tuple_takes_2_args"),
([Type::I32, Type::I32], [Type::I32]).into(),
),
(String::from("stub_pong_env"), ([], [Type::I32]).into()),
]
}
Expand Down Expand Up @@ -59,7 +63,7 @@ fn callable_point_export_works() {
Some(exported_function) => {
assert_eq!(*exported_function, required_export.1);
}
None => assert!(false),
None => panic!("{} is not exported.", required_export.0),
}
}
}
Expand All @@ -72,9 +76,11 @@ fn callable_point_pong_works() {
let param_region_ptr = write_data_to_mock_env(&instance.env, &serialized_param).unwrap();

let required_exports = required_exports();
let export_index = 0;
assert_eq!("stub_pong".to_string(), required_exports[export_index].0);
let call_result = instance
.call_function_strict(
&required_exports[0].1,
&required_exports[export_index].1,
"stub_pong",
&[param_region_ptr.into()],
)
Expand All @@ -99,9 +105,14 @@ fn callable_point_pong_with_struct_works() {
let param_region_ptr = write_data_to_mock_env(&instance.env, &serialized_param).unwrap();

let required_exports = required_exports();
let export_index = 1;
assert_eq!(
"stub_pong_with_struct".to_string(),
required_exports[export_index].0
);
let call_result = instance
.call_function_strict(
&required_exports[1].1,
&required_exports[export_index].1,
"stub_pong_with_struct",
&[param_region_ptr.into()],
)
Expand All @@ -123,9 +134,14 @@ fn callable_point_pong_with_tuple_works() {
let param_region_ptr = write_data_to_mock_env(&instance.env, &serialized_param).unwrap();

let required_exports = required_exports();
let export_index = 2;
assert_eq!(
"stub_pong_with_tuple".to_string(),
required_exports[export_index].0
);
let call_result = instance
.call_function_strict(
&required_exports[1].1,
&required_exports[export_index].1,
"stub_pong_with_tuple",
&[param_region_ptr.into()],
)
Expand All @@ -150,9 +166,14 @@ fn callable_point_pong_with_tuple_takes_2_args_works() {
let param_region_ptr2 = write_data_to_mock_env(&instance.env, &serialized_param2).unwrap();

let required_exports = required_exports();
let export_index = 3;
assert_eq!(
"stub_pong_with_tuple_takes_2_args".to_string(),
required_exports[export_index].0
);
let call_result = instance
.call_function_strict(
&required_exports[1].1,
&required_exports[export_index].1,
"stub_pong_with_tuple_takes_2_args",
&[param_region_ptr1.into(), param_region_ptr2.into()],
)
Expand All @@ -174,8 +195,13 @@ fn callable_point_pong_env_works() {
instance
.env
.set_serialized_env(&to_vec(&mock_env()).unwrap());
let export_index = 4;
assert_eq!(
"stub_pong_env".to_string(),
required_exports[export_index].0
);
let call_result = instance
.call_function_strict(&required_exports[2].1, "stub_pong_env", &[])
.call_function_strict(&required_exports[export_index].1, "stub_pong_env", &[])
.unwrap();
assert_eq!(call_result.len(), 1);

Expand All @@ -184,19 +210,3 @@ fn callable_point_pong_env_works() {
let result: Env = from_slice(&serialized_return).unwrap();
assert_eq!(result.contract.address, Addr::unchecked(MOCK_CONTRACT_ADDR));
}

#[test]
fn callable_point_pong_deps_works() {
let instance = make_callee_instance();

let required_exports = required_exports();
let call_result = instance
.call_function_strict(&required_exports[1].1, "stub_pong_env", &[])
.unwrap();
assert_eq!(call_result.len(), 1);

let serialized_return =
read_data_from_mock_env(&instance.env, &call_result[0], u32::MAX as usize).unwrap();
let result: Env = from_slice(&serialized_return).unwrap();
assert_eq!(result.contract.address, Addr::unchecked("cosmos2contract"));
}
16 changes: 8 additions & 8 deletions contracts/dynamic-caller-contract/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ fn dynamic_link_import_works() {
.collect();

let required_imports = required_imports();
for required_export in required_imports {
match import_function_map.get(&required_export.0) {
Some(exported_function) => {
let module_name = &exported_function.0;
let function_type = &exported_function.1;
assert_eq!(*module_name, required_export.1);
assert_eq!(*function_type, required_export.2);
for required_import in required_imports {
match import_function_map.get(&required_import.0) {
Some(imported_function) => {
let module_name = &imported_function.0;
let function_type = &imported_function.1;
assert_eq!(*module_name, required_import.1);
assert_eq!(*function_type, required_import.2);
}
None => assert!(false),
None => panic!("{} is not imported.", required_import.0),
}
}
}