From 3af35492320077b2abf7cc70117ea02aa24389a3 Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Sun, 28 Feb 2021 16:56:16 +0100 Subject: [PATCH] integration_tests: Use tempdir() in config_location_when_generating For cleaner code. Use the tempdir() function since that is recommended by the crate docs: https://docs.rs/tempfile/3.2.0/tempfile/index.html --- tests/integration_tests.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 4047d5c8..de673784 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -6,6 +6,7 @@ use std::fs::File; use std::path::Path; use std::process::{Command, Stdio}; use std::str::from_utf8; +use tempfile::tempdir; #[cfg(unix)] use std::time::Duration; @@ -663,12 +664,8 @@ fn config_location_test() { #[test] fn config_location_when_generating() { - let tmp_config_path = std::path::PathBuf::from("/tmp/should-be-created.conf"); - - // Make sure the file does not exist before the test is run - // If this fails, run the following before running tests: - // rm /tmp/should-be-created.conf - assert!(!tmp_config_path.exists()); + let tmp_dir = tempdir().expect("can create temporary directory"); + let tmp_config_path = tmp_dir.path().join("should-be-created.conf"); // Create the file with bat bat_with_config() @@ -676,13 +673,13 @@ fn config_location_when_generating() { .arg("--generate-config-file") .assert() .success() - .stdout("Success! Config file written to /tmp/should-be-created.conf\n"); + .stdout( + predicate::str::is_match("Success! Config file written to .*should-be-created.conf\n") + .unwrap(), + ); // Now we expect the file to exist. If it exists, we assume contents are correct assert!(tmp_config_path.exists()); - - // Cleanup - std::fs::remove_file(tmp_config_path).unwrap(); } #[test] @@ -977,9 +974,7 @@ fn file_with_invalid_utf8_filename() { use std::io::Write; use std::os::unix::ffi::OsStrExt; - use tempfile::TempDir; - - let tmp_dir = TempDir::new().expect("can create temporary directory"); + let tmp_dir = tempdir().expect("can create temporary directory"); let file_path = tmp_dir .path() .join(OsStr::from_bytes(b"test-invalid-utf8-\xC3(.rs"));