From 6a4105e4d8cc26e73bcc0ca6da1d2d3443fdc7fd Mon Sep 17 00:00:00 2001 From: Balthazar Rouberol Date: Sun, 1 May 2022 12:13:05 +0200 Subject: [PATCH] Avoid leaking a test saved file in the cwd --- src/editor_test.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/editor_test.rs b/src/editor_test.rs index 5a163af..a4af734 100644 --- a/src/editor_test.rs +++ b/src/editor_test.rs @@ -6,7 +6,7 @@ use std::fs; use std::io::Error; use std::io::Write; use std::path::PathBuf; -use tempfile::NamedTempFile; +use tempfile::{tempdir, NamedTempFile}; use termion::color; use termion::event::{Event, Key, MouseEvent}; @@ -849,12 +849,15 @@ fn test_process_command_not_found() { #[test] fn test_save_and_quit() { - let mut editor = get_test_editor(); - process_keystrokes(&mut editor, vec!['G', 'o', 'd', 'e', 'r', 'p']); - editor.process_keystroke(Key::Esc); - assert_eq!(editor.unsaved_edits, 4); - assert!(!editor.should_quit); - process_command(&mut editor, ":wq"); - assert_eq!(editor.unsaved_edits, 0); - assert!(editor.should_quit); + let dir = tempdir().unwrap(); + if std::env::set_current_dir(&dir).is_ok() { + let mut editor = get_test_editor(); + process_keystrokes(&mut editor, vec!['G', 'o', 'd', 'e', 'r', 'p']); + editor.process_keystroke(Key::Esc); + assert_eq!(editor.unsaved_edits, 4); + assert!(!editor.should_quit); + process_command(&mut editor, ":wq"); + assert_eq!(editor.unsaved_edits, 0); + assert!(editor.should_quit); + }; }