From c537893773c0732724e0a044e0b00c74d08bca56 Mon Sep 17 00:00:00 2001 From: Kirill Date: Mon, 2 Mar 2026 13:46:42 +0300 Subject: [PATCH] tests: Added tests for installing rclone --- Cargo.lock | 344 ++++++++++++++++++++++++++++++++++-- Cargo.toml | 5 + src/tests.rs | 4 + src/tests/rclone_install.rs | 190 ++++++++++++++++++++ 4 files changed, 530 insertions(+), 13 deletions(-) create mode 100644 src/tests.rs create mode 100644 src/tests/rclone_install.rs diff --git a/Cargo.lock b/Cargo.lock index acf5771..da75339 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,9 +9,12 @@ dependencies = [ "dirs", "flate2", "futures", + "mockito", "reqwest", "serde_json", + "serial_test", "tar", + "tempfile", "tokio", "which", "zip", @@ -34,6 +37,31 @@ dependencies = [ "cpufeatures", ] +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "assert-json-diff" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e4f2b81832e72834d7518d8487a0396a28cc408186a2e8854c0f98011faf12" +dependencies = [ + "serde", + "serde_json", +] + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + [[package]] name = "base64" version = "0.21.7" @@ -133,6 +161,15 @@ dependencies = [ "inout", ] +[[package]] +name = "colored" +version = "3.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "faf9468729b8cbcea668e36183cb69d317348c2e08e994829fb56ebfdfbaac34" +dependencies = [ + "windows-sys 0.61.2", +] + [[package]] name = "constant_time_eq" version = "0.1.5" @@ -189,6 +226,19 @@ dependencies = [ "typenum", ] +[[package]] +name = "dashmap" +version = "5.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" +dependencies = [ + "cfg-if", + "hashbrown 0.14.5", + "lock_api", + "once_cell", + "parking_lot_core", +] + [[package]] name = "deranged" version = "0.5.8" @@ -272,6 +322,12 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + [[package]] name = "filetime" version = "0.2.27" @@ -446,7 +502,7 @@ dependencies = [ "futures-core", "futures-sink", "futures-util", - "http", + "http 0.2.12", "indexmap", "slab", "tokio", @@ -454,6 +510,31 @@ dependencies = [ "tracing", ] +[[package]] +name = "h2" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f44da3a8150a6703ed5d34e164b875fd14c2cdab9af1252a9a1020bde2bdc54" +dependencies = [ + "atomic-waker", + "bytes", + "fnv", + "futures-core", + "futures-sink", + "http 1.4.0", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + [[package]] name = "hashbrown" version = "0.16.1" @@ -489,6 +570,16 @@ dependencies = [ "itoa", ] +[[package]] +name = "http" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3ba2a386d7f85a81f119ad7498ebe444d2e22c2af0b86b069416ace48b3311a" +dependencies = [ + "bytes", + "itoa", +] + [[package]] name = "http-body" version = "0.4.6" @@ -496,7 +587,30 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" dependencies = [ "bytes", - "http", + "http 0.2.12", + "pin-project-lite", +] + +[[package]] +name = "http-body" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" +dependencies = [ + "bytes", + "http 1.4.0", +] + +[[package]] +name = "http-body-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a" +dependencies = [ + "bytes", + "futures-core", + "http 1.4.0", + "http-body 1.0.1", "pin-project-lite", ] @@ -522,9 +636,9 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "h2", - "http", - "http-body", + "h2 0.3.27", + "http 0.2.12", + "http-body 0.4.6", "httparse", "httpdate", "itoa", @@ -536,6 +650,28 @@ dependencies = [ "want", ] +[[package]] +name = "hyper" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ab2d4f250c3d7b1c9fcdff1cece94ea4e2dfbec68614f7b87cb205f24ca9d11" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "h2 0.4.13", + "http 1.4.0", + "http-body 1.0.1", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "pin-utils", + "smallvec", + "tokio", +] + [[package]] name = "hyper-rustls" version = "0.24.2" @@ -543,13 +679,27 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590" dependencies = [ "futures-util", - "http", - "hyper", + "http 0.2.12", + "hyper 0.14.32", "rustls", "tokio", "tokio-rustls", ] +[[package]] +name = "hyper-util" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96547c2556ec9d12fb1578c4eaf448b04993e7fb79cbaad930a656880a6bdfa0" +dependencies = [ + "bytes", + "http 1.4.0", + "http-body 1.0.1", + "hyper 1.8.1", + "pin-project-lite", + "tokio", +] + [[package]] name = "icu_collections" version = "2.1.1" @@ -659,7 +809,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.16.1", ] [[package]] @@ -703,6 +853,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.182" @@ -786,6 +942,31 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "mockito" +version = "1.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90820618712cab19cfc46b274c6c22546a82affcb3c3bdf0f29e3db8e1bb92c0" +dependencies = [ + "assert-json-diff", + "bytes", + "colored", + "futures-core", + "http 1.4.0", + "http-body 1.0.1", + "http-body-util", + "hyper 1.8.1", + "hyper-util", + "log", + "pin-project-lite", + "rand", + "regex", + "serde_json", + "serde_urlencoded", + "similar", + "tokio", +] + [[package]] name = "num-conv" version = "0.2.0" @@ -834,7 +1015,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7676374caaee8a325c9e7a2ae557f216c5563a171d6997b0ef8a65af35147700" dependencies = [ "base64ct", - "rand_core", + "rand_core 0.6.4", "subtle", ] @@ -862,6 +1043,12 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "pkg-config" version = "0.3.32" @@ -883,6 +1070,15 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + [[package]] name = "proc-macro2" version = "1.0.106" @@ -907,12 +1103,41 @@ version = "5.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" +[[package]] +name = "rand" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1" +dependencies = [ + "rand_chacha", + "rand_core 0.9.5", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core 0.9.5", +] + [[package]] name = "rand_core" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + [[package]] name = "redox_syscall" version = "0.5.18" @@ -942,6 +1167,35 @@ dependencies = [ "thiserror", ] +[[package]] +name = "regex" +version = "1.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a" + [[package]] name = "reqwest" version = "0.11.27" @@ -953,10 +1207,10 @@ dependencies = [ "encoding_rs", "futures-core", "futures-util", - "h2", - "http", - "http-body", - "hyper", + "h2 0.3.27", + "http 0.2.12", + "http-body 0.4.6", + "hyper 0.14.32", "hyper-rustls", "ipnet", "js-sys", @@ -1138,6 +1392,31 @@ dependencies = [ "serde", ] +[[package]] +name = "serial_test" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e56dd856803e253c8f298af3f4d7eb0ae5e23a737252cd90bb4f3b435033b2d" +dependencies = [ + "dashmap", + "futures", + "lazy_static", + "log", + "parking_lot", + "serial_test_derive", +] + +[[package]] +name = "serial_test_derive" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91d129178576168c589c9ec973feedf7d3126c01ac2bf08795109aa35b69fb8f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "sha1" version = "0.10.6" @@ -1182,6 +1461,12 @@ version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e320a6c5ad31d271ad523dcf3ad13e2767ad8b1cb8f047f75a8aeaf8da139da2" +[[package]] +name = "similar" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbbb5d9659141646ae647b42fe094daf6c6192d1620870b449d9557f748b2daa" + [[package]] name = "slab" version = "0.4.12" @@ -1286,6 +1571,19 @@ dependencies = [ "xattr", ] +[[package]] +name = "tempfile" +version = "3.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82a72c767771b47409d2345987fda8628641887d5466101319899796367354a0" +dependencies = [ + "fastrand", + "getrandom 0.3.4", + "once_cell", + "rustix 1.1.4", + "windows-sys 0.61.2", +] + [[package]] name = "thiserror" version = "1.0.69" @@ -1881,6 +2179,26 @@ dependencies = [ "synstructure", ] +[[package]] +name = "zerocopy" +version = "0.8.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a789c6e490b576db9f7e6b6d661bcc9799f7c0ac8352f56ea20193b2681532e5" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f65c489a7071a749c849713807783f70672b28094011623e200cb86dcb835953" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "zerofrom" version = "0.1.6" diff --git a/Cargo.toml b/Cargo.toml index 6d8312a..49f4eab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,8 @@ zip = "0.6" dirs = "5.0" which = "6.0" serde_json = "1.0" + +[dev-dependencies] +tempfile = "3.8" +serial_test = "2.0" +mockito = "1.0" diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 0000000..b14e8b8 --- /dev/null +++ b/src/tests.rs @@ -0,0 +1,4 @@ +#[cfg(test)] +mod rclone_install; + + diff --git a/src/tests/rclone_install.rs b/src/tests/rclone_install.rs new file mode 100644 index 0000000..612d56b --- /dev/null +++ b/src/tests/rclone_install.rs @@ -0,0 +1,190 @@ +#[cfg(test)] +mod tests { + use crate::rcloneInstall::RcloneApp; + use std::fs; + use std::env; + use tempfile::tempdir; + use serial_test::serial; + + fn setup_test_dir() -> tempfile::TempDir { + tempdir().expect("Failed to create temp dir") + } + + #[test] + fn test_new_with_path_constructor() { + let temp_dir = setup_test_dir(); + let rclone_path = temp_dir.path().join("rclone"); + + let app = RcloneApp::new_with_path(rclone_path.clone(), true); + + assert_eq!(app.get_rclone_path(), &rclone_path); + assert!(app.is_using_system_rclone()); + } + + #[test] + fn test_getters() { + let temp_dir = setup_test_dir(); + let rclone_path = temp_dir.path().join("rclone"); + + let app = RcloneApp::new_with_path(rclone_path.clone(), true); + + assert_eq!(app.get_rclone_path(), &rclone_path); + assert!(app.is_using_system_rclone()); + } + + #[test] + fn test_setters() { + let temp_dir = setup_test_dir(); + let rclone_path1 = temp_dir.path().join("rclone1"); + let rclone_path2 = temp_dir.path().join("rclone2"); + + let mut app = RcloneApp::new_with_path(rclone_path1, false); + + assert!(!app.is_using_system_rclone()); + + app.set_using_system_rclone(true); + assert!(app.is_using_system_rclone()); + + app.set_rclone_path(rclone_path2.clone()); + assert_eq!(app.get_rclone_path(), &rclone_path2); + } + + #[test] + fn test_version_method_with_nonexistent_rclone() { + let temp_dir = setup_test_dir(); + let rclone_path = if cfg!(windows) { + temp_dir.path().join("rclone.exe") + } else { + temp_dir.path().join("rclone") + }; + + let app = RcloneApp::new_with_path(rclone_path, false); + + let result = app.version(); + assert!(result.is_err()); + } + + #[test] + fn test_version_method_with_mock_rclone() { + let temp_dir = setup_test_dir(); + let rclone_path = if cfg!(windows) { + temp_dir.path().join("rclone.exe") + } else { + temp_dir.path().join("rclone") + }; + + #[cfg(not(windows))] + { + use std::os::unix::fs::PermissionsExt; + fs::write(&rclone_path, "#!/bin/sh\necho 'rclone v1.60.0'").unwrap(); + let mut perms = fs::metadata(&rclone_path).unwrap().permissions(); + perms.set_mode(0o755); + fs::set_permissions(&rclone_path, perms).unwrap(); + } + + #[cfg(windows)] + { + fs::write(&rclone_path, "").unwrap(); + } + + let app = RcloneApp::new_with_path(rclone_path, false); + + if cfg!(not(windows)) { + let result = app.version(); + println!("Version result: {:?}", result); + } + } + + #[test] + fn test_using_system_rclone_flag() { + let temp_dir = setup_test_dir(); + let rclone_path = temp_dir.path().join("rclone"); + + let app = RcloneApp::new_with_path(rclone_path.clone(), true); + assert!(app.is_using_system_rclone()); + + let app2 = RcloneApp::new_with_path(rclone_path, false); + assert!(!app2.is_using_system_rclone()); + } + + #[test] + fn test_multiple_instances() { + let temp_dir = setup_test_dir(); + + let app1 = RcloneApp::new_with_path(temp_dir.path().join("rclone1"), true); + let app2 = RcloneApp::new_with_path(temp_dir.path().join("rclone2"), false); + let app3 = RcloneApp::new_with_path(temp_dir.path().join("rclone3"), true); + + assert!(app1.is_using_system_rclone()); + assert!(!app2.is_using_system_rclone()); + assert!(app3.is_using_system_rclone()); + + assert_ne!(app1.get_rclone_path(), app2.get_rclone_path()); + } + + #[tokio::test] + #[serial] + async fn test_check_system_rclone() { + let result = RcloneApp::check_system_rclone(); + println!("System rclone found: {:?}", result); + } + + #[test] + fn test_path_manipulation() { + let temp_dir = setup_test_dir(); + let original_path = temp_dir.path().join("original_rclone"); + let new_path = temp_dir.path().join("new_rclone"); + + let mut app = RcloneApp::new_with_path(original_path.clone(), false); + assert_eq!(app.get_rclone_path(), &original_path); + + app.set_rclone_path(new_path.clone()); + assert_eq!(app.get_rclone_path(), &new_path); + } + + #[test] + fn test_os_arch_detection() { + let os = if cfg!(target_os = "linux") { + "linux" + } else if cfg!(target_os = "macos") { + "osx" + } else if cfg!(target_os = "windows") { + "windows" + } else { + "unknown" + }; + + let arch = if cfg!(target_arch = "x86_64") { + "amd64" + } else if cfg!(target_arch = "aarch64") { + "arm64" + } else if cfg!(target_arch = "arm") { + "arm" + } else { + "unknown" + }; + + assert_ne!(os, "unknown", "ОС должна быть определена"); + assert_ne!(arch, "unknown", "Архитектура должна быть определена"); + + println!("OS: {}, ARCH: {}", os, arch); + } + + #[tokio::test] + #[ignore] + async fn test_actual_rclone_installation() { + match RcloneApp::new().await { + Ok(app) => { + assert!(app.get_rclone_path().exists()); + let version = app.version(); + assert!(version.is_ok()); + println!("Установлен rclone по пути: {:?}", app.get_rclone_path()); + println!("Версия: {:?}", version); + println!("Используется системный rclone: {}", app.is_using_system_rclone()); + } + Err(e) => { + panic!("Не удалось установить rclone: {}", e); + } + } + } +}