Skip to content

Commit

Permalink
setup unit tests for cli #700
Browse files Browse the repository at this point in the history
  • Loading branch information
o2sh committed Aug 14, 2022
1 parent 2d57637 commit c8c70ba
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 17 deletions.
73 changes: 67 additions & 6 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use regex::Regex;
use std::env;
use std::io;
use std::path::PathBuf;
use std::str::FromStr;
use strum::IntoEnumIterator;

#[derive(Parser)]
#[derive(Clone, Debug, Parser, PartialEq, Eq)]
#[clap(version, about, long_about = None, rename_all = "kebab-case")]
#[clap(global_setting(AppSettings::DeriveDisplayOrder))]
pub struct Config {
Expand Down Expand Up @@ -89,7 +90,7 @@ pub struct Config {
pub exclude: Vec<PathBuf>,
/// Exclude [bot] commits. Use <REGEX> to override the default pattern
#[clap(long, value_name = "REGEX")]
pub no_bots: Option<Option<Regex>>,
pub no_bots: Option<Option<MyRegex>>,
/// Prints out supported languages
#[clap(long, short)]
pub languages: bool,
Expand Down Expand Up @@ -126,13 +127,13 @@ pub struct Config {
)]
pub text_colors: Vec<u8>,
/// Use ISO 8601 formatted timestamps
#[clap(long, short = 'z', action)]
#[clap(long, short = 'z')]
pub iso_time: bool,
/// Show the email address of each author
#[clap(long, short = 'E', action)]
#[clap(long, short = 'E')]
pub email: bool,
/// Count hidden files and directories
#[clap(long, action)]
#[clap(long)]
pub include_hidden: bool,
/// Filters output by language type
#[clap(
Expand Down Expand Up @@ -183,9 +184,69 @@ pub fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
}

#[derive(clap::ValueEnum, Clone)]
#[derive(clap::ValueEnum, Clone, PartialEq, Eq, Debug)]
pub enum When {
Auto,
Never,
Always,
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_default_config() {
let config = get_default_config();
assert_eq!(config, Config::parse_from(&[""]))
}

fn get_default_config() -> Config {
Config {
input: PathBuf::from("."),
ascii_input: Default::default(),
ascii_language: Default::default(),
ascii_colors: Default::default(),
disabled_fields: Default::default(),
image: Default::default(),
image_protocol: Default::default(),
color_resolution: 16,
no_bold: Default::default(),
no_merges: Default::default(),
no_color_palette: Default::default(),
number_of_authors: 3,
exclude: Default::default(),
no_bots: Default::default(),
languages: Default::default(),
package_managers: Default::default(),
output: Default::default(),
true_color: When::Auto,
show_logo: When::Always,
text_colors: Default::default(),
iso_time: Default::default(),
email: Default::default(),
include_hidden: Default::default(),
r#type: vec![LanguageType::Programming, LanguageType::Markup],
completion: Default::default(),
}
}
}

#[derive(Clone, Debug)]
pub struct MyRegex(pub Regex);

impl Eq for MyRegex {}

impl PartialEq for MyRegex {
fn eq(&self, other: &MyRegex) -> bool {
self.0.as_str() == other.0.as_str()
}
}

impl FromStr for MyRegex {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self> {
Ok(MyRegex(Regex::new(s)?))
}
}
1 change: 1 addition & 0 deletions src/info/deps/package_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ fn pub_packages(contents: &str) -> Result<usize> {
Err(_) => Ok(0),
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/info/info_field.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Clone, clap::ValueEnum)]
#[derive(Clone, clap::ValueEnum, Debug, Eq, PartialEq)]
pub enum InfoField {
GitInfo,
Project,
Expand Down
4 changes: 2 additions & 2 deletions src/info/langs/language.tera
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ pub struct Colors {
true_colors: Option<Vec<DynColors>>,
}

#[derive(Clone, PartialEq, clap::ValueEnum)]
#[derive(Clone, PartialEq, Eq, Debug, clap::ValueEnum)]
pub enum LanguageType {
Programming,
Markup,
Prose,
Data,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, EnumIter, clap::ValueEnum)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, EnumIter, clap::ValueEnum, Debug)]
#[allow(clippy::upper_case_acronyms)]
#[clap(rename_all = "lowercase")]
pub enum Language {
Expand Down
4 changes: 2 additions & 2 deletions src/info/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli::{self, is_truecolor_terminal, Config, When};
use crate::cli::{self, is_truecolor_terminal, Config, MyRegex, When};
use crate::info::info_field::InfoFieldOff;
use crate::ui::get_ascii_colors;
use crate::ui::text_colors::TextColors;
Expand Down Expand Up @@ -196,7 +196,7 @@ impl Info {
let no_bots = if let Some(r) = config.no_bots.clone() {
match r {
Some(p) => Some(p),
None => Some(Regex::from_str(r"(b|B)ot")?),
None => Some(MyRegex(Regex::from_str(r"(b|B)ot")?)),
}
} else {
None
Expand Down
8 changes: 4 additions & 4 deletions src/info/repo.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::cli::MyRegex;
use crate::info::author::Author;
use crate::info::head_refs::HeadRefs;
use anyhow::{Context, Result};
Expand All @@ -6,7 +7,6 @@ use git::bstr::BString;
use git2::{Repository, RepositoryOpenFlags, Status, StatusOptions, StatusShow};
use git_repository as git;
use git_repository::bstr::ByteSlice;
use regex::Regex;
use std::collections::HashMap;
use std::path::Path;
use std::path::PathBuf;
Expand Down Expand Up @@ -45,7 +45,7 @@ impl Commits {
pub fn new(
mut repo: git::Repository,
no_merges: bool,
bot_regex_pattern: &Option<Regex>,
bot_regex_pattern: &Option<MyRegex>,
number_of_authors_to_display: usize,
) -> Result<Self> {
// assure that objects we just traversed are coming from cache
Expand Down Expand Up @@ -271,10 +271,10 @@ impl Repo {
}
}

fn is_bot(author_name: &BString, bot_regex_pattern: &Option<Regex>) -> bool {
fn is_bot(author_name: &BString, bot_regex_pattern: &Option<MyRegex>) -> bool {
bot_regex_pattern
.as_ref()
.map(|regex| regex.is_match(author_name.to_str_lossy().as_ref()))
.map(|regex| regex.0.is_match(author_name.to_str_lossy().as_ref()))
.unwrap_or(false)
}

Expand Down
2 changes: 1 addition & 1 deletion src/ui/image_backends/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use image::DynamicImage;

#[derive(clap::ValueEnum, Clone)]
#[derive(clap::ValueEnum, Clone, PartialEq, Eq, Debug)]
pub enum ImageProtocol {
Kitty,
Sixel,
Expand Down
2 changes: 1 addition & 1 deletion src/ui/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use terminal_size::{terminal_size, Width};
const CENTER_PAD_LENGTH: usize = 3;
const MAX_TERM_WIDTH: u16 = 95;

#[derive(Clone, clap::ValueEnum)]
#[derive(Clone, clap::ValueEnum, PartialEq, Eq, Debug)]
pub enum SerializationFormat {
Json,
Yaml,
Expand Down

0 comments on commit c8c70ba

Please sign in to comment.