From b133d10cf1e75c7abcb2d0e714c49850b0ebeb0b Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdx@users.noreply.github.com> Date: Sat, 10 Feb 2024 05:31:08 -0600 Subject: [PATCH] wip --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 1 + cli/Cargo.toml | 1 + cli/src/lib.rs | 3 +++ cli/src/test.rs | 6 ++++++ src/complete/bash.rs | 2 +- src/complete/fish.rs | 2 +- src/env.rs | 8 ++++++++ src/lib.rs | 3 +++ src/test.rs | 6 ++++++ 10 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 cli/src/test.rs create mode 100644 src/test.rs diff --git a/Cargo.lock b/Cargo.lock index f752f2a..6280997 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -335,6 +335,16 @@ dependencies = [ "typenum", ] +[[package]] +name = "ctor" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30d2b3721e861707777e3195b0158f950ae6dc4a27e4d02ff9f67e3eb3de199e" +dependencies = [ + "quote", + "syn 2.0.48", +] + [[package]] name = "deunicode" version = "1.4.2" @@ -1320,6 +1330,7 @@ dependencies = [ "assert_cmd", "clap", "contracts", + "ctor", "env_logger", "exec", "heck", @@ -1344,6 +1355,7 @@ name = "usage-lib" version = "0.1.0" dependencies = [ "clap", + "ctor", "heck", "indexmap", "insta", diff --git a/Cargo.toml b/Cargo.toml index e8f2529..e18664e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,6 +42,7 @@ heck = "0.4.1" default = ["clap"] [dev-dependencies] +ctor = "0.2" insta = "1" [package.metadata.release] diff --git a/cli/Cargo.toml b/cli/Cargo.toml index dfcb92b..b2ca265 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -43,4 +43,5 @@ xx = "0.2" [dev-dependencies] assert_cmd = { version = "2", features = ["color-auto"] } +ctor = "0.2" predicates = "3" diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 04eb332..b8b5b4c 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -17,6 +17,9 @@ mod errors; mod hash; mod shebang; +#[cfg(test)] +mod test; + pub fn run(args: &[String]) -> Result<()> { trace!("args: {:?}", args); // if let Some("__USAGE__") = args.get(2).map(|s| s.as_str()) { diff --git a/cli/src/test.rs b/cli/src/test.rs new file mode 100644 index 0000000..a8eccae --- /dev/null +++ b/cli/src/test.rs @@ -0,0 +1,6 @@ +use crate::env; + +#[ctor::ctor] +fn init() { + env::set_var("USAGE_BIN", "usage"); +} diff --git a/src/complete/bash.rs b/src/complete/bash.rs index a4793e0..ec7b628 100644 --- a/src/complete/bash.rs +++ b/src/complete/bash.rs @@ -39,7 +39,7 @@ mod tests { _USAGE_SPEC_MYCLI="$(mycli complete --usage)" fi - COMPREPLY=( $(/Users/jdx/src/usage/target/debug/deps/usage-6b6342071eb3064a complete-word -s "${_USAGE_SPEC_MYCLI}" --cword="$COMP_CWORD" -- "${COMP_WORDS[@]}") ) + COMPREPLY=( $(usage complete-word -s "${_USAGE_SPEC_MYCLI}" --cword="$COMP_CWORD" -- "${COMP_WORDS[@]}") ) if [[ $? -ne 0 ]]; then unset COMPREPLY fi diff --git a/src/complete/fish.rs b/src/complete/fish.rs index 8519a1a..35e32df 100644 --- a/src/complete/fish.rs +++ b/src/complete/fish.rs @@ -23,7 +23,7 @@ mod tests { // let spec = Spec::parse(&Default::default(), spec).unwrap(); assert_snapshot!(complete_fish("mycli", "mycli complete --usage").trim(), @r###" set _usage_spec_mycli (mycli complete --usage) - complete -xc mycli -a '(/Users/jdx/src/usage/target/debug/deps/usage-6b6342071eb3064a complete-word -s "$_usage_spec_mycli" --ctoken=(commandline -t) -- (commandline -op))' + complete -xc mycli -a '(usage complete-word -s "$_usage_spec_mycli" --ctoken=(commandline -t) -- (commandline -op))' "###); } } diff --git a/src/env.rs b/src/env.rs index affd11e..50e2f73 100644 --- a/src/env.rs +++ b/src/env.rs @@ -10,3 +10,11 @@ pub static USAGE_BIN: Lazy = Lazy::new(|| { .or_else(|| current_exe().ok()) .unwrap_or_else(|| "usage".into()) }); + +/// On linux, env::current_exe() follows symlinks which causes problems when updating the CLI. +#[cfg(not(target_os = "macos"))] +pub static USAGE_BIN: Lazy = Lazy::new(|| { + var_os("USAGE_BIN") + .map(PathBuf::from) + .unwrap_or_else(|| "usage".into()) +}); diff --git a/src/lib.rs b/src/lib.rs index 8ce2b6a..9af59c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,9 @@ pub mod context; pub(crate) mod env; pub mod parse; +#[cfg(test)] +mod test; + pub use crate::parse::arg::SpecArg; pub use crate::parse::cmd::SpecCommand; pub use crate::parse::complete::Complete; diff --git a/src/test.rs b/src/test.rs new file mode 100644 index 0000000..a8eccae --- /dev/null +++ b/src/test.rs @@ -0,0 +1,6 @@ +use crate::env; + +#[ctor::ctor] +fn init() { + env::set_var("USAGE_BIN", "usage"); +}