From c737956a4e4fccca69b76e4ebb86fc9b26557108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Wed, 23 Nov 2022 00:30:35 -0300 Subject: [PATCH 1/2] check for EOF when asking questions when invoking Ouch from shell scripts, it is likely that questions will be left unanswered with EOF, this commit fixes Ouch interpreting EOF as Yes in Y/N questions --- src/utils/question.rs | 14 ++++++++++++-- tests/utils.rs | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/utils/question.rs b/src/utils/question.rs index a4ee4bb..4a77733 100644 --- a/src/utils/question.rs +++ b/src/utils/question.rs @@ -14,7 +14,7 @@ use fs_err as fs; use super::{strip_cur_dir, to_utf}; use crate::{ accessible::is_running_in_accessible_mode, - error::{Error, Result}, + error::{Error, FinalError, Result}, utils::{self, colors}, }; @@ -145,7 +145,17 @@ impl<'a> Confirmation<'a> { io::stdout().flush()?; let mut answer = String::new(); - io::stdin().read_line(&mut answer)?; + let bytes_read = io::stdin().read_line(&mut answer)?; + + if bytes_read == 0 { + let error = FinalError::with_title("Unexpected EOF when asking question.") + .detail("When asking the user:") + .detail(format!(" \"{message}\"")) + .detail("Expected 'y' or 'n' as answer, but found EOF instead.") + .hint("If using Ouch in scripting, consider using `--yes` and `--no`."); + + return Err(error.into()); + } answer.make_ascii_lowercase(); match answer.trim() { diff --git a/tests/utils.rs b/tests/utils.rs index a2b9231..b69ae0a 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -9,6 +9,7 @@ macro_rules! ouch { ($($e:expr),*) => { $crate::utils::cargo_bin() $(.arg($e))* + .arg("--yes") .unwrap(); } } From b3646d3255844bd9b127ee087e5be61ec8d4620a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Wed, 23 Nov 2022 00:36:06 -0300 Subject: [PATCH 2/2] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b9e38e..85af6a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,7 @@ Categories Used: - Fix incorrect warnings for decompression [\#270](https://github.com/ouch-org/ouch/pull/270) ([figsoda](https://github.com/figsoda)) - Fix infinite compression if output file is inside the input folder [\#288](https://github.com/ouch-org/ouch/pull/288) ([figsoda](https://github.com/figsoda)) - Fix not overwriting a folder when compressing [\#295](https://github.com/ouch-org/ouch/pull/295) ([marcospb19](https://github.com/marcospb19)) +- Check for EOF when asking questions [\#311](https://github.com/ouch-org/ouch/pull/311) ([marcospb19](https://github.com/marcospb19)) ### Improvements