chshersh / tool-sync

🧰 Download pre-built binaries of all your favourite tools with a single command
https://crates.io/crates/tool-sync
Mozilla Public License 2.0
69 stars 16 forks source link

Use the 'pretty-assertions' crate #96

Open chshersh opened 1 year ago

chshersh commented 1 year ago

This package provides a better UX for failing tests.

Honestly, I think it should be the default. But it's possible to use this crate only for tests and not for users so it shouldn't affect people install tool-sync with cargo install.

MitchellBerend commented 1 year ago

I was thinking of refactoring the way errors are printed to stdout/err if that is not included in #97, we can include this in the same refactor and I think we can also just add this as a dev dependency so it will only be included in the binary that gets built with cargo test

chshersh commented 1 year ago

97 looks pretty separate to me 👍🏻

This issue can be implemented separately. I imagine a pretty small diff. It's just adding proper imports, dev dependencies and quickly verifying that it does work as expected.

Come to think of it, it would be nice to enforce the usage of pretty-assertions everywhere in the codebase so it would be impossible to use plain assert_eq! 🤔 Is there a way to do this in Rust? Kinda like a custom listing rule.

MitchellBerend commented 1 year ago

Come to think of it, it would be nice to enforce the usage of pretty-assertions everywhere in the codebase so it would be impossible to use plain assert_eq! thinking Is there a way to do this in Rust?

I don't know of any but this can be caught in review too.

chshersh commented 1 year ago

Looks like we can use clippy configuration options disallow_methods to forbid the usage of specific functions:

My plan would be the following:

If this works — great! If not, we need to regroup and think 🤔

I'm not feeling confident in catching such kinds of problems during the review. There's too much I can remember to think about while reviewing the code 😞

MitchellBerend commented 1 year ago

Unless Im doing something wrong I can't seem to get clippy to run on tests. Note that the top of the test section in src/config/toml.rs contains the following lint option:

#[cfg(test)]
mod tests {
    #![deny(clippy::disallowed_methods)]
    use super::*;
$ cat .clippy.toml
disallowed-methods = [
    "std::assert_eq",
    "std::assert_ne",
    "std::assert_str_eq",

    "std::boxed::Box::new",
]
#![deny(clippy::disallowed_methods)]

use std::collections::BTreeMap;
use std::fmt::{Display, Formatter};
use std::fs;
use std::path::PathBuf;
use toml::{map::Map, Value};

use crate::config::schema::{Config, ConfigAsset};
use crate::infra::err;
use crate::model::asset_name::AssetName;

#[derive(Debug, PartialEq, Eq)]
pub enum TomlError {
    IO(String),
    Parse(toml::de::Error),
    Decode,
}

impl Display for TomlError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let _: std::boxed::Box<String> = std::boxed::Box::new(String::new());

        match self {
            TomlError::IO(e) => write!(f, "[IO Error] {}", e),
            TomlError::Parse(e) => write!(f, "[Parsing Error] {}", e),
            TomlError::Decode => write!(f, "[Decode Error]"),
        }
    }
}
$ cargo clippy
    Checking tool-sync v0.2.0 (/home/mitchell/rust/tool-sync)
error: use of a disallowed method `std::boxed::Box::new`
  --> src/config/toml.rs:23:42
   |
23 |         let _: std::boxed::Box<String> = std::boxed::Box::new(String::new());
   |                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: the lint level is defined here
  --> src/config/toml.rs:1:9
   |
1  | #![deny(clippy::disallowed_methods)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_methods

warning: use of a disallowed method `std::boxed::Box::new`
  --> src/lib.rs:24:26
   |
24 |     let _: Box<String> = Box::new(String::new());
   |                          ^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(clippy::disallowed_methods)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_methods

warning: `tool-sync` (lib) generated 1 warning
error: could not compile `tool-sync` due to previous error; 1 warning emitted
$ cargo clippy --tests
    Checking tool-sync v0.2.0 (/home/mitchell/rust/tool-sync)
error: use of a disallowed method `std::boxed::Box::new`
  --> src/config/toml.rs:23:42
   |
23 |         let _: std::boxed::Box<String> = std::boxed::Box::new(String::new());
   |                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: the lint level is defined here
  --> src/config/toml.rs:1:9
   |
1  | #![deny(clippy::disallowed_methods)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_methods

warning: `assert!(false, ..)` should probably be replaced
   --> src/config/toml.rs:178:17
    |
178 |                 assert!(false, "Unexpected succces")
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: `#[warn(clippy::assertions_on_constants)]` on by default
    = help: use `panic!(..)` or `unreachable!(..)`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_constants

warning: `assert!(true)` will be optimized out by the compiler
   --> src/config/toml.rs:181:17
    |
181 |                 assert!(true, "Exepected a parsing error")
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: remove it
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_constants

warning: use of a disallowed method `std::boxed::Box::new`
  --> src/lib.rs:24:26
   |
24 |     let _: Box<String> = Box::new(String::new());
   |                          ^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(clippy::disallowed_methods)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_methods

warning: `tool-sync` (lib) generated 1 warning
error: could not compile `tool-sync` due to previous error; 1 warning emitted
warning: build failed, waiting for other jobs to finish...
warning: `tool-sync` (lib test) generated 3 warnings (1 duplicate)
error: could not compile `tool-sync` due to previous error; 3 warnings emitted
chshersh commented 1 year ago

@MitchellBerend I found this clippy issue:

It suggests that there's a hidden option --tests that you can use to run clippy in tests (and it doesn't run in tests by default). Could you try with it?

MitchellBerend commented 1 year ago

I did, its the last output in the previous comment.

chshersh commented 1 year ago

Oh, didn't notice 😮 In that case, I'm not sure how to fix it 🤔