From 1b3b25fc008fd7418e3bc15dab293798ec89f5eb Mon Sep 17 00:00:00 2001 From: Florian Guggi Date: Wed, 11 Sep 2024 19:08:59 +0200 Subject: [PATCH 1/2] Fix the more pedantic clippy warnings --- Cargo.toml | 1 + filevec/src/lib.rs | 35 +++++++++++----------- scheduler/src/command/execute_program.rs | 9 ++---- scheduler/src/command/execution_context.rs | 5 +++- scheduler/tests/simulation/mod.rs | 1 + scheduler/tests/software_tests/common.rs | 1 + 6 files changed, 27 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a49199d..397fdae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,4 @@ pedantic = { level = "warn", priority = -1 } missing_errors_doc = "allow" module_name_repetitions = "allow" missing_panics_doc = "allow" +should_panic_without_expect = "allow" diff --git a/filevec/src/lib.rs b/filevec/src/lib.rs index 91ece30..a8f932e 100644 --- a/filevec/src/lib.rs +++ b/filevec/src/lib.rs @@ -45,8 +45,12 @@ impl FileVec { /// **Note:** If the file exists and contains invalid data, it is interpreted as /// empty and overwritten. pub fn open(path: impl AsRef) -> Result { - let mut file = - std::fs::OpenOptions::new().read(true).write(true).create(true).open(path)?; + let mut file = std::fs::OpenOptions::new() + .read(true) + .write(true) + .create(true) + .truncate(false) + .open(path)?; let metadata = file.metadata()?; let vec = if metadata.len() > 0 { @@ -151,17 +155,12 @@ impl<'a, T: Serialize + DeserializeOwned> Drop for FileVecGuard<'a, T> { #[cfg(test)] mod test { - use std::{ - fs::File, - io::{Read, Write}, - ops::DerefMut, - }; - use super::FileVec; + use std::io::{Read, Write}; #[test] fn empty_vec() { - let f = FileVec::::open("__empty_vec".to_string()).unwrap(); + let f = FileVec::::open("__empty_vec").unwrap(); assert_eq!(f.as_ref().len(), 0); assert!(std::path::Path::new("__empty_vec").exists()); @@ -175,7 +174,7 @@ mod test { let buffer = rmp_serde::to_vec(&DATA).unwrap(); std::fs::File::create("__prefilled").unwrap().write_all(&buffer).unwrap(); - let f = FileVec::::open("__prefilled".to_string()).unwrap(); + let f = FileVec::::open("__prefilled").unwrap(); assert_eq!(&DATA, f.as_ref().as_slice()); let _ = std::fs::remove_file("__prefilled"); @@ -183,12 +182,12 @@ mod test { #[test] fn push_single() { - let mut f = FileVec::::open("__push_single".to_string()).unwrap(); + let mut f = FileVec::::open("__push_single").unwrap(); f.push(123).unwrap(); assert_eq!(f[0], 123); drop(f); - let f = FileVec::::open("__push_single".to_string()).unwrap(); + let f = FileVec::::open("__push_single").unwrap(); assert_eq!(f[0], 123); let _ = std::fs::remove_file("__push_single"); @@ -203,13 +202,13 @@ mod test { #[test] fn push_multiple_structs() { - let mut f = FileVec::open("__push_multiple".to_string()).unwrap(); + let mut f = FileVec::open("__push_multiple").unwrap(); f.push(TestaStruct { int16: 1, uint32: 2, stringa: "Hello".into() }).unwrap(); f.push(TestaStruct { int16: 3, uint32: 4, stringa: "Hello2".into() }).unwrap(); f.push(TestaStruct { int16: 5, uint32: 6, stringa: "Hello3".into() }).unwrap(); drop(f); - let f: FileVec = FileVec::open("__push_multiple".to_string()).unwrap(); + let f: FileVec = FileVec::open("__push_multiple").unwrap(); assert_eq!(f[0], TestaStruct { int16: 1, uint32: 2, stringa: "Hello".into() }); assert_eq!(f[2], TestaStruct { int16: 5, uint32: 6, stringa: "Hello3".into() }); @@ -218,8 +217,8 @@ mod test { #[test] fn remove() { - let mut f: FileVec = FileVec::open("__remove".to_string()).unwrap(); - f.extend([0i32, 1, 2, 3, 4, 5, 6].into_iter()); + let mut f: FileVec = FileVec::open("__remove").unwrap(); + f.extend([0i32, 1, 2, 3, 4, 5, 6]); let mut buffer = Vec::new(); std::fs::File::open("__remove").unwrap().read_to_end(&mut buffer).unwrap(); @@ -237,8 +236,8 @@ mod test { #[test] fn pop() { - let mut f = FileVec::open("__pop".to_string()).unwrap(); - f.extend([0, 1, 2, 3].into_iter()); + let mut f = FileVec::open("__pop").unwrap(); + f.extend([0, 1, 2, 3]); assert_eq!(f.pop().unwrap(), Some(3)); assert_eq!(f.pop().unwrap(), Some(2)); diff --git a/scheduler/src/command/execute_program.rs b/scheduler/src/command/execute_program.rs index 1683292..dc9378b 100644 --- a/scheduler/src/command/execute_program.rs +++ b/scheduler/src/command/execute_program.rs @@ -1,16 +1,14 @@ use super::{CommandError, CommandResult, SyncExecutionContext}; use crate::{ command::{ - check_length, terminate_student_program, truncate_to_size, Event, ProgramStatus, ResultId, - RetryEvent, + check_length, terminate_student_program, Event, ProgramStatus, ResultId, RetryEvent, }, communication::{CEPPacket, CommunicationHandle}, }; use anyhow::anyhow; use simple_archive::Compression; use std::{ - fs::File, - io::{ErrorKind, Read, Write}, + io::{ErrorKind, Write}, path::{Path, PathBuf}, time::Duration, }; @@ -152,8 +150,7 @@ fn build_result_archive(res: ResultId) -> Result<(), std::io::Error> { let res_path = PathBuf::from(format!("./archives/{}/results/{}", res.program_id, res.timestamp)); - let student_log_path = - PathBuf::from(format!("./data/{res}.log")); + let student_log_path = PathBuf::from(format!("./data/{res}.log")); let log_path = PathBuf::from("./log"); add_to_archive_if_exists(&mut archive, &res.to_string(), res_path, Compression::None)?; diff --git a/scheduler/src/command/execution_context.rs b/scheduler/src/command/execution_context.rs index f662d04..f26ee35 100644 --- a/scheduler/src/command/execution_context.rs +++ b/scheduler/src/command/execution_context.rs @@ -1,6 +1,9 @@ use filevec::FileVec; use std::{ - fmt::Display, str::FromStr, sync::{Arc, Mutex}, thread + fmt::Display, + str::FromStr, + sync::{Arc, Mutex}, + thread, }; const EVENT_SEND_TRIES: u32 = 5; diff --git a/scheduler/tests/simulation/mod.rs b/scheduler/tests/simulation/mod.rs index cddb258..29b329f 100644 --- a/scheduler/tests/simulation/mod.rs +++ b/scheduler/tests/simulation/mod.rs @@ -166,6 +166,7 @@ pub fn execute_program(program_id: u16, timestamp: u32, timeout: u16) -> Vec vec } +#[allow(dead_code)] pub fn stop_program() -> Vec { vec![3u8] } diff --git a/scheduler/tests/software_tests/common.rs b/scheduler/tests/software_tests/common.rs index 564d855..ccb07fa 100644 --- a/scheduler/tests/software_tests/common.rs +++ b/scheduler/tests/software_tests/common.rs @@ -140,6 +140,7 @@ pub fn cleanup(unique: &str) { let _ = std::fs::remove_file(format!("tests/tmp/{unique}_r")); } +#[allow(dead_code)] pub fn store_archive(program_id: u16) -> Vec { let mut vec = vec![1u8]; vec.extend(program_id.to_le_bytes()); From 041900df987fef402c785ec7be6f6fdaec4a4c2a Mon Sep 17 00:00:00 2001 From: Florian Guggi Date: Sun, 15 Sep 2024 11:30:47 +0200 Subject: [PATCH 2/2] Run static checks in CI and compile only on master --- .github/workflows/cross.yml | 30 ++++++++++++++++++++++++++++++ .github/workflows/rust.yml | 30 ++++++++---------------------- 2 files changed, 38 insertions(+), 22 deletions(-) create mode 100644 .github/workflows/cross.yml diff --git a/.github/workflows/cross.yml b/.github/workflows/cross.yml new file mode 100644 index 0000000..95e6f98 --- /dev/null +++ b/.github/workflows/cross.yml @@ -0,0 +1,30 @@ +name: Compile for the RPi + +on: + push: + branches: [ "master" ] + workflow_dispatch: + +jobs: + cross-compile: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Prepare + run: cargo install cross --git https://github.com/cross-rs/cross + - name: Build + run: | + cross build --examples --bins --release --target aarch64-unknown-linux-gnu + mv target/aarch64-unknown-linux-gnu/release/STS1_EDU_Scheduler . + mv target/aarch64-unknown-linux-gnu/release/examples/cli . + cp scheduler/config.toml . + cp scheduler/scheduler.service . + - name: Upload artifact + uses: actions/upload-artifact@v4 + with: + name: aarch64 package + path: | + STS1_EDU_Scheduler + config.toml + scheduler.service + cli diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 1a18f22..d115c6a 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -1,8 +1,6 @@ name: Build & Test on: - push: - branches: [ "master" ] pull_request: branches: [ "master" ] @@ -21,25 +19,13 @@ jobs: - name: Run tests run: cargo test --release --features mock - cross-compile: + static-checks: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - name: Prepare - run: cargo install cross --git https://github.com/cross-rs/cross - - name: Build - run: | - cross build --examples --bins --release --target aarch64-unknown-linux-gnu - mv target/aarch64-unknown-linux-gnu/release/STS1_EDU_Scheduler . - mv target/aarch64-unknown-linux-gnu/release/examples/cli . - cp scheduler/config.toml . - cp scheduler/scheduler.service . - - name: Upload artifact - uses: actions/upload-artifact@v4 - with: - name: aarch64 package - path: | - STS1_EDU_Scheduler - config.toml - scheduler.service - cli + - uses: actions/checkout@v4 + - name: Install + run: sudo apt install libudev-dev + - name: Clippy + run: cargo clippy -- -D warnings + - name: Formatting + run: cargo fmt --all --check