Skip to content

Commit 175c0db

Browse files
authored
Merge pull request #213 from pbs-data-solutions/tmp-path
Use tmp-path crate in testing
2 parents 7939fff + 320c569 commit 175c0db

File tree

3 files changed

+69
-51
lines changed

3 files changed

+69
-51
lines changed

Cargo.lock

Lines changed: 39 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ sha2 = "0.10.8"
2727

2828
[dev-dependencies]
2929
tempfile = "3.14.0"
30+
tmp-path = "0.1.0"

src/main.rs

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -199,22 +199,21 @@ fn main() {
199199
mod tests {
200200
use super::*;
201201
use tempfile::tempdir;
202+
use tmp_path::tmp_path;
202203

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

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

211211
file_path
212212
}
213213

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

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

271270
#[test]
271+
#[tmp_path]
272272
fn generate_md5_file() {
273273
let test_file = fake_file_path();
274-
let base = tempdir().unwrap().path().to_path_buf();
275-
fs::create_dir_all(&base).unwrap();
276-
let output_file = base.join("output.txt");
274+
let output_file = tmp_path.join("output.txt");
277275

278276
process_checksum(
279277
&test_file,
@@ -293,12 +291,11 @@ mod tests {
293291
}
294292

295293
#[test]
294+
#[tmp_path]
296295
fn generate_md5_file_directory_overwrite() {
297296
let test_file_1 = fake_file_path();
298297
let test_file_2 = fake_file_path_2();
299-
let base = tempdir().unwrap().path().to_path_buf();
300-
fs::create_dir_all(&base).unwrap();
301-
let output_file = base.join("output.txt");
298+
let output_file = tmp_path.join("output.txt");
302299

303300
process_checksum(
304301
&test_file_1,
@@ -335,12 +332,11 @@ mod tests {
335332
}
336333

337334
#[test]
335+
#[tmp_path]
338336
fn generate_md5_file_directory_no_overwrite() {
339337
let test_file_1 = fake_file_path();
340338
let test_file_2 = fake_file_path_2();
341-
let base = tempdir().unwrap().path().to_path_buf();
342-
fs::create_dir_all(&base).unwrap();
343-
let output_file = base.join("output.txt");
339+
let output_file = tmp_path.join("output.txt");
344340

345341
process_checksum(
346342
&test_file_1,
@@ -371,11 +367,10 @@ mod tests {
371367
}
372368

373369
#[test]
370+
#[tmp_path]
374371
fn generate_sha256_file() {
375372
let test_file = fake_file_path();
376-
let base = tempdir().unwrap().path().to_path_buf();
377-
fs::create_dir_all(&base).unwrap();
378-
let output_file = base.join("output.txt");
373+
let output_file = tmp_path.join("output.txt");
379374

380375
process_checksum(
381376
&test_file,
@@ -395,12 +390,11 @@ mod tests {
395390
}
396391

397392
#[test]
393+
#[tmp_path]
398394
fn generate_sha256_file_directory_overwrite() {
399395
let test_file_1 = fake_file_path();
400396
let test_file_2 = fake_file_path_2();
401-
let base = tempdir().unwrap().path().to_path_buf();
402-
fs::create_dir_all(&base).unwrap();
403-
let output_file = base.join("output.txt");
397+
let output_file = tmp_path.join("output.txt");
404398

405399
process_checksum(
406400
&test_file_1,
@@ -437,12 +431,11 @@ mod tests {
437431
}
438432

439433
#[test]
434+
#[tmp_path]
440435
fn generate_sha256_file_directory_no_overwrite() {
441436
let test_file_1 = fake_file_path();
442437
let test_file_2 = fake_file_path_2();
443-
let base = tempdir().unwrap().path().to_path_buf();
444-
fs::create_dir_all(&base).unwrap();
445-
let output_file = base.join("output.txt");
438+
let output_file = tmp_path.join("output.txt");
446439

447440
process_checksum(
448441
&test_file_1,
@@ -473,11 +466,10 @@ mod tests {
473466
}
474467

475468
#[test]
469+
#[tmp_path]
476470
fn generate_sha512() {
477471
let test_file = fake_file_path();
478-
let base = tempdir().unwrap().path().to_path_buf();
479-
fs::create_dir_all(&base).unwrap();
480-
let output_file = base.join("output.txt");
472+
let output_file = tmp_path.join("output.txt");
481473

482474
process_checksum(
483475
&test_file,
@@ -497,12 +489,11 @@ mod tests {
497489
}
498490

499491
#[test]
492+
#[tmp_path]
500493
fn generate_sha512_file_directory_overwrite() {
501494
let test_file_1 = fake_file_path();
502495
let test_file_2 = fake_file_path_2();
503-
let base = tempdir().unwrap().path().to_path_buf();
504-
fs::create_dir_all(&base).unwrap();
505-
let output_file = base.join("output.txt");
496+
let output_file = tmp_path.join("output.txt");
506497

507498
process_checksum(
508499
&test_file_1,
@@ -539,12 +530,11 @@ mod tests {
539530
}
540531

541532
#[test]
533+
#[tmp_path]
542534
fn generate_sha512_file_directory_no_overwrite() {
543535
let test_file_1 = fake_file_path();
544536
let test_file_2 = fake_file_path_2();
545-
let base = tempdir().unwrap().path().to_path_buf();
546-
fs::create_dir_all(&base).unwrap();
547-
let output_file = base.join("output.txt");
537+
let output_file = tmp_path.join("output.txt");
548538

549539
process_checksum(
550540
&test_file_1,
@@ -575,11 +565,10 @@ mod tests {
575565
}
576566

577567
#[test]
568+
#[tmp_path]
578569
fn generate_sha1_file() {
579570
let test_file = fake_file_path();
580-
let base = tempdir().unwrap().path().to_path_buf();
581-
fs::create_dir_all(&base).unwrap();
582-
let output_file = base.join("output.txt");
571+
let output_file = tmp_path.join("output.txt");
583572

584573
process_checksum(
585574
&test_file,
@@ -599,12 +588,11 @@ mod tests {
599588
}
600589

601590
#[test]
591+
#[tmp_path]
602592
fn generate_sha1_file_directory_overwrite() {
603593
let test_file_1 = fake_file_path();
604594
let test_file_2 = fake_file_path_2();
605-
let base = tempdir().unwrap().path().to_path_buf();
606-
fs::create_dir_all(&base).unwrap();
607-
let output_file = base.join("output.txt");
595+
let output_file = tmp_path.join("output.txt");
608596

609597
process_checksum(
610598
&test_file_1,
@@ -641,12 +629,11 @@ mod tests {
641629
}
642630

643631
#[test]
632+
#[tmp_path]
644633
fn generate_sha1_file_directory_no_overwrite() {
645634
let test_file_1 = fake_file_path();
646635
let test_file_2 = fake_file_path_2();
647-
let base = tempdir().unwrap().path().to_path_buf();
648-
fs::create_dir_all(&base).unwrap();
649-
let output_file = base.join("output.txt");
636+
let output_file = tmp_path.join("output.txt");
650637

651638
process_checksum(
652639
&test_file_1,

0 commit comments

Comments
 (0)