Skip to content

Commit

Permalink
feat: add subcommand to get graveyard path
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Apr 15, 2024
1 parent fa926fb commit 448caf7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
6 changes: 4 additions & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Args {
pub decompose: bool,

/// Prints files that were deleted
/// in the current working directory
/// in the current directory
#[arg(short, long)]
pub seance: bool,

Expand All @@ -39,12 +39,14 @@ pub struct Args {
#[derive(Subcommand, Debug)]
pub enum Commands {
/// Generate shell completions file
/// for the specified shell
Completions {
/// The shell to generate completions for
#[arg(value_name = "SHELL")]
shell: String,
},

/// Print the graveyard path
Graveyard,
}

struct IsDefault {
Expand Down
34 changes: 16 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,7 @@ pub fn run(cli: Args, mode: impl util::TestingMode, stream: &mut impl Write) ->
// 2. Path pointed by the $GRAVEYARD variable
// 3. $XDG_DATA_HOME/graveyard (only if XDG_DATA_HOME is defined)
// 4. /tmp/graveyard-user
let graveyard: &PathBuf = &{
if let Some(flag) = cli.graveyard {
flag
} else if let Ok(env_graveyard) = env::var("RIP_GRAVEYARD") {
PathBuf::from(env_graveyard)
} else if let Ok(mut env_graveyard) = env::var("XDG_DATA_HOME") {
if !env_graveyard.ends_with(std::path::MAIN_SEPARATOR) {
env_graveyard.push(std::path::MAIN_SEPARATOR);
}
env_graveyard.push_str("graveyard");
PathBuf::from(env_graveyard)
} else {
default_graveyard()
}
};
let graveyard: &PathBuf = &get_graveyard(cli.graveyard);

if !graveyard.exists() {
fs::create_dir_all(graveyard)?;
Expand Down Expand Up @@ -439,7 +425,19 @@ pub fn copy_file(
}
}

fn default_graveyard() -> PathBuf {
let user = util::get_user();
env::temp_dir().join(format!("graveyard-{}", user))
pub fn get_graveyard(graveyard: Option<PathBuf>) -> PathBuf {
if let Some(flag) = graveyard {
flag
} else if let Ok(env_graveyard) = env::var("RIP_GRAVEYARD") {
PathBuf::from(env_graveyard)
} else if let Ok(mut env_graveyard) = env::var("XDG_DATA_HOME") {
if !env_graveyard.ends_with(std::path::MAIN_SEPARATOR) {
env_graveyard.push(std::path::MAIN_SEPARATOR);
}
env_graveyard.push_str("graveyard");
PathBuf::from(env_graveyard)
} else {
let user = util::get_user();
env::temp_dir().join(format!("graveyard-{}", user))
}
}
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::Parser;
use std::io;
use std::process::ExitCode;

use rip2::args::Commands;
use rip2::{args, completions, util};

fn main() -> ExitCode {
Expand All @@ -10,14 +11,18 @@ fn main() -> ExitCode {
let mode = util::ProductionMode;

match &cli.command {
Some(args::Commands::Completions { shell }) => {
Some(Commands::Completions { shell }) => {
let result = completions::generate_shell_completions(shell, &mut io::stdout());
if result.is_err() {
eprintln!("{}", result.unwrap_err());
return ExitCode::FAILURE;
}
return ExitCode::SUCCESS;
}
Some(Commands::Graveyard) => {
println!("{}", rip2::get_graveyard(None).display());
return ExitCode::SUCCESS;
}
None => {}
}

Expand Down
26 changes: 26 additions & 0 deletions tests/unit_tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use lazy_static::lazy_static;
use rip2::args::{validate_args, Args, Commands};
use rip2::completions;
use rip2::util::TestMode;
Expand All @@ -6,6 +7,7 @@ use std::fs;
use std::io::{Cursor, ErrorKind};
use std::path::PathBuf;
use std::process;
use std::sync::{Mutex, MutexGuard};
use tempfile::tempdir;

#[cfg(unix)]
Expand All @@ -20,6 +22,14 @@ use std::os::unix::net::UnixListener;
#[cfg(target_os = "macos")]
use std::os::unix::fs::FileTypeExt;

lazy_static! {
static ref GLOBAL_LOCK: Mutex<()> = Mutex::new(());
}

fn aquire_lock() -> MutexGuard<'static, ()> {
GLOBAL_LOCK.lock().unwrap()
}

#[rstest]
fn test_validation() {
let bad_completions = Args {
Expand Down Expand Up @@ -203,3 +213,19 @@ fn test_completions(
_ => {}
}
}

#[rstest]
fn test_graveyard_path() {
let _env_lock = aquire_lock();

// Clear env:
std::env::remove_var("RIP_GRAVEYARD");
std::env::remove_var("XDG_DATA_HOME");

// Check default graveyard path
let graveyard = rip2::get_graveyard(None);
assert_eq!(
graveyard,
std::env::temp_dir().join(format!("graveyard-{}", rip2::util::get_user()))
);
}

0 comments on commit 448caf7

Please sign in to comment.