summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2021-12-11 21:44:45 -0500
committerDan Davison <dandavison7@gmail.com>2021-12-12 00:20:52 -0500
commitf88f0a4edae61ee735894c4d8d8679b2c2f06796 (patch)
tree4cfdfa68fa2dbdde02996e34ce3e4b2372b9f07a
parent9d1b25ac43da9f66ab6fdcfeb1e68f74bec2ce6d (diff)
Fix tests: don't access lazy_static in tests
-rw-r--r--src/config.rs4
-rw-r--r--src/handlers/hunk.rs34
2 files changed, 26 insertions, 12 deletions
diff --git a/src/config.rs b/src/config.rs
index b2076184..61369cc0 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -292,7 +292,7 @@ impl From<cli::Opt> for Config {
} else {
line_fill_method
},
- line_numbers: opt.line_numbers && !*handlers::hunk::IS_WORD_DIFF,
+ line_numbers: opt.line_numbers && !handlers::hunk::is_word_diff(),
line_numbers_format: LeftRight::new(
opt.line_numbers_left_format,
opt.line_numbers_right_format,
@@ -351,7 +351,7 @@ impl From<cli::Opt> for Config {
git_plus_style: styles["git-plus-style"],
relative_paths: opt.relative_paths,
show_themes: opt.show_themes,
- side_by_side: opt.side_by_side && !*handlers::hunk::IS_WORD_DIFF,
+ side_by_side: opt.side_by_side && !handlers::hunk::is_word_diff(),
side_by_side_data,
styles_map,
syntax_dummy_theme: SyntaxTheme::default(),
diff --git a/src/handlers/hunk.rs b/src/handlers/hunk.rs
index e08641fb..f7aedb5f 100644
--- a/src/handlers/hunk.rs
+++ b/src/handlers/hunk.rs
@@ -9,19 +9,36 @@ use crate::style;
use crate::utils::process::{self, CallingProcess};
use unicode_segmentation::UnicodeSegmentation;
+// HACK: WordDiff should probably be a distinct top-level line state
+pub fn is_word_diff() -> bool {
+ #[cfg(not(test))]
+ {
+ *CACHED_IS_WORD_DIFF
+ }
+ #[cfg(test)]
+ {
+ compute_is_word_diff()
+ }
+}
+
lazy_static! {
- pub static ref IS_WORD_DIFF: bool = match process::calling_process().as_deref() {
+ static ref CACHED_IS_WORD_DIFF: bool = compute_is_word_diff();
+}
+
+fn compute_is_word_diff() -> bool {
+ match process::calling_process().as_deref() {
Some(
CallingProcess::GitDiff(cmd_line)
| CallingProcess::GitShow(cmd_line, _)
| CallingProcess::GitLog(cmd_line)
| CallingProcess::GitReflog(cmd_line),
- ) =>
+ ) => {
cmd_line.long_options.contains("--word-diff")
|| cmd_line.long_options.contains("--word-diff-regex")
- || cmd_line.long_options.contains("--color-words"),
+ || cmd_line.long_options.contains("--color-words")
+ }
_ => false,
- };
+ }
}
impl<'a> StateMachine<'a> {
@@ -95,8 +112,7 @@ impl<'a> StateMachine<'a> {
// sequence of consecutive minus (removed) and/or plus (added) lines). Process that
// subhunk and flush the line buffers.
self.painter.paint_buffered_minus_and_plus_lines();
- let n_parents = if *IS_WORD_DIFF {
- // HACK: WordDiff should probably be a distinct top-level line state
+ let n_parents = if is_word_diff() {
0
} else {
diff_type.n_parents()
@@ -124,8 +140,7 @@ impl<'a> StateMachine<'a> {
}
fn maybe_raw_line(&self, n_parents: usize, non_raw_styles: &[style::Style]) -> Option<String> {
- // HACK: WordDiff should probably be a distinct top-level line state
- let emit_raw_line = *IS_WORD_DIFF
+ let emit_raw_line = is_word_diff()
|| self.config.inspect_raw_lines == cli::InspectRawLines::True
&& style::line_has_style_other_than(&self.raw_line, non_raw_styles);
if emit_raw_line {
@@ -143,8 +158,7 @@ fn new_line_state(new_line: &str, prev_state: &State) -> Option<State> {
use MergeParents::*;
use State::*;
- if *IS_WORD_DIFF {
- // HACK: WordDiff should probably be a distinct top-level line state
+ if is_word_diff() {
return Some(HunkZero(Unified, None));
}