Skip to content

Commit 5d5bed6

Browse files
committed
tests: add tests to patch_render
This commit add patch_render functions tests, increasing patch hub tests coverage. Signed-off-by: JGBSouza <[email protected]>
1 parent ad93a74 commit 5d5bed6

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

src/app/patch_renderer.rs

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,134 @@ fn diff_so_fancy_renderer(patch: &str) -> color_eyre::Result<String> {
179179
let output = dsf.wait_with_output()?;
180180
Ok(String::from_utf8(output.stdout)?)
181181
}
182+
183+
#[cfg(test)]
184+
mod tests {
185+
use super::*;
186+
use crate::app::config::Config;
187+
use crate::infrastructure::logging::Logger;
188+
use std::io::ErrorKind;
189+
use std::sync::Once;
190+
191+
static INIT: Once = Once::new();
192+
193+
fn init_test_logger() {
194+
INIT.call_once(|| {
195+
let mut config = Config::default();
196+
let mut temp_dir = std::env::temp_dir();
197+
temp_dir.push("patch-hub-renderer-tests");
198+
let _ = std::fs::create_dir_all(&temp_dir);
199+
200+
config.set_data_dir(temp_dir.to_string_lossy().to_string());
201+
202+
let _ = std::panic::catch_unwind(|| {
203+
Logger::init_log_file(&config).ok();
204+
});
205+
});
206+
}
207+
208+
fn get_sample_patch() -> &'static str {
209+
"diff --git a/file.rs b/file.rs\n\
210+
index 123..456 100644\n\
211+
--- a/file.rs\n\
212+
+++ b/file.rs\n\
213+
@@ -1,3 +1,3 @@\n\
214+
-old line\n\
215+
+new line\n\
216+
context line"
217+
}
218+
219+
#[test]
220+
fn test_patch_renderer_enum_conversions_and_display() {
221+
init_test_logger();
222+
223+
assert!(matches!(PatchRenderer::from("bat"), PatchRenderer::Bat));
224+
assert!(matches!(PatchRenderer::from("delta"), PatchRenderer::Delta));
225+
assert!(matches!(
226+
PatchRenderer::from("diff-so-fancy"),
227+
PatchRenderer::DiffSoFancy
228+
));
229+
230+
assert!(matches!(
231+
PatchRenderer::from("unknown-tool"),
232+
PatchRenderer::Default
233+
));
234+
assert!(matches!(PatchRenderer::from(""), PatchRenderer::Default));
235+
236+
assert_eq!(PatchRenderer::Bat.to_string(), "bat");
237+
assert_eq!(PatchRenderer::Default.to_string(), "default");
238+
assert_eq!(PatchRenderer::DiffSoFancy.to_string(), "diff-so-fancy");
239+
}
240+
241+
#[test]
242+
fn test_clean_patch_for_preview() {
243+
init_test_logger();
244+
245+
let raw = "line1\nline2\nline3";
246+
let cleaned = clean_patch_for_preview(raw);
247+
assert_eq!(cleaned, raw);
248+
249+
let raw_with_sig = "line1\nline2\n--\nRegards,\nAuthor";
250+
let expected = "line1\nline2";
251+
let cleaned_sig = clean_patch_for_preview(raw_with_sig);
252+
assert_eq!(cleaned_sig, expected);
253+
254+
let raw_code_decrement = "cnt--;\nif (x) {\n--\nSig";
255+
let expected_code = "cnt--;\nif (x) {";
256+
let cleaned_code = clean_patch_for_preview(raw_code_decrement);
257+
assert_eq!(cleaned_code, expected_code);
258+
259+
assert_eq!(clean_patch_for_preview(""), "");
260+
}
261+
262+
#[test]
263+
fn test_render_patch_preview() {
264+
init_test_logger();
265+
let patch = get_sample_patch();
266+
267+
let result = render_patch_preview(patch, &PatchRenderer::Default);
268+
assert!(result.is_ok());
269+
assert_eq!(result.unwrap(), patch.to_string());
270+
271+
let result_bat = render_patch_preview(patch, &PatchRenderer::Bat);
272+
assert!(result_bat.is_ok() || result_bat.is_err());
273+
}
274+
275+
#[test]
276+
fn test_delta_patch_renderer() {
277+
init_test_logger();
278+
let patch = get_sample_patch();
279+
let result = delta_patch_renderer(patch);
280+
281+
match result {
282+
Ok(output) => assert!(!output.is_empty()),
283+
Err(e) => {
284+
if let Some(io_err) = e.downcast_ref::<std::io::Error>() {
285+
if io_err.kind() == ErrorKind::NotFound {
286+
println!("Skipping delta test: command not found");
287+
return;
288+
}
289+
}
290+
}
291+
}
292+
}
293+
294+
#[test]
295+
fn test_diff_so_fancy_renderer() {
296+
init_test_logger();
297+
let patch = get_sample_patch();
298+
let result = diff_so_fancy_renderer(patch);
299+
300+
match result {
301+
Ok(output) => assert!(!output.is_empty()),
302+
Err(e) => {
303+
if let Some(io_err) = e.downcast_ref::<std::io::Error>() {
304+
if io_err.kind() == ErrorKind::NotFound {
305+
println!("Skipping diff-so-fancy test: command not found");
306+
return;
307+
}
308+
}
309+
}
310+
}
311+
}
312+
}

0 commit comments

Comments
 (0)