From 5ffbb51b39463d3fab09767c961740f34f77d5a0 Mon Sep 17 00:00:00 2001 From: Mario Date: Fri, 24 May 2024 15:59:46 +0200 Subject: [PATCH] Removed old tokio stream dl --- Cargo.lock | 297 +---------------------------------- Cargo.toml | 13 +- README.md | 2 +- src/config.rs | 1 + src/dependency_downloader.rs | 77 +++++---- src/lib.rs | 14 +- src/lock.rs | 107 +++++-------- 7 files changed, 109 insertions(+), 402 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 01540ce..ab0870d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -134,12 +134,6 @@ dependencies = [ "syn", ] -[[package]] -name = "atomic-waker" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" - [[package]] name = "autocfg" version = "1.3.0" @@ -173,12 +167,6 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - [[package]] name = "bitflags" version = "2.5.0" @@ -337,16 +325,6 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" -[[package]] -name = "core-foundation" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" -dependencies = [ - "core-foundation-sys", - "libc", -] - [[package]] name = "core-foundation-sys" version = "0.8.6" @@ -420,37 +398,12 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "encoding_rs" -version = "0.8.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59" -dependencies = [ - "cfg-if", -] - [[package]] name = "equivalent" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" -[[package]] -name = "errno" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" -dependencies = [ - "libc", - "windows-sys 0.52.0", -] - -[[package]] -name = "fastrand" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" - [[package]] name = "flate2" version = "1.0.30" @@ -467,21 +420,6 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" -[[package]] -name = "foreign-types" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" -dependencies = [ - "foreign-types-shared", -] - -[[package]] -name = "foreign-types-shared" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" - [[package]] name = "form_urlencoded" version = "1.2.1" @@ -607,25 +545,6 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" -[[package]] -name = "h2" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa82e28a107a8cc405f0839610bdc9b15f1e25ec7d696aa5cf173edbcb1486ab" -dependencies = [ - "atomic-waker", - "bytes", - "fnv", - "futures-core", - "futures-sink", - "http", - "indexmap", - "slab", - "tokio", - "tokio-util", - "tracing", -] - [[package]] name = "hashbrown" version = "0.14.5" @@ -708,7 +627,6 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "h2", "http", "http-body", "httparse", @@ -736,22 +654,6 @@ dependencies = [ "tower-service", ] -[[package]] -name = "hyper-tls" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" -dependencies = [ - "bytes", - "http-body-util", - "hyper", - "hyper-util", - "native-tls", - "tokio", - "tokio-native-tls", - "tower-service", -] - [[package]] name = "hyper-util" version = "0.1.3" @@ -872,12 +774,6 @@ version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" -[[package]] -name = "linux-raw-sys" -version = "0.4.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" - [[package]] name = "lock_api" version = "0.4.12" @@ -936,24 +832,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "native-tls" -version = "0.2.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e" -dependencies = [ - "lazy_static", - "libc", - "log", - "openssl", - "openssl-probe", - "openssl-sys", - "schannel", - "security-framework", - "security-framework-sys", - "tempfile", -] - [[package]] name = "num-conv" version = "0.1.0" @@ -994,50 +872,6 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" -[[package]] -name = "openssl" -version = "0.10.64" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95a0481286a310808298130d22dd1fef0fa571e05a8f44ec801801e84b216b1f" -dependencies = [ - "bitflags 2.5.0", - "cfg-if", - "foreign-types", - "libc", - "once_cell", - "openssl-macros", - "openssl-sys", -] - -[[package]] -name = "openssl-macros" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "openssl-probe" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" - -[[package]] -name = "openssl-sys" -version = "0.9.102" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c597637d56fbc83893a35eb0dd04b2b8e7a50c91e64e9493e398b5df4fb45fa2" -dependencies = [ - "cc", - "libc", - "pkg-config", - "vcpkg", -] - [[package]] name = "parking_lot" version = "0.12.2" @@ -1218,7 +1052,7 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "469052894dcb553421e483e4209ee581a45100d31b4018de03e5a7ad86374a7e" dependencies = [ - "bitflags 2.5.0", + "bitflags", ] [[package]] @@ -1258,24 +1092,20 @@ checksum = "566cafdd92868e0939d3fb961bd0dc25fcfaaed179291093b3d43e6b3150ea10" dependencies = [ "base64", "bytes", - "encoding_rs", "futures-channel", "futures-core", "futures-util", - "h2", "http", "http-body", "http-body-util", "hyper", "hyper-rustls", - "hyper-tls", "hyper-util", "ipnet", "js-sys", "log", "mime", "mime_guess", - "native-tls", "once_cell", "percent-encoding", "pin-project-lite", @@ -1286,9 +1116,7 @@ dependencies = [ "serde_json", "serde_urlencoded", "sync_wrapper", - "system-configuration", "tokio", - "tokio-native-tls", "tokio-rustls", "tokio-util", "tower-service", @@ -1343,19 +1171,6 @@ version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" -[[package]] -name = "rustix" -version = "0.38.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" -dependencies = [ - "bitflags 2.5.0", - "errno", - "libc", - "linux-raw-sys", - "windows-sys 0.52.0", -] - [[package]] name = "rustls" version = "0.22.4" @@ -1421,15 +1236,6 @@ dependencies = [ "sdd", ] -[[package]] -name = "schannel" -version = "0.1.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534" -dependencies = [ - "windows-sys 0.52.0", -] - [[package]] name = "scopeguard" version = "1.2.0" @@ -1442,29 +1248,6 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b84345e4c9bd703274a082fb80caaa99b7612be48dfaa1dd9266577ec412309d" -[[package]] -name = "security-framework" -version = "2.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c627723fd09706bacdb5cf41499e95098555af3c3c29d014dc3c458ef6be11c0" -dependencies = [ - "bitflags 2.5.0", - "core-foundation", - "core-foundation-sys", - "libc", - "security-framework-sys", -] - -[[package]] -name = "security-framework-sys" -version = "2.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "317936bbbd05227752583946b9e66d7ce3b489f84e11a94a510b4437fef407d7" -dependencies = [ - "core-foundation-sys", - "libc", -] - [[package]] name = "serde" version = "1.0.202" @@ -1577,15 +1360,6 @@ dependencies = [ "tokio", ] -[[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 = "simple-home-dir" version = "0.3.4" @@ -1622,11 +1396,12 @@ dependencies = [ [[package]] name = "soldeer" -version = "0.2.12" +version = "0.2.13" dependencies = [ "chrono", "clap", "email-address-parser", + "futures", "lazy_static", "regex", "reqwest", @@ -1638,7 +1413,7 @@ dependencies = [ "sha256", "simple-home-dir", "tokio", - "tokio-dl-stream-to-disk", + "tokio-stream", "tokio-test", "toml", "toml_edit", @@ -1684,39 +1459,6 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" -[[package]] -name = "system-configuration" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7" -dependencies = [ - "bitflags 1.3.2", - "core-foundation", - "system-configuration-sys", -] - -[[package]] -name = "system-configuration-sys" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a75fb188eb626b924683e3b95e3a48e63551fcfb51949de2f06a9d91dbee93c9" -dependencies = [ - "core-foundation-sys", - "libc", -] - -[[package]] -name = "tempfile" -version = "3.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" -dependencies = [ - "cfg-if", - "fastrand", - "rustix", - "windows-sys 0.52.0", -] - [[package]] name = "thiserror" version = "1.0.61" @@ -1782,27 +1524,12 @@ dependencies = [ "libc", "mio", "num_cpus", - "parking_lot", "pin-project-lite", - "signal-hook-registry", "socket2", "tokio-macros", "windows-sys 0.48.0", ] -[[package]] -name = "tokio-dl-stream-to-disk" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1f364cd47675674b7d9dead65b7ef11e46bc918f1b058f37195fc81dfb0ef62" -dependencies = [ - "bytes", - "futures-util", - "reqwest", - "tokio", - "tokio-util", -] - [[package]] name = "tokio-macros" version = "2.2.0" @@ -1814,16 +1541,6 @@ dependencies = [ "syn", ] -[[package]] -name = "tokio-native-tls" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" -dependencies = [ - "native-tls", - "tokio", -] - [[package]] name = "tokio-rustls" version = "0.25.0" @@ -2035,12 +1752,6 @@ dependencies = [ "serde", ] -[[package]] -name = "vcpkg" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" - [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index ed1d130..33dda67 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,15 +10,16 @@ license = "MIT" name = "soldeer" readme = "./README.md" repository = "https://github.com/mario-eth/soldeer" -version = "0.2.12" +version = "0.2.13" [dependencies] chrono = {version = "0.4.37", features = ["serde"]} clap = {version = "4.5.4", features = ["derive"]} email-address-parser = "2.0.0" +futures = "0.3.30" lazy_static = "1.4.0" regex = "1.10.4" -reqwest = {version = "0.12.3", features = ["blocking", "json", "multipart"], default-features = false} +reqwest = {version = "0.12.4", features = ["blocking", "json", "multipart", "stream"], default-features = false} rpassword = "7.3.1" serde = "1.0.197" serde_derive = "1.0.197" @@ -26,10 +27,10 @@ serde_json = "1.0.115" serial_test = "3.0.0" sha256 = "1.5.0" simple-home-dir = "0.3.2" -tokio = "1.37.0" -tokio-dl-stream-to-disk = "1.0.1" -toml = "0.8.12" -toml_edit = "0.22.9" +tokio = {version = "1.37.0", features = ["rt", "macros"]} +tokio-stream = "0.1.15" +toml = "0.8.13" +toml_edit = "0.22.13" uuid = {version = "1.8.0", features = ["serde", "v4"]} walkdir = "2.5.0" yansi = "1.0.1" diff --git a/README.md b/README.md index 1f418e0..b72d9f1 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ This project was started to solve the following issues: - npmjs was built for the js ecosystem not for solidity - github versioning of the releases is a pain and not all the projects are using it correctly -## Version 0.2.12 +## Version 0.2.13 ### Version 0.2.7 introduces the following breaking changes diff --git a/src/config.rs b/src/config.rs index d0a5286..f3eee32 100644 --- a/src/config.rs +++ b/src/config.rs @@ -73,6 +73,7 @@ pub async fn read_config(filename: String) -> Result, ConfigErro let mut dependencies: Vec = Vec::new(); let iterator = data.dependencies.iter(); for (name, v) in iterator { + #[allow(clippy::needless_late_init)] let url; let version; diff --git a/src/dependency_downloader.rs b/src/dependency_downloader.rs index 04aa5b5..946d437 100644 --- a/src/dependency_downloader.rs +++ b/src/dependency_downloader.rs @@ -1,7 +1,11 @@ +use futures::StreamExt; use std::fs; use std::io::Cursor; use std::path::PathBuf; -use tokio_dl_stream_to_disk::AsyncDownload; +use tokio::{ + fs::File, + io::AsyncWriteExt, +}; use yansi::Paint; use crate::config::Dependency; @@ -78,36 +82,49 @@ pub async fn download_dependency( fs::create_dir(&dependency_directory).unwrap(); } - let download_result: Result<(), tokio_dl_stream_to_disk::error::Error> = - AsyncDownload::new(dependency_url, &dependency_directory, dependency_name) - .download(&None) - .await; - if download_result.is_ok() { - println!( - "{}", - Paint::green(&format!("Dependency {} downloaded! ", dependency_name)) - ); - Ok(()) - } else if download_result - .err() - .unwrap() - .to_string() - .contains("already exists") - { - println!( - "{}", - Paint::yellow(&format!( - "Dependency {} already downloaded", - dependency_name - )) - ); - return Ok(()); - } else { - return Err(DownloadError { - name: "Unknown".to_string(), - version: "Unknown".to_string(), - }); + let mut file = File::create(&dependency_directory.join(dependency_name)) + .await + .unwrap(); + + let mut stream = match reqwest::get(dependency_url).await { + Ok(res) => res.bytes_stream(), + Err(_) => { + return Err(DownloadError { + name: "Unknown".to_string(), + version: "Unknown".to_string(), + }); + } + }; + + while let Some(chunk_result) = stream.next().await { + let chunk = chunk_result; + match file.write_all(&chunk.unwrap()).await { + Ok(_) => {} + Err(_) => { + return Err(DownloadError { + name: "Unknown".to_string(), + version: "Unknown".to_string(), + }); + } + } } + + match file.flush().await { + Ok(_) => {} + Err(_) => { + return Err(DownloadError { + name: "Unknown".to_string(), + version: "Unknown".to_string(), + }); + } + }; + + println!( + "{}", + Paint::green(&format!("Dependency {} downloaded! ", dependency_name)) + ); + + Ok(()) } pub fn unzip_dependency( diff --git a/src/lib.rs b/src/lib.rs index 65aaab7..9ac2299 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,13 +75,14 @@ pub async fn run(command: Subcommands) -> Result<(), SoldeerError> { let remote_url = install.remote_url.unwrap(); let mut dependencies: Vec = Vec::new(); dependency_url = remote_url.clone(); - dependencies.push(Dependency { + let dependency = Dependency { name: dependency_name.clone(), version: dependency_version.clone(), url: dependency_url.clone(), - }); + }; + dependencies.push(dependency.clone()); - match lock_check(&dependencies) { + match lock_check(&dependency) { Ok(dep) => dependencies = dep, Err(err) => { return Err(SoldeerError { message: err.cause }); @@ -108,12 +109,13 @@ pub async fn run(command: Subcommands) -> Result<(), SoldeerError> { } } } else { - let mut dependencies: Vec = vec![Dependency { + let dependency = Dependency { name: dependency_name.clone(), version: dependency_version.clone(), url: String::new(), - }]; - match lock_check(&dependencies) { + }; + let mut dependencies: Vec; + match lock_check(&dependency) { Ok(dep) => dependencies = dep, Err(err) => { return Err(SoldeerError { message: err.cause }); diff --git a/src/lock.rs b/src/lock.rs index ea115e6..515bbd4 100644 --- a/src/lock.rs +++ b/src/lock.rs @@ -70,34 +70,31 @@ fn read_lock() -> Result, LockError> { Ok(data.dependencies) } -pub fn lock_check(dependencies: &[Dependency]) -> Result, LockError> { +pub fn lock_check(dependency: &Dependency) -> Result, LockError> { let lock_entries = match read_lock() { Ok(entries) => entries, - Err(err) => { - if err.cause != *"Lock does not exists" { - return Err(err); - } - vec![] + Err(_) => { + return Err(LockError { + cause: "Lock does not exists".to_string(), + }); } }; let mut unlock_dependencies: Vec = Vec::new(); let mut is_locked: bool = false; let mut string_err = String::new(); - dependencies.iter().for_each(|dependency| { - lock_entries.iter().for_each(|lock_entry| { - if lock_entry.name == dependency.name && lock_entry.version == dependency.version { - string_err = format!( - "Dependency {}-{} is already installed", - lock_entry.name, lock_entry.version - ); - is_locked = true; - } - }); - - unlock_dependencies.push(dependency.clone()); + lock_entries.iter().for_each(|lock_entry| { + if lock_entry.name == dependency.name && lock_entry.version == dependency.version { + string_err = format!( + "Dependency {}-{} is already installed", + lock_entry.name, lock_entry.version + ); + is_locked = true; + } }); + unlock_dependencies.push(dependency.clone()); + if is_locked { return Err(LockError { cause: string_err }); } @@ -264,13 +261,13 @@ checksum = "5019418b1e9128185398870f77a42e51d624c44315bb1572e7545be51d707016" #[serial] fn lock_file_not_present_test() { let lock_file = check_lock_file(); - let mut dependencies: Vec = Vec::new(); - dependencies.push(Dependency { + let dependency = Dependency { name: "@openzeppelin-contracts".to_string(), version: "2.3.0".to_string(), url: "https://github.com/mario-eth/soldeer-versions/raw/main/all_versions/@openzeppelin-contracts~2.3.0.zip".to_string(), - }); - let _result: Vec = lock_check(&dependencies).unwrap(); + }; + + assert!(lock_check(&dependency).is_err_and(|e| { e.cause == "Lock does not exists" })); assert!(!lock_file.exists()); } @@ -278,57 +275,31 @@ checksum = "5019418b1e9128185398870f77a42e51d624c44315bb1572e7545be51d707016" #[serial] fn check_lock_all_locked_test() { initialize(); - let mut dependencies: Vec = Vec::new(); - dependencies.push(Dependency { + let dependency = Dependency { name: "@openzeppelin-contracts".to_string(), version: "2.3.0".to_string(), url: "https://github.com/mario-eth/soldeer-versions/raw/main/all_versions/@openzeppelin-contracts~2.3.0.zip".to_string(), - }); - let result: Vec = lock_check(&dependencies).unwrap(); - assert_eq!(result.len(), 0); - } + }; - #[test] - #[serial] - fn check_lock_not_all_locked_test() { - initialize(); - let lock_file = get_current_working_dir() - .unwrap() - .join("test") - .join("soldeer.lock"); - let mut dependencies: Vec = Vec::new(); - dependencies.push(Dependency { - name: "@openzeppelin-contracts".to_string(), - version: "2.3.0".to_string(), - url: "https://github.com/mario-eth/soldeer-versions/raw/main/all_versions/@openzeppelin-contracts~2.3.0.zip".to_string(), - }); - dependencies.push(Dependency { - name: "@openzeppelin-contracts".to_string(), - version: "2.4.0".to_string(), - url: "https://github.com/mario-eth/soldeer-versions/raw/main/all_versions/@openzeppelin-contracts~2.4.0.zip".to_string(), - }); - let result: Vec = lock_check(&dependencies).unwrap(); - assert_eq!(result.len(), 1); - assert_eq!(result[0].name, "@openzeppelin-contracts"); - assert_eq!(result[0].version, "2.4.0"); - fs::remove_file(lock_file).unwrap(); + assert!(lock_check(&dependency).is_err_and(|e| { + e.cause == "Dependency @openzeppelin-contracts-2.3.0 is already installed" + })); } #[test] #[serial] fn write_clean_lock_test() { let lock_file = check_lock_file(); - let mut dependencies: Vec = Vec::new(); - dependencies.push(Dependency { + let dependency = Dependency { name: "@openzeppelin-contracts".to_string(), version: "2.5.0".to_string(), url: "https://github.com/mario-eth/soldeer-versions/raw/main/all_versions/@openzeppelin-contracts~2.5.0.zip".to_string(), - }); - let mut result: Vec = lock_check(&dependencies).unwrap(); - assert_eq!(result.len(), 1); - assert_eq!(result[0].name, "@openzeppelin-contracts"); - assert_eq!(result[0].version, "2.5.0"); - write_lock(&result, false).unwrap(); + }; + let dependencies = vec![dependency.clone()]; + write_lock(&dependencies, false).unwrap(); + assert!(lock_check(&dependency).is_err_and(|e| { + e.cause == "Dependency @openzeppelin-contracts-2.5.0 is already installed" + })); let contents = read_file_to_string(&lock_file.to_str().unwrap().to_string()); assert_eq!( @@ -341,8 +312,9 @@ source = "https://github.com/mario-eth/soldeer-versions/raw/main/all_versions/@o checksum = "5019418b1e9128185398870f77a42e51d624c44315bb1572e7545be51d707016" "# ); - result = lock_check(&dependencies).unwrap(); - assert_eq!(result.len(), 0); + assert!(lock_check(&dependency).is_err_and(|e| { + e.cause == "Dependency @openzeppelin-contracts-2.5.0 is already installed" + })); } #[test] @@ -351,11 +323,12 @@ checksum = "5019418b1e9128185398870f77a42e51d624c44315bb1572e7545be51d707016" let lock_file = check_lock_file(); initialize(); let mut dependencies: Vec = Vec::new(); - dependencies.push(Dependency { + let dependency = Dependency { name: "@openzeppelin-contracts-2".to_string(), version: "2.6.0".to_string(), url: "https://github.com/mario-eth/soldeer-versions/raw/main/all_versions/@openzeppelin-contracts~2.6.0.zip".to_string(), - }); + }; + dependencies.push(dependency.clone()); write_lock(&dependencies, false).unwrap(); let contents = read_file_to_string(&lock_file.to_str().unwrap().to_string()); @@ -381,7 +354,9 @@ source = "https://github.com/mario-eth/soldeer-versions/raw/main/all_versions/@o checksum = "5019418b1e9128185398870f77a42e51d624c44315bb1572e7545be51d707016" "# ); - let result = lock_check(&dependencies).unwrap(); - assert_eq!(result.len(), 0); + + assert!(lock_check(&dependency).is_err_and(|e| { + e.cause == "Dependency @openzeppelin-contracts-2-2.6.0 is already installed" + })); } }