1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Add pre-commit hook + run fmt (#1959)

This commit is contained in:
desbma 2021-03-30 21:24:01 +02:00 committed by GitHub
parent da18ffa496
commit be03c948ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 34 deletions

8
.pre-commit-config.yaml Normal file
View file

@ -0,0 +1,8 @@
# https://pre-commit.com
repos:
- repo: https://github.com/doublify/pre-commit-rust
rev: v1.0
hooks:
- id: cargo-check
- id: clippy
- id: fmt

View file

@ -9,10 +9,10 @@
extern crate uucore; extern crate uucore;
use clap::{App, Arg}; use clap::{App, Arg};
use retain_mut::RetainMut;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::{copy, sink, stdin, stdout, Error, ErrorKind, Read, Result, Write}; use std::io::{copy, sink, stdin, stdout, Error, ErrorKind, Read, Result, Write};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use retain_mut::RetainMut;
#[cfg(unix)] #[cfg(unix)]
use uucore::libc; use uucore::libc;
@ -98,19 +98,19 @@ fn tee(options: Options) -> Result<()> {
.files .files
.clone() .clone()
.into_iter() .into_iter()
.map(|file| .map(|file| NamedWriter {
NamedWriter { name: file.clone(),
name: file.clone(), inner: open(file, options.append),
inner: open(file, options.append), })
}
)
.collect(); .collect();
writers.insert(
writers.insert(0, NamedWriter { 0,
name: "'standard output'".to_owned(), NamedWriter {
inner: Box::new(stdout()), name: "'standard output'".to_owned(),
}); inner: Box::new(stdout()),
},
);
let mut output = MultiWriter::new(writers); let mut output = MultiWriter::new(writers);
let input = &mut NamedReader { let input = &mut NamedReader {
@ -119,8 +119,7 @@ fn tee(options: Options) -> Result<()> {
// TODO: replaced generic 'copy' call to be able to stop copying // TODO: replaced generic 'copy' call to be able to stop copying
// if all outputs are closed (due to errors) // if all outputs are closed (due to errors)
if copy(input, &mut output).is_err() || output.flush().is_err() || if copy(input, &mut output).is_err() || output.flush().is_err() || output.error_occured() {
output.error_occured() {
Err(Error::new(ErrorKind::Other, "")) Err(Error::new(ErrorKind::Other, ""))
} else { } else {
Ok(()) Ok(())
@ -150,7 +149,7 @@ struct MultiWriter {
} }
impl MultiWriter { impl MultiWriter {
fn new (writers: Vec<NamedWriter>) -> Self { fn new(writers: Vec<NamedWriter>) -> Self {
Self { Self {
initial_len: writers.len(), initial_len: writers.len(),
writers, writers,
@ -170,7 +169,7 @@ impl Write for MultiWriter {
show_info!("{}: {}", writer.name, f.to_string()); show_info!("{}: {}", writer.name, f.to_string());
false false
} }
_ => true _ => true,
} }
}); });
Ok(buf.len()) Ok(buf.len())
@ -184,7 +183,7 @@ impl Write for MultiWriter {
show_info!("{}: {}", writer.name, f.to_string()); show_info!("{}: {}", writer.name, f.to_string());
false false
} }
_ => true _ => true,
} }
}); });
Ok(()) Ok(())

View file

@ -38,10 +38,7 @@ fn test_posix_mode() {
// fail on long path // fail on long path
new_ucmd!() new_ucmd!()
.args(&[ .args(&["-p", &"dir".repeat(libc::PATH_MAX as usize + 1).as_str()])
"-p",
&"dir".repeat(libc::PATH_MAX as usize + 1).as_str(),
])
.fails() .fails()
.no_stdout(); .no_stdout();
@ -79,10 +76,7 @@ fn test_posix_special() {
// fail on long path // fail on long path
new_ucmd!() new_ucmd!()
.args(&[ .args(&["-P", &"dir".repeat(libc::PATH_MAX as usize + 1).as_str()])
"-P",
&"dir".repeat(libc::PATH_MAX as usize + 1).as_str(),
])
.fails() .fails()
.no_stdout(); .no_stdout();
@ -107,7 +101,10 @@ fn test_posix_all() {
// test the posix special mode // test the posix special mode
// accept some reasonable default // accept some reasonable default
new_ucmd!().args(&["-p", "-P", "dir/file"]).succeeds().no_stdout(); new_ucmd!()
.args(&["-p", "-P", "dir/file"])
.succeeds()
.no_stdout();
// accept non-leading hyphen // accept non-leading hyphen
new_ucmd!() new_ucmd!()
@ -136,10 +133,16 @@ fn test_posix_all() {
.no_stdout(); .no_stdout();
// fail on non-portable chars // fail on non-portable chars
new_ucmd!().args(&["-p", "-P", "dir#/$file"]).fails().no_stdout(); new_ucmd!()
.args(&["-p", "-P", "dir#/$file"])
.fails()
.no_stdout();
// fail on leading hyphen char // fail on leading hyphen char
new_ucmd!().args(&["-p", "-P", "dir/-file"]).fails().no_stdout(); new_ucmd!()
.args(&["-p", "-P", "dir/-file"])
.fails()
.no_stdout();
// fail on empty path // fail on empty path
new_ucmd!().args(&["-p", "-P", ""]).fails().no_stdout(); new_ucmd!().args(&["-p", "-P", ""]).fails().no_stdout();
@ -149,8 +152,5 @@ fn test_posix_all() {
fn test_args_parsing() { fn test_args_parsing() {
// fail on no args // fail on no args
let empty_args: [String; 0] = []; let empty_args: [String; 0] = [];
new_ucmd!() new_ucmd!().args(&empty_args).fails().no_stdout();
.args(&empty_args)
.fails()
.no_stdout();
} }