oxedyne/fe2o3/fe2o3_pearlite/tests/register.rs
2.5 KiB, 1 run
created by r1870400018:60986, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use oxedyne_fe2o3_pearlite::register::{ |
| 2 | self, |
| 3 | Step, |
| 4 | DESKTOP_ID, |
| 5 | MIME_TYPE, |
| 6 | PROG_ID, |
| 7 | }; |
| 8 | |
| 9 | use oxedyne_fe2o3_core::prelude::*; |
| 10 | |
| 11 | use std::path::Path; |
| 12 | |
| 13 | |
| 14 | #[test] |
| 15 | fn test_linux_plan_points_the_entry_at_the_binary() { |
| 16 | let steps = register::plan_linux(Path::new("/opt/my apps/pearlite"), Path::new("/home/u/.local/share")); |
| 17 | let desktop = steps.iter().find_map(|s| match s { |
| 18 | Step::Write { path, body } if path.ends_with(DESKTOP_ID) => Some(body.clone()), |
| 19 | _ => None, |
| 20 | }); |
| 21 | match desktop { |
| 22 | Some(b) => { |
| 23 | assert!(b.contains("Exec=\"/opt/my apps/pearlite\" %f\n"), "{}", b); |
| 24 | assert!(b.contains("MimeType=application/x-pearlite;")); |
| 25 | }, |
| 26 | None => panic!("no .desktop entry in the plan"), |
| 27 | } |
| 28 | let last = steps.last().cloned(); |
| 29 | assert_eq!(last, Some(Step::Run { |
| 30 | prog: "xdg-mime".to_string(), |
| 31 | args: vec!["default".into(), DESKTOP_ID.into(), MIME_TYPE.into()], |
| 32 | required: true, |
| 33 | })); |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn test_desktop_exec_arg_escapes_reserved_characters() { |
| 38 | assert_eq!(register::desktop_exec_arg(Path::new("/a/$b\"c`d\\e")), "\"/a/\\$b\\\"c\\`d\\\\e\""); |
| 39 | } |
| 40 | |
| 41 | #[test] |
| 42 | fn test_windows_plan_writes_the_open_command() { |
| 43 | let steps = register::plan_windows(Path::new("C:\\Tools\\pearlite-reader.exe")); |
| 44 | let open = steps.iter().any(|s| match s { |
| 45 | Step::Run { prog, args, .. } => prog == "reg" |
| 46 | && args.iter().any(|a| a == "HKCU\\Software\\Classes\\Pearlite.Document\\shell\\open\\command") |
| 47 | && args.iter().any(|a| a == "\"C:\\Tools\\pearlite-reader.exe\" \"%1\""), |
| 48 | _ => false, |
| 49 | }); |
| 50 | assert!(open); |
| 51 | let ext = steps.iter().any(|s| match s { |
| 52 | Step::Run { args, .. } => args.get(1).map(|k| k.ends_with("\\.prl")).unwrap_or(false) |
| 53 | && args.iter().any(|a| a == PROG_ID), |
| 54 | _ => false, |
| 55 | }); |
| 56 | assert!(ext); |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn test_execute_writes_files_and_passes_an_optional_failure() { |
| 61 | let dir = std::env::temp_dir().join(fmt!("pearlite-register-{}", std::process::id())); |
| 62 | let steps = vec![ |
| 63 | Step::Write { path: dir.join("a").join("b.txt"), body: "hi".to_string() }, |
| 64 | Step::Run { prog: "pearlite-no-such-tool".to_string(), args: vec![], required: false }, |
| 65 | ]; |
| 66 | let report = match register::execute(&steps) { |
| 67 | Ok(r) => r, |
| 68 | Err(e) => panic!("{}", e), |
| 69 | }; |
| 70 | assert_eq!(report.len(), 2); |
| 71 | assert!(report[1].starts_with("skipped")); |
| 72 | assert_eq!(std::fs::read_to_string(dir.join("a").join("b.txt")).ok(), Some("hi".to_string())); |
| 73 | let _ = std::fs::remove_dir_all(&dir); |
| 74 | let failing = vec![Step::Run { prog: "pearlite-no-such-tool".to_string(), args: vec![], required: true }]; |
| 75 | assert!(register::execute(&failing).is_err()); |
| 76 | } |