on master help sign in
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

commit3af35492320077b2abf7cc70117ea02aa24389a3 authorMartin Nordholts <enselic@gmail.com> dateSun 28 Feb 2021, 16:56 (6 years ago) parent643f0bc
1 file changed+8−13
tests/integration_tests.rs+8−13
tests/integration_tests.rs+8−13
@@ -6,6 +6,7 @@ use std::fs::File;
66use std::path::Path;
77use std::process::{Command, Stdio};
88use std::str::from_utf8;
9+use tempfile::tempdir;
910
1011#[cfg(unix)]
1112use std::time::Duration;
@@ -663,12 +664,8 @@ fn config_location_test() {
663664
664665#[test]
665666fn config_location_when_generating() {
666− let tmp_config_path = std::path::PathBuf::from("/tmp/should-be-created.conf");
667−
668− // Make sure the file does not exist before the test is run
669− // If this fails, run the following before running tests:
670− // rm /tmp/should-be-created.conf
671− assert!(!tmp_config_path.exists());
667+ let tmp_dir = tempdir().expect("can create temporary directory");
668+ let tmp_config_path = tmp_dir.path().join("should-be-created.conf");
672669
673670 // Create the file with bat
674671 bat_with_config()
@@ -676,13 +673,13 @@ fn config_location_when_generating() {
676673 .arg("--generate-config-file")
677674 .assert()
678675 .success()
679− .stdout("Success! Config file written to /tmp/should-be-created.conf\n");
676+ .stdout(
677+ predicate::str::is_match("Success! Config file written to .*should-be-created.conf\n")
678+ .unwrap(),
679+ );
680680
681681 // Now we expect the file to exist. If it exists, we assume contents are correct
682682 assert!(tmp_config_path.exists());
683−
684− // Cleanup
685− std::fs::remove_file(tmp_config_path).unwrap();
686683}
687684
688685#[test]
@@ -977,9 +974,7 @@ fn file_with_invalid_utf8_filename() {
977974 use std::io::Write;
978975 use std::os::unix::ffi::OsStrExt;
979976
980− use tempfile::TempDir;
981−
982− let tmp_dir = TempDir::new().expect("can create temporary directory");
977+ let tmp_dir = tempdir().expect("can create temporary directory");
983978 let file_path = tmp_dir
984979 .path()
985980 .join(OsStr::from_bytes(b"test-invalid-utf8-\xC3(.rs"));