提交 15a3497c 编写于 作者: H hustccc

update

上级 958baf90
......@@ -7,3 +7,6 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
xscommand = { path = "xscommand" }
simple_logger = "1.11.0"
log = "0.4"
/// Auto Background Test Routine for XiangShan Processor
/// XiangShan: https://github.com/RISCVERS/XiangShan
///
/// Never panic
extern crate xscommand;
extern crate simple_logger;
use std::{
path::Path,
fs,
};
use simple_logger::SimpleLogger;
use xscommand::{
XSCommand,
git::Git,
};
fn main() {
println!("Hello, rust-xs-test!");
// init simple logger
let logger = SimpleLogger::new();
let workload = Path::new("temp");
let stdout = workload.join("stdout");
let stderr = workload.join("stderr");
if !workload.exists() {
fs::create_dir(workload).unwrap();
}
logger.init().unwrap();
let mut git = Git::new();
git.set_args(vec!["log"]).unwrap();
git.set_workdir(workload.to_str()).unwrap();
let exit_code = git.excute(stdout.to_str(), stderr.to_str()).unwrap();
log::info!("git exit with code: {}", exit_code);
if workload.exists() {
fs::remove_dir_all(workload).unwrap();
}
}
......@@ -7,5 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
log = "0.4"
simple_logger = "1.11.0"
\ No newline at end of file
log = "0.4"
\ No newline at end of file
......@@ -3,12 +3,10 @@
use super::{XSCommand, XSCommandErr};
use std::{
process::{Command, Stdio},
os::unix::io::{FromRawFd, IntoRawFd},
fs::File,
os::unix::io::{FromRawFd, IntoRawFd},
process::{Command, Stdio}
};
#[allow(unused_imports)]
use simple_logger::SimpleLogger;
/// Git Command
pub struct Git<'a> {
......@@ -45,9 +43,9 @@ impl<'a> XSCommand<'a, GitErr> for Git<'a> {
args
}
fn set_workdir(&mut self, work_dir: &'a str) -> Result<(), GitErr> {
fn set_workdir(&mut self, work_dir: Option<&'a str>) -> Result<(), GitErr> {
// TODO: check the rationality of workdir
self.work_dir = Some(work_dir);
self.work_dir = work_dir;
Ok(())
}
......@@ -55,13 +53,17 @@ impl<'a> XSCommand<'a, GitErr> for Git<'a> {
for arg in &self.args {
self.exe.arg(arg);
}
log::info!("git excute args: {:?}", self.args);
let workload = if let Some(dir) = self.work_dir { dir } else { "./" };
log::info!("git excute args: {:?} in workload: {}", self.args, workload);
// TODO: use clouse here to reduce code lines
if let Some(stdout_path) = stdout {
// TODO: should not panic here
let stdout_fd = File::create(stdout_path).unwrap().into_raw_fd();
let std_out = unsafe { Stdio::from_raw_fd(stdout_fd) };
self.exe.stdout(std_out);
}
if let Some(stderr_path) = stderr {
// TODO: should not panic here
let stderr_fd = File::create(stderr_path).unwrap().into_raw_fd();
let err_out = unsafe { Stdio::from_raw_fd(stderr_fd) };
self.exe.stderr(err_out);
......@@ -69,6 +71,7 @@ impl<'a> XSCommand<'a, GitErr> for Git<'a> {
if let Some(dir) = self.work_dir {
self.exe.current_dir(dir);
}
// Block here until command return
let res = self.exe.status();
match res {
Ok(exit_status) => {
......@@ -105,136 +108,164 @@ impl XSCommandErr for GitErr {
#[test]
fn test_git_version() {
#[allow(unused_imports)]
use std::fs::{remove_file, File};
let stdout_file = "git_version_stdout.txt";
let stderr_file = "git_version_stderr.txt";
use std::fs;
use std::path::Path;
let workload = Path::new("git_version_test");
if !workload.exists() {
// workload not exists, create
fs::create_dir(workload).unwrap();
}
let stdout_file = workload.join("git_version_stdout.txt");
let stderr_file = workload.join("git_version_stderr.txt");
let mut git = Git::new();
let args = vec!["--version"];
git.set_args(args).unwrap();
match git.excute(Some(stdout_file), Some(stderr_file)) {
git.set_workdir(workload.to_str()).unwrap();
match git.excute(stdout_file.to_str(), stderr_file.to_str()) {
Ok(exit_code) => {
assert_eq!(exit_code, 0);
},
Err(errtype) => panic!("ErrType: {:?}", errtype),
}
// TODO: check the content of `stdout.txt` and `stderr.txt`
// File::create(stdout_file).unwrap();
use std::path::Path;
if Path::new(stdout_file).exists() {
remove_file(stdout_file).unwrap();
}
// File::create(stderr_file).unwrap();
if Path::new(stderr_file).exists() {
remove_file(stderr_file).unwrap();
}
// TODO: check the content of `stdout` and `stderr`
// remove a dir after removing all its contents
if workload.exists() {
fs::remove_dir_all(workload).unwrap();
}
// if stdout_file.exists() {
// fs::remove_file(stdout_file).unwrap();
// }
// if stderr_file.exists() {
// fs::remove_file(stderr_file).unwrap();
// }
}
// TODO: add more test
#[test]
fn test_git_status() {
#[allow(unused_imports)]
use std::fs::{remove_file, File};
let stdout_file = "git_status_stdout.txt";
let stderr_file = "git_status_stderr.txt";
use std::fs;
use std::path::Path;
let workload = Path::new("git_status_test");
if !workload.exists() {
// workload not exists, create
fs::create_dir(workload).unwrap();
}
let stdout_file = workload.join("git_status_stdout.txt");
let stderr_file = workload.join("git_status_stderr.txt");
let mut git = Git::new();
let args = vec!["status", "--help"];
match git.set_args(args) {
Err(errtype) => panic!("ErrType: {:?}", errtype),
Ok(_) => {},
}
match git.excute(Some(stdout_file), Some(stderr_file)) {
git.set_workdir(workload.to_str()).unwrap();
match git.excute(stdout_file.to_str(), stderr_file.to_str()) {
Ok(exit_code) => {
assert_eq!(exit_code, 0);
},
Err(errtype) => panic!("ErrType: {:?}", errtype),
}
// TODO: check the content of `stdout.txt` and `stderr.txt`
use std::path::Path;
if Path::new(stdout_file).exists() {
remove_file(stdout_file).unwrap();
}
// File::create(stderr_file).unwrap();
if Path::new(stderr_file).exists() {
remove_file(stderr_file).unwrap();
// TODO: check the content of `stdout` and `stderr`
// remove a dir after removing all its contents
if workload.exists() {
fs::remove_dir_all(workload).unwrap();
}
// if stdout_file.exists() {
// fs::remove_file(stdout_file).unwrap();
// }
// if stderr_file.exists() {
// fs::remove_file(stderr_file).unwrap();
// }
}
#[test]
fn test_git_clone() {
#[allow(unused_imports)]
use std::fs::{remove_file, File};
use std::process::Command;
let stdout_file = "git_clone_stdout.txt";
let stderr_file = "git_clone_stderr.txt";
use std::fs;
use std::path::Path;
let workload = Path::new("git_clone_test");
if !workload.exists() {
// workload not exists, create
fs::create_dir(workload).unwrap();
}
let stdout_file = workload.join("git_clone_stdout.txt");
let stderr_file = workload.join("git_clone_stderr.txt");
let repo = "https://github.com/SKTT1Ryze/rust-xs-evaluation";
let repo_path = "rust-xs-evaluation";
let repo_path = workload.join("rust-xs-evaluation");
if repo_path.exists() {
fs::remove_dir_all(repo_path).unwrap();
}
let mut git = Git::new();
let args = vec!["clone", repo];
match git.set_args(args) {
Err(errtype) => panic!("ErrType: {:?}", errtype),
Ok(_) => {},
}
match git.excute(Some(stdout_file), Some(stderr_file)) {
git.set_workdir(workload.to_str()).unwrap();
match git.excute(stdout_file.to_str(), stderr_file.to_str()) {
Ok(exit_code) => {
assert_eq!(exit_code, 0);
},
Err(errtype) => panic!("ErrType: {:?}", errtype),
}
// TODO: check the content of `stdout.txt` and `stderr.txt`
use std::path::Path;
if Path::new(stdout_file).exists() {
remove_file(stdout_file).unwrap();
}
// File::create(stderr_file).unwrap();
if Path::new(stderr_file).exists() {
remove_file(stderr_file).unwrap();
// TODO: check the content of `stdout` and `stderr`
// remove a dir after removing all its contents
if workload.exists() {
fs::remove_dir_all(workload).unwrap();
}
let mut rm = Command::new("rm")
.arg("-rf")
.arg(repo_path)
.spawn()
.unwrap();
rm.wait().unwrap();
// if stdout_file.exists() {
// fs::remove_file(stdout_file).unwrap();
// }
// // File::create(stderr_file).unwrap();
// if stderr_file.exists() {
// fs::remove_file(stderr_file).unwrap();
// }
}
#[test]
fn test_git_pull() {
#[allow(unused_imports)]
use std::fs::{remove_file, File};
use std::fs;
use std::path::Path;
use std::process::Command;
let stdout_file = "git_pull_stdout.txt";
let stderr_file = "git_pull_stderr.txt";
let workload = Path::new("git_pull_test");
if !workload.exists() {
// workload not exists, create
fs::create_dir(workload).unwrap();
}
let stdout_file = workload.join("git_pull_stdout.txt");
let stderr_file = workload.join("git_pull_stderr.txt");
let repo = "https://github.com/SKTT1Ryze/rust-xs-evaluation";
let repo_path = "rust-xs-evaluation";
let repo_path = workload.join("rust-xs-evaluation");
if repo_path.exists() {
fs::remove_dir_all(repo_path).unwrap();
}
let mut git_clone = Command::new("git")
.arg("clone")
.arg(repo)
.current_dir(workload)
.spawn()
.unwrap();
git_clone.wait().unwrap();
let mut git = Git::new();
let args = vec!["pull"];
git.set_args(args).unwrap();
match git.excute(Some(stdout_file), Some(stderr_file)) {
git.set_workdir(workload.to_str()).unwrap();
match git.excute(stdout_file.to_str(), stderr_file.to_str()) {
Ok(exit_code) => {
assert_eq!(exit_code, 0);
},
Err(errtype) => panic!("ErrType: {:?}", errtype),
}
// TODO: check the content of `stdout.txt` and `stderr.txt`
use std::path::Path;
if Path::new(stdout_file).exists() {
remove_file(stdout_file).unwrap();
}
// File::create(stderr_file).unwrap();
if Path::new(stderr_file).exists() {
remove_file(stderr_file).unwrap();
// TODO: check the content of `stdout` and `stderr`
if workload.exists() {
fs::remove_dir_all(workload).unwrap();
}
let mut rm = Command::new("rm")
.arg("-rf")
.arg(repo_path)
.spawn()
.unwrap();
rm.wait().unwrap();
// if stdout_file.exists() {
// fs::remove_file(stdout_file).unwrap();
// }
// // File::create(stderr_file).unwrap();
// if stderr_file.exists() {
// fs::remove_file(stderr_file).unwrap();
// }
}
\ No newline at end of file
......@@ -9,9 +9,11 @@
//! fn main() {
//! let logger = SimpleLogger::new();
//! logger.init().unwrap();
//! ...
//! }
//! ```
//!
//! Never panic in this crate
pub mod git;
use std::fmt::Debug;
......@@ -26,7 +28,7 @@ pub trait XSCommand<'a, T: XSCommandErr + Debug> {
/// Get arguments
fn get_args(&self) -> Vec<&str>;
/// Set the working dir for the XSCommand
fn set_workdir(&mut self, work_dir: &'a str) -> Result<(), T>;
fn set_workdir(&mut self, work_dir: Option<&'a str>) -> Result<(), T>;
/// Excute the command
/// Return exit code
fn excute(&mut self, stdout: Option<&str>, stderr: Option<&str>) -> Result<i32, T>;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册