mod deserialize;
use std::fs::File;
use std::io;
use std::io::Write;
use std::path::Path;
use hurl_core::input::Input;
use crate::report::ReportError;
use crate::runner::HurlResult;
pub fn write_report(
filename: &Path,
testcases: &[Testcase],
response_dir: &Path,
secrets: &[&str],
) -> Result<(), ReportError> {
let mut report = deserialize::parse_json_report(filename)?;
let json = testcases
.iter()
.map(|t| t.to_json(response_dir, secrets))
.collect::<Result<Vec<_>, _>>()
.map_err(|e| ReportError::from_io_error(&e, filename, "Issue creating JSON report"))?;
report.extend(json);
let serialized = serde_json::to_string(&report)?;
let bytes = format!("{serialized}\n");
let bytes = bytes.into_bytes();
let mut file_out = File::create(filename)
.map_err(|e| ReportError::from_io_error(&e, filename, "Issue creating JSON report"))?;
file_out
.write_all(&bytes)
.map_err(|e| ReportError::from_io_error(&e, filename, "Issue writing JSON report"))?;
Ok(())
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Testcase<'a> {
result: &'a HurlResult,
content: &'a str,
filename: &'a Input,
}
impl<'a> Testcase<'a> {
pub fn new(hurl_result: &'a HurlResult, content: &'a str, filename: &'a Input) -> Self {
Testcase {
result: hurl_result,
content,
filename,
}
}
fn to_json(
&self,
response_dir: &Path,
secrets: &[&str],
) -> Result<serde_json::Value, io::Error> {
self.result
.to_json(self.content, self.filename, Some(response_dir), secrets)
}
}