From e7a20c63fb75843603f3a5e51c56847444c2405e Mon Sep 17 00:00:00 2001 From: 255doesnotexist <1293883574jcy@gmail.com> Date: Sat, 7 Sep 2024 20:12:01 +0800 Subject: [PATCH] style: formatting --- src/config/connection_config.rs | 4 +--- src/config/distro_config.rs | 5 ++-- src/config/mod.rs | 4 ++-- src/config/root_config.rs | 3 +-- src/main.rs | 11 ++++++--- src/test_runner/local.rs | 15 ++++++------ src/test_runner/mod.rs | 8 ++----- src/test_runner/remote.rs | 41 ++++++++++++++++++++------------- src/testenv_manager.rs | 2 +- src/testscript_manager.rs | 6 ++--- src/utils.rs | 2 +- 11 files changed, 52 insertions(+), 49 deletions(-) diff --git a/src/config/connection_config.rs b/src/config/connection_config.rs index ee3098c..746c23d 100644 --- a/src/config/connection_config.rs +++ b/src/config/connection_config.rs @@ -1,4 +1,3 @@ - /// Represents the configuration for a connection. /// /// This struct is used to store the connection details such as the method, IP address, port, username, and password. @@ -12,7 +11,6 @@ /// - `username`: An optional string representing the username. /// - `password`: An optional string representing the password. /// - use serde::Deserialize; #[derive(Clone, Debug, Deserialize)] @@ -22,4 +20,4 @@ pub struct ConnectionConfig { pub port: Option, pub username: Option, pub password: Option, -} \ No newline at end of file +} diff --git a/src/config/distro_config.rs b/src/config/distro_config.rs index 7af73f0..0607499 100644 --- a/src/config/distro_config.rs +++ b/src/config/distro_config.rs @@ -1,3 +1,4 @@ +use crate::config::connection_config::ConnectionConfig; /// Represents the configuration for each distro. /// /// This struct is used to deserialize the configuration from a file using the `from_file` method. @@ -8,17 +9,15 @@ /// - `connection`: An instance of `ConnectionConfig` struct representing the connection configuration. /// - `skip_packages`: An optional vector of strings representing the packages to be skipped. /// - use serde::Deserialize; use std::fs; -use crate::config::connection_config::ConnectionConfig; #[derive(Debug, Deserialize)] pub struct DistroConfig { pub testing_type: String, // 'locally' or 'remote' or 'qemu-based-remote' #[serde(rename = "startup_script")] #[serde(default, skip_serializing_if = "is_not_qemu_based_remote")] - pub startup_script: String, + pub startup_script: String, #[serde(rename = "stop_script")] #[serde(default, skip_serializing_if = "is_not_qemu_based_remote")] diff --git a/src/config/mod.rs b/src/config/mod.rs index 695c671..44f461b 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,3 +1,3 @@ -pub mod root_config; pub mod connection_config; -pub mod distro_config; \ No newline at end of file +pub mod distro_config; +pub mod root_config; diff --git a/src/config/root_config.rs b/src/config/root_config.rs index 708f9fc..93bcf95 100644 --- a/src/config/root_config.rs +++ b/src/config/root_config.rs @@ -22,7 +22,6 @@ /// } /// } /// ``` - use serde::Deserialize; use std::fs; @@ -39,4 +38,4 @@ impl Config { let config: Config = toml::from_str(&content)?; Ok(config) } -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index fe31f19..dcf86e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,8 @@ mod testenv_manager; mod testscript_manager; mod utils; +use crate::config::{distro_config::DistroConfig, root_config::Config}; use crate::test_runner::{local::LocalTestRunner, remote::RemoteTestRunner, TestRunner}; -use crate::config::{root_config::Config, distro_config::DistroConfig}; use clap::{Arg, ArgMatches, Command}; use std::fs::remove_file; use std::path::Path; @@ -166,7 +166,12 @@ fn run_tests(distros: &[&str], packages: &[&str], cleanup: bool, verbose: bool) continue; } - println!("Running test for {}/{}, {}.", distro, package, if run_locally {"locally"} else {"with QEMU"}); + println!( + "Running test for {}/{}, {}.", + distro, + package, + if run_locally { "locally" } else { "with QEMU" } + ); let test_runner: Box = if run_locally { Box::new(LocalTestRunner::new(distro, package, verbose)) @@ -190,7 +195,7 @@ fn run_tests(distros: &[&str], packages: &[&str], cleanup: bool, verbose: bool) ip.to_string(), port, username.to_string(), - password.map(|p| p.to_string(),), + password.map(|p| p.to_string()), verbose, )) }; diff --git a/src/test_runner/local.rs b/src/test_runner/local.rs index 76b2f21..935561a 100644 --- a/src/test_runner/local.rs +++ b/src/test_runner/local.rs @@ -1,7 +1,7 @@ +use crate::aggregator::generate_report; use crate::test_runner::TestRunner; use crate::testscript_manager::TestScriptManager; use crate::utils::{Report, TestResult, REMOTE_TMP_DIR}; -use crate::aggregator::generate_report; use std::fs::read_to_string; use std::process::{Command, Stdio}; @@ -35,11 +35,7 @@ impl TestRunner for LocalTestRunner { /// * Reading the package version from the temporary file fails. /// * Generating the report fails. /// * Not all tests passed for the given distribution and package. - fn run_test( - &self, - distro: &str, - package: &str, - ) -> Result<(), Box> { + fn run_test(&self, distro: &str, package: &str) -> Result<(), Box> { let script_manager = TestScriptManager::new(distro, package); let os_version = read_to_string("/proc/version")?; @@ -53,7 +49,10 @@ impl TestRunner for LocalTestRunner { for script in script_manager?.get_test_scripts() { let output = Command::new("bash") .arg("-c") - .arg(&format!("source {} && echo -n $PACKAGE_VERSION > {}", script, pkgver_tmpfile)) + .arg(&format!( + "source {} && echo -n $PACKAGE_VERSION > {}", + script, pkgver_tmpfile + )) .stdout(if self.verbose { Stdio::inherit() } else { @@ -101,4 +100,4 @@ impl TestRunner for LocalTestRunner { Ok(()) } -} \ No newline at end of file +} diff --git a/src/test_runner/mod.rs b/src/test_runner/mod.rs index a5005a0..1acdc18 100644 --- a/src/test_runner/mod.rs +++ b/src/test_runner/mod.rs @@ -2,9 +2,5 @@ pub mod local; pub mod remote; pub trait TestRunner { - fn run_test( - &self, - distro: &str, - package: &str, - ) -> Result<(), Box>; -} \ No newline at end of file + fn run_test(&self, distro: &str, package: &str) -> Result<(), Box>; +} diff --git a/src/test_runner/remote.rs b/src/test_runner/remote.rs index 68cabd1..aac054c 100644 --- a/src/test_runner/remote.rs +++ b/src/test_runner/remote.rs @@ -1,6 +1,6 @@ +use crate::aggregator::generate_report; use crate::test_runner::TestRunner; use crate::testscript_manager::TestScriptManager; -use crate::aggregator::generate_report; use crate::utils::{CommandOutput, Report, TempFile, TestResult, REMOTE_TMP_DIR}; use ssh2::Session; use std::fs::File; @@ -18,7 +18,13 @@ pub struct RemoteTestRunner { } impl RemoteTestRunner { - pub fn new(remote_ip: String, port: u16, username: String, password: Option, verbose: bool) -> Self { + pub fn new( + remote_ip: String, + port: u16, + username: String, + password: Option, + verbose: bool, + ) -> Self { RemoteTestRunner { remote_ip, port, @@ -55,7 +61,6 @@ impl RemoteTestRunner { } } - /// Implements the `TestRunner` trait for the `RemoteTestRunner` struct. /// /// This struct allows running tests on a remote server using SSH. @@ -70,11 +75,7 @@ impl TestRunner for RemoteTestRunner { /// # Errors /// /// Returns an error if the test fails or encounters any issues. - fn run_test( - &self, - distro: &str, - package: &str, - ) -> Result<(), Box> { + fn run_test(&self, distro: &str, package: &str) -> Result<(), Box> { // 创建 SSH 会话 let tcp = TcpStream::connect((self.remote_ip.as_str(), self.port))?; let mut sess = Session::new()?; @@ -130,12 +131,20 @@ impl TestRunner for RemoteTestRunner { // 上传 prerequisite.sh 到远程服务器 let prerequisite_path = format!("{}/prerequisite.sh", distro); let remote_prerequisite_path = "/tmp/prerequisite.sh".to_string(); - let mut remote_file = sess.scp_send(Path::new(&remote_prerequisite_path), 0o644, std::fs::metadata(&prerequisite_path)?.len(), None)?; + let mut remote_file = sess.scp_send( + Path::new(&remote_prerequisite_path), + 0o644, + std::fs::metadata(&prerequisite_path)?.len(), + None, + )?; let mut local_file = File::open(&prerequisite_path)?; let mut buffer = Vec::new(); local_file.read_to_end(&mut buffer)?; remote_file.write_all(&buffer)?; - self.print_ssh_msg(&format!("File {} uploaded to remote server", prerequisite_path)); + self.print_ssh_msg(&format!( + "File {} uploaded to remote server", + prerequisite_path + )); // 确保远程文件在继续之前关闭 drop(remote_file); @@ -173,7 +182,10 @@ impl TestRunner for RemoteTestRunner { for script in script_manager?.get_test_scripts() { let result = self.run_command( &sess, - &format!("source {} && echo -n $PACKAGE_VERSION > {}", script, pkgver_tmpfile), + &format!( + "source {} && echo -n $PACKAGE_VERSION > {}", + script, pkgver_tmpfile + ), ); let test_passed = result.is_ok(); all_tests_passed &= test_passed; @@ -194,10 +206,7 @@ impl TestRunner for RemoteTestRunner { if all_tests_passed { self.print_ssh_msg(&format!("Test successful for {}/{}", distro, package)); } else { - self.print_ssh_msg(&format!( - "Test failed for {}/{}", - distro, package - )); + self.print_ssh_msg(&format!("Test failed for {}/{}", distro, package)); } let report: Report = Report { distro: distro.to_string(), @@ -278,4 +287,4 @@ impl TestRunner for RemoteTestRunner { } Ok(()) } -} \ No newline at end of file +} diff --git a/src/testenv_manager.rs b/src/testenv_manager.rs index 2e8b1b1..bfb8c39 100644 --- a/src/testenv_manager.rs +++ b/src/testenv_manager.rs @@ -93,4 +93,4 @@ impl TestEnvManager { pub fn stop(&self) -> Result<(), Error> { self.run_script(&self.stop_script) } -} \ No newline at end of file +} diff --git a/src/testscript_manager.rs b/src/testscript_manager.rs index 90ed769..a8fb9c8 100644 --- a/src/testscript_manager.rs +++ b/src/testscript_manager.rs @@ -46,9 +46,7 @@ impl TestScriptManager { test_scripts.push(path.to_str().unwrap_or_default().to_string()); } } - Ok(TestScriptManager { - test_scripts, - }) + Ok(TestScriptManager { test_scripts }) } /// Returns a slice containing the paths of all discovered test scripts. @@ -71,4 +69,4 @@ impl TestScriptManager { pub fn get_test_scripts(&self) -> &[String] { &self.test_scripts } -} \ No newline at end of file +} diff --git a/src/utils.rs b/src/utils.rs index 4e461c2..922feb8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -78,4 +78,4 @@ pub struct CommandOutput { pub exit_status: i32, /// The output (stdout and stderr) of the command. pub output: String, -} \ No newline at end of file +}