Skip to content

Commit

Permalink
test: move osakit to a separate test with no harness
Browse files Browse the repository at this point in the history
to force it run on the main thread
  • Loading branch information
eugenesvk committed Dec 9, 2024
1 parent 8c69c26 commit 1ec7904
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 37 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ env_logger = "0.10.0"
tempfile = "3.8.0"
defer = "0.2.1"

[[test]]
name = "osakit"
path = "tests/osakit.rs"
harness = false


[target.'cfg(target_os = "macos")'.dependencies]
objc2 = "0.5.1"
Expand Down
37 changes: 0 additions & 37 deletions src/macos/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,43 +47,6 @@ fn test_delete_with_finder_with_info() {
assert!(File::open(&trashed_path).is_err()); // cleaned up trash items
}

/*
#[test]
#[serial]
fn test_delete_with_finder_osakit_with_info() { // tested to work, but not always: can randomly timeout, so disabled by default
init_logging();
let mut trash_ctx = TrashContext::default();
trash_ctx.set_delete_method(DeleteMethod::Finder);
trash_ctx.set_script_method(ScriptMethod::Osakit);
let mut path1 = PathBuf::from(get_unique_name());
let mut path2 = PathBuf::from(get_unique_name());
path1.set_extension(r#"a"b,"#);
path2.set_extension(r#"x80=%80 slash=\ pc=% quote=" comma=,"#);
File::create_new(&path1).unwrap();
File::create_new(&path2).unwrap();
let trashed_items = trash_ctx.delete_all_with_info(&[path1.clone(), path2.clone()]).unwrap().unwrap(); //Ok + Some trashed paths
assert!(File::open(&path1).is_err()); // original files deleted
assert!(File::open(&path2).is_err());
for item in trashed_items {
let trashed_path = item.id;
assert!(!File::open(&trashed_path).is_err()); // returned trash items exist
std::fs::remove_file(&trashed_path).unwrap(); // clean up
assert!(File::open(&trashed_path).is_err()); // cleaned up trash items
}
// test a single file (in case returned paths aren't an array anymore)
let mut path3 = PathBuf::from(get_unique_name());
path3.set_extension(r#"a"b,"#);
File::create_new(&path3).unwrap();
let item = trash_ctx.delete_with_info(&path3).unwrap().unwrap(); //Ok + Some trashed paths
assert!(File::open(&path3).is_err()); // original files deleted
let trashed_path = item.id;
assert!(!File::open(&trashed_path).is_err()); // returned trash items exist
std::fs::remove_file(&trashed_path).unwrap(); // clean up
assert!(File::open(&trashed_path).is_err()); // cleaned up trash items
}*/

#[test]
#[serial]
fn test_delete_binary_with_finder_with_info() {
Expand Down
49 changes: 49 additions & 0 deletions tests/osakit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Separate file to force running the tests on the main thread, which is required for any macOS OSAkit APIs, which otherwise fail after a 2-min stall
use trash::{
canonicalize_paths,
macos::{percent_encode, DeleteMethod, ScriptMethod, TrashContextExtMacos},
tests::{get_unique_name, init_logging},
TrashContext,
};
use serial_test::serial;
use std::ffi::OsStr;
use std::fs::File;
use std::os::unix::ffi::OsStrExt;
use std::path::PathBuf;
use std::process::Command;

#[test]
#[serial]
fn test_delete_with_finder_osakit_with_info() { // tested to work, but not always: can randomly timeout, so disabled by default
init_logging();
let mut trash_ctx = TrashContext::default();
trash_ctx.set_delete_method(DeleteMethod::Finder);
trash_ctx.set_script_method(ScriptMethod::Osakit);

let mut path1 = PathBuf::from(get_unique_name());
let mut path2 = PathBuf::from(get_unique_name());
path1.set_extension(r#"a"b,"#);
path2.set_extension(r#"x80=%80 slash=\ pc=% quote=" comma=,"#);
File::create_new(&path1).unwrap();
File::create_new(&path2).unwrap();
let trashed_items = trash_ctx.delete_all_with_info(&[path1.clone(), path2.clone()]).unwrap().unwrap(); //Ok + Some trashed paths
assert!(File::open(&path1).is_err()); // original files deleted
assert!(File::open(&path2).is_err());
for item in trashed_items {
let trashed_path = item.id;
assert!(!File::open(&trashed_path).is_err()); // returned trash items exist
std::fs::remove_file(&trashed_path).unwrap(); // clean up
assert!(File::open(&trashed_path).is_err()); // cleaned up trash items
}

// test a single file (in case returned paths aren't an array anymore)
let mut path3 = PathBuf::from(get_unique_name());
path3.set_extension(r#"a"b,"#);
File::create_new(&path3).unwrap();
let item = trash_ctx.delete_with_info(&path3).unwrap().unwrap(); //Ok + Some trashed paths
assert!(File::open(&path3).is_err()); // original files deleted
let trashed_path = item.id;
assert!(!File::open(&trashed_path).is_err()); // returned trash items exist
std::fs::remove_file(&trashed_path).unwrap(); // clean up
assert!(File::open(&trashed_path).is_err()); // cleaned up trash items
}

0 comments on commit 1ec7904

Please sign in to comment.