Skip to content

Commit

Permalink
Create rust unit test command
Browse files Browse the repository at this point in the history
  • Loading branch information
bitdivine committed Sep 18, 2024
1 parent cc53dc5 commit f3223dd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
38 changes: 19 additions & 19 deletions scripts/did.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
#!/usr/bin/env bash

did_file_location_from_cargo() {
# Warning: Does no _- conversion.
cargo metadata --format-version 1 | jq -r --arg v "$1" '.packages[] | select(.name==$v) | .manifest_path | sub("Cargo.toml";"\($v).did")'
# Warning: Does no _- conversion.
cargo metadata --format-version 1 | jq -r --arg v "$1" '.packages[] | select(.name==$v) | .manifest_path | sub("Cargo.toml";"\($v).did")'
}
cargo_manifest_path() {
# Warning: Does no _- conversion.
cargo metadata --format-version 1 | jq -r --arg v "$1" '.packages[] | select(.name==$v) | .manifest_path")'
# Warning: Does no _- conversion.
cargo metadata --format-version 1 | jq -r --arg v "$1" '.packages[] | select(.name==$v) | .manifest_path")'
}
did_file_location_from_dfx_json() {
# Warning: Does no _- conversion.
jq -r --arg v "$1" '.canisters[$v].candid' dfx.json
# Warning: Does no _- conversion.
jq -r --arg v "$1" '.canisters[$v].candid' dfx.json
}

function generate_did() {
local canister=$1
echo "Deriving candid file from Rust for $canister"
#local manifest_path="$(cargo_manifest_path "$canister")"
#local candid_file="${manifest_path%Cargo.toml}$canister.did"
local candid_file="$(did_file_location_from_dfx_json "$canister")"
local canister=$1
echo "Deriving candid file from Rust for $canister"
#local manifest_path="$(cargo_manifest_path "$canister")"
#local candid_file="${manifest_path%Cargo.toml}$canister.did"
local candid_file="$(did_file_location_from_dfx_json "$canister")"

test -e "target/wasm32-unknown-unknown/release/$canister.wasm" ||
cargo build -p "$canister" \
--target wasm32-unknown-unknown \
--release --package "$canister"
test -e "target/wasm32-unknown-unknown/release/$canister.wasm" ||
cargo build -p "$canister" \
--target wasm32-unknown-unknown \
--release --package "$canister"

# cargo install candid-extractor
candid-extractor "target/wasm32-unknown-unknown/release/$canister.wasm" >"$candid_file"
echo "Written: $candid_file"
# cargo install candid-extractor
candid-extractor "target/wasm32-unknown-unknown/release/$canister.wasm" >"$candid_file"
echo "Written: $candid_file"
}

CANISTERS=(example_app_backend example_paid_service)

for canister in ${CANISTERS[@]}; do
generate_did "$canister"
generate_did "$canister"
done
2 changes: 1 addition & 1 deletion scripts/pic-install
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ else
exit 1
fi

if "$POCKET_IC_SERVER_PATH" --version &>/dev/null | grep -wq "$POCKET_IC_SERVER_VERSION" ; then
if "$POCKET_IC_SERVER_PATH" --version &>/dev/null | grep -wq "$POCKET_IC_SERVER_VERSION"; then
echo "PocketIC server already exists at: $POCKET_IC_SERVER_PATH"
echo "Skipping download."
else
Expand Down
9 changes: 9 additions & 0 deletions scripts/test-rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

print_help() {
echo "Runs rust unit tests."
}

[[ "${1:-}" != "--help" ]] || print_help

cargo test --lib --bins "${@}"

0 comments on commit f3223dd

Please sign in to comment.