Skip to content

Commit

Permalink
wip: test delayed write to file in unittests
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Gherghescu <[email protected]>
  • Loading branch information
andrei-ng committed May 1, 2024
1 parent 0b86727 commit 938cad2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
20 changes: 15 additions & 5 deletions plotly/src/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,9 @@ impl PartialEq for Plot {

#[cfg(test)]
mod tests {
use std::path::PathBuf;

use serde_json::{json, to_value};
use std::path::PathBuf;
use std::thread;

use super::*;
use crate::Scatter;
Expand All @@ -499,6 +499,14 @@ mod tests {
plot
}

fn delay_file_check() {
#[cfg(target_os = "linux")]
let delay = std::time::Duration::from_millis(0);
#[cfg(not(target_os = "linux"))]
let delay = std::time::Duration::from_millis(100);
thread::sleep(delay);
}

#[test]
fn test_inline_plot() {
let plot = create_test_plot();
Expand Down Expand Up @@ -686,26 +694,28 @@ mod tests {
assert!(!dst.exists());
}

#[cfg(not(target_os = "windows"))]
// #[cfg(not(target_os = "windows"))]
#[test]
#[ignore] // This seems to fail unpredictably on MacOs.
// #[ignore] // This seems to fail unpredictably on MacOs.
#[cfg(feature = "kaleido")]
fn test_save_to_eps() {
let plot = create_test_plot();
let dst = PathBuf::from("example.eps");
plot.write_image(&dst, ImageFormat::EPS, 1024, 680, 1.0);
delay_file_check();
assert!(dst.exists());
assert!(std::fs::remove_file(&dst).is_ok());
assert!(!dst.exists());
}

#[cfg(not(target_os = "windows"))]
// #[cfg(not(target_os = "windows"))]
#[test]
#[cfg(feature = "kaleido")]
fn test_save_to_pdf() {
let plot = create_test_plot();
let dst = PathBuf::from("example.pdf");
plot.write_image(&dst, ImageFormat::PDF, 1024, 680, 1.0);
delay_file_check();
assert!(dst.exists());
assert!(std::fs::remove_file(&dst).is_ok());
assert!(!dst.exists());
Expand Down
30 changes: 22 additions & 8 deletions plotly_kaleido/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ impl Kaleido {

#[cfg(test)]
mod tests {
use std::path::PathBuf;

use serde_json::{json, to_value};
use std::path::PathBuf;
use std::thread;

use super::*;

Expand Down Expand Up @@ -218,6 +218,14 @@ mod tests {
.unwrap()
}

fn delay_file_check() {
#[cfg(target_os = "linux")]
let delay = std::time::Duration::from_millis(0);
#[cfg(not(target_os = "linux"))]
let delay = std::time::Duration::from_millis(100);
thread::sleep(delay);
}

#[test]
fn test_can_find_kaleido_executable() {
let _k = Kaleido::new();
Expand All @@ -238,62 +246,67 @@ mod tests {
assert_eq!(to_value(kaleido_data).unwrap(), expected);
}

#[cfg(not(target_os = "windows"))]
// #[cfg(not(target_os = "windows"))]
#[test]
fn test_save_png() {
let test_plot = create_test_plot();
let k = Kaleido::new();
let dst = PathBuf::from("example.png");
let r = k.save(dst.as_path(), &test_plot, "png", 1200, 900, 4.5);
assert!(r.is_ok());
delay_file_check();
assert!(std::fs::remove_file(dst.as_path()).is_ok());
}

#[cfg(not(target_os = "windows"))]
// #[cfg(not(target_os = "windows"))]
#[test]
fn test_save_jpeg() {
let test_plot = create_test_plot();
let k = Kaleido::new();
let dst = PathBuf::from("example.jpeg");
let r = k.save(dst.as_path(), &test_plot, "jpeg", 1200, 900, 4.5);
assert!(r.is_ok());
delay_file_check();
assert!(std::fs::remove_file(dst.as_path()).is_ok());
}

#[cfg(not(target_os = "windows"))]
// #[cfg(not(target_os = "windows"))]
#[test]
fn test_save_webp() {
let test_plot = create_test_plot();
let k = Kaleido::new();
let dst = PathBuf::from("example.webp");
let r = k.save(dst.as_path(), &test_plot, "webp", 1200, 900, 4.5);
assert!(r.is_ok());
delay_file_check();
assert!(std::fs::remove_file(dst.as_path()).is_ok());
}

#[cfg(not(target_os = "windows"))]
// #[cfg(not(target_os = "windows"))]
#[test]
fn test_save_svg() {
let test_plot = create_test_plot();
let k = Kaleido::new();
let dst = PathBuf::from("example.svg");
let r = k.save(dst.as_path(), &test_plot, "svg", 1200, 900, 4.5);
assert!(r.is_ok());
delay_file_check();
assert!(std::fs::remove_file(dst.as_path()).is_ok());
}

#[cfg(not(target_os = "windows"))]
// #[cfg(not(target_os = "windows"))]
#[test]
fn test_save_pdf() {
let test_plot = create_test_plot();
let k = Kaleido::new();
let dst = PathBuf::from("example.pdf");
let r = k.save(dst.as_path(), &test_plot, "pdf", 1200, 900, 4.5);
assert!(r.is_ok());
delay_file_check();
assert!(std::fs::remove_file(dst.as_path()).is_ok());
}

#[cfg(not(target_os = "windows"))]
// #[cfg(not(target_os = "windows"))]
#[test]
#[ignore]
fn test_save_eps() {
Expand All @@ -302,6 +315,7 @@ mod tests {
let dst = PathBuf::from("example.eps");
let r = k.save(dst.as_path(), &test_plot, "eps", 1200, 900, 4.5);
assert!(r.is_ok());
delay_file_check();
assert!(std::fs::remove_file(dst.as_path()).is_ok());
}
}

0 comments on commit 938cad2

Please sign in to comment.