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

Need an option to prevent cargo test from initializing logging #7743

Closed
dfoxfranke opened this issue Dec 24, 2019 · 1 comment
Closed

Need an option to prevent cargo test from initializing logging #7743

dfoxfranke opened this issue Dec 24, 2019 · 1 comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`

Comments

@dfoxfranke
Copy link

I'm trying to unit test program functionality that reopens log files on receiving a SIGHUP. My unit test does this:

        let tempdir = tempfile::TempDir::new().unwrap();
        let mut logpath = path::PathBuf::from(tempdir.path());
        let mut logpath_rotated = logpath.clone();
        logpath.push("logfile");
        logpath_rotated.push("logfile.old");
        let logconfig = vec![LogConfig::FileLog(logpath.clone(), log::LevelFilter::Info)];
        let handle = init_logging(&logconfig).unwrap();
        info!("PRE-ROTATION");
        fs::rename(&logpath, &logpath_rotated).unwrap();
        info!("MID-ROTATION");
        reinit_logging(&logconfig, &handle).unwrap();
        info!("POST-ROTATION");

My init_logging and reinit_logging functions call into my log provider, which is log4rs. After running the above code, I'd be rereading "logfile" to make sure it contains the "POST-ROTATION" message, and "logfile.old" to make sure it contains the "PRE-ROTATION" and "MID-ROTATION" messages.

Problem is, it doesn't work, because it panics on init_logging(&logconfig).unwrap(). log4rs is calling log::set_logger and getting back a SetLoggerError, because cargo test has already initialized before it ever calls into my test.

There seems to be no way around this without abandoning the cargo test framework altogether, since as of 0.4 the log crate no longer provides any mechanism for de-initializing logging. I'd like to be able to add some attribute like #[test_no_init_logging] to my unit test to tell cargo "don't touch".

@dfoxfranke dfoxfranke added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Dec 24, 2019
@ehuss
Copy link
Contributor

ehuss commented Dec 27, 2019

This is an inherent limitation of multi-threaded tests and using a global system like the log crate.

I don't offhand have any great suggestions for you. Some random things:

The first option above would probably be the easiest. You might want to follow up on one of the user forums to see if anyone has better ideas.

I'm going to close this since it isn't a Cargo issue. The test framework which handles tests (libtest) lives in https://github.com/rust-lang/rust/.

@ehuss ehuss closed this as completed Dec 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
Projects
None yet
Development

No branches or pull requests

2 participants