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

Disable light mode detection in tests #1765

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/features/side_by_side.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,6 @@ pub mod ansifill {
pub mod tests {
use crate::ansi::strip_ansi_codes;
use crate::features::line_numbers::tests::*;
use crate::options::theme;
use crate::tests::integration_test_utils::{make_config_from_args, run_delta, DeltaTest};
use insta::assert_snapshot;

Expand Down Expand Up @@ -646,7 +645,6 @@ pub mod tests {

#[test]
fn test_two_plus_lines_spaces_and_ansi() {
let _override = theme::test_utils::DetectLightModeOverride::new(false);
DeltaTest::with_args(&[
"--side-by-side",
"--width",
Expand Down
37 changes: 6 additions & 31 deletions src/options/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,54 +125,29 @@ fn should_detect_dark_light(opt: &cli::Opt) -> bool {
}
}

#[cfg(not(test))]
fn detect_light_mode() -> bool {
use terminal_colorsaurus::{color_scheme, ColorScheme, QueryOptions};

#[cfg(test)]
if let Some(value) = test_utils::DETECT_LIGHT_MODE_OVERRIDE.get() {
return value;
}

color_scheme(QueryOptions::default()).unwrap_or_default() == ColorScheme::Light
}

#[cfg(test)]
pub(crate) mod test_utils {
thread_local! {
pub(super) static DETECT_LIGHT_MODE_OVERRIDE: std::cell::Cell<Option<bool>> = const { std::cell::Cell::new(None) };
}

pub(crate) struct DetectLightModeOverride {
old_value: Option<bool>,
}

impl DetectLightModeOverride {
pub(crate) fn new(value: bool) -> Self {
let old_value = DETECT_LIGHT_MODE_OVERRIDE.get();
DETECT_LIGHT_MODE_OVERRIDE.set(Some(value));
DetectLightModeOverride { old_value }
}
}

impl Drop for DetectLightModeOverride {
fn drop(&mut self) {
DETECT_LIGHT_MODE_OVERRIDE.set(self.old_value)
}
}
fn detect_light_mode() -> bool {
LIGHT_MODE_IN_TESTS
}

#[cfg(test)]
pub(crate) const LIGHT_MODE_IN_TESTS: bool = false;

#[cfg(test)]
mod tests {
use super::test_utils::DetectLightModeOverride;
use super::*;
use crate::color;
use crate::tests::integration_test_utils;

// TODO: Test influence of BAT_THEME env var. E.g. see utils::process::tests::FakeParentArgs.
#[test]
fn test_syntax_theme_selection() {
let _override = DetectLightModeOverride::new(false);

#[derive(PartialEq)]
enum Mode {
Light,
Expand Down
Loading