rust-cli / rust-cli/rexpect

Testing rexpect app

Open
#79 0 comments 0 reactions 1 assignee View on GitHub

@ekarlsn is already working on this.

Since Oct 14, 2022.

Dominant language
Rust
Stars
391
Forks
69
Avg merge
1d 34m
Merged PRs (30d)
2

Description

It would be nice if we could provide an easy way to test rexpect apps. Maybe something like this?

fn main() -> Result<(), Error> {
    let mut p = rexpect::spawn("cat", Some(300))?;

    use_cat(&mut p)?;
    Ok(())
}

fn use_cat(p: &mut PtySession) -> Result<(), Error> {
    p.send_line("I'm a cat")?;
    p.exp_string("I'm a cat\r\n")?;
    Ok(())
}

mod test {
    use super::*;

    #[test]
    fn option1() -> Result<(), Error> {
        use_cat(
            rexpect::testing::FakePtySession()
                .recv("I'm a cat")
                .send("I'm a cat"),
        )?;
        Ok(())
    }

    fn option2() -> Result<(), Error> {
        let mut p = rexpect::testing::FakePtySession();
        std::thread::spawn(|| use_cat(p));
        p.recv("I'm a cat");
        p.send("I'm a cat");
        Ok(())
    }
}

Or any suggestions for a better interface? I think option2 looks nicer in the test case, but I think option1 is overall better, since it doesn't require spinning up a new thread, and needing to sync between the two.
I'm not sure how we could get nice error messages with either approach, but I'm working on option1 at the moment and will see how it turns out.

I haven't seen any crates providing special testing-utilities, is it normally not done? Should we have it behind a feature-flag?

Implementation wise, it looks like we would have to store Box<impl PtyProcess> in PtySession and Box<impl Write> in StreamSession. So that we can keep PtySession without template arguments. This would break the API, but I don't think it should be a problem?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.