Skip to content
This repository has been archived by the owner on Dec 29, 2021. It is now read-only.

fix(output): Re-work API to work with rustfmt #74

Closed
wants to merge 1 commit into from

Conversation

epage
Copy link
Collaborator

@epage epage commented Oct 28, 2017

rustfmt will split long builers across lines, even when that breaks
logical grouping.

For example

assert_cli::Assert::command(&["ls", "foo-bar-foo"])
    .fails()
    .and()
    .stderr().contains("foo-bar-foo")
    .unwrap();

will be turned into

assert_cli::Assert::command(&["ls", "foo-bar-foo"])
    .fails()
    .and()
    .stderr()
    .contains("foo-bar-foo")
    .unwrap();

which obscures intent.

Normally, I don't like working around tools but this one seems
sufficient to do so.

assert_cli::Assert::command(&["ls", "foo-bar-foo"])
    .fails()
    .and()
    .stderr(assert_cli::Output::contains("foo-bar-foo"))
    .unwrap();

Pros

  • More consistent with with_env
  • Can add support for accepting arrays
  • Still auto-complete / docs friendly
  • Still expandable to additional assertions without much duplication or
    losing out on good error reporting

Cons

  • More verbose if you don't use assert_cli::{Assert, Environment, Output}

Alternatives

  • Accept distinct predicates
    • e.g. .stderr(assert_cli::Is::text("foo-bar-foo"))
    • e.g. .stderr(assert_cli::Is::not("foo-bar-foo"))
    • Strange text function
    • More structs to use
    • Less auto-complete / docs friendly (lacks contextual discovery or
      whatever the UX term is)

Fixes #70

BREAKING CHANGE: .stdout().contains(text) is now
.stdout(assert_cli::Output::contains(text), etc.

  • I have created tests for any new feature, or added regression tests for bugfixes.
  • cargo test succeeds
  • Clippy is happy: cargo +nightly clippy succeeds
  • Rustfmt is happy: cargo +nightly fmt succeeds

@epage
Copy link
Collaborator Author

epage commented Oct 28, 2017

I can take or leave the API change.

This did push me to making the code more composable which is nice.

@epage epage force-pushed the feat/out branch 2 times, most recently from c760d80 to 3afd1e6 Compare October 28, 2017 18:21
rustfmt will split long builers across lines, even when that breaks
logical grouping.

For example
```rust
assert_cli::Assert::command(&["ls", "foo-bar-foo"])
    .fails()
    .and()
    .stderr().contains("foo-bar-foo")
    .unwrap();
```
will be turned into
```rust
assert_cli::Assert::command(&["ls", "foo-bar-foo"])
    .fails()
    .and()
    .stderr()
    .contains("foo-bar-foo")
    .unwrap();
```
which obscures intent.

Normally, I don't like working around tools but this one seems
sufficient to do so.
```rust
assert_cli::Assert::command(&["ls", "foo-bar-foo"])
    .fails()
    .and()
    .stderr(assert_cli::Output::contains("foo-bar-foo"))
    .unwrap();
```

Pros
- More consistent with `with_env`
- Can add support for accepting arrays
- Still auto-complete / docs friendly
- Still expandable to additional assertions without much duplication or
  losing out on good error reporting

Cons
- More verbose if you don't `use assert_cli::{Assert, Environment, Output}`

Alternatives
- Accept distinct predicates
  - e.g. `.stderr(assert_cli::Is::text("foo-bar-foo"))`
  - e.g. `.stderr(assert_cli::Is::not("foo-bar-foo"))`
  - Strange `text` function
  - More structs to `use`
  - Less auto-complete / docs friendly (lacks contextual discovery or
    whatever the UX term is)

Fixes assert-rs#70

BREAKING CHANGE: `.stdout().contains(text)` is now
`.stdout(assert_cli::Output::contains(text)`, etc.
Copy link
Collaborator

@killercup killercup left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this! I feel like we are slowly moving to a structure of fractal asserts. We should try to structure (Cli)Assert, Environment(Assert), and Output(Assert) in a similar way.

More verbose if you don't use assert_cli::{Assert, Environment, Output}

Yes, that's not really nice. But I have an idea. Let me whip up a PR based on this one with free standing functions instead of constructors on Output and a prelude module.

@@ -37,20 +37,20 @@ fn main() {
assert_cli::Assert::command(&["ls", "foo-bar-foo"])
.fails()
.and()
.stderr().contains("foo-bar-foo")
.stderr(assert_cli::Output::contains("foo-bar-foo"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably use assert_cli::{Assert, Output}; in most examples.

if result != self.expected_result {
if self.expected_result {
bail!(ErrorKind::OutputDoesntContain(
let nice_diff = diff::render(&differences)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to early-return this error? I think I'd .unwrap_or_else(|e| format!("(unable to render nice diff, error was: {:?})", e)) instead of ? here and give the user both the interesting error as well as the diff-error in one go

expected_result: false,
};
Self::new(StrPredicate::Is(pred))
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reading for of these I fell the need to write a macro for this… which would probably not make the code much clearer 😅

kind: OutputKind::StdErr,
expected_result: true,
}
pub fn stderr(mut self, pred: Output) -> Self {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to document that you can call this method multiple times

killercup added a commit that referenced this pull request Oct 29, 2017
@killercup
Copy link
Collaborator

Rustfmt is happy

Sorry this discussion has gone stale. Just wanted to ask here: Were there any rustfmt updates that changed this since October?

@epage
Copy link
Collaborator Author

epage commented Jan 12, 2018

Sorry this discussion has gone stale. Just wanted to ask here: Were there any rustfmt updates that changed this since October?

Unsure. I'll need to check that when I go and take care of the rest of your feedback. I held off on all of it because I didn't want to do a lot of churn if we decided to go with #75 instead.

@epage epage mentioned this pull request Jan 25, 2018
4 tasks
@epage epage closed this Feb 2, 2018
@epage epage deleted the feat/out branch February 2, 2018 23:58
@epage epage restored the feat/out branch February 2, 2018 23:58
@epage epage reopened this Feb 3, 2018
@epage epage mentioned this pull request Feb 3, 2018
@epage
Copy link
Collaborator Author

epage commented May 29, 2018

Addressed in https://github.com/assert-rs/assert_cmd

@epage epage closed this May 29, 2018
@epage epage deleted the feat/out branch May 29, 2018 12:33
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fluent Output Tests Look off with rustfmt
2 participants