Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing rexpect app #79

Open
souze opened this issue Oct 12, 2022 · 0 comments
Open

Testing rexpect app #79

souze opened this issue Oct 12, 2022 · 0 comments
Assignees

Comments

@souze
Copy link
Collaborator

souze commented Oct 12, 2022

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?

@souze souze self-assigned this Oct 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant