dragonflyoss / nydus

Nydus - the Dragonfly image service, providing fast, secure and easy access to container images.
https://nydus.dev/
Apache License 2.0
1.22k stars 205 forks source link

Proposal: support logger output in unit test #1290

Closed hangvane closed 1 year ago

hangvane commented 1 year ago

Seems that there's no easy way to show the logger output in unit test for debugging currently, even with the command arg --nocapture:

cargo test --package nydus-storage --lib -- meta::tests::test_load_meta_ci_zran_get_chunks_compressed --exact --nocapture

flexi-logger has supported output in unit test since 0.25.3, so we can initialize flexi_logger in test:


#[test]
    fn example_test() {
        use flexi_logger::*;
        Logger::try_with_env_or_str("trace")
            .map_err(|_e| enosys!())
            .unwrap()
            .format(colored_opt_format)
            .write_mode(WriteMode::SupportCapture)
            .start()
            .map_err(|e| eother!(e))
            .unwrap();
        debug!("some debug outputs");

Maybe a more elegant way is to reuse src/logger.rs#setup_logging(). But the logger.rs is placed in root package nydus-rs that cannot call from child packages such as nydus-storage. Move logger.rs to nydus-utils seems to be a good way.

adamqqqplay commented 1 year ago

Maybe you could try make ut-nextest, it uses cargo nextest which may provide better output than cargo test.

hangvane commented 1 year ago

Maybe you could try make ut-nextest, it uses cargo nextest which may provide better output than cargo test.

It works. Thanks!