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

chore: run only rust tests affected with changed files #928

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
11 changes: 10 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,17 @@ jobs:
- name: Build
run: cargo build --verbose

- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v45
with:
files: |
**.rs

- name: Run tests
env:
CHANGED_RUST_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
run: |
chmod +x $GITHUB_WORKSPACE/scripts/build-all-apps.sh
chmod +x $GITHUB_WORKSPACE/scripts/test.sh
$GITHUB_WORKSPACE/scripts/test.sh
$GITHUB_WORKSPACE/scripts/test.sh --local ${CHANGED_RUST_FILES}
9 changes: 6 additions & 3 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
echo "Running pre-commit hook..."

# Check for changes in Markdown or MDX files
if git diff --cached --name-only | grep -qE '\.mdx?$'; then
if git diff --cached --name-only | grep -q '\.mdx?$'; then
echo "Markdown or MDX files have been changed."
pnpm format:md
fi

# Check for changes in Rust files
if git diff --cached --name-only | grep -qE '\.rs$'; then
if git diff --cached --name-only | grep -q '\.rs$'; then
echo "Running checks for the Rust code..."
cargo +nightly fmt
# Run tests only for the changed files
# changed_rust_files=$(git diff --cached --name-only | grep -E '\.rs$')
# ./scripts/test.sh --local ${changed_rust_files}
fi

# Check for changes in the 'node-ui' directory (Next.js app)
Expand All @@ -25,4 +28,4 @@ fi
if git diff --cached --name-only | grep -q '^packages/calimero-sdk/'; then
echo "Running checks for the packages/calimero-sdk..."
(cd packages/calimero-sdk && pnpm prettier && pnpm lint:fix)
fi
fi
1 change: 1 addition & 0 deletions crates/meroctl/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ where

// return Ok(result);
// }
//

pub fn load_config(home: &Utf8Path, node_name: &str) -> EyreResult<ConfigFile> {
let path = home.join(node_name);
Expand Down
1 change: 1 addition & 0 deletions crates/merod/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub const EXAMPLES: &str = r"
"Examples:",
EXAMPLES
))]

pub struct RootCommand {
#[command(flatten)]
pub args: RootArgs,
Expand Down
1 change: 1 addition & 0 deletions crates/server-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
145 changes: 143 additions & 2 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,45 @@ set -ex

cd "$(dirname $0)"

# Check for valid arguments
if [ "$#" -eq 0 ]; then
echo "No arguments provided. Use --all or --local followed by files."
exit 1
fi

# Initialize variables
mode=""
changed_files=()

# Parse arguments
for arg in "$@"; do
case $arg in
--all)
mode="all"
;;
--local)
mode="local"
;;
*)
if [ -n "$mode" ]; then
changed_files+=("$arg")
fi
;;
esac
done

# Check if neither --all nor --local was provided
if [ -z "$mode" ]; then
echo "Either --all or --local must be provided."
exit 1
fi

# Check for changed Rust files if local is specified
if [ "$mode" = "local" ] && [ -z "$changed_files" ]; then
echo "No files provided for local mode. Either no changes were made or the files were not provided."
exit 0
fi

# prepare apps
./build-all-apps.sh

Expand All @@ -13,5 +52,107 @@ cd "$(dirname $0)"
../contracts/context-config/build.sh
../contracts/proxy-lib/build-test-deps.sh

# Run cargo test
cargo test
# Handle the cases based on the mode
if [ "$mode" = "all" ]; then
echo "Running all tests..."
cargo +nightly test
exit 0
fi

# # Step 1: Find changed files
echo "The following Rust files have changed:" $changed_files

# Step 2: Find affected modules
modules_to_test=""

# 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

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

crates_to_test=()
seen=()
# Loop through the calimero_package_names array
for item in "${calimero_package_names[@]}"; do
if [[ -z "${seen[$item]}" ]]; then
seen[$item]=1 # Mark the item as seen
crates_to_test+=("$item")
fi
done

# Loop through the dep_arr array
for item in "${dep_arr[@]}"; do
if [[ -z "${seen[$item]}" ]]; then
seen[$item]=1
crates_to_test+=("$item")
fi
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

#Test all crates from changed files
for crate in "${crates_to_test[@]}"; do
if [[ "$crate" == "calimero-merod" ]]; then
echo "Testing crate merod"
cargo +nightly test -p "merod"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why test with nightly?

elif [[ "$crate" == "calimero-meroctl" ]]; then
echo "Testing crate meroctl"
cargo +nightly test -p "meroctl"
else
echo "Testing crate $crate"
cargo +nightly test -p "$crate"
fi
done
Loading