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

View file

@ -38,10 +38,7 @@ fn test_posix_mode() {
// fail on long path
new_ucmd!()
.args(&[
"-p",
&"dir".repeat(libc::PATH_MAX as usize + 1).as_str(),
])
.args(&["-p", &"dir".repeat(libc::PATH_MAX as usize + 1).as_str()])
.fails()
.no_stdout();
@ -79,10 +76,7 @@ fn test_posix_special() {
// fail on long path
new_ucmd!()
.args(&[
"-P",
&"dir".repeat(libc::PATH_MAX as usize + 1).as_str(),
])
.args(&["-P", &"dir".repeat(libc::PATH_MAX as usize + 1).as_str()])
.fails()
.no_stdout();
@ -107,7 +101,10 @@ fn test_posix_all() {
// test the posix special mode
// 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
new_ucmd!()
@ -136,10 +133,16 @@ fn test_posix_all() {
.no_stdout();
// 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
new_ucmd!().args(&["-p", "-P", "dir/-file"]).fails().no_stdout();
new_ucmd!()
.args(&["-p", "-P", "dir/-file"])
.fails()
.no_stdout();
// fail on empty path
new_ucmd!().args(&["-p", "-P", ""]).fails().no_stdout();
@ -149,8 +152,5 @@ fn test_posix_all() {
fn test_args_parsing() {
// fail on no args
let empty_args: [String; 0] = [];
new_ucmd!()
.args(&empty_args)
.fails()
.no_stdout();
}
new_ucmd!().args(&empty_args).fails().no_stdout();
}

View file

@ -177,7 +177,7 @@ fn test_rm_directory_without_flag() {
let dir = "test_rm_directory_without_flag_dir";
at.mkdir(dir);
let result = ucmd.arg(dir).fails();
println!("{}", result.stderr);
assert!(result