From d41777c52c34d5c772d5e200710a73ba5b96ca3b Mon Sep 17 00:00:00 2001 From: tpeters Date: Mon, 22 May 2023 20:07:14 +0200 Subject: [PATCH 1/6] more: change file unwrap to match --- src/uu/more/src/more.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/uu/more/src/more.rs b/src/uu/more/src/more.rs index 64683a1be..b9d9c8272 100644 --- a/src/uu/more/src/more.rs +++ b/src/uu/more/src/more.rs @@ -104,7 +104,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { if length > 1 { buff.push_str(&MULTI_FILE_TOP_PROMPT.replace("{}", file.to_str().unwrap())); } - let mut reader = BufReader::new(File::open(file).unwrap()); + let opened_file = match File::open(file) { + Err(why) => { + return Err(UUsageError::new( + 1, + format!("cannot open {}: {}", file.quote(), why.kind()), + )) + } + Ok(opened_file) => opened_file, + }; + let mut reader = BufReader::new(opened_file); reader.read_to_string(&mut buff).unwrap(); more(&buff, &mut stdout, next_file.copied(), &options)?; buff.clear(); From 50bff30c67cff3a87bb956ae52d2f5e91948c91b Mon Sep 17 00:00:00 2001 From: tpeters Date: Tue, 23 May 2023 18:03:25 +0200 Subject: [PATCH 2/6] more: add test and change error type --- src/uu/more/src/more.rs | 2 +- tests/by-util/test_more.rs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/uu/more/src/more.rs b/src/uu/more/src/more.rs index b9d9c8272..b5487ef87 100644 --- a/src/uu/more/src/more.rs +++ b/src/uu/more/src/more.rs @@ -106,7 +106,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } let opened_file = match File::open(file) { Err(why) => { - return Err(UUsageError::new( + return Err(USimpleError::new( 1, format!("cannot open {}: {}", file.quote(), why.kind()), )) diff --git a/tests/by-util/test_more.rs b/tests/by-util/test_more.rs index cb12343fa..75f0eed77 100644 --- a/tests/by-util/test_more.rs +++ b/tests/by-util/test_more.rs @@ -1,5 +1,7 @@ use crate::common::util::TestScenario; use is_terminal::IsTerminal; +use std::fs::{set_permissions, Permissions}; +use std::os::unix::fs::PermissionsExt; #[test] fn test_more_no_arg() { @@ -32,3 +34,14 @@ fn test_more_dir_arg() { .usage_error("'.' is a directory."); } } + +#[test] +fn test_more_invalid_file_perms() { + let (at, mut ucmd) = at_and_ucmd!(); + let permissions = Permissions::from_mode(0o244); + at.make_file("invalid-perms.txt").metadata().unwrap(); + set_permissions(at.plus("invalid-perms.txt"), permissions).unwrap(); + ucmd.arg("invalid-perms.txt").fails(); + //.code_is(1) + //.stderr_is("more: cannot open 'invalid-perms.txt': permission denied"); +} From eed916a0760e80225e73caecf1a4b4ab9855ddb1 Mon Sep 17 00:00:00 2001 From: tpeters Date: Wed, 24 May 2023 12:30:28 +0200 Subject: [PATCH 3/6] more: disable raw mode on error --- src/uu/more/src/more.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/uu/more/src/more.rs b/src/uu/more/src/more.rs index b5487ef87..8c6cdf6e2 100644 --- a/src/uu/more/src/more.rs +++ b/src/uu/more/src/more.rs @@ -106,10 +106,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } let opened_file = match File::open(file) { Err(why) => { + terminal::disable_raw_mode().unwrap(); return Err(USimpleError::new( 1, format!("cannot open {}: {}", file.quote(), why.kind()), - )) + )); } Ok(opened_file) => opened_file, }; From 53ffd55bdfa9ac929023754679e7849610f9e667 Mon Sep 17 00:00:00 2001 From: tpeters Date: Wed, 24 May 2023 13:28:30 +0200 Subject: [PATCH 4/6] more: run test only on unix systems --- tests/by-util/test_more.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/tests/by-util/test_more.rs b/tests/by-util/test_more.rs index 75f0eed77..7753aa0ca 100644 --- a/tests/by-util/test_more.rs +++ b/tests/by-util/test_more.rs @@ -1,6 +1,10 @@ use crate::common::util::TestScenario; use is_terminal::IsTerminal; + +//Both following includes are only needed inside the test_more_invalid_file_perms() +#[cfg(target_family = "unix")] use std::fs::{set_permissions, Permissions}; +#[cfg(target_family = "unix")] use std::os::unix::fs::PermissionsExt; #[test] @@ -36,12 +40,13 @@ fn test_more_dir_arg() { } #[test] +#[cfg(target_family = "unix")] fn test_more_invalid_file_perms() { - let (at, mut ucmd) = at_and_ucmd!(); - let permissions = Permissions::from_mode(0o244); - at.make_file("invalid-perms.txt").metadata().unwrap(); - set_permissions(at.plus("invalid-perms.txt"), permissions).unwrap(); - ucmd.arg("invalid-perms.txt").fails(); - //.code_is(1) - //.stderr_is("more: cannot open 'invalid-perms.txt': permission denied"); + if std::io::stdout().is_terminal() { + let (at, mut ucmd) = at_and_ucmd!(); + let permissions = Permissions::from_mode(0o244); + at.make_file("invalid-perms.txt").metadata().unwrap(); + set_permissions(at.plus("invalid-perms.txt"), permissions).unwrap(); + ucmd.arg("invalid-perms.txt").fails(); + } } From ccdb76aef7b57cd2dd2251bc821cf5533e2deaf3 Mon Sep 17 00:00:00 2001 From: Ludmuterol <45458713+Ludmuterol@users.noreply.github.com> Date: Wed, 24 May 2023 15:57:55 +0200 Subject: [PATCH 5/6] more: Update tests/by-util/test_more.rs Co-authored-by: Daniel Hofstetter --- tests/by-util/test_more.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/by-util/test_more.rs b/tests/by-util/test_more.rs index 7753aa0ca..7171ca4fe 100644 --- a/tests/by-util/test_more.rs +++ b/tests/by-util/test_more.rs @@ -45,7 +45,7 @@ fn test_more_invalid_file_perms() { if std::io::stdout().is_terminal() { let (at, mut ucmd) = at_and_ucmd!(); let permissions = Permissions::from_mode(0o244); - at.make_file("invalid-perms.txt").metadata().unwrap(); + at.make_file("invalid-perms.txt"); set_permissions(at.plus("invalid-perms.txt"), permissions).unwrap(); ucmd.arg("invalid-perms.txt").fails(); } From 7cad501c2c6883327911eaedf44876ca25fa6a6c Mon Sep 17 00:00:00 2001 From: tpeters Date: Wed, 24 May 2023 16:32:29 +0200 Subject: [PATCH 6/6] more: add output check to test --- tests/by-util/test_more.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/by-util/test_more.rs b/tests/by-util/test_more.rs index 7171ca4fe..97353deb8 100644 --- a/tests/by-util/test_more.rs +++ b/tests/by-util/test_more.rs @@ -1,12 +1,6 @@ use crate::common::util::TestScenario; use is_terminal::IsTerminal; -//Both following includes are only needed inside the test_more_invalid_file_perms() -#[cfg(target_family = "unix")] -use std::fs::{set_permissions, Permissions}; -#[cfg(target_family = "unix")] -use std::os::unix::fs::PermissionsExt; - #[test] fn test_more_no_arg() { // Reading from stdin is now supported, so this must succeed @@ -42,11 +36,17 @@ fn test_more_dir_arg() { #[test] #[cfg(target_family = "unix")] fn test_more_invalid_file_perms() { + use std::fs::{set_permissions, Permissions}; + use std::os::unix::fs::PermissionsExt; + if std::io::stdout().is_terminal() { let (at, mut ucmd) = at_and_ucmd!(); let permissions = Permissions::from_mode(0o244); at.make_file("invalid-perms.txt"); set_permissions(at.plus("invalid-perms.txt"), permissions).unwrap(); - ucmd.arg("invalid-perms.txt").fails(); + ucmd.arg("invalid-perms.txt") + .fails() + .code_is(1) + .stderr_contains("permission denied"); } }