From 69cbfc8375a00b41cae2128727d2348b55f8c101 Mon Sep 17 00:00:00 2001
From: Samuel Marks <807580+SamuelMarks@users.noreply.github.com>
Date: Sun, 7 Jul 2024 23:25:02 -0400
Subject: [PATCH 01/21] [src/lib/{mod,cli,cli_parser,runner,descriptor/mod}.rs]
Make `pub`lic useful symbols for use `cargo-make` as library
---
src/lib/cli.rs | 2 +-
src/lib/cli_parser.rs | 2 +-
src/lib/descriptor/mod.rs | 2 +-
src/lib/mod.rs | 2 +-
src/lib/runner.rs | 2 +-
5 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/src/lib/cli.rs b/src/lib/cli.rs
index d0e74358..9032bc82 100644
--- a/src/lib/cli.rs
+++ b/src/lib/cli.rs
@@ -170,7 +170,7 @@ fn run(cli_args: CliArgs, global_config: &GlobalConfig) {
}
/// Handles the command line arguments and executes the runner.
-pub(crate) fn run_cli(command_name: String, sub_command: bool) {
+pub fn run_cli(command_name: String, sub_command: bool) {
let global_config = config::load();
let cli_args = cli_parser::parse(&global_config, &command_name, sub_command);
diff --git a/src/lib/cli_parser.rs b/src/lib/cli_parser.rs
index 4169a788..1c756f12 100644
--- a/src/lib/cli_parser.rs
+++ b/src/lib/cli_parser.rs
@@ -500,7 +500,7 @@ pub(crate) fn parse_args(
Err(error) => {
let help_text = cliparser::help(&spec);
println!("{}\n{}", &error, help_text);
- exit();
+ exit()
}
}
}
diff --git a/src/lib/descriptor/mod.rs b/src/lib/descriptor/mod.rs
index 882351f1..040ecaf1 100755
--- a/src/lib/descriptor/mod.rs
+++ b/src/lib/descriptor/mod.rs
@@ -578,7 +578,7 @@ fn load_cargo_aliases(config: &mut Config) {
/// the default descriptor.
If one of the descriptor requires a newer
/// version of cargo-make, returns an error with the minimum version required by
/// the descriptor.
-pub(crate) fn load(
+pub fn load(
file_name: &str,
force: bool,
env_map: Option>,
diff --git a/src/lib/mod.rs b/src/lib/mod.rs
index c98aad53..a7706856 100755
--- a/src/lib/mod.rs
+++ b/src/lib/mod.rs
@@ -68,7 +68,7 @@ mod plugin;
mod profile;
mod proxy_task;
mod recursion_level;
-mod runner;
+pub mod runner;
mod scriptengine;
mod storage;
mod time_summary;
diff --git a/src/lib/runner.rs b/src/lib/runner.rs
index c774314a..0ba358b3 100755
--- a/src/lib/runner.rs
+++ b/src/lib/runner.rs
@@ -609,7 +609,7 @@ fn run_protected_flow(flow_info: &FlowInfo, flow_state: Rc>)
///
/// * Create an execution plan based on the requested task and its dependencies
/// * Run all tasks defined in the execution plan
-pub(crate) fn run(
+pub fn run(
config: Config,
task: &str,
env_info: EnvInfo,
From 6cf59426a6e33ebaf5a769f88f5dda51a7e4cc1f Mon Sep 17 00:00:00 2001
From: Samuel Marks <807580+SamuelMarks@users.noreply.github.com>
Date: Mon, 8 Jul 2024 12:44:38 -0400
Subject: [PATCH 02/21] [src/lib/**.rs] Make `pub`lic useful symbols for use
`cargo-make` as library
---
src/lib/cli.rs | 2 +-
src/lib/cli_commands/list_steps.rs | 4 ++--
src/lib/cli_commands/mod.rs | 4 ++--
src/lib/cli_commands/print_steps.rs | 2 +-
src/lib/cli_parser.rs | 6 +++---
src/lib/config.rs | 8 ++++----
src/lib/mod.rs | 8 ++++----
7 files changed, 17 insertions(+), 17 deletions(-)
diff --git a/src/lib/cli.rs b/src/lib/cli.rs
index 9032bc82..7760b974 100644
--- a/src/lib/cli.rs
+++ b/src/lib/cli.rs
@@ -31,7 +31,7 @@ pub(crate) static DEFAULT_LOG_LEVEL: &str = "info";
pub(crate) static DEFAULT_TASK_NAME: &str = "default";
pub(crate) static DEFAULT_OUTPUT_FORMAT: &str = "default";
-fn run(cli_args: CliArgs, global_config: &GlobalConfig) {
+pub fn run(cli_args: CliArgs, global_config: &GlobalConfig) {
let start_time = SystemTime::now();
recursion_level::increment();
diff --git a/src/lib/cli_commands/list_steps.rs b/src/lib/cli_commands/list_steps.rs
index daf13908..a49f7c15 100644
--- a/src/lib/cli_commands/list_steps.rs
+++ b/src/lib/cli_commands/list_steps.rs
@@ -13,7 +13,7 @@ use crate::io;
use crate::types::{Config, DeprecationInfo};
use std::collections::{BTreeMap, BTreeSet};
-pub(crate) fn run(
+pub fn run(
config: &Config,
output_format: &str,
output_file: &Option,
@@ -186,7 +186,7 @@ pub(crate) fn create_list(
}
if !just_task_name {
- buffer.push_str(&format!("\n"));
+ buffer.push('\n');
}
}
diff --git a/src/lib/cli_commands/mod.rs b/src/lib/cli_commands/mod.rs
index 85208434..a72e91ef 100644
--- a/src/lib/cli_commands/mod.rs
+++ b/src/lib/cli_commands/mod.rs
@@ -4,5 +4,5 @@
//!
pub(crate) mod diff_steps;
-pub(crate) mod list_steps;
-pub(crate) mod print_steps;
+pub mod list_steps;
+pub mod print_steps;
diff --git a/src/lib/cli_commands/print_steps.rs b/src/lib/cli_commands/print_steps.rs
index cdc5a2ff..c4540af4 100644
--- a/src/lib/cli_commands/print_steps.rs
+++ b/src/lib/cli_commands/print_steps.rs
@@ -61,7 +61,7 @@ fn print_default(execution_plan: &ExecutionPlan) {
}
/// Only prints the execution plan
-pub(crate) fn print(
+pub fn print(
config: &Config,
task: &str,
output_format: &str,
diff --git a/src/lib/cli_parser.rs b/src/lib/cli_parser.rs
index 1c756f12..1ab7306d 100644
--- a/src/lib/cli_parser.rs
+++ b/src/lib/cli_parser.rs
@@ -146,7 +146,7 @@ fn get_args(
cli_args
}
-fn create_cli(global_config: &GlobalConfig) -> CliSpec {
+pub fn create_cli(global_config: &GlobalConfig) -> CliSpec {
let default_task_name = match global_config.default_task_name {
Some(ref value) => value.as_str(),
None => &DEFAULT_TASK_NAME,
@@ -468,7 +468,7 @@ fn create_cli(global_config: &GlobalConfig) -> CliSpec {
spec
}
-pub(crate) fn parse_args(
+pub fn parse_args(
global_config: &GlobalConfig,
command_name: &str,
sub_command: bool,
@@ -505,7 +505,7 @@ pub(crate) fn parse_args(
}
}
-pub(crate) fn parse(
+pub fn parse(
global_config: &GlobalConfig,
command_name: &str,
sub_command: bool,
diff --git a/src/lib/config.rs b/src/lib/config.rs
index 8e4498d6..ea1a1957 100644
--- a/src/lib/config.rs
+++ b/src/lib/config.rs
@@ -13,14 +13,14 @@ use fsio::file::read_text_file;
use fsio::path::from_path::FromPath;
use std::path::{Path, PathBuf};
-static CONFIG_FILE: &'static str = "config.toml";
+pub static CONFIG_FILE: &'static str = "config.toml";
-fn get_config_directory() -> Option {
+pub fn get_config_directory() -> Option {
let os_directory = dirs_next::config_dir();
storage::get_storage_directory(os_directory, CONFIG_FILE, true)
}
-fn load_from_path(directory: PathBuf) -> GlobalConfig {
+pub fn load_from_path(directory: PathBuf) -> GlobalConfig {
let file_path = Path::new(&directory).join(CONFIG_FILE);
debug!("Loading config from: {:#?}", &file_path);
@@ -47,7 +47,7 @@ fn load_from_path(directory: PathBuf) -> GlobalConfig {
}
/// Returns the configuration
-pub(crate) fn load() -> GlobalConfig {
+pub fn load() -> GlobalConfig {
match get_config_directory() {
Some(directory) => load_from_path(directory),
None => GlobalConfig::new(),
diff --git a/src/lib/mod.rs b/src/lib/mod.rs
index a7706856..917a0cba 100755
--- a/src/lib/mod.rs
+++ b/src/lib/mod.rs
@@ -50,12 +50,12 @@ mod test;
pub mod types;
mod cache;
-mod cli;
-mod cli_commands;
-mod cli_parser;
+pub mod cli;
+pub mod cli_commands;
+pub mod cli_parser;
mod command;
mod condition;
-mod config;
+pub mod config;
mod descriptor;
mod environment;
mod execution_plan;
From 2234422b060ab8410323dd09ff6fecb42e5f24c9 Mon Sep 17 00:00:00 2001
From: Samuel Marks <807580+SamuelMarks@users.noreply.github.com>
Date: Thu, 11 Jul 2024 21:52:15 -0400
Subject: [PATCH 03/21] [src/lib/*.rs] Percolate exit codes around
---
Cargo.lock | 1 +
Cargo.toml | 3 +-
src/lib/cli.rs | 11 +++--
src/lib/cli_parser.rs | 26 ++++-------
src/lib/cli_parser_test.rs | 96 +++++++++++++++++++-------------------
src/lib/cli_test.rs | 35 ++++++++------
src/lib/logger.rs | 15 ++----
src/lib/mod.rs | 4 +-
8 files changed, 96 insertions(+), 95 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
index 60d35463..afccbe98 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -150,6 +150,7 @@ dependencies = [
"dirs-next",
"duckscript",
"duckscriptsdk",
+ "either",
"envmnt",
"expect-test",
"fern",
diff --git a/Cargo.toml b/Cargo.toml
index cb75a76f..1742039a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -52,15 +52,16 @@ ctrlc = "^3"
dirs-next = "^2"
duckscript = "^0.8.0"
duckscriptsdk = { version = "^0.9.3", default-features = false }
+either = "1.13.0"
envmnt = "^0.10.4"
fern = "^0.6"
fsio = { version = "^0.4", features = ["temp-path"] }
git_info = "^0.1.2"
glob = "^0.3.1"
home = "^0.5"
+ignore = "^0.4"
indexmap = { version = "^2", features = ["serde"] }
itertools = "^0.13"
-ignore = "^0.4"
lenient_semver = "^0.4.2"
log = "^0.4"
once_cell = "^1.19.0"
diff --git a/src/lib/cli.rs b/src/lib/cli.rs
index 7760b974..9fed804d 100644
--- a/src/lib/cli.rs
+++ b/src/lib/cli.rs
@@ -22,6 +22,7 @@ use crate::toolchain;
use crate::types::{CliArgs, GlobalConfig};
use crate::version;
use std::time::SystemTime;
+use either::{Either, try_left};
pub(crate) static VERSION: &str = env!("CARGO_PKG_VERSION");
pub(crate) static AUTHOR: &str = env!("CARGO_PKG_AUTHORS");
@@ -31,7 +32,7 @@ pub(crate) static DEFAULT_LOG_LEVEL: &str = "info";
pub(crate) static DEFAULT_TASK_NAME: &str = "default";
pub(crate) static DEFAULT_OUTPUT_FORMAT: &str = "default";
-pub fn run(cli_args: CliArgs, global_config: &GlobalConfig) {
+pub fn run(cli_args: CliArgs, global_config: &GlobalConfig) -> Either {
let start_time = SystemTime::now();
recursion_level::increment();
@@ -167,13 +168,15 @@ pub fn run(cli_args: CliArgs, global_config: &GlobalConfig) {
time_summary_vec,
);
}
+ Either::Right(std::process::ExitCode::SUCCESS)
}
/// Handles the command line arguments and executes the runner.
-pub fn run_cli(command_name: String, sub_command: bool) {
+pub fn run_cli(command_name: String, sub_command: bool) -> Either {
let global_config = config::load();
- let cli_args = cli_parser::parse(&global_config, &command_name, sub_command);
+ let cli_args = try_left!(cli_parser::parse(&global_config, &command_name, sub_command));
- run(cli_args, &global_config);
+ run(cli_args.clone(), &global_config);
+ Either::Left(cli_args)
}
diff --git a/src/lib/cli_parser.rs b/src/lib/cli_parser.rs
index 1ab7306d..eafc2639 100644
--- a/src/lib/cli_parser.rs
+++ b/src/lib/cli_parser.rs
@@ -17,18 +17,8 @@ use cliparser::types::{
CliSpecMetaInfo, PositionalArgument,
};
-#[cfg(test)]
-fn exit() -> ! {
- panic!("{}", "exit test");
-}
-
-#[cfg(not(test))]
-use std::process;
-#[cfg(not(test))]
-fn exit() -> ! {
- process::exit(0);
-}
+use either::Either;
fn get_args(
cli_parsed: &CliParsed,
@@ -472,8 +462,8 @@ pub fn parse_args(
global_config: &GlobalConfig,
command_name: &str,
sub_command: bool,
- args: Option>,
-) -> CliArgs {
+ args: Option>
+) -> Either {
let spec = create_cli(&global_config);
let parse_result = match args {
@@ -487,20 +477,20 @@ pub fn parse_args(
// generate help text
let help_text = cliparser::help(&spec);
println!("{}", help_text);
- exit();
+ return Either::Right(std::process::ExitCode::from(std::process::ExitCode::SUCCESS))
} else if cli_parsed.arguments.contains("version") {
// generate version text
let version_text = cliparser::version(&spec);
println!("{}", version_text);
- exit();
+ return Either::Right(std::process::ExitCode::SUCCESS)
}
- get_args(&cli_parsed, &global_config, command_name, sub_command)
+ return Either::Left(get_args(&cli_parsed, &global_config, command_name, sub_command))
}
Err(error) => {
let help_text = cliparser::help(&spec);
println!("{}\n{}", &error, help_text);
- exit()
+ return Either::Right(std::process::ExitCode::FAILURE)
}
}
}
@@ -509,7 +499,7 @@ pub fn parse(
global_config: &GlobalConfig,
command_name: &str,
sub_command: bool,
-) -> CliArgs {
+) -> Either {
parse_args(global_config, command_name, sub_command, None)
}
diff --git a/src/lib/cli_parser_test.rs b/src/lib/cli_parser_test.rs
index ae85c7cc..139232f1 100644
--- a/src/lib/cli_parser_test.rs
+++ b/src/lib/cli_parser_test.rs
@@ -1,6 +1,6 @@
use super::*;
-fn default_parse_cli_args(mut args: Vec<&str>) -> CliArgs {
+fn default_parse_cli_args(mut args: Vec<&str>) -> Either {
let global_config = GlobalConfig::new();
args.insert(0, "makers");
parse_args(&global_config, "makers", false, Some(args))
@@ -59,7 +59,7 @@ fn parse_args_makers() {
let expected = default_parsed_cli_args();
- assert_cli_args(&cli_args, &expected);
+ assert_cli_args(&cli_args.left().unwrap(), &expected);
}
#[test]
@@ -70,7 +70,7 @@ fn parse_args_cargo_make() {
let mut expected = default_parsed_cli_args();
expected.command = "cargo make".to_string();
- assert_cli_args(&cli_args, &expected);
+ assert_cli_args(&cli_args.left().unwrap(), &expected);
}
#[test]
@@ -104,44 +104,44 @@ fn parse_args_makefile() {
let mut expected = default_parsed_cli_args();
expected.build_file = Some("./mymakefile.toml".to_string());
- assert_cli_args(&cli_args, &expected);
+ assert_cli_args(&cli_args.left().unwrap(), &expected);
cli_args = default_parse_cli_args(vec!["--makefile", "./mymakefile.toml", "taskname"]);
expected.task = "taskname".to_string();
expected.arguments = Some(vec![]);
- assert_cli_args(&cli_args, &expected);
+ assert_cli_args(&cli_args.left().unwrap(), &expected);
}
#[test]
fn parse_args_task() {
- let mut cli_args = default_parse_cli_args(vec!["--task", "sometask"]);
+ let mut cli_args = default_parse_cli_args(vec!["--task", "sometask"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.task = "sometask".to_string();
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["-t", "sometask"]);
+ cli_args = default_parse_cli_args(vec!["-t", "sometask"]).left().unwrap();
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["sometask"]);
+ cli_args = default_parse_cli_args(vec!["sometask"]).left().unwrap();
expected.arguments = Some(vec![]);
assert_cli_args(&cli_args, &expected);
}
#[test]
fn parse_args_profile() {
- let mut cli_args = default_parse_cli_args(vec!["--profile", "someprofile"]);
+ let mut cli_args = default_parse_cli_args(vec!["--profile", "someprofile"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.profile = Some("someprofile".to_string());
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["-p", "someprofile"]);
+ cli_args = default_parse_cli_args(vec!["-p", "someprofile"]).left().unwrap();
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--profile", "someprofile", "taskname"]);
+ cli_args = default_parse_cli_args(vec!["--profile", "someprofile", "taskname"]).left().unwrap();
expected.task = "taskname".to_string();
expected.arguments = Some(vec![]);
assert_cli_args(&cli_args, &expected);
@@ -149,7 +149,7 @@ fn parse_args_profile() {
#[test]
fn parse_args_cwd() {
- let cli_args = default_parse_cli_args(vec!["--cwd", "./mydir/subdir/"]);
+ let cli_args = default_parse_cli_args(vec!["--cwd", "./mydir/subdir/"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.cwd = Some("./mydir/subdir/".to_string());
@@ -159,7 +159,7 @@ fn parse_args_cwd() {
#[test]
fn parse_args_no_workspace() {
- let cli_args = default_parse_cli_args(vec!["--no-workspace"]);
+ let cli_args = default_parse_cli_args(vec!["--no-workspace"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.disable_workspace = true;
@@ -169,7 +169,7 @@ fn parse_args_no_workspace() {
#[test]
fn parse_args_allow_private() {
- let cli_args = default_parse_cli_args(vec!["--allow-private"]);
+ let cli_args = default_parse_cli_args(vec!["--allow-private"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.allow_private = true;
@@ -179,7 +179,7 @@ fn parse_args_allow_private() {
#[test]
fn parse_args_skip_init_end_tasks() {
- let cli_args = default_parse_cli_args(vec!["--skip-init-end-tasks"]);
+ let cli_args = default_parse_cli_args(vec!["--skip-init-end-tasks"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.skip_init_end_tasks = true;
@@ -189,14 +189,14 @@ fn parse_args_skip_init_end_tasks() {
#[test]
fn parse_args_skip_tasks() {
- let mut cli_args = default_parse_cli_args(vec!["--skip-tasks", "pre-.*"]);
+ let mut cli_args = default_parse_cli_args(vec!["--skip-tasks", "pre-.*"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.skip_tasks_pattern = Some("pre-.*".to_string());
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--skip-tasks", "pre-.*", "taskname"]);
+ cli_args = default_parse_cli_args(vec!["--skip-tasks", "pre-.*", "taskname"]).left().unwrap();
expected.task = "taskname".to_string();
expected.arguments = Some(vec![]);
assert_cli_args(&cli_args, &expected);
@@ -204,38 +204,38 @@ fn parse_args_skip_tasks() {
#[test]
fn parse_args_env_file() {
- let mut cli_args = default_parse_cli_args(vec!["--env-file", "./.env"]);
+ let mut cli_args = default_parse_cli_args(vec!["--env-file", "./.env"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.env_file = Some("./.env".to_string());
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--env-file=./.env"]);
+ cli_args = default_parse_cli_args(vec!["--env-file=./.env"]).left().unwrap();
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--env-file", "./.env", "taskname"]);
+ cli_args = default_parse_cli_args(vec!["--env-file", "./.env", "taskname"]).left().unwrap();
expected.task = "taskname".to_string();
expected.arguments = Some(vec![]);
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--env-file=./.env", "taskname"]);
+ cli_args = default_parse_cli_args(vec!["--env-file=./.env", "taskname"]).left().unwrap();
assert_cli_args(&cli_args, &expected);
}
#[test]
fn parse_args_env() {
- let mut cli_args = default_parse_cli_args(vec!["--env", "K=V"]);
+ let mut cli_args = default_parse_cli_args(vec!["--env", "K=V"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.env = Some(vec!["K=V".to_string()]);
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["-e", "K=V"]);
+ cli_args = default_parse_cli_args(vec!["-e", "K=V"]).left().unwrap();
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["-e", "K1=V1", "-e", "K2=V2", "--env", "K3=V3"]);
+ cli_args = default_parse_cli_args(vec!["-e", "K1=V1", "-e", "K2=V2", "--env", "K3=V3"]).left().unwrap();
expected.env = Some(vec![
"K1=V1".to_string(),
"K2=V2".to_string(),
@@ -245,7 +245,7 @@ fn parse_args_env() {
cli_args = default_parse_cli_args(vec![
"-e", "K1=V1", "-e", "K2=V2", "--env", "K3=V3", "taskname",
- ]);
+ ]).left().unwrap();
expected.task = "taskname".to_string();
expected.arguments = Some(vec![]);
assert_cli_args(&cli_args, &expected);
@@ -253,17 +253,17 @@ fn parse_args_env() {
#[test]
fn parse_args_loglevel() {
- let mut cli_args = default_parse_cli_args(vec!["--loglevel", "verbose"]);
+ let mut cli_args = default_parse_cli_args(vec!["--loglevel", "verbose"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.log_level = "verbose".to_string();
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["-l", "verbose"]);
+ cli_args = default_parse_cli_args(vec!["-l", "verbose"]).left().unwrap();
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--loglevel", "verbose", "taskname"]);
+ cli_args = default_parse_cli_args(vec!["--loglevel", "verbose", "taskname"]).left().unwrap();
expected.task = "taskname".to_string();
expected.arguments = Some(vec![]);
assert_cli_args(&cli_args, &expected);
@@ -271,20 +271,20 @@ fn parse_args_loglevel() {
#[test]
fn parse_args_verbose() {
- let mut cli_args = default_parse_cli_args(vec!["--verbose"]);
+ let mut cli_args = default_parse_cli_args(vec!["--verbose"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.log_level = "verbose".to_string();
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["-v"]);
+ cli_args = default_parse_cli_args(vec!["-v"]).left().unwrap();
assert_cli_args(&cli_args, &expected);
}
#[test]
fn parse_args_quiet() {
- let cli_args = default_parse_cli_args(vec!["--quiet"]);
+ let cli_args = default_parse_cli_args(vec!["--quiet"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.log_level = "error".to_string();
@@ -294,7 +294,7 @@ fn parse_args_quiet() {
#[test]
fn parse_args_no_color() {
- let cli_args = default_parse_cli_args(vec!["--no-color"]);
+ let cli_args = default_parse_cli_args(vec!["--no-color"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.disable_color = true;
@@ -304,7 +304,7 @@ fn parse_args_no_color() {
#[test]
fn parse_args_time_summary() {
- let cli_args = default_parse_cli_args(vec!["--time-summary"]);
+ let cli_args = default_parse_cli_args(vec!["--time-summary"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.print_time_summary = true;
@@ -314,7 +314,7 @@ fn parse_args_time_summary() {
#[test]
fn parse_args_experimental() {
- let cli_args = default_parse_cli_args(vec!["--experimental"]);
+ let cli_args = default_parse_cli_args(vec!["--experimental"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.experimental = true;
@@ -324,7 +324,7 @@ fn parse_args_experimental() {
#[test]
fn parse_args_disable_check_for_updates() {
- let cli_args = default_parse_cli_args(vec!["--disable-check-for-updates"]);
+ let cli_args = default_parse_cli_args(vec!["--disable-check-for-updates"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.disable_check_for_updates = true;
@@ -334,14 +334,14 @@ fn parse_args_disable_check_for_updates() {
#[test]
fn parse_args_output_format() {
- let mut cli_args = default_parse_cli_args(vec!["--output-format", "autocomplete"]);
+ let mut cli_args = default_parse_cli_args(vec!["--output-format", "autocomplete"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.output_format = "autocomplete".to_string();
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--output-format", "autocomplete", "taskname"]);
+ cli_args = default_parse_cli_args(vec!["--output-format", "autocomplete", "taskname"]).left().unwrap();
expected.task = "taskname".to_string();
expected.arguments = Some(vec![]);
assert_cli_args(&cli_args, &expected);
@@ -349,14 +349,14 @@ fn parse_args_output_format() {
#[test]
fn parse_args_output_file() {
- let mut cli_args = default_parse_cli_args(vec!["--output-file", "./out"]);
+ let mut cli_args = default_parse_cli_args(vec!["--output-file", "./out"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.output_file = Some("./out".to_string());
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--output-file", "./out", "taskname"]);
+ cli_args = default_parse_cli_args(vec!["--output-file", "./out", "taskname"]).left().unwrap();
expected.task = "taskname".to_string();
expected.arguments = Some(vec![]);
assert_cli_args(&cli_args, &expected);
@@ -364,7 +364,7 @@ fn parse_args_output_file() {
#[test]
fn parse_args_print_steps() {
- let cli_args = default_parse_cli_args(vec!["--print-steps"]);
+ let cli_args = default_parse_cli_args(vec!["--print-steps"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.print_only = true;
@@ -374,7 +374,7 @@ fn parse_args_print_steps() {
#[test]
fn parse_args_list_all_steps() {
- let cli_args = default_parse_cli_args(vec!["--list-all-steps"]);
+ let cli_args = default_parse_cli_args(vec!["--list-all-steps"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.list_all_steps = true;
@@ -384,14 +384,14 @@ fn parse_args_list_all_steps() {
#[test]
fn parse_args_list_category_steps() {
- let mut cli_args = default_parse_cli_args(vec!["--list-category-steps", "build"]);
+ let mut cli_args = default_parse_cli_args(vec!["--list-category-steps", "build"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.list_category_steps = Some("build".to_string());
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--list-category-steps", "build", "taskname"]);
+ cli_args = default_parse_cli_args(vec!["--list-category-steps", "build", "taskname"]).left().unwrap();
expected.task = "taskname".to_string();
expected.arguments = Some(vec![]);
assert_cli_args(&cli_args, &expected);
@@ -399,7 +399,7 @@ fn parse_args_list_category_steps() {
#[test]
fn parse_args_diff_steps() {
- let cli_args = default_parse_cli_args(vec!["--diff-steps"]);
+ let cli_args = default_parse_cli_args(vec!["--diff-steps"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.diff_execution_plan = true;
@@ -409,7 +409,7 @@ fn parse_args_diff_steps() {
#[test]
fn parse_args_task_cmd() {
- let mut cli_args = default_parse_cli_args(vec!["task1"]);
+ let mut cli_args = default_parse_cli_args(vec!["task1"]).left().unwrap();
let mut expected = default_parsed_cli_args();
expected.task = "task1".to_string();
@@ -417,13 +417,13 @@ fn parse_args_task_cmd() {
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--", "task1"]);
+ cli_args = default_parse_cli_args(vec!["--", "task1"]).left().unwrap();
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["task1", "arg1", "arg2"]);
+ cli_args = default_parse_cli_args(vec!["task1", "arg1", "arg2"]).left().unwrap();
expected.arguments = Some(vec!["arg1".to_string(), "arg2".to_string()]);
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--", "task1", "arg1", "arg2"]);
+ cli_args = default_parse_cli_args(vec!["--", "task1", "arg1", "arg2"]).left().unwrap();
assert_cli_args(&cli_args, &expected);
}
diff --git a/src/lib/cli_test.rs b/src/lib/cli_test.rs
index de2bc78e..674afae9 100644
--- a/src/lib/cli_test.rs
+++ b/src/lib/cli_test.rs
@@ -393,7 +393,13 @@ fn run_bad_subcommand() {
let cli_args =
cli_parser::parse_args(&global_config, &"make".to_string(), true, Some(vec!["bad"]));
- run(cli_args, &global_config);
+
+ assert_eq!(
+ format!("{:?}", &cli_args.clone().right().unwrap()),
+ format!("{:?}", std::process::ExitCode::FAILURE)
+ );
+
+ run(cli_args.left().unwrap(), &global_config);
}
#[test]
@@ -418,7 +424,10 @@ fn run_valid() {
]),
);
- run(cli_args, &global_config);
+ assert_eq!(
+ format!("{:?}", run(cli_args.left().unwrap(), &global_config).right().unwrap()),
+ format!("{:?}", std::process::ExitCode::FAILURE)
+ );
}
#[test]
@@ -433,7 +442,7 @@ fn run_with_global_config() {
&"make".to_string(),
true,
Some(vec!["cargo", "make"]),
- );
+ ).left().unwrap();
run(cli_args, &global_config);
}
@@ -457,7 +466,7 @@ fn run_log_level_override() {
"error",
"-v",
]),
- );
+ ).left().unwrap();
run(cli_args, &global_config);
}
@@ -484,7 +493,7 @@ fn run_set_env_values() {
"-t",
"empty",
]),
- );
+ ).left().unwrap();
envmnt::set("ENV1_TEST", "EMPTY");
envmnt::set("ENV2_TEST", "EMPTY");
@@ -520,7 +529,7 @@ fn run_set_env_via_file() {
envmnt::set("ENV2_TEST", "EMPTY");
envmnt::set("ENV3_TEST", "EMPTY");
- run(cli_args, &global_config);
+ run(cli_args.left().unwrap(), &global_config);
assert_eq!(envmnt::get_or_panic("ENV1_TEST"), "TEST1");
assert_eq!(envmnt::get_or_panic("ENV2_TEST"), "TEST2");
@@ -559,7 +568,7 @@ fn run_set_env_both() {
envmnt::set("ENV5_TEST", "EMPTY");
envmnt::set("ENV6_TEST", "EMPTY");
- run(cli_args, &global_config);
+ run(cli_args.left().unwrap(), &global_config);
assert_eq!(envmnt::get_or_panic("ENV1_TEST"), "TEST1");
assert_eq!(envmnt::get_or_panic("ENV2_TEST"), "TEST2");
@@ -595,7 +604,7 @@ fn run_print_only() {
]),
);
- run(cli_args, &global_config);
+ run(cli_args.left().unwrap(), &global_config);
}
#[test]
@@ -620,7 +629,7 @@ fn run_diff_steps() {
]),
);
- run(cli_args, &global_config);
+ run(cli_args.left().unwrap(), &global_config);
}
#[test]
@@ -640,7 +649,7 @@ fn run_protected_flow_example() {
]),
);
- run(cli_args, &global_config);
+ run(cli_args.left().unwrap(), &global_config);
}
#[test]
@@ -661,7 +670,7 @@ fn run_no_task_args() {
envmnt::set("CARGO_MAKE_TASK_ARGS", "EMPTY");
- run(cli_args, &global_config);
+ run(cli_args.left().unwrap(), &global_config);
assert_eq!(envmnt::get_or_panic("CARGO_MAKE_TASK_ARGS"), "");
}
@@ -687,7 +696,7 @@ fn run_set_task_args() {
envmnt::set("CARGO_MAKE_TASK_ARGS", "EMPTY");
- run(cli_args, &global_config);
+ run(cli_args.left().unwrap(), &global_config);
assert_eq!(
envmnt::get_or_panic("CARGO_MAKE_TASK_ARGS"),
@@ -710,7 +719,7 @@ fn run_set_task_var_args() {
envmnt::set("CARGO_MAKE_TASK_ARGS", "EMPTY");
- run(cli_args, &global_config);
+ run(cli_args.left().unwrap(), &global_config);
assert_eq!(
envmnt::get_or_panic("CARGO_MAKE_TASK_ARGS"),
diff --git a/src/lib/logger.rs b/src/lib/logger.rs
index 0b73a0b7..9cd5be99 100755
--- a/src/lib/logger.rs
+++ b/src/lib/logger.rs
@@ -32,17 +32,12 @@ pub(crate) struct LoggerOptions {
}
pub(crate) fn get_level(level_name: &str) -> LogLevel {
- let mut level = LogLevel::INFO;
-
- if level_name == "verbose" {
- level = LogLevel::VERBOSE;
- } else if level_name == "error" {
- level = LogLevel::ERROR;
- } else if level_name == "off" {
- level = LogLevel::OFF;
+ match level_name {
+ "verbose" => LogLevel::VERBOSE,
+ "error" => LogLevel::ERROR,
+ "off" => LogLevel::OFF,
+ _ => LogLevel::INFO
}
-
- level
}
/// Returns the current logger level name
diff --git a/src/lib/mod.rs b/src/lib/mod.rs
index 917a0cba..3d8ba9dd 100755
--- a/src/lib/mod.rs
+++ b/src/lib/mod.rs
@@ -34,9 +34,11 @@
//! [Apache 2](https://github.com/sagiegurari/cargo-make/blob/master/LICENSE) open source license.
//!
+use either::Either;
// Dependencies used in the binary `makers`:
#[cfg(windows)]
use nu_ansi_term as _;
+use crate::types::CliArgs;
#[macro_use]
extern crate log;
@@ -76,6 +78,6 @@ mod toolchain;
mod version;
/// Handles the command line arguments and executes the runner.
-pub fn run_cli(command_name: String, sub_command: bool) {
+pub fn run_cli(command_name: String, sub_command: bool) -> Either {
cli::run_cli(command_name, sub_command)
}
From 38780a3ec222393b20ba82b1f3ea694587b114d9 Mon Sep 17 00:00:00 2001
From: Samuel Marks <807580+SamuelMarks@users.noreply.github.com>
Date: Thu, 11 Jul 2024 22:15:31 -0400
Subject: [PATCH 04/21] [src/lib/*.rs] Fix tests broken by previous commit ;
`rustfmt`
---
src/lib/cli.rs | 13 ++-
src/lib/cli_parser.rs | 18 +++--
src/lib/cli_parser_test.rs | 162 +++++++++++++++++++++++++++----------
src/lib/cli_test.rs | 20 +++--
src/lib/logger.rs | 2 +-
src/lib/mod.rs | 2 +-
6 files changed, 160 insertions(+), 57 deletions(-)
diff --git a/src/lib/cli.rs b/src/lib/cli.rs
index 9fed804d..e460a214 100644
--- a/src/lib/cli.rs
+++ b/src/lib/cli.rs
@@ -21,8 +21,8 @@ use crate::time_summary;
use crate::toolchain;
use crate::types::{CliArgs, GlobalConfig};
use crate::version;
+use either::{try_left, Either};
use std::time::SystemTime;
-use either::{Either, try_left};
pub(crate) static VERSION: &str = env!("CARGO_PKG_VERSION");
pub(crate) static AUTHOR: &str = env!("CARGO_PKG_AUTHORS");
@@ -32,7 +32,10 @@ pub(crate) static DEFAULT_LOG_LEVEL: &str = "info";
pub(crate) static DEFAULT_TASK_NAME: &str = "default";
pub(crate) static DEFAULT_OUTPUT_FORMAT: &str = "default";
-pub fn run(cli_args: CliArgs, global_config: &GlobalConfig) -> Either {
+pub fn run(
+ cli_args: CliArgs,
+ global_config: &GlobalConfig,
+) -> Either {
let start_time = SystemTime::now();
recursion_level::increment();
@@ -175,7 +178,11 @@ pub fn run(cli_args: CliArgs, global_config: &GlobalConfig) -> Either Either {
let global_config = config::load();
- let cli_args = try_left!(cli_parser::parse(&global_config, &command_name, sub_command));
+ let cli_args = try_left!(cli_parser::parse(
+ &global_config,
+ &command_name,
+ sub_command
+ ));
run(cli_args.clone(), &global_config);
Either::Left(cli_args)
diff --git a/src/lib/cli_parser.rs b/src/lib/cli_parser.rs
index eafc2639..110b9150 100644
--- a/src/lib/cli_parser.rs
+++ b/src/lib/cli_parser.rs
@@ -17,7 +17,6 @@ use cliparser::types::{
CliSpecMetaInfo, PositionalArgument,
};
-
use either::Either;
fn get_args(
@@ -462,7 +461,7 @@ pub fn parse_args(
global_config: &GlobalConfig,
command_name: &str,
sub_command: bool,
- args: Option>
+ args: Option>,
) -> Either {
let spec = create_cli(&global_config);
@@ -477,20 +476,27 @@ pub fn parse_args(
// generate help text
let help_text = cliparser::help(&spec);
println!("{}", help_text);
- return Either::Right(std::process::ExitCode::from(std::process::ExitCode::SUCCESS))
+ return Either::Right(std::process::ExitCode::from(
+ std::process::ExitCode::SUCCESS,
+ ));
} else if cli_parsed.arguments.contains("version") {
// generate version text
let version_text = cliparser::version(&spec);
println!("{}", version_text);
- return Either::Right(std::process::ExitCode::SUCCESS)
+ return Either::Right(std::process::ExitCode::SUCCESS);
}
- return Either::Left(get_args(&cli_parsed, &global_config, command_name, sub_command))
+ return Either::Left(get_args(
+ &cli_parsed,
+ &global_config,
+ command_name,
+ sub_command,
+ ));
}
Err(error) => {
let help_text = cliparser::help(&spec);
println!("{}\n{}", &error, help_text);
- return Either::Right(std::process::ExitCode::FAILURE)
+ return Either::Right(std::process::ExitCode::FAILURE);
}
}
}
diff --git a/src/lib/cli_parser_test.rs b/src/lib/cli_parser_test.rs
index 139232f1..b1de348d 100644
--- a/src/lib/cli_parser_test.rs
+++ b/src/lib/cli_parser_test.rs
@@ -74,27 +74,41 @@ fn parse_args_cargo_make() {
}
#[test]
-#[should_panic]
fn parse_args_help_long() {
- default_parse_cli_args(vec!["--help"]);
+ assert_eq!(
+ format!("{:?}", std::process::ExitCode::SUCCESS),
+ format!(
+ "{:?}",
+ default_parse_cli_args(vec!["--help"]).right().unwrap()
+ )
+ );
}
#[test]
-#[should_panic]
fn parse_args_help_short() {
- default_parse_cli_args(vec!["-h"]);
+ assert_eq!(
+ format!("{:?}", std::process::ExitCode::SUCCESS),
+ format!("{:?}", default_parse_cli_args(vec!["-h"]).right().unwrap())
+ );
}
#[test]
-#[should_panic]
fn parse_args_version_long() {
- default_parse_cli_args(vec!["--version"]);
+ assert_eq!(
+ format!("{:?}", std::process::ExitCode::SUCCESS),
+ format!(
+ "{:?}",
+ default_parse_cli_args(vec!["--version"]).right().unwrap()
+ )
+ );
}
#[test]
-#[should_panic]
fn parse_args_version_short() {
- default_parse_cli_args(vec!["-V"]);
+ assert_eq!(
+ format!("{:?}", std::process::ExitCode::SUCCESS),
+ format!("{:?}", default_parse_cli_args(vec!["-V"]).right().unwrap())
+ );
}
#[test]
@@ -114,14 +128,18 @@ fn parse_args_makefile() {
#[test]
fn parse_args_task() {
- let mut cli_args = default_parse_cli_args(vec!["--task", "sometask"]).left().unwrap();
+ let mut cli_args = default_parse_cli_args(vec!["--task", "sometask"])
+ .left()
+ .unwrap();
let mut expected = default_parsed_cli_args();
expected.task = "sometask".to_string();
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["-t", "sometask"]).left().unwrap();
+ cli_args = default_parse_cli_args(vec!["-t", "sometask"])
+ .left()
+ .unwrap();
assert_cli_args(&cli_args, &expected);
cli_args = default_parse_cli_args(vec!["sometask"]).left().unwrap();
@@ -131,17 +149,23 @@ fn parse_args_task() {
#[test]
fn parse_args_profile() {
- let mut cli_args = default_parse_cli_args(vec!["--profile", "someprofile"]).left().unwrap();
+ let mut cli_args = default_parse_cli_args(vec!["--profile", "someprofile"])
+ .left()
+ .unwrap();
let mut expected = default_parsed_cli_args();
expected.profile = Some("someprofile".to_string());
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["-p", "someprofile"]).left().unwrap();
+ cli_args = default_parse_cli_args(vec!["-p", "someprofile"])
+ .left()
+ .unwrap();
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--profile", "someprofile", "taskname"]).left().unwrap();
+ cli_args = default_parse_cli_args(vec!["--profile", "someprofile", "taskname"])
+ .left()
+ .unwrap();
expected.task = "taskname".to_string();
expected.arguments = Some(vec![]);
assert_cli_args(&cli_args, &expected);
@@ -149,7 +173,9 @@ fn parse_args_profile() {
#[test]
fn parse_args_cwd() {
- let cli_args = default_parse_cli_args(vec!["--cwd", "./mydir/subdir/"]).left().unwrap();
+ let cli_args = default_parse_cli_args(vec!["--cwd", "./mydir/subdir/"])
+ .left()
+ .unwrap();
let mut expected = default_parsed_cli_args();
expected.cwd = Some("./mydir/subdir/".to_string());
@@ -159,7 +185,9 @@ fn parse_args_cwd() {
#[test]
fn parse_args_no_workspace() {
- let cli_args = default_parse_cli_args(vec!["--no-workspace"]).left().unwrap();
+ let cli_args = default_parse_cli_args(vec!["--no-workspace"])
+ .left()
+ .unwrap();
let mut expected = default_parsed_cli_args();
expected.disable_workspace = true;
@@ -169,7 +197,9 @@ fn parse_args_no_workspace() {
#[test]
fn parse_args_allow_private() {
- let cli_args = default_parse_cli_args(vec!["--allow-private"]).left().unwrap();
+ let cli_args = default_parse_cli_args(vec!["--allow-private"])
+ .left()
+ .unwrap();
let mut expected = default_parsed_cli_args();
expected.allow_private = true;
@@ -179,7 +209,9 @@ fn parse_args_allow_private() {
#[test]
fn parse_args_skip_init_end_tasks() {
- let cli_args = default_parse_cli_args(vec!["--skip-init-end-tasks"]).left().unwrap();
+ let cli_args = default_parse_cli_args(vec!["--skip-init-end-tasks"])
+ .left()
+ .unwrap();
let mut expected = default_parsed_cli_args();
expected.skip_init_end_tasks = true;
@@ -189,14 +221,18 @@ fn parse_args_skip_init_end_tasks() {
#[test]
fn parse_args_skip_tasks() {
- let mut cli_args = default_parse_cli_args(vec!["--skip-tasks", "pre-.*"]).left().unwrap();
+ let mut cli_args = default_parse_cli_args(vec!["--skip-tasks", "pre-.*"])
+ .left()
+ .unwrap();
let mut expected = default_parsed_cli_args();
expected.skip_tasks_pattern = Some("pre-.*".to_string());
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--skip-tasks", "pre-.*", "taskname"]).left().unwrap();
+ cli_args = default_parse_cli_args(vec!["--skip-tasks", "pre-.*", "taskname"])
+ .left()
+ .unwrap();
expected.task = "taskname".to_string();
expected.arguments = Some(vec![]);
assert_cli_args(&cli_args, &expected);
@@ -204,22 +240,30 @@ fn parse_args_skip_tasks() {
#[test]
fn parse_args_env_file() {
- let mut cli_args = default_parse_cli_args(vec!["--env-file", "./.env"]).left().unwrap();
+ let mut cli_args = default_parse_cli_args(vec!["--env-file", "./.env"])
+ .left()
+ .unwrap();
let mut expected = default_parsed_cli_args();
expected.env_file = Some("./.env".to_string());
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--env-file=./.env"]).left().unwrap();
+ cli_args = default_parse_cli_args(vec!["--env-file=./.env"])
+ .left()
+ .unwrap();
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--env-file", "./.env", "taskname"]).left().unwrap();
+ cli_args = default_parse_cli_args(vec!["--env-file", "./.env", "taskname"])
+ .left()
+ .unwrap();
expected.task = "taskname".to_string();
expected.arguments = Some(vec![]);
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--env-file=./.env", "taskname"]).left().unwrap();
+ cli_args = default_parse_cli_args(vec!["--env-file=./.env", "taskname"])
+ .left()
+ .unwrap();
assert_cli_args(&cli_args, &expected);
}
@@ -235,7 +279,9 @@ fn parse_args_env() {
cli_args = default_parse_cli_args(vec!["-e", "K=V"]).left().unwrap();
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["-e", "K1=V1", "-e", "K2=V2", "--env", "K3=V3"]).left().unwrap();
+ cli_args = default_parse_cli_args(vec!["-e", "K1=V1", "-e", "K2=V2", "--env", "K3=V3"])
+ .left()
+ .unwrap();
expected.env = Some(vec![
"K1=V1".to_string(),
"K2=V2".to_string(),
@@ -245,7 +291,9 @@ fn parse_args_env() {
cli_args = default_parse_cli_args(vec![
"-e", "K1=V1", "-e", "K2=V2", "--env", "K3=V3", "taskname",
- ]).left().unwrap();
+ ])
+ .left()
+ .unwrap();
expected.task = "taskname".to_string();
expected.arguments = Some(vec![]);
assert_cli_args(&cli_args, &expected);
@@ -253,17 +301,23 @@ fn parse_args_env() {
#[test]
fn parse_args_loglevel() {
- let mut cli_args = default_parse_cli_args(vec!["--loglevel", "verbose"]).left().unwrap();
+ let mut cli_args = default_parse_cli_args(vec!["--loglevel", "verbose"])
+ .left()
+ .unwrap();
let mut expected = default_parsed_cli_args();
expected.log_level = "verbose".to_string();
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["-l", "verbose"]).left().unwrap();
+ cli_args = default_parse_cli_args(vec!["-l", "verbose"])
+ .left()
+ .unwrap();
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--loglevel", "verbose", "taskname"]).left().unwrap();
+ cli_args = default_parse_cli_args(vec!["--loglevel", "verbose", "taskname"])
+ .left()
+ .unwrap();
expected.task = "taskname".to_string();
expected.arguments = Some(vec![]);
assert_cli_args(&cli_args, &expected);
@@ -304,7 +358,9 @@ fn parse_args_no_color() {
#[test]
fn parse_args_time_summary() {
- let cli_args = default_parse_cli_args(vec!["--time-summary"]).left().unwrap();
+ let cli_args = default_parse_cli_args(vec!["--time-summary"])
+ .left()
+ .unwrap();
let mut expected = default_parsed_cli_args();
expected.print_time_summary = true;
@@ -314,7 +370,9 @@ fn parse_args_time_summary() {
#[test]
fn parse_args_experimental() {
- let cli_args = default_parse_cli_args(vec!["--experimental"]).left().unwrap();
+ let cli_args = default_parse_cli_args(vec!["--experimental"])
+ .left()
+ .unwrap();
let mut expected = default_parsed_cli_args();
expected.experimental = true;
@@ -324,7 +382,9 @@ fn parse_args_experimental() {
#[test]
fn parse_args_disable_check_for_updates() {
- let cli_args = default_parse_cli_args(vec!["--disable-check-for-updates"]).left().unwrap();
+ let cli_args = default_parse_cli_args(vec!["--disable-check-for-updates"])
+ .left()
+ .unwrap();
let mut expected = default_parsed_cli_args();
expected.disable_check_for_updates = true;
@@ -334,14 +394,18 @@ fn parse_args_disable_check_for_updates() {
#[test]
fn parse_args_output_format() {
- let mut cli_args = default_parse_cli_args(vec!["--output-format", "autocomplete"]).left().unwrap();
+ let mut cli_args = default_parse_cli_args(vec!["--output-format", "autocomplete"])
+ .left()
+ .unwrap();
let mut expected = default_parsed_cli_args();
expected.output_format = "autocomplete".to_string();
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--output-format", "autocomplete", "taskname"]).left().unwrap();
+ cli_args = default_parse_cli_args(vec!["--output-format", "autocomplete", "taskname"])
+ .left()
+ .unwrap();
expected.task = "taskname".to_string();
expected.arguments = Some(vec![]);
assert_cli_args(&cli_args, &expected);
@@ -349,14 +413,18 @@ fn parse_args_output_format() {
#[test]
fn parse_args_output_file() {
- let mut cli_args = default_parse_cli_args(vec!["--output-file", "./out"]).left().unwrap();
+ let mut cli_args = default_parse_cli_args(vec!["--output-file", "./out"])
+ .left()
+ .unwrap();
let mut expected = default_parsed_cli_args();
expected.output_file = Some("./out".to_string());
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--output-file", "./out", "taskname"]).left().unwrap();
+ cli_args = default_parse_cli_args(vec!["--output-file", "./out", "taskname"])
+ .left()
+ .unwrap();
expected.task = "taskname".to_string();
expected.arguments = Some(vec![]);
assert_cli_args(&cli_args, &expected);
@@ -364,7 +432,9 @@ fn parse_args_output_file() {
#[test]
fn parse_args_print_steps() {
- let cli_args = default_parse_cli_args(vec!["--print-steps"]).left().unwrap();
+ let cli_args = default_parse_cli_args(vec!["--print-steps"])
+ .left()
+ .unwrap();
let mut expected = default_parsed_cli_args();
expected.print_only = true;
@@ -374,7 +444,9 @@ fn parse_args_print_steps() {
#[test]
fn parse_args_list_all_steps() {
- let cli_args = default_parse_cli_args(vec!["--list-all-steps"]).left().unwrap();
+ let cli_args = default_parse_cli_args(vec!["--list-all-steps"])
+ .left()
+ .unwrap();
let mut expected = default_parsed_cli_args();
expected.list_all_steps = true;
@@ -384,14 +456,18 @@ fn parse_args_list_all_steps() {
#[test]
fn parse_args_list_category_steps() {
- let mut cli_args = default_parse_cli_args(vec!["--list-category-steps", "build"]).left().unwrap();
+ let mut cli_args = default_parse_cli_args(vec!["--list-category-steps", "build"])
+ .left()
+ .unwrap();
let mut expected = default_parsed_cli_args();
expected.list_category_steps = Some("build".to_string());
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--list-category-steps", "build", "taskname"]).left().unwrap();
+ cli_args = default_parse_cli_args(vec!["--list-category-steps", "build", "taskname"])
+ .left()
+ .unwrap();
expected.task = "taskname".to_string();
expected.arguments = Some(vec![]);
assert_cli_args(&cli_args, &expected);
@@ -420,10 +496,14 @@ fn parse_args_task_cmd() {
cli_args = default_parse_cli_args(vec!["--", "task1"]).left().unwrap();
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["task1", "arg1", "arg2"]).left().unwrap();
+ cli_args = default_parse_cli_args(vec!["task1", "arg1", "arg2"])
+ .left()
+ .unwrap();
expected.arguments = Some(vec!["arg1".to_string(), "arg2".to_string()]);
assert_cli_args(&cli_args, &expected);
- cli_args = default_parse_cli_args(vec!["--", "task1", "arg1", "arg2"]).left().unwrap();
+ cli_args = default_parse_cli_args(vec!["--", "task1", "arg1", "arg2"])
+ .left()
+ .unwrap();
assert_cli_args(&cli_args, &expected);
}
diff --git a/src/lib/cli_test.rs b/src/lib/cli_test.rs
index 674afae9..0b5ae261 100644
--- a/src/lib/cli_test.rs
+++ b/src/lib/cli_test.rs
@@ -393,7 +393,6 @@ fn run_bad_subcommand() {
let cli_args =
cli_parser::parse_args(&global_config, &"make".to_string(), true, Some(vec!["bad"]));
-
assert_eq!(
format!("{:?}", &cli_args.clone().right().unwrap()),
format!("{:?}", std::process::ExitCode::FAILURE)
@@ -425,7 +424,12 @@ fn run_valid() {
);
assert_eq!(
- format!("{:?}", run(cli_args.left().unwrap(), &global_config).right().unwrap()),
+ format!(
+ "{:?}",
+ run(cli_args.left().unwrap(), &global_config)
+ .right()
+ .unwrap()
+ ),
format!("{:?}", std::process::ExitCode::FAILURE)
);
}
@@ -442,7 +446,9 @@ fn run_with_global_config() {
&"make".to_string(),
true,
Some(vec!["cargo", "make"]),
- ).left().unwrap();
+ )
+ .left()
+ .unwrap();
run(cli_args, &global_config);
}
@@ -466,7 +472,9 @@ fn run_log_level_override() {
"error",
"-v",
]),
- ).left().unwrap();
+ )
+ .left()
+ .unwrap();
run(cli_args, &global_config);
}
@@ -493,7 +501,9 @@ fn run_set_env_values() {
"-t",
"empty",
]),
- ).left().unwrap();
+ )
+ .left()
+ .unwrap();
envmnt::set("ENV1_TEST", "EMPTY");
envmnt::set("ENV2_TEST", "EMPTY");
diff --git a/src/lib/logger.rs b/src/lib/logger.rs
index 9cd5be99..0c68ad98 100755
--- a/src/lib/logger.rs
+++ b/src/lib/logger.rs
@@ -36,7 +36,7 @@ pub(crate) fn get_level(level_name: &str) -> LogLevel {
"verbose" => LogLevel::VERBOSE,
"error" => LogLevel::ERROR,
"off" => LogLevel::OFF,
- _ => LogLevel::INFO
+ _ => LogLevel::INFO,
}
}
diff --git a/src/lib/mod.rs b/src/lib/mod.rs
index 3d8ba9dd..e12397ed 100755
--- a/src/lib/mod.rs
+++ b/src/lib/mod.rs
@@ -36,9 +36,9 @@
use either::Either;
// Dependencies used in the binary `makers`:
+use crate::types::CliArgs;
#[cfg(windows)]
use nu_ansi_term as _;
-use crate::types::CliArgs;
#[macro_use]
extern crate log;
From a5dd7c668079135a14aa60e2380f692dc75b9cb9 Mon Sep 17 00:00:00 2001
From: Samuel Marks <807580+SamuelMarks@users.noreply.github.com>
Date: Thu, 11 Jul 2024 22:20:35 -0400
Subject: [PATCH 05/21] [src/{main,makers}.rs] Percolate exit code up
---
src/main.rs | 6 ++++--
src/makers.rs | 6 ++++--
2 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/src/main.rs b/src/main.rs
index dc7ae7d3..4eb6a67f 100755
--- a/src/main.rs
+++ b/src/main.rs
@@ -33,7 +33,9 @@ fn get_name() -> String {
"make".to_string()
}
-fn main() {
+fn main() -> std::process::ExitCode {
let name = get_name();
- cli::run_cli(name, true);
+ cli::run_cli(name, true)
+ .right()
+ .unwrap_or(std::process::ExitCode::SUCCESS)
}
diff --git a/src/makers.rs b/src/makers.rs
index f30621fd..7b6308c3 100644
--- a/src/makers.rs
+++ b/src/makers.rs
@@ -20,9 +20,11 @@ fn get_name() -> String {
"makers".to_string()
}
-fn main() {
+fn main() -> std::process::ExitCode {
#[cfg(windows)]
let _ = nu_ansi_term::enable_ansi_support();
let name = get_name();
- cli::run_cli(name, false);
+ cli::run_cli(name, false)
+ .right()
+ .unwrap_or(std::process::ExitCode::SUCCESS)
}
From 58fd745dce407c32806f5efc98b6d78c4ec38303 Mon Sep 17 00:00:00 2001
From: Samuel Marks <807580+SamuelMarks@users.noreply.github.com>
Date: Thu, 11 Jul 2024 23:27:37 -0400
Subject: [PATCH 06/21] [src/lib/types.rs] Implement helper `either_to_result`
---
src/lib/types.rs | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/src/lib/types.rs b/src/lib/types.rs
index 4582c09e..4bfd5f43 100755
--- a/src/lib/types.rs
+++ b/src/lib/types.rs
@@ -10,6 +10,7 @@ mod types_test;
use crate::legacy;
use crate::plugin::types::Plugins;
use ci_info::types::CiInfo;
+use either::Either;
use git_info::types::GitInfo;
use indexmap::{IndexMap, IndexSet};
use regex::Regex;
@@ -2451,3 +2452,10 @@ pub struct CommandSpec {
/// The command args
pub args: Option>,
}
+
+pub fn either_to_result(either: Either) -> Result {
+ match either {
+ Either::Left(lhs) => Ok(lhs),
+ Either::Right(rhs) => Err(rhs),
+ }
+}
From 2a2e03f207333d598f690445d4fc3d304d918aac Mon Sep 17 00:00:00 2001
From: Samuel Marks <807580+SamuelMarks@users.noreply.github.com>
Date: Fri, 12 Jul 2024 00:32:06 -0400
Subject: [PATCH 07/21] [src/lib/cli.rs] Change function definition of `run`
and related functions with `&` to avoid `clone`s ; [src/lib/**.rs] Fix build
with new function definitions
---
docs/api/src/cli/cli.rs.html | 4 +--
src/lib/cli.rs | 8 ++---
src/lib/cli_commands/list_steps.rs | 4 +--
src/lib/cli_commands/list_steps_test.rs | 4 +--
src/lib/cli_commands/print_steps.rs | 2 +-
src/lib/cli_commands/print_steps_test.rs | 6 ++--
src/lib/cli_test.rs | 46 ++++++++++++------------
7 files changed, 37 insertions(+), 37 deletions(-)
diff --git a/docs/api/src/cli/cli.rs.html b/docs/api/src/cli/cli.rs.html
index 97f426f8..69f3a013 100644
--- a/docs/api/src/cli/cli.rs.html
+++ b/docs/api/src/cli/cli.rs.html
@@ -210,7 +210,7 @@
pub(crate) static DEFAULT_TASK_NAME: &str = "default";
pub(crate) static DEFAULT_OUTPUT_FORMAT: &str = "default";
-fn run(cli_args: CliArgs, global_config: &GlobalConfig) {
+fn run(&cli_args: CliArgs, global_config: &GlobalConfig) {
let start_time = SystemTime::now();
recursion_level::increment();
@@ -354,6 +354,6 @@
let cli_args = cli_parser::parse(&global_config, &command_name, sub_command);
- run(cli_args, &global_config);
+ run(&cli_args, &global_config);
}