From 8300305dc1dfae0f9b36426870b67c6de33d87b0 Mon Sep 17 00:00:00 2001 From: Firelight Flagboy Date: Fri, 17 Jan 2025 12:07:17 +0100 Subject: [PATCH] [macos|rust] Add mountpoint test that perform a copy using finder.app Closes #9366 --- Cargo.lock | 10 +++++ .../crates/platform_mountpoint/Cargo.toml | 2 +- .../scripts/macos/copy-using-finder.scpt | 23 ++++++++++ .../tests/unit/operations/macos/copy.rs | 42 +++++++++++++++++++ .../tests/unit/operations/macos/mod.rs | 1 + .../tests/unit/operations/mod.rs | 2 + 6 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 libparsec/crates/platform_mountpoint/scripts/macos/copy-using-finder.scpt create mode 100644 libparsec/crates/platform_mountpoint/tests/unit/operations/macos/copy.rs create mode 100644 libparsec/crates/platform_mountpoint/tests/unit/operations/macos/mod.rs diff --git a/Cargo.lock b/Cargo.lock index f59a0a50c05..fd64c65ac6d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3947,6 +3947,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + [[package]] name = "signature" version = "1.6.4" @@ -4395,6 +4404,7 @@ dependencies = [ "libc", "mio", "pin-project-lite", + "signal-hook-registry", "socket2", "tokio-macros", "windows-sys 0.52.0", diff --git a/libparsec/crates/platform_mountpoint/Cargo.toml b/libparsec/crates/platform_mountpoint/Cargo.toml index 1558e7b0b65..dd372d60ad0 100644 --- a/libparsec/crates/platform_mountpoint/Cargo.toml +++ b/libparsec/crates/platform_mountpoint/Cargo.toml @@ -37,7 +37,7 @@ env_logger = { workspace = true } libparsec_client_connection = { workspace = true } libparsec_tests_fixtures = { workspace = true, features = ["default"] } libparsec_tests_lite = { workspace = true } -tokio = { workspace = true, features = ["fs"] } +tokio = { workspace = true, features = ["fs", "process"] } windows-sys = { workspace = true, features = ["Win32"] } [target.'cfg(target_family = "unix")'.dev-dependencies] diff --git a/libparsec/crates/platform_mountpoint/scripts/macos/copy-using-finder.scpt b/libparsec/crates/platform_mountpoint/scripts/macos/copy-using-finder.scpt new file mode 100644 index 00000000000..3fcf1161461 --- /dev/null +++ b/libparsec/crates/platform_mountpoint/scripts/macos/copy-using-finder.scpt @@ -0,0 +1,23 @@ +#!/usr/bin/osascript + +on run argv + try + -- Get file and dest path from arguments + set file_path to item 1 of argv + set dest_path to item 2 of argv + + -- Use Finder to copy the file to dest + tell application "Finder" + set source_file to POSIX file file_path as alias + set destination_folder to POSIX file dest_path as alias + duplicate source_file to destination_folder + end tell + + on error err_msg number err_num + -- Print error message to stderr + do shell script "echo " & quoted form of ("Error: " & err_msg) & " >&2" + return err_num + end try +end run + + diff --git a/libparsec/crates/platform_mountpoint/tests/unit/operations/macos/copy.rs b/libparsec/crates/platform_mountpoint/tests/unit/operations/macos/copy.rs new file mode 100644 index 00000000000..d2895acea4b --- /dev/null +++ b/libparsec/crates/platform_mountpoint/tests/unit/operations/macos/copy.rs @@ -0,0 +1,42 @@ +use std::path::PathBuf; + +use libparsec_tests_fixtures::{tmp_path, TestbedEnv, TmpPath}; +use libparsec_tests_lite::parsec_test; + +use crate::operations::utils::mount_and_test; + +#[parsec_test(testbed = "minimal_client_ready")] +async fn copy_file_using_finder(tmp_path: TmpPath, env: &TestbedEnv) { + mount_and_test!( + env, + &tmp_path, + |_client, _wksp_ops, mountpoint_path: PathBuf| async move { + const FILE_CONTENT: &str = "I'm the file content that should be copied"; + let src_file = tmp_path.join("foo.txt"); + let dst_file = mountpoint_path.join("foo.txt"); + let script_path = std::path::PathBuf::from(std::env!("CARGO_MANIFEST_DIR")) + .join("scripts/macos/copy-using-finder.scpt"); + + assert!( + !tokio::fs::try_exists(&dst_file).await.unwrap(), + "The destination file should not exist before the copy" + ); + + // Create the source file with some content + tokio::fs::write(&src_file, FILE_CONTENT).await.unwrap(); + + // Call the osascript that perform the copy using Finder.app + tokio::process::Command::new("osascript") + .args([&script_path, &src_file, &dst_file]) + .spawn() + .unwrap() + .wait() + .await + .unwrap(); + + // Verify the content of the copied file + let data = tokio::fs::read_to_string(&dst_file).await.unwrap(); + assert_eq!(data, FILE_CONTENT); + } + ); +} diff --git a/libparsec/crates/platform_mountpoint/tests/unit/operations/macos/mod.rs b/libparsec/crates/platform_mountpoint/tests/unit/operations/macos/mod.rs new file mode 100644 index 00000000000..61f085b49c6 --- /dev/null +++ b/libparsec/crates/platform_mountpoint/tests/unit/operations/macos/mod.rs @@ -0,0 +1 @@ +mod copy; diff --git a/libparsec/crates/platform_mountpoint/tests/unit/operations/mod.rs b/libparsec/crates/platform_mountpoint/tests/unit/operations/mod.rs index b01dbdd3549..27632b3219e 100644 --- a/libparsec/crates/platform_mountpoint/tests/unit/operations/mod.rs +++ b/libparsec/crates/platform_mountpoint/tests/unit/operations/mod.rs @@ -3,6 +3,8 @@ mod create_folder; mod flush_file; mod list_directory; +#[cfg(target_os = "macos")] +mod macos; mod mount_unmount; mod move_entry; mod open_file;