Skip to content

Commit

Permalink
Fixes 83 (#85)
Browse files Browse the repository at this point in the history

* Fixed broken zipping when pushing new dependencies with custom path
  • Loading branch information
mario-eth authored Jul 3, 2024
1 parent b3b827f commit 2de3b44
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ src/soldeer.toml
test/*
!emptyfile
!emptyfile2
test_push_sensitive
test_push_sensitive
test_push_skip_sensitive
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = "MIT"
name = "soldeer"
readme = "./README.md"
repository = "https://github.com/mario-eth/soldeer"
version = "0.2.16"
version = "0.2.17"

[dependencies]
chrono = {version = "0.4.37", default-features = false, features = [
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.16
## Version 0.2.17

### Version 0.2.7 introduces the following breaking changes

Expand Down
10 changes: 4 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ mod tests {
};
use std::fs::{
create_dir_all,
remove_dir,
remove_dir_all,
remove_file,
File,
Expand Down Expand Up @@ -650,7 +651,7 @@ libs = ["dependencies"]
if target_file.exists() {
let _ = remove_file(target_file);
}
let mut file: std::fs::File = fs::OpenOptions::new()
let mut file: File = fs::OpenOptions::new()
.create_new(true)
.write(true)
.open(target_file)
Expand Down Expand Up @@ -682,11 +683,8 @@ libs = ["dependencies"]
let _ = remove_dir_all(DEPENDENCY_DIR.clone());
let _ = remove_file(LOCK_FILE.clone());
let test_dir = env::current_dir().unwrap().join("test_push_sensitive");

// Create test directory
if !test_dir.exists() {
std::fs::create_dir(&test_dir).unwrap();
}
let _ = remove_dir(&test_dir);
let _ = create_dir_all(&test_dir);

// Create a .env file in the test directory
let env_file_path = test_dir.join(".env");
Expand Down
83 changes: 82 additions & 1 deletion src/versioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ fn zip_file(
files_to_copy: &Vec<FilePair>,
file_name: &String,
) -> Result<PathBuf, PushError> {
let root_dir_as_string = root_directory_path.to_str().unwrap();
let zip_file_path = root_directory_path.join(file_name.to_owned() + ".zip");
let file = File::create(zip_file_path.to_str().unwrap()).unwrap();
let mut zip = ZipWriter::new(file);
Expand All @@ -129,10 +130,14 @@ fn zip_file(
let path = Path::new(&file_path.path);
let mut buffer = Vec::new();

// This is the relative path, we basically get the relative path to the target folder that we want to push
// and zip that as a name so we won't screw up the file/dir hierarchy in the zip file.
let relative_file_path = file_path.path.to_string().replace(root_dir_as_string, "");

// Write file or directory explicitly
// Some unzip tools unzip files with directory paths correctly, some do not!
if path.is_file() {
match zip.start_file(file_path.path.as_str(), options) {
match zip.start_file(relative_file_path, options) {
Ok(_) => {}
Err(err) => {
return Err(PushError {
Expand Down Expand Up @@ -363,6 +368,7 @@ mod tests {
remove_file,
};

use io::Cursor;
use serial_test::serial;

use super::*;
Expand Down Expand Up @@ -569,6 +575,81 @@ mod tests {
let _ = remove_dir_all(target_dir);
}

#[test]
#[serial]
fn zipping_file_structure_check() {
let target_dir = get_current_working_dir().join("test").join("test_zip");
let target_dir_unzip = get_current_working_dir().join("test").join("test_unzip");
let _ = remove_dir_all(&target_dir);
let _ = remove_dir_all(&target_dir_unzip);
let _ = create_dir_all(&target_dir);
let _ = create_dir_all(&target_dir_unzip);

// File structure that should be preserved
// - target_dir/
// --- random_dir_1/
// --- --- random_dir_2/
// --- --- --- random_file_3.txt
// --- --- random_file_2.txt
// --- random_file_1.txt
let random_dir_1 = create_random_directory(&target_dir, "".to_string());
let random_dir_2 = create_random_directory(Path::new(&random_dir_1), "".to_string());
let random_file_1 = create_random_file(&target_dir, ".txt".to_string());
let random_file_2 = create_random_file(Path::new(&random_dir_1), ".txt".to_string());
let random_file_3 = create_random_file(Path::new(&random_dir_2), ".txt".to_string());

let dep_name = "test_dep".to_string();
let dep_version = "1.1".to_string();
let files_to_copy: Vec<FilePair> = vec![
FilePair {
name: "random_file_1".to_string(),
path: random_file_1.clone(),
},
FilePair {
name: "random_file_1".to_string(),
path: random_file_3.clone(),
},
FilePair {
name: "random_file_1".to_string(),
path: random_file_2.clone(),
},
];
let result = match zip_file(
&dep_name,
&dep_version,
&target_dir,
&files_to_copy,
&"test_zip".to_string(),
) {
Ok(r) => r,
Err(_) => {
assert_eq!("Invalid State", "");
return;
}
};

// unzipping for checks
let archive = read_file(result).unwrap();
match zip_extract::extract(Cursor::new(archive), &target_dir_unzip, true) {
Ok(_) => {}
Err(_) => {
assert_eq!("Invalid State", "");
}
}

let random_file_1_unzipped = random_file_1.replace("test_zip", "test_unzip");
let random_file_2_unzipped = random_file_2.replace("test_zip", "test_unzip");
let random_file_3_unzipped = random_file_3.replace("test_zip", "test_unzip");

assert!(Path::new(&random_file_1_unzipped).exists());
assert!(Path::new(&random_file_2_unzipped).exists());
assert!(Path::new(&random_file_3_unzipped).exists());

//cleaning up
let _ = remove_dir_all(&target_dir);
let _ = remove_dir_all(&target_dir_unzip);
}

fn define_ignore_file(git: bool) -> PathBuf {
let mut target = ".soldeerignore";
if git {
Expand Down

0 comments on commit 2de3b44

Please sign in to comment.