mirror of
https://github.com/Orange-OpenSource/hurl.git
synced 2024-12-26 20:42:01 +03:00
Finalize cleaning main structure.
This commit is contained in:
parent
7d85fa9a46
commit
dc1a8b1b1c
@ -43,6 +43,11 @@ const EXIT_ERROR_RUNTIME: i32 = 3;
|
||||
const EXIT_ERROR_ASSERT: i32 = 4;
|
||||
const EXIT_ERROR_UNDEFINED: i32 = 127;
|
||||
|
||||
struct Run {
|
||||
content: String,
|
||||
result: HurlResult,
|
||||
}
|
||||
|
||||
/// Executes Hurl entry point.
|
||||
fn main() {
|
||||
init_colored();
|
||||
@ -88,8 +93,7 @@ fn main() {
|
||||
let current_dir = current_dir.as_path();
|
||||
|
||||
let start = Instant::now();
|
||||
let mut hurl_results = vec![];
|
||||
let mut testcases = vec![];
|
||||
let mut runs = vec![];
|
||||
|
||||
for (current, filename) in filenames.iter().enumerate() {
|
||||
// We check the input file existence and check that we can read its contents.
|
||||
@ -154,45 +158,38 @@ fn main() {
|
||||
unwrap_or_exit(result, EXIT_ERROR_RUNTIME, &base_logger);
|
||||
}
|
||||
|
||||
if cli_options.junit_file.is_some() {
|
||||
let testcase = report::Testcase::from_hurl_result(&hurl_result, &content);
|
||||
testcases.push(testcase);
|
||||
}
|
||||
|
||||
hurl_results.push(hurl_result);
|
||||
let run = Run {
|
||||
content,
|
||||
result: hurl_result,
|
||||
};
|
||||
runs.push(run);
|
||||
}
|
||||
|
||||
if let Some(filename) = cli_options.junit_file {
|
||||
base_logger.debug(format!("Writing Junit report to {filename}").as_str());
|
||||
let result = report::create_junit_report(filename, testcases);
|
||||
base_logger.debug(format!("Writing JUnit report to {filename}").as_str());
|
||||
let result = create_junit_report(&runs, &filename);
|
||||
unwrap_or_exit(result, EXIT_ERROR_UNDEFINED, &base_logger);
|
||||
}
|
||||
|
||||
if let Some(dir_path) = cli_options.html_dir {
|
||||
base_logger.debug(format!("Writing html report to {}", dir_path.display()).as_str());
|
||||
let result = report::write_html_report(&dir_path, &hurl_results);
|
||||
if let Some(dir) = cli_options.html_dir {
|
||||
base_logger.debug(format!("Writing HTML report to {}", dir.display()).as_str());
|
||||
let result = create_html_report(&runs, &dir);
|
||||
unwrap_or_exit(result, EXIT_ERROR_UNDEFINED, &base_logger);
|
||||
|
||||
for filename in filenames {
|
||||
let result = format_html(filename.as_str(), &dir_path);
|
||||
unwrap_or_exit(result, EXIT_ERROR_UNDEFINED, &base_logger);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(cookie_output_file) = cli_options.cookie_output_file {
|
||||
base_logger.debug(format!("Writing cookies to {cookie_output_file}").as_str());
|
||||
let path = Path::new(&cookie_output_file);
|
||||
let result = write_cookies_file(path, &hurl_results);
|
||||
if let Some(filename) = cli_options.cookie_output_file {
|
||||
base_logger.debug(format!("Writing cookies to {filename}").as_str());
|
||||
let result = create_cookies_file(&runs, &filename);
|
||||
unwrap_or_exit(result, EXIT_ERROR_UNDEFINED, &base_logger);
|
||||
}
|
||||
|
||||
if cli_options.test {
|
||||
let duration = start.elapsed().as_millis();
|
||||
let summary = get_summary(duration, &hurl_results);
|
||||
let summary = get_summary(duration, &runs);
|
||||
base_logger.info(summary.as_str());
|
||||
}
|
||||
|
||||
std::process::exit(exit_code(&hurl_results));
|
||||
std::process::exit(exit_code(&runs));
|
||||
}
|
||||
|
||||
/// Runs a Hurl format `content` originated form the file `filename` and returns a result.
|
||||
@ -278,12 +275,35 @@ fn exit_with_error(message: &str, code: i32, logger: &BaseLogger) -> ! {
|
||||
std::process::exit(code);
|
||||
}
|
||||
|
||||
/// Create a JUnit report for this run.
|
||||
fn create_junit_report(runs: &[Run], filename: &str) -> Result<(), CliError> {
|
||||
let mut testcases = vec![];
|
||||
for run in runs.iter() {
|
||||
let hurl_result = &run.result;
|
||||
let content = &run.content;
|
||||
let testcase = report::Testcase::from(hurl_result, content);
|
||||
testcases.push(testcase);
|
||||
}
|
||||
report::create_junit_report(filename, &testcases)
|
||||
}
|
||||
|
||||
/// Create an HTML report for this run.
|
||||
fn create_html_report(runs: &[Run], dir_path: &Path) -> Result<(), CliError> {
|
||||
let hurl_results = runs.iter().map(|it| &it.result).collect::<Vec<_>>();
|
||||
report::write_html_report(dir_path, &hurl_results)?;
|
||||
for run in runs.iter() {
|
||||
let filename = &run.result.filename;
|
||||
format_html(filename, dir_path)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns an exit code for a list of HurlResult.
|
||||
fn exit_code(hurl_results: &[HurlResult]) -> i32 {
|
||||
fn exit_code(runs: &[Run]) -> i32 {
|
||||
let mut count_errors_runner = 0;
|
||||
let mut count_errors_assert = 0;
|
||||
for hurl_result in hurl_results {
|
||||
let errors = hurl_result.errors();
|
||||
for run in runs.iter() {
|
||||
let errors = run.result.errors();
|
||||
if errors.is_empty() {
|
||||
} else if errors.iter().filter(|e| !e.assert).count() == 0 {
|
||||
count_errors_assert += 1;
|
||||
@ -366,11 +386,11 @@ fn format_html(input_file: &str, dir_path: &Path) -> Result<(), CliError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_cookies_file(file_path: &Path, hurl_results: &[HurlResult]) -> Result<(), CliError> {
|
||||
let mut file = match std::fs::File::create(file_path) {
|
||||
fn create_cookies_file(runs: &[Run], filename: &str) -> Result<(), CliError> {
|
||||
let mut file = match std::fs::File::create(filename) {
|
||||
Err(why) => {
|
||||
return Err(CliError {
|
||||
message: format!("Issue writing to {}: {:?}", file_path.display(), why),
|
||||
message: format!("Issue writing to {filename}: {why:?}"),
|
||||
});
|
||||
}
|
||||
Ok(file) => file,
|
||||
@ -380,14 +400,14 @@ fn write_cookies_file(file_path: &Path, hurl_results: &[HurlResult]) -> Result<(
|
||||
|
||||
"#
|
||||
.to_string();
|
||||
match hurl_results.first() {
|
||||
match runs.first() {
|
||||
None => {
|
||||
return Err(CliError {
|
||||
message: "Issue fetching results".to_string(),
|
||||
});
|
||||
}
|
||||
Some(result) => {
|
||||
for cookie in result.cookies.clone() {
|
||||
Some(run) => {
|
||||
for cookie in run.result.cookies.clone() {
|
||||
s.push_str(cookie.to_string().as_str());
|
||||
s.push('\n');
|
||||
}
|
||||
@ -396,15 +416,15 @@ fn write_cookies_file(file_path: &Path, hurl_results: &[HurlResult]) -> Result<(
|
||||
|
||||
if let Err(why) = file.write_all(s.as_bytes()) {
|
||||
return Err(CliError {
|
||||
message: format!("Issue writing to {}: {:?}", file_path.display(), why),
|
||||
message: format!("Issue writing to {filename}: {why:?}"),
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_summary(duration: u128, hurl_results: &[HurlResult]) -> String {
|
||||
let total = hurl_results.len();
|
||||
let success = hurl_results.iter().filter(|r| r.success).count();
|
||||
fn get_summary(duration: u128, runs: &[Run]) -> String {
|
||||
let total = runs.len();
|
||||
let success = runs.iter().filter(|r| r.result.success).count();
|
||||
let failed = total - success;
|
||||
let mut s =
|
||||
"--------------------------------------------------------------------------------\n"
|
||||
|
@ -34,10 +34,10 @@ struct HTMLResult {
|
||||
pub success: bool,
|
||||
}
|
||||
|
||||
pub fn write_html_report(dir_path: &Path, hurl_results: &[HurlResult]) -> Result<(), CliError> {
|
||||
pub fn write_html_report(dir_path: &Path, hurl_results: &[&HurlResult]) -> Result<(), CliError> {
|
||||
let index_path = dir_path.join("index.html");
|
||||
let mut results = parse_html(&index_path)?;
|
||||
for result in hurl_results {
|
||||
for result in hurl_results.iter() {
|
||||
let html_result = HTMLResult {
|
||||
filename: canonicalize_filename(&result.filename),
|
||||
time_in_ms: result.time_in_ms,
|
||||
@ -48,7 +48,7 @@ pub fn write_html_report(dir_path: &Path, hurl_results: &[HurlResult]) -> Result
|
||||
let now: DateTime<Local> = Local::now();
|
||||
let s = create_html_index(&now.to_rfc2822(), &results);
|
||||
|
||||
let file_path = dir_path.join("index.html");
|
||||
let file_path = index_path;
|
||||
let mut file = match std::fs::File::create(&file_path) {
|
||||
Err(why) => {
|
||||
return Err(CliError {
|
||||
|
@ -66,7 +66,7 @@ use crate::cli::CliError;
|
||||
|
||||
mod testcase;
|
||||
|
||||
pub fn create_report(filename: String, testcases: Vec<Testcase>) -> Result<(), CliError> {
|
||||
pub fn create_report(filename: &str, testcases: &[Testcase]) -> Result<(), CliError> {
|
||||
let mut testsuites = vec![];
|
||||
|
||||
let path = std::path::Path::new(&filename);
|
||||
@ -113,7 +113,7 @@ pub fn create_report(filename: String, testcases: Vec<Testcase>) -> Result<(), C
|
||||
}
|
||||
}
|
||||
|
||||
fn create_testsuite(testcases: Vec<Testcase>) -> XMLNode {
|
||||
fn create_testsuite(testcases: &[Testcase]) -> XMLNode {
|
||||
let children = testcases
|
||||
.iter()
|
||||
.map(|t| XMLNode::Element(t.to_xml()))
|
||||
|
@ -32,7 +32,7 @@ pub struct Testcase {
|
||||
|
||||
impl Testcase {
|
||||
/// Creates an XML Junit <testcase> from an Hurl result.
|
||||
pub fn from_hurl_result(hurl_result: &HurlResult, content: &str) -> Testcase {
|
||||
pub fn from(hurl_result: &HurlResult, content: &str) -> Testcase {
|
||||
let id = hurl_result.filename.clone();
|
||||
let name = hurl_result.filename.clone();
|
||||
let time_in_ms = hurl_result.time_in_ms;
|
||||
@ -117,7 +117,7 @@ mod test {
|
||||
|
||||
let mut buffer = Vec::new();
|
||||
let content = "";
|
||||
Testcase::from_hurl_result(&hurl_result, content)
|
||||
Testcase::from(&hurl_result, content)
|
||||
.to_xml()
|
||||
.write(&mut buffer)
|
||||
.unwrap();
|
||||
@ -154,7 +154,7 @@ HTTP/1.0 200
|
||||
cookies: vec![],
|
||||
};
|
||||
let mut buffer = Vec::new();
|
||||
Testcase::from_hurl_result(&hurl_result, content)
|
||||
Testcase::from(&hurl_result, content)
|
||||
.to_xml()
|
||||
.write(&mut buffer)
|
||||
.unwrap();
|
||||
@ -195,7 +195,7 @@ HTTP/1.0 200
|
||||
cookies: vec![],
|
||||
};
|
||||
let mut buffer = Vec::new();
|
||||
Testcase::from_hurl_result(&hurl_result, content)
|
||||
Testcase::from(&hurl_result, content)
|
||||
.to_xml()
|
||||
.write(&mut buffer)
|
||||
.unwrap();
|
||||
|
Loading…
Reference in New Issue
Block a user