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

feat(testing): expose ConfigFile in testing API #944

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

simonsan
Copy link
Contributor

@simonsan simonsan commented Nov 15, 2024

For testing purposes, it would be useful, to have the testing::config::ConfigFile struct being exposed.

For example I can't use the internal CmdRunner, also because it doesn't support searching in command output ergonomically. So I use assert_cmd, but I like the ease of use of my config being serialized and injected in the call to the command runner, so I implemented:

pub trait AssertCmdExt {
    /// Add the given configuration file
    fn config(&mut self, config: &impl Serialize) -> &mut Self;
}
impl AssertCmdExt for Command {
    fn config(&mut self, config: &impl Serialize) -> &mut Self {
        let target_bin = self.get_program().to_owned();

        let config_file = ConfigFile::create(&target_bin, config);

        // Leak the config file to keep it alive for the duration of the test
        let static_config: &'static mut ConfigFile = Box::leak(Box::new(config_file));

        self.args(&["-c", &static_config.path().display().to_string()]);
        self
    }
}
/// Use configured value
#[rstest]
fn start_with_config_no_args(setup: Result<Command>) -> Result<()> {
    let mut config = RusticServerConfig::default();
    config.server.listen = SocketAddr::from(([127, 0, 0, 1], 8081));

    let assert = setup?.config(&config).arg("serve").arg("-v").assert();

    assert
        .stdout(predicates::str::contains("listen: 127.0.0.1:8081"))
        .success();

    Ok(())
}

So can we expose the ConfigFile in the public API for testing, please? 🙏🏽

For testing purposes, it would be useful, to have the `testing::config::ConfigFile` struct being exposed.
@simonsan
Copy link
Contributor Author

simonsan commented Dec 3, 2024

If another approach would be more interesting, e.g. exposing some more functionality around that, to not need to leak the Config file (so it can be removed within the drop impl from the temp files), let me know.

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

Successfully merging this pull request may close these issues.

1 participant