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

✨ feat(config): override config bools with env vars #9

Merged
merged 1 commit into from
Feb 5, 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
86 changes: 67 additions & 19 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,34 @@ pub struct Opt {
pub config: Option<String>,

/// Suppress progress messages.
#[arg(short = 'q', long, env = "GIT_SUMI_QUIET")]
pub quiet: bool,
#[arg(
short = 'q',
num_args = 0,
default_missing_value = "true",
long,
env = "GIT_SUMI_QUIET"
)]
pub quiet: Option<bool>,

/// Process each non-empty line as an individual commit.
#[arg(short = 's', long, env = "GIT_SUMI_SPLIT_LINES")]
pub split_lines: bool,
#[arg(
short = 's',
long,
env = "GIT_SUMI_SPLIT_LINES",
num_args = 0,
default_missing_value = "true"
)]
pub split_lines: Option<bool>,

/// Display the parsed commit message.
#[arg(short = 'd', long, env = "GIT_SUMI_DISPLAY")]
pub display: bool,
#[arg(
short = 'd',
long,
env = "GIT_SUMI_DISPLAY",
num_args = 0,
default_missing_value = "true"
)]
pub display: Option<bool>,

/// Specify the output format for displaying the parsed commit message.
/// Options: "cli", "table", "json", "toml". Default: "cli"
Expand All @@ -59,23 +77,47 @@ pub struct Opt {
pub force: bool,

/// Follow Conventional Commits format.
#[arg(short = 'C', long, env = "GIT_SUMI_CONVENTIONAL", default_value_ifs([
("types_allowed", ArgPredicate::IsPresent, Some("true")),
("scopes_allowed", ArgPredicate::IsPresent, Some("true")),
]), help_heading = "Rules")]
pub conventional: bool,
#[arg(short = 'C',
long,
env = "GIT_SUMI_CONVENTIONAL",
num_args = 0,
default_missing_value = "true",
default_value_ifs([
("types_allowed", ArgPredicate::IsPresent, Some("true")),
("scopes_allowed", ArgPredicate::IsPresent, Some("true")),
]),
help_heading = "Rules")]
pub conventional: Option<bool>,

/// Use the imperative mood in the description.
#[arg(short = 'I', long, env = "GIT_SUMI_IMPERATIVE", help_heading = "Rules")]
pub imperative: bool,
#[arg(
short = 'I',
long,
env = "GIT_SUMI_IMPERATIVE",
num_args = 0,
default_missing_value = "true"
)]
pub imperative: Option<bool>,

/// Include one valid Gitmoji.
#[arg(short = 'G', long, env = "GIT_SUMI_GITMOJI", help_heading = "Rules")]
pub gitmoji: bool,
#[arg(
short = 'G',
long,
env = "GIT_SUMI_GITMOJI",
num_args = 0,
default_missing_value = "true"
)]
pub gitmoji: Option<bool>,

/// Disallow leading/trailing whitespace and consecutive spaces.
#[arg(short = 'W', long, env = "GIT_SUMI_WHITESPACE", help_heading = "Rules")]
pub whitespace: bool,
#[arg(
short = 'W',
long,
env = "GIT_SUMI_WHITESPACE",
num_args = 0,
default_missing_value = "true"
)]
pub whitespace: Option<bool>,

/// Commit description must start with the selected case.
/// Options: "lower", "upper", "any". Default: "any".
Expand All @@ -89,8 +131,14 @@ pub struct Opt {
pub description_case: Option<DescriptionCase>,

/// Do not end commit header with a period.
#[arg(short = 'P', long, env = "GIT_SUMI_NO_PERIOD", help_heading = "Rules")]
pub no_period: bool,
#[arg(
short = 'P',
long,
env = "GIT_SUMI_NO_PERIOD",
num_args = 0,
default_missing_value = "true"
)]
pub no_period: Option<bool>,

/// Limit the header to the specified length.
#[arg(short = 'H', long, env = "GIT_SUMI_MAX_HEADER_LENGTH", value_parser = clap::value_parser!(usize), help_heading = "Rules")]
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ pub fn count_active_rules(config: &Config) -> usize {
/// - For lists, updates the `Config` field only if the list is not empty.
macro_rules! update_field {
($config_field:expr, $self_field:expr) => {
if $self_field {
$config_field = true;
if let Some(val) = $self_field {
$config_field = val;
}
};
($config_field:expr, $self_field:expr, option) => {
Expand Down
27 changes: 27 additions & 0 deletions tests/lint/test_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate tempfile;
use super::contains;
use super::run_isolated_git_sumi;
use super::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::tempdir;

Expand Down Expand Up @@ -159,6 +160,32 @@ fn error_fail_override_config_file_gitmoji() {
.stderr(contains("Header must contain exactly 1 emoji, found 2"));
}

#[test]
fn success_override_quiet_with_env_var() {
let mut cmd = Command::cargo_bin("git-sumi").unwrap();
cmd.env("GIT_SUMI_QUIET", "false") // Should override config.
.arg("--config")
.arg("tests/resources/good_config_ciqf.toml") // Sets quiet = true.
.arg("Commit message")
.assert()
.success()
.stdout(contains("Input"))
.stdout(contains("passed"));
}

#[test]
fn success_override_gitmoji_with_env_var() {
let mut cmd = Command::cargo_bin("git-sumi").unwrap();
cmd.env("GIT_SUMI_GITMOJI", "false") // Override gitmoji = true in config.
.arg("--config")
.arg("tests/resources/good_config_gitmoji.toml")
.arg("-d") // Needs a rule/display/commit.
.arg("refactor(HTML): remove unused code")
.assert()
.success()
.stderr(contains("Header must contain exactly 1 emoji").not());
}

#[test]
fn error_valid_config_file_imperative() {
let mut cmd = Command::cargo_bin("git-sumi").unwrap();
Expand Down
Loading