Skip to content
Draft
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
28 changes: 28 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use clap::{Parser, Subcommand};

#[derive(Parser, Debug)]
#[command(name = "my-app", args_conflicts_with_subcommands = true)]
pub struct Cli {
#[arg(short, long, default_value_t = false, conflicts_with_all = ["update"])]
pub tui: bool,

#[arg(short, long, default_value_t = false, conflicts_with_all = ["tui"])]
pub update: bool,

#[command(subcommand)]
pub commands: Option<Commands>,
}

#[derive(Subcommand, Debug, PartialEq)]
pub enum Commands {
/// Install plugin
Install {
/// Path to install from
#[arg(short = 'p', long, value_name = "PATH")]
path: String,

/// Branch to use (optional)
#[arg(short = 'b', long, value_name = "BRANCH", requires = "path")]
branch: Option<String>,
},
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod cli;
pub mod utils;
12 changes: 2 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ use crossterm::{
use ratatui::{Terminal, prelude::CrosstermBackend};

use crate::{
cli::Cli,
plugins::run_plugins,
tmuxedo::{ensure_structure, source_all_tmuxedo_files},
tui::run_tmuxedo_tui,
};

mod bindings;
mod cli;
mod plugins;
mod register;
mod state;
Expand Down Expand Up @@ -46,16 +48,6 @@ impl TmuxCommand {
}
}

#[derive(Parser, Debug)]
#[command(name = "my-app")]
struct Cli {
#[arg(short, long, default_value_t = false)]
tui: bool,

#[arg(short, long, default_value_t = false)]
update: bool,
}

async fn run_app(cli: &Cli) -> Result<(), Box<dyn Error>> {
ensure_structure();
source_all_tmuxedo_files(cli.update).await;
Expand Down
121 changes: 121 additions & 0 deletions tests/cli_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use clap::Parser;
use tmuxedo::cli::{Cli, Commands};

// Helper to simulate running the app with arguments
fn parse_args(args: Vec<&str>) -> Result<Cli, clap::Error> {
Cli::try_parse_from(args)
}

#[test]
fn test_no_args_is_valid() {
// Running just `my-app` should be valid (Commands is Option<>)
let result = parse_args(vec!["my-app"]);
assert!(result.is_ok());
let cli = result.unwrap();
assert_eq!(cli.tui, false);
assert_eq!(cli.update, false);
assert_eq!(cli.commands, None);
}

#[test]
fn test_tui_flag() {
let result = parse_args(vec!["my-app", "--tui"]);
assert!(result.is_ok());
let cli = result.unwrap();
assert!(cli.tui);
assert!(!cli.update);
}

#[test]
fn test_update_flag() {
let result = parse_args(vec!["my-app", "--update"]);
assert!(result.is_ok());
let cli = result.unwrap();
assert!(!cli.tui);
assert!(cli.update);
}

#[test]
fn test_conflict_tui_and_update() {
// Fails because tui conflicts with update
let result = parse_args(vec!["my-app", "--tui", "--update"]);
assert!(result.is_err());
assert_eq!(
result.unwrap_err().kind(),
clap::error::ErrorKind::ArgumentConflict
);
}

#[test]
fn test_global_conflict_tui_and_subcommand() {
// Fails because of #[command(args_conflicting_with_subcommands = true)]
let result = parse_args(vec!["my-app", "--tui", "install", "--path", "."]);
assert!(result.is_err());
assert_eq!(
result.unwrap_err().kind(),
clap::error::ErrorKind::ArgumentConflict
);
}

#[test]
fn test_global_conflict_update_and_subcommand() {
let result = parse_args(vec!["my-app", "--update", "install", "--path", "."]);
assert!(result.is_err());
assert_eq!(
result.unwrap_err().kind(),
clap::error::ErrorKind::ArgumentConflict
);
}

#[test]
fn test_install_valid() {
let result = parse_args(vec!["my-app", "install", "--path", "/tmp"]);
assert!(result.is_ok());

match result.unwrap().commands {
Some(Commands::Install { path, branch }) => {
assert_eq!(path, "/tmp");
assert_eq!(branch, None);
}
_ => panic!("Expected Install command"),
}
}

#[test]
fn test_install_with_branch() {
let result = parse_args(vec![
"my-app", "install", "--path", "/tmp", "--branch", "dev",
]);
assert!(result.is_ok());

match result.unwrap().commands {
Some(Commands::Install { path, branch }) => {
assert_eq!(path, "/tmp");
assert_eq!(branch, Some("dev".to_string()));
}
_ => panic!("Expected Install command"),
}
}

#[test]
fn test_install_missing_required_path() {
// Path is mandatory, so this must fail
let result = parse_args(vec!["my-app", "install"]);
assert!(result.is_err());
assert_eq!(
result.unwrap_err().kind(),
clap::error::ErrorKind::MissingRequiredArgument
);
}

#[test]
fn test_branch_requires_path_enforcement() {
// Even if path wasn't mandatory by itself, `branch` requires `path`.
// Since `path` IS mandatory, this fails with MissingRequiredArgument.
let result = parse_args(vec!["my-app", "install", "--branch", "main"]);
assert!(result.is_err());
assert_eq!(
result.unwrap_err().kind(),
clap::error::ErrorKind::MissingRequiredArgument
);
}
37 changes: 37 additions & 0 deletions tests/utils_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use tmuxedo::utils::format_plugin_dir_name;

#[test]
fn test_replaces_single_slash() {
let input = "user/repo";
let expected = "user_repo";
assert_eq!(format_plugin_dir_name(input), expected);
}

#[test]
fn test_replaces_multiple_slashes() {
let input = "github.com/user/repo";
let expected = "github.com_user_repo";
assert_eq!(format_plugin_dir_name(input), expected);
}

#[test]
fn test_no_slashes_remains_unchanged() {
let input = "simple-plugin";
let expected = "simple-plugin";
assert_eq!(format_plugin_dir_name(input), expected);
}

#[test]
fn test_handles_trailing_slash() {
// Important edge case for directory paths
let input = "user/repo/";
let expected = "user_repo_";
assert_eq!(format_plugin_dir_name(input), expected);
}

#[test]
fn test_handles_empty_string() {
let input = "";
let expected = "";
assert_eq!(format_plugin_dir_name(input), expected);
}
Loading