Skip to content

Commit

Permalink
Merge pull request #213 from pbs-data-solutions/tmp-path
Browse files Browse the repository at this point in the history
Use tmp-path crate in testing
  • Loading branch information
sanders41 authored Jan 4, 2025
2 parents 7939fff + 320c569 commit 175c0db
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 51 deletions.
48 changes: 39 additions & 9 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ sha2 = "0.10.8"

[dev-dependencies]
tempfile = "3.14.0"
tmp-path = "0.1.0"
71 changes: 29 additions & 42 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,22 +199,21 @@ fn main() {
mod tests {
use super::*;
use tempfile::tempdir;
use tmp_path::tmp_path;

#[tmp_path]
fn fake_file_path() -> PathBuf {
let base = tempdir().unwrap().path().to_path_buf();
fs::create_dir_all(&base).unwrap();
let file_path = base.join("test.txt");
let file_path = tmp_path.join("test.txt");

let mut file = File::create(&file_path).unwrap();
file.write_all(b"This is a test").unwrap();

file_path
}

#[tmp_path]
fn fake_file_path_2() -> PathBuf {
let base = tempdir().unwrap().path().to_path_buf();
fs::create_dir_all(&base).unwrap();
let file_path = base.join("test2.txt");
let file_path = tmp_path.join("test2.txt");

let mut file = File::create(&file_path).unwrap();
file.write_all(b"This is another test").unwrap();
Expand Down Expand Up @@ -269,11 +268,10 @@ mod tests {
}

#[test]
#[tmp_path]
fn generate_md5_file() {
let test_file = fake_file_path();
let base = tempdir().unwrap().path().to_path_buf();
fs::create_dir_all(&base).unwrap();
let output_file = base.join("output.txt");
let output_file = tmp_path.join("output.txt");

process_checksum(
&test_file,
Expand All @@ -293,12 +291,11 @@ mod tests {
}

#[test]
#[tmp_path]
fn generate_md5_file_directory_overwrite() {
let test_file_1 = fake_file_path();
let test_file_2 = fake_file_path_2();
let base = tempdir().unwrap().path().to_path_buf();
fs::create_dir_all(&base).unwrap();
let output_file = base.join("output.txt");
let output_file = tmp_path.join("output.txt");

process_checksum(
&test_file_1,
Expand Down Expand Up @@ -335,12 +332,11 @@ mod tests {
}

#[test]
#[tmp_path]
fn generate_md5_file_directory_no_overwrite() {
let test_file_1 = fake_file_path();
let test_file_2 = fake_file_path_2();
let base = tempdir().unwrap().path().to_path_buf();
fs::create_dir_all(&base).unwrap();
let output_file = base.join("output.txt");
let output_file = tmp_path.join("output.txt");

process_checksum(
&test_file_1,
Expand Down Expand Up @@ -371,11 +367,10 @@ mod tests {
}

#[test]
#[tmp_path]
fn generate_sha256_file() {
let test_file = fake_file_path();
let base = tempdir().unwrap().path().to_path_buf();
fs::create_dir_all(&base).unwrap();
let output_file = base.join("output.txt");
let output_file = tmp_path.join("output.txt");

process_checksum(
&test_file,
Expand All @@ -395,12 +390,11 @@ mod tests {
}

#[test]
#[tmp_path]
fn generate_sha256_file_directory_overwrite() {
let test_file_1 = fake_file_path();
let test_file_2 = fake_file_path_2();
let base = tempdir().unwrap().path().to_path_buf();
fs::create_dir_all(&base).unwrap();
let output_file = base.join("output.txt");
let output_file = tmp_path.join("output.txt");

process_checksum(
&test_file_1,
Expand Down Expand Up @@ -437,12 +431,11 @@ mod tests {
}

#[test]
#[tmp_path]
fn generate_sha256_file_directory_no_overwrite() {
let test_file_1 = fake_file_path();
let test_file_2 = fake_file_path_2();
let base = tempdir().unwrap().path().to_path_buf();
fs::create_dir_all(&base).unwrap();
let output_file = base.join("output.txt");
let output_file = tmp_path.join("output.txt");

process_checksum(
&test_file_1,
Expand Down Expand Up @@ -473,11 +466,10 @@ mod tests {
}

#[test]
#[tmp_path]
fn generate_sha512() {
let test_file = fake_file_path();
let base = tempdir().unwrap().path().to_path_buf();
fs::create_dir_all(&base).unwrap();
let output_file = base.join("output.txt");
let output_file = tmp_path.join("output.txt");

process_checksum(
&test_file,
Expand All @@ -497,12 +489,11 @@ mod tests {
}

#[test]
#[tmp_path]
fn generate_sha512_file_directory_overwrite() {
let test_file_1 = fake_file_path();
let test_file_2 = fake_file_path_2();
let base = tempdir().unwrap().path().to_path_buf();
fs::create_dir_all(&base).unwrap();
let output_file = base.join("output.txt");
let output_file = tmp_path.join("output.txt");

process_checksum(
&test_file_1,
Expand Down Expand Up @@ -539,12 +530,11 @@ mod tests {
}

#[test]
#[tmp_path]
fn generate_sha512_file_directory_no_overwrite() {
let test_file_1 = fake_file_path();
let test_file_2 = fake_file_path_2();
let base = tempdir().unwrap().path().to_path_buf();
fs::create_dir_all(&base).unwrap();
let output_file = base.join("output.txt");
let output_file = tmp_path.join("output.txt");

process_checksum(
&test_file_1,
Expand Down Expand Up @@ -575,11 +565,10 @@ mod tests {
}

#[test]
#[tmp_path]
fn generate_sha1_file() {
let test_file = fake_file_path();
let base = tempdir().unwrap().path().to_path_buf();
fs::create_dir_all(&base).unwrap();
let output_file = base.join("output.txt");
let output_file = tmp_path.join("output.txt");

process_checksum(
&test_file,
Expand All @@ -599,12 +588,11 @@ mod tests {
}

#[test]
#[tmp_path]
fn generate_sha1_file_directory_overwrite() {
let test_file_1 = fake_file_path();
let test_file_2 = fake_file_path_2();
let base = tempdir().unwrap().path().to_path_buf();
fs::create_dir_all(&base).unwrap();
let output_file = base.join("output.txt");
let output_file = tmp_path.join("output.txt");

process_checksum(
&test_file_1,
Expand Down Expand Up @@ -641,12 +629,11 @@ mod tests {
}

#[test]
#[tmp_path]
fn generate_sha1_file_directory_no_overwrite() {
let test_file_1 = fake_file_path();
let test_file_2 = fake_file_path_2();
let base = tempdir().unwrap().path().to_path_buf();
fs::create_dir_all(&base).unwrap();
let output_file = base.join("output.txt");
let output_file = tmp_path.join("output.txt");

process_checksum(
&test_file_1,
Expand Down

0 comments on commit 175c0db

Please sign in to comment.