From 0d2f89b7e1402e659deb046fad1e764eb35cd1e7 Mon Sep 17 00:00:00 2001 From: "matej.vukosav" Date: Sun, 3 Nov 2024 23:46:10 +0800 Subject: [PATCH] fix: script --- .husky/pre-commit | 6 +- crates/server-primitives/src/jsonrpc.rs | 1 + crates/server-primitives/src/lib.rs | 1 + scripts/test.sh | 80 +++++++++++++++++++++---- 4 files changed, 73 insertions(+), 15 deletions(-) diff --git a/.husky/pre-commit b/.husky/pre-commit index 64e425ab9..0a823c32b 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -10,12 +10,12 @@ if git diff --cached --name-only | grep -q '\.mdx?$'; then fi # Check for changes in Rust files -changed_rust_files=$(git diff --cached --name-only | grep -E '\.rs$' || echo "" ) -if [ -n "${changed_rust_files}" ]; then +changed_rust_files=$(git diff --cached --name-only | grep -E '\.rs$') +if [ -n "${changed_rust_files:-""}" ]; then echo "Running checks for the Rust code..." cargo +nightly fmt # Run tests only for the changed files - ./scripts/test.sh --local ${changed_rust_files} + #./scripts/test.sh --local ${changed_rust_files} fi # Check for changes in the 'node-ui' directory (Next.js app) diff --git a/crates/server-primitives/src/jsonrpc.rs b/crates/server-primitives/src/jsonrpc.rs index 235edcd4a..9744282da 100644 --- a/crates/server-primitives/src/jsonrpc.rs +++ b/crates/server-primitives/src/jsonrpc.rs @@ -161,6 +161,7 @@ impl ExecuteRequest { #[serde(rename_all = "camelCase")] #[non_exhaustive] pub struct ExecuteResponse { + //test pub output: Option, } diff --git a/crates/server-primitives/src/lib.rs b/crates/server-primitives/src/lib.rs index c74dfbc7c..b08297a03 100644 --- a/crates/server-primitives/src/lib.rs +++ b/crates/server-primitives/src/lib.rs @@ -8,4 +8,5 @@ pub mod ws; #[derive(Clone, Copy, Debug, Deserialize, Serialize, ThisError)] #[error("Infallible")] #[expect(clippy::exhaustive_enums, reason = "This will never have any variants")] +//tests pub enum Infallible {} diff --git a/scripts/test.sh b/scripts/test.sh index e4c393b3e..c6309579e 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -13,7 +13,7 @@ fi # Initialize variables mode="" -changed_rust_files="" +changed_files=() # Parse arguments for arg in "$@"; do @@ -26,7 +26,7 @@ for arg in "$@"; do ;; *) if [ -n "$mode" ]; then - changed_files="$changed_files $arg" + changed_files+=("$arg") fi ;; esac @@ -65,20 +65,76 @@ echo "The following Rust files have changed:" $changed_files # Step 2: Find affected modules modules_to_test="" -for file in $changed_files; do - echo $file - # Convert file paths to module names (strip .rs) - module_name=$(echo "$file" | sed -e 's/\.rs//') - modules_to_test="$modules_to_test $module_name" +# Initialize an array to hold matched crates +matched_crates=() +all_crates=($(awk '/\[workspace\]/,/^\s*$/' ../Cargo.toml | grep -E 'members' -A 100 | grep -Eo '"[^"]*"' | tr -d '"' | sed 's|^\./||')) + +# Loop through each changed file +for file in "${changed_files[@]}"; do + # Extract the crate name by stripping the path to get the crate folder + crate_name=$(echo "$file" | sed -E 's|^(crates/[^/]+)/.*|\1|') + # Check if the crate exists in the list of crates + for crate in "${all_crates[@]}"; do + if [[ "$crate" == "$crate_name" ]]; then + matched_crates+=("$crate") + fi + done done -# Step 3: Run tests for each module and its dependencies -echo "Running tests for affected modules and their dependencies..." +echo "vuuuuuuuuuki" +echo $matched_crates + +calimero_package_names=() +# Loop through each element in the original array +for item in "${matched_crates[@]}"; do + echo $item + # Replace "crates/" with "calimero-" and add to new_names array + calimero_package_names+=("${item/crates\//calimero-}") +done + +dep_arr=() + +#for each crate from changed file find his dependencies +for calimero_package_name in "${calimero_package_names[@]}"; do + echo $calimero_package_name + # Initialize an array to hold the matched dependencies + for matched_crate in "${matched_crates[@]}"; do + dependencies=($(cargo metadata --format-version=1 --no-deps | jq -r --arg CRATE "$calimero_package_name" ' + .packages[] | + select(.dependencies | any(.name == $CRATE)) | + .name + ')) + # e.g. calimero-node-primitives. In list I have crates/node-primitives + # Loop through each dependency + for dep in "${dependencies[@]}"; do + # Create the full crate path + echo "Checking dependency: $dep" + #replace crates with calimero- + calimero_dep_crate_path="${dep/calimero-/crates/}" + for crate in "${all_crates[@]}"; do + if [[ "$crate" == "$calimero_dep_crate_path" ]]; then + echo "Found matching dependency: $calimero_dep_crate_path" + dep_arr+=("$calimero_dep_crate_path") + fi + done + done + done +done + +# Run tests for each module and its dependencies +echo "Running tests for affected modules and their dependencies..." # Install the nightly toolchain rustup toolchain install nightly -for module in $modules_to_test; do - echo "Testing module $module and its dependencies" - cargo +nightly test $module +#Test crates from changed files +for crate in "${calimero_package_names[@]}"; do + echo "Testing crate $crate" + cargo +nightly test -p "$crate" +done + +#Test dependencies +for crate in "${dep_arr[@]}"; do + echo "Testing crate $crate" + cargo +nightly test -p "$crate" done \ No newline at end of file