Skip to content

Commit

Permalink
Run cargo fmt and cargo clippy on whole project (#15)
Browse files Browse the repository at this point in the history
* some stuff

* update todo text

* Update main.rs

* Update format.sh

* clippy fixes

* clippy suggestion

* clippy suggestion

* more clippy fixes

* clippy suggestions

* clippy fixes

* remove code which causes compiler error

* more fixes

* also run cargo test

* Update check.sh

* make it a proper check script

* update comment

* add workflow

* update script to error and stop early

* add rustfmt

* try to break ci

* fix ci

* undo some clippy fixes

* undo more str

* use clone again

* undo

* add missing todo

* missing todo

* add newline

* bring it back

* Update check.sh
  • Loading branch information
shawntabrizi authored Jun 25, 2024
1 parent b053dad commit 4fa11a8
Show file tree
Hide file tree
Showing 111 changed files with 426 additions and 317 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Check Tutorial

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
cargo:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
components: rustfmt, clippy

- name: Run format, lint, and test script
run: ./check.sh
shell: bash
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
book
**/target/
77 changes: 77 additions & 0 deletions check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

set -e

# Default to 'check' mode if no argument is provided
MODE=${1:-check}

# Check if the mode is valid
if [[ "$MODE" != "check" && "$MODE" != "fix" ]]; then
echo "Invalid mode: $MODE. Use 'check' or 'fix'."
exit 1
fi

# Check if the 'steps' directory exists
if [ ! -d "steps" ]; then
echo "Directory 'steps' does not exist."
exit 1
fi

# Iterate through each subdirectory in the 'steps' directory
for dir in steps/*/; do
# Extract the directory name
dir_name=$(basename "$dir")

# Check if the directory contains a Cargo.toml file
if [ ! -f "$dir/Cargo.toml" ]; then
echo "Skipping directory (no Cargo.toml found): $dir"
continue
fi

if [ -d "$dir" ]; then
echo "Entering directory: $dir"
cd "$dir"

# Run cargo fmt and cargo clippy based on the mode
if [ "$MODE" == "check" ]; then

echo "Checking cargo fmt"
cargo +nightly fmt -- --check

echo "Checking cargo clippy"
if [[ "$dir_name" =~ ^([1-9]|1[0-9]|2[0-6])$ ]]; then
# We specifically allow `clippy::ptr-arg` in the first 26 steps, because
# it helps minimize the transition to generic types.
RUSTFLAGS="-A unused -A clippy::ptr-arg" cargo +nightly clippy -- -D warnings
else
RUSTFLAGS="-A unused" cargo +nightly clippy -- -D warnings
fi

echo "Checking cargo test"
RUSTFLAGS="-A unused -D warnings" cargo test

elif [ "$MODE" == "fix" ]; then

echo "Running cargo fmt"
RUSTFLAGS="-A unused" cargo +nightly fmt

echo "Running cargo clippy"
if [[ "$dir_name" =~ ^([1-9]|1[0-9]|2[0-6])$ ]]; then
# We specifically allow `clippy::ptr-arg` in the first 26 steps, because
# it helps minimize the transition to generic types.
RUSTFLAGS="-A unused -A clippy::ptr-arg" cargo +nightly clippy --fix --allow-dirty
else
RUSTFLAGS="-A unused" cargo +nightly clippy --fix --allow-dirty
fi

echo "Running cargo test"
RUSTFLAGS="-A unused -D warnings" cargo test

fi

# Return to the previous directory
cd - > /dev/null
fi
done

echo "All operations completed."
2 changes: 2 additions & 0 deletions steps/17/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ mod test {
/* TODO: Create a test which checks the following:
- Increment the current block number.
- Increment the nonce of `alice`.
- Check the block number is what we expect.
- Check the nonce of `alice` is what we expect.
- Check the nonce of `bob` is what we expect.
*/
}
}
4 changes: 2 additions & 2 deletions steps/18/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
6 changes: 4 additions & 2 deletions steps/19/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ mod system;
// This is our main Runtime.
// It accumulates all of the different pallets we want to use.
pub struct Runtime {
/* TODO: create a field `system` which is of type `system::Pallet`. */
/* TODO: create a field `balances` which is of type `balances::Pallet`. */
/* TODO:
- Create a field `system` which is of type `system::Pallet`.
- Create a field `balances` which is of type `balances::Pallet`.
*/
}

impl Runtime {
Expand Down
4 changes: 2 additions & 2 deletions steps/19/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/20/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/21/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/22/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/23/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/24/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/25/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/26/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/27/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/28/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/29/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/30/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/31/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/32/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/33/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/34/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/35/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/36/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/37/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/38/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/39/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/40/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/41/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/42/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
4 changes: 2 additions & 2 deletions steps/43/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod test {
system.inc_nonce(&"alice".to_string());

assert_eq!(system.block_number(), 1);
assert_eq!(system.nonce.get(&"alice".to_string()), Some(&1));
assert_eq!(system.nonce.get(&"bob".to_string()), None);
assert_eq!(system.nonce.get("alice"), Some(&1));
assert_eq!(system.nonce.get("bob"), None);
}
}
Loading

0 comments on commit 4fa11a8

Please sign in to comment.