Skip to content

Commit

Permalink
tests: add tests from pr 50
Browse files Browse the repository at this point in the history
  • Loading branch information
muja committed Aug 31, 2024
1 parent 254025c commit 69a659c
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions tests/simple.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::{fs, path::PathBuf};

#[test]
fn version_list() {
Expand Down Expand Up @@ -30,14 +30,60 @@ fn version_cat() {
fn extract_to_tempdir() {
// see https://github.com/muja/unrar.rs/issues/34
let file = "data/version.rar".to_owned();
let mut archive = unrar::Archive::new(&file).open_for_processing().expect("open archive");
let mut archive = unrar::Archive::new(&file)
.open_for_processing()
.expect("open archive");
let temp_path = tempfile::tempdir().expect("creating tempdir");
let temp_path = temp_path.path();
while let Some(header) = archive.read_header().expect("read header") {
let temp_file_path = temp_path.join(header.entry().filename.as_path());
archive = header.extract_to(temp_file_path.as_path()).expect("extract_to");
archive = header
.extract_to(temp_file_path.as_path())
.expect("extract_to");
}
let entries = std::fs::read_dir(&temp_path).expect("read tempdir").collect::<Result<Vec<_>, _>>().unwrap();
let entries = std::fs::read_dir(&temp_path)
.expect("read tempdir")
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert!(entries.len() == 1);
assert!(entries[0].file_name() == "VERSION");
}

#[test]
fn version_extract_to_absolute_path() {
let archive = unrar::Archive::new("data/version.rar")
.open_for_processing()
.unwrap();

let tmp_dir = tempfile::tempdir().unwrap();

let header = archive.read_header().unwrap().unwrap();
let filename = header.entry().filename.clone();
header.extract_to(tmp_dir.path().join(filename)).unwrap();

assert_eq!(
fs::read_to_string(tmp_dir.path().join("VERSION")).unwrap(),
"unrar-0.4.0"
);
}

#[test]
fn version_extract_to_relative_path() {
let archive = unrar::Archive::new("data/version.rar")
.open_for_processing()
.unwrap();

let tmp_dir = tempfile::tempdir().unwrap();

let header = archive.read_header().unwrap().unwrap();
let cwd = std::env::current_dir().unwrap();
std::env::set_current_dir(&tmp_dir).unwrap();
let e = header.extract_to("some-folder/VERSION");
std::env::set_current_dir(&cwd).unwrap();
assert!(e.is_ok());

assert_eq!(
fs::read_to_string(tmp_dir.path().join("some-folder/VERSION")).unwrap(),
"unrar-0.4.0"
);
}

0 comments on commit 69a659c

Please sign in to comment.