summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2020-12-03 19:18:44 -0500
committerGitHub <noreply@github.com>2020-12-03 19:18:44 -0500
commitd68001471ddaf231911b29b8d6c6d20dd555cc2e (patch)
tree292e3818fe14cba2190b7767b46aac503fe4f4b6
parent0ddd0648190f2d0497a7f3a9e89b26d04186feaf (diff)
Add test coverage of main.rs (#423)
* Refactor: pass handle to writer * Add test of list_syntax_themes * Add test of show_syntax_themes * Refactor: pass writer handle * Add test of show_config * Exclude main function from coverage calculations * DEBUGGING * Refactor: only use stdin if non-empty Motivation: in the CI test build, atty::is(stdin) seems to be returning false.
-rw-r--r--.github/workflows/ci.yml2
-rw-r--r--src/main.rs154
2 files changed, 115 insertions, 41 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index bcad4b42..e09da869 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -31,7 +31,7 @@ jobs:
with:
command: test
use-cross: ${{ matrix.job.use-cross }}
- args: --target ${{ matrix.job.target }} --verbose
+ args: --target ${{ matrix.job.target }} --verbose -- --nocapture
integration_tests:
name: Integration tests
diff --git a/src/main.rs b/src/main.rs
index 69154f30..cae7e696 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -48,6 +48,7 @@ pub mod errors {
}
}
+#[cfg(not(tarpaulin_include))]
fn main() -> std::io::Result<()> {
let assets = HighlightingAssets::new();
let opt = cli::Opt::from_args_and_git_config(&mut git_config::GitConfig::try_create(), assets);
@@ -67,7 +68,9 @@ fn main() -> std::io::Result<()> {
let config = config::Config::from(opt);
if _show_config {
- show_config(&config);
+ let stdout = io::stdout();
+ let mut stdout = stdout.lock();
+ show_config(&config, &mut stdout)?;
process::exit(0);
} else if atty::is(atty::Stream::Stdin) {
return diff(
@@ -129,9 +132,10 @@ fn diff(
Ok(())
}
-fn show_config(config: &config::Config) {
+fn show_config(config: &config::Config, writer: &mut dyn Write) -> std::io::Result<()> {
// styles first
- println!(
+ writeln!(
+ writer,
" commit-style = {commit_style}
file-style = {file_style}
hunk-header-style = {hunk_header_style}
@@ -158,9 +162,10 @@ fn show_config(config: &config::Config) {
plus_style = config.plus_style.to_painted_string(),
whitespace_error_style = config.whitespace_error_style.to_painted_string(),
zero_style = config.zero_style.to_painted_string(),
- );
+ )?;
// Everything else
- println!(
+ writeln!(
+ writer,
" 24-bit-color = {true_color}
file-added-label = {file_added_label}
file-modified-label = {file_modified_label}
@@ -171,18 +176,21 @@ fn show_config(config: &config::Config) {
file_modified_label = format_option_value(&config.file_modified_label),
file_removed_label = format_option_value(&config.file_removed_label),
file_renamed_label = format_option_value(&config.file_renamed_label),
- );
- println!(
+ )?;
+ writeln!(
+ writer,
" hyperlinks = {hyperlinks}",
hyperlinks = config.hyperlinks
- );
+ )?;
if config.hyperlinks {
- println!(
+ writeln!(
+ writer,
" hyperlinks-file-link-format = {hyperlinks_file_link_format}",
hyperlinks_file_link_format = format_option_value(&config.hyperlinks_file_link_format),
- )
+ )?
}
- println!(
+ writeln!(
+ writer,
" inspect-raw-lines = {inspect_raw_lines}
keep-plus-minus-markers = {keep_plus_minus_markers}",
inspect_raw_lines = match config.inspect_raw_lines {
@@ -190,13 +198,15 @@ fn show_config(config: &config::Config) {
cli::InspectRawLines::False => "false",
},
keep_plus_minus_markers = config.keep_plus_minus_markers,
- );
- println!(
+ )?;
+ writeln!(
+ writer,
" line-numbers = {line_numbers}",
line_numbers = config.line_numbers
- );
+ )?;
if config.line_numbers {
- println!(
+ writeln!(
+ writer,
" line-numbers-minus-style = {line_numbers_minus_style}
line-numbers-zero-style = {line_numbers_zero_style}
line-numbers-plus-style = {line_numbers_plus_style}
@@ -211,9 +221,10 @@ fn show_config(config: &config::Config) {
line_numbers_right_style = config.line_numbers_right_style.to_painted_string(),
line_numbers_left_format = format_option_value(&config.line_numbers_left_format),
line_numbers_right_format = format_option_value(&config.line_numbers_right_format),
- )
+ )?
}
- println!(
+ writeln!(
+ writer,
" max-line-distance = {max_line_distance}
max-line-length = {max_line_length}
navigate = {navigate}
@@ -243,7 +254,8 @@ fn show_config(config: &config::Config) {
},
tab_width = config.tab_width,
tokenization_regex = format_option_value(&config.tokenization_regex.to_string()),
- );
+ )?;
+ Ok(())
}
// Heuristics determining whether to quote string option values when printing values intended for
@@ -264,6 +276,7 @@ where
}
}
+#[cfg(not(tarpaulin_include))]
fn show_syntax_themes() -> std::io::Result<()> {
let mut opt = cli::Opt::from_args();
let assets = HighlightingAssets::new();
@@ -294,12 +307,7 @@ fn _show_syntax_themes(
) -> std::io::Result<()> {
use bytelines::ByteLines;
use std::io::BufReader;
- let input = if !atty::is(atty::Stream::Stdin) {
- let mut buf = Vec::new();
- io::stdin().lock().read_to_end(&mut buf)?;
- buf
- } else {
- b"\
+ let mut input = b"\
diff --git a/example.rs b/example.rs
index f38589a..0f1bb83 100644
--- a/example.rs
@@ -314,7 +322,13 @@ index f38589a..0f1bb83 100644
+ let result = f64::powf(num, 3.0);
+ println!(\"The cube of {:.2} is {:.2}.\", num, result);
"
- .to_vec()
+ .to_vec();
+ if !atty::is(atty::Stream::Stdin) {
+ let mut buf = Vec::new();
+ io::stdin().lock().read_to_end(&mut buf)?;
+ if !buf.is_empty() {
+ input = buf;
+ }
};
opt.computed.is_light_mode = is_light_mode;
@@ -341,47 +355,46 @@ index f38589a..0f1bb83 100644
Ok(())
}
+#[cfg(not(tarpaulin_include))]
pub fn list_syntax_themes() -> std::io::Result<()> {
+ let stdout = io::stdout();
+ let mut stdout = stdout.lock();
if atty::is(atty::Stream::Stdout) {
- _list_syntax_themes_for_humans()
+ _list_syntax_themes_for_humans(&mut stdout)
} else {
- _list_syntax_themes_for_machines()
+ _list_syntax_themes_for_machines(&mut stdout)
}
}
-pub fn _list_syntax_themes_for_humans() -> std::io::Result<()> {
+pub fn _list_syntax_themes_for_humans(writer: &mut dyn Write) -> std::io::Result<()> {
let assets = HighlightingAssets::new();
let themes = &assets.theme_set.themes;
- let stdout = io::stdout();
- let mut stdout = stdout.lock();
- writeln!(stdout, "Light themes:")?;
+ writeln!(writer, "Light themes:")?;
for (theme, _) in themes.iter().filter(|(t, _)| is_light_syntax_theme(*t)) {
- writeln!(stdout, " {}", theme)?;
+ writeln!(writer, " {}", theme)?;
}
- writeln!(stdout, "\nDark themes:")?;
+ writeln!(writer, "\nDark themes:")?;
for (theme, _) in themes.iter().filter(|(t, _)| !is_light_syntax_theme(*t)) {
- writeln!(stdout, " {}", theme)?;
+ writeln!(writer, " {}", theme)?;
}
writeln!(
- stdout,
+ writer,
"\nUse delta --show-syntax-themes to demo the themes."
)?;
Ok(())
}
-pub fn _list_syntax_themes_for_machines() -> std::io::Result<()> {
+pub fn _list_syntax_themes_for_machines(writer: &mut dyn Write) -> std::io::Result<()> {
let assets = HighlightingAssets::new();
let themes = &assets.theme_set.themes;
- let stdout = io::stdout();
- let mut stdout = stdout.lock();
for (theme, _) in themes
.iter()
.sorted_by_key(|(t, _)| is_light_syntax_theme(*t))
{
writeln!(
- stdout,
- "{:5}\t{}",
+ writer,
+ "{}\t{}",
if is_light_syntax_theme(theme) {
"light"
} else {
@@ -392,3 +405,64 @@ pub fn _list_syntax_themes_for_machines() -> std::io::Result<()> {
}
Ok(())
}
+
+#[cfg(test)]
+mod main_tests {
+ use super::*;
+ use std::io::{Cursor, Seek, SeekFrom};
+
+ use crate::ansi;
+ use crate::tests::integration_test_utils::integration_test_utils;
+
+ #[test]
+ fn test_show_config() {
+ let config = integration_test_utils::make_config_from_args(&[]);
+ let mut writer = Cursor::new(vec![0; 1024]);
+ show_config(&config, &mut writer).unwrap();
+ let mut s = String::new();
+ writer.seek(SeekFrom::Start(0)).unwrap();
+ writer.read_to_string(&mut s).unwrap();
+ let s = ansi::strip_ansi_codes(&s);
+ assert!(s.contains(" commit-style = raw\n"));
+ assert!(s.contains(r" word-diff-regex = '\w+'"));
+ }
+
+ #[test]
+ fn test_show_syntax_themes() {
+ let opt = integration_test_utils::make_options_from_args(&[]);
+
+ let mut writer = Cursor::new(vec![0; 1024]);
+ _show_syntax_themes(opt, true, &mut writer).unwrap();
+ let mut s = String::new();
+ writer.seek(SeekFrom::Start(0)).unwrap();
+ writer.read_to_string(&mut s).unwrap();
+ let s = ansi::strip_ansi_codes(&s);
+ assert!(s.contains("\nTheme: gruvbox-white\n"));
+ println!("{}", s);
+ assert!(s.contains("\nfn print_cube(num: f64) {\n"));
+ }
+
+ #[test]
+ fn test_list_syntax_themes_for_humans() {
+ let mut writer = Cursor::new(vec![0; 512]);
+ _list_syntax_themes_for_humans(&mut writer).unwrap();
+ let mut s = String::new();
+ writer.seek(SeekFrom::Start(0)).unwrap();
+ writer.read_to_string(&mut s).unwrap();
+ assert!(s.contains("Light themes:\n"));
+ assert!(s.contains(" GitHub\n"));
+ assert!(s.contains("Dark themes:\n"));
+ assert!(s.contains(" Dracula\n"));
+ }
+
+ #[test]
+ fn test_list_syntax_themes_for_machines() {
+ let mut writer = Cursor::new(vec![0; 512]);
+ _list_syntax_themes_for_machines(&mut writer).unwrap();
+ let mut s = String::new();
+ writer.seek(SeekFrom::Start(0)).unwrap();
+ writer.read_to_string(&mut s).unwrap();
+ assert!(s.contains("light GitHub\n"));
+ assert!(s.contains("dark Dracula\n"));
+ }
+}