1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

Merge pull request #5791 from sylvestre/handle-error

Handle better some errors
This commit is contained in:
Daniel Hofstetter 2024-01-15 15:19:10 +01:00 committed by GitHub
commit 112eb21eb3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 54 additions and 8 deletions

View file

@ -6,7 +6,7 @@
use std::io::{stdout, Read, Write};
use uucore::display::Quotable;
use uucore::encoding::{wrap_print, Data, Format};
use uucore::encoding::{wrap_print, Data, EncodeError, Format};
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
use uucore::format_usage;
@ -174,6 +174,7 @@ pub fn handle_input<R: Read>(
wrap_print(&data, &s);
Ok(())
}
Err(EncodeError::InvalidInput) => Err(USimpleError::new(1, "error: invalid input")),
Err(_) => Err(USimpleError::new(
1,
"error: invalid input (length must be multiple of 4 characters)",

View file

@ -11,11 +11,12 @@ use std::fmt;
use std::fs::File;
use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write};
use std::num::IntErrorKind;
use std::path::Path;
use std::str::from_utf8;
use unicode_width::UnicodeWidthChar;
use uucore::display::Quotable;
use uucore::error::{FromIo, UError, UResult};
use uucore::{format_usage, help_about, help_usage};
use uucore::error::{set_exit_code, FromIo, UError, UResult};
use uucore::{format_usage, help_about, help_usage, show_error};
const ABOUT: &str = help_about!("expand.md");
const USAGE: &str = help_usage!("expand.md");
@ -465,6 +466,12 @@ fn expand(options: &Options) -> UResult<()> {
let mut buf = Vec::new();
for file in &options.files {
if Path::new(file).is_dir() {
show_error!("{}: Is a directory", file);
set_exit_code(1);
continue;
}
let mut fh = open(file)?;
while match fh.read_until(b'\n', &mut buf) {

View file

@ -73,9 +73,18 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let stdin = stdin();
let lines = stdin.lock().lines();
for line in lines {
for number in line.unwrap().split_whitespace() {
print_factors_str(number, &mut w, print_exponents)
.map_err_context(|| "write error".into())?;
match line {
Ok(line) => {
for number in line.split_whitespace() {
print_factors_str(number, &mut w, print_exponents)
.map_err_context(|| "write error".into())?;
}
}
Err(e) => {
set_exit_code(1);
show_error!("error reading input: {}", e);
return Ok(());
}
}
}
}

View file

@ -27,6 +27,7 @@ pub enum DecodeError {
pub enum EncodeError {
Z85InputLenNotMultipleOf4,
InvalidInput,
}
pub type DecodeResult = Result<Vec<u8>, DecodeError>;
@ -148,8 +149,10 @@ impl<R: Read> Data<R> {
pub fn encode(&mut self) -> Result<String, EncodeError> {
let mut buf: Vec<u8> = vec![];
self.input.read_to_end(&mut buf).unwrap();
encode(self.format, buf.as_slice())
match self.input.read_to_end(&mut buf) {
Ok(_) => encode(self.format, buf.as_slice()),
Err(_) => Err(EncodeError::InvalidInput),
}
}
}

View file

@ -18,3 +18,16 @@ fn test_z85_not_padded() {
.fails()
.stderr_only("basenc: error: invalid input (length must be multiple of 4 characters)\n");
}
#[test]
fn test_invalid_input() {
let error_message = if cfg!(windows) {
"basenc: .: Permission denied\n"
} else {
"basenc: error: invalid input\n"
};
new_ucmd!()
.args(&["--base32", "."])
.fails()
.stderr_only(error_message);
}

View file

@ -409,3 +409,11 @@ int main() {
",
);
}
#[test]
fn test_expand_directory() {
new_ucmd!()
.args(&["."])
.fails()
.stderr_contains("expand: .: Is a directory");
}

View file

@ -1258,3 +1258,8 @@ const PRIMES50: &[u64] = &[
1125899906841623,
1125899906841613,
];
#[test]
fn fails_on_directory() {
new_ucmd!().pipe_in(".").fails();
}