Skip to content

Commit

Permalink
revert(dependency_downloader): make clean_dependency_directory sync…
Browse files Browse the repository at this point in the history
… again
  • Loading branch information
beeb committed Aug 4, 2024
1 parent f12fe0e commit 4a588f8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
50 changes: 25 additions & 25 deletions src/dependency_downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub async fn download_dependencies(
// clean dependencies folder if flag is true
if clean {
// creates the directory
clean_dependency_directory().await;
clean_dependency_directory();
}

// create the dependency directory if it doesn't exist
Expand Down Expand Up @@ -120,10 +120,10 @@ pub fn unzip_dependency(dependency: &HttpDependency) -> Result<()> {
Ok(())
}

pub async fn clean_dependency_directory() {
if tokio_fs::metadata(DEPENDENCY_DIR.clone()).await.is_ok() {
tokio_fs::remove_dir_all(DEPENDENCY_DIR.clone()).await.unwrap();
tokio_fs::create_dir(DEPENDENCY_DIR.clone()).await.unwrap();
pub fn clean_dependency_directory() {
if fs::metadata(DEPENDENCY_DIR.clone()).is_ok() {
fs::remove_dir_all(DEPENDENCY_DIR.clone()).unwrap();
fs::create_dir(DEPENDENCY_DIR.clone()).unwrap();
}
}

Expand Down Expand Up @@ -293,13 +293,13 @@ mod tests {
assert!(path_zip.exists());
assert!(results.len() == 1);
assert!(!results[0].hash.is_empty());
clean_dependency_directory().await;
clean_dependency_directory();
}

#[tokio::test]
#[serial]
async fn download_dependencies_git_one_success() {
clean_dependency_directory().await;
clean_dependency_directory();
let mut dependencies: Vec<Dependency> = Vec::new();
let dependency = Dependency::Git(GitDependency {
name: "@openzeppelin-contracts".to_string(),
Expand All @@ -315,13 +315,13 @@ mod tests {
assert!(path_dir.join("src").join("auth").join("Owned.sol").exists());
assert!(results.len() == 1);
assert!(!results[0].hash.is_empty());
clean_dependency_directory().await;
clean_dependency_directory();
}

#[tokio::test]
#[serial]
async fn download_dependencies_gitlab_giturl_one_success() {
clean_dependency_directory().await;
clean_dependency_directory();
let mut dependencies: Vec<Dependency> = Vec::new();
let dependency = Dependency::Git(GitDependency {
name: "@openzeppelin-contracts".to_string(),
Expand All @@ -337,13 +337,13 @@ mod tests {
assert!(path_dir.join("JustATest3.md").exists());
assert!(results.len() == 1);
assert_eq!(results[0].hash, "22868f426bd4dd0e682b5ec5f9bd55507664240c"); // this is the last commit, hash == commit
clean_dependency_directory().await;
clean_dependency_directory();
}

#[tokio::test]
#[serial]
async fn download_dependency_gitlab_giturl_with_a_specific_revision() {
clean_dependency_directory().await;
clean_dependency_directory();
let mut dependencies: Vec<Dependency> = Vec::new();
let dependency = Dependency::Git(GitDependency {
name: "@openzeppelin-contracts".to_string(),
Expand All @@ -366,13 +366,13 @@ mod tests {
.join("JustATest2.md");
assert!(test_right_revision.exists());

clean_dependency_directory().await;
clean_dependency_directory();
}

#[tokio::test]
#[serial]
async fn download_dependencies_gitlab_httpurl_one_success() {
clean_dependency_directory().await;
clean_dependency_directory();
let mut dependencies: Vec<Dependency> = Vec::new();
let dependency = Dependency::Git(GitDependency {
name: "@openzeppelin-contracts".to_string(),
Expand All @@ -388,7 +388,7 @@ mod tests {
assert!(path_dir.join("README.md").exists());
assert!(results.len() == 1);
assert_eq!(results[0].hash, "22868f426bd4dd0e682b5ec5f9bd55507664240c"); // this is the last commit, hash == commit
clean_dependency_directory().await;
clean_dependency_directory();
}

#[tokio::test]
Expand Down Expand Up @@ -428,7 +428,7 @@ mod tests {
assert!(results.len() == 2);
assert!(!results[0].hash.is_empty());
assert!(!results[1].hash.is_empty());
clean_dependency_directory().await;
clean_dependency_directory();
}

#[tokio::test]
Expand Down Expand Up @@ -480,7 +480,7 @@ mod tests {
assert!(results.len() == 2);
assert!(!results[0].hash.is_empty());
assert!(!results[1].hash.is_empty());
clean_dependency_directory().await;
clean_dependency_directory();
}

#[tokio::test]
Expand Down Expand Up @@ -519,7 +519,7 @@ mod tests {
assert!(size_of_two > size_of_one);
assert!(results.len() == 1);
assert!(!results[0].hash.is_empty());
clean_dependency_directory().await;
clean_dependency_directory();
}

#[tokio::test]
Expand Down Expand Up @@ -560,7 +560,7 @@ mod tests {
assert!(path_zip.exists());
assert!(results.len() == 1);
assert!(!results[0].hash.is_empty());
clean_dependency_directory().await;
clean_dependency_directory();
}

#[tokio::test]
Expand All @@ -584,7 +584,7 @@ mod tests {
assert_eq!(err.to_string(), "error downloading dependency: HTTP status client error (404 Not Found) for url (https://github.com/mario-eth/soldeer-versions/raw/main/all_versions/@openzeppelin-contracts~.zip)");
}
}
clean_dependency_directory().await;
clean_dependency_directory();
}

#[tokio::test]
Expand All @@ -610,7 +610,7 @@ mod tests {
assert!(err.to_string().contains("Cloning into"));
}
}
clean_dependency_directory().await;
clean_dependency_directory();
}

#[tokio::test]
Expand All @@ -632,11 +632,11 @@ mod tests {
assert!(metadata(&path).unwrap().len() > 0);
}
Err(_) => {
clean_dependency_directory().await;
clean_dependency_directory();
assert_eq!("Error", "");
}
}
clean_dependency_directory().await;
clean_dependency_directory();
}

#[tokio::test]
Expand All @@ -656,14 +656,14 @@ mod tests {
download_dependencies(&dependencies, false).await.unwrap();
match unzip_dependencies(&dependencies) {
Ok(_) => {
clean_dependency_directory().await;
clean_dependency_directory();
assert_eq!("Wrong State", "");
}
Err(err) => {
assert!(matches!(err, DownloadError::UnzipError(_)));
}
}
clean_dependency_directory().await;
clean_dependency_directory();
}

#[tokio::test]
Expand All @@ -685,7 +685,7 @@ mod tests {
.join("ERC20")
.join("ERC20.sol")
.exists());
clean_dependency_directory().await;
clean_dependency_directory();
}

#[test]
Expand Down
15 changes: 5 additions & 10 deletions src/janitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,16 @@ mod tests {
use super::*;
use crate::{
config::HttpDependency,
dependency_downloader::{download_dependencies, unzip_dependency},
dependency_downloader::{
clean_dependency_directory, download_dependencies, unzip_dependency,
},
};
use serial_test::serial;

fn clean_dependency_directory_sync() {
if DEPENDENCY_DIR.is_dir() {
fs::remove_dir_all(DEPENDENCY_DIR.clone()).unwrap();
fs::create_dir(DEPENDENCY_DIR.clone()).unwrap();
}
}

struct CleanupDependency;
impl Drop for CleanupDependency {
fn drop(&mut self) {
clean_dependency_directory_sync();
clean_dependency_directory();
}
}

Expand Down Expand Up @@ -141,7 +136,7 @@ mod tests {
let _ = unzip_dependency(dependencies[0].as_http().unwrap());
let result: Result<()> = cleanup_after(&dependencies);
assert!(result.is_ok());
clean_dependency_directory_sync();
clean_dependency_directory();
}

#[tokio::test]
Expand Down

0 comments on commit 4a588f8

Please sign in to comment.