From 71c6efab7ec244cc6cb46c45b93856892c3182f3 Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdxcode@users.noreply.github.com> Date: Sat, 28 Jan 2023 03:01:41 -0600 Subject: [PATCH] use exec method for direnv --- .github/workflows/rtx.yml | 22 ++++--- .tool-versions | 1 - Cargo.lock | 18 ++++++ Cargo.toml | 1 + e2e/run_all_tests | 1 + e2e/test_local | 2 - justfile | 12 +++- src/cli/activate.rs | 6 +- src/cli/alias/ls.rs | 5 +- src/cli/asdf.rs | 10 ++-- src/cli/current.rs | 10 ++-- src/cli/deactivate.rs | 6 +- src/cli/direnv/activate.rs | 3 +- src/cli/direnv/envrc.rs | 13 ++-- src/cli/direnv/exec.rs | 59 +++++++++++++++++++ src/cli/direnv/mod.rs | 3 + ...li__direnv__envrc__test__direnv_envrc.snap | 2 + src/cli/env.rs | 23 ++++---- src/cli/exec.rs | 6 +- src/cli/global.rs | 13 ++-- src/cli/latest.rs | 14 ++--- src/cli/local.rs | 18 +++--- src/cli/ls.rs | 12 ++-- src/cli/ls_remote.rs | 6 +- src/cli/mod.rs | 12 ++-- src/cli/plugins/install.rs | 14 ++--- src/cli/plugins/ls.rs | 34 +++++------ src/cli/plugins/ls_remote.rs | 5 +- ...ns__install__test__plugin_install_url.snap | 11 ++++ ...__uninstall__test__plugin_uninstall-2.snap | 3 +- ...ns__uninstall__test__plugin_uninstall.snap | 3 +- src/cli/plugins/uninstall.rs | 9 ++- src/cli/render_help.rs | 6 +- src/cli/settings/get.rs | 5 +- src/cli/settings/ls.rs | 6 +- src/cli/settings/set.rs | 10 ++-- ..._cli__settings__ls__test__settings_ls.snap | 3 +- ...__settings__set__test__settings_set-2.snap | 3 +- ...li__settings__set__test__settings_set.snap | 3 +- src/cli/settings/unset.rs | 10 ++-- ...tx__cli__activate__test__activate_zsh.snap | 3 +- ...cli__deactivate__test__deactivate_zsh.snap | 3 +- .../rtx__cli__global__test__global-2.snap | 3 +- .../rtx__cli__global__test__global-3.snap | 3 +- .../rtx__cli__global__test__global.snap | 3 +- .../rtx__cli__latest__test__latest.snap | 3 +- ...cli__latest__test__latest_asdf_format.snap | 3 +- .../rtx__cli__local__test__local-2.snap | 3 +- .../rtx__cli__local__test__local-3.snap | 3 +- .../rtx__cli__local__test__local.snap | 3 +- src/cli/where.rs | 10 ++-- src/config/toolset.rs | 13 ++-- src/env.rs | 1 + src/git.rs | 14 +++++ src/plugins.rs | 5 ++ src/test.rs | 14 ++++- test/.tool-versions | 1 + 57 files changed, 293 insertions(+), 198 deletions(-) create mode 100644 src/cli/direnv/exec.rs create mode 100644 src/cli/plugins/snapshots/rtx__cli__plugins__install__test__plugin_install_url.snap diff --git a/.github/workflows/rtx.yml b/.github/workflows/rtx.yml index 745a01e54..12330bc1c 100644 --- a/.github/workflows/rtx.yml +++ b/.github/workflows/rtx.yml @@ -16,12 +16,15 @@ jobs: unit: runs-on: ubuntu-22.04 steps: - - uses: actions/checkout@v3 + - name: Checkout + uses: actions/checkout@v3 - name: Rust Cache uses: Swatinem/rust-cache@v2 + - name: Install direnv + run: sudo apt-get update; sudo apt-get install direnv - name: Install just uses: taiki-e/install-action@just - - name: just test-unit + - name: Run just test-unit uses: nick-fields/retry@v2 env: GITHUB_API_TOKEN: ${{ secrets.RTX_GITHUB_BOT_TOKEN }} @@ -33,17 +36,19 @@ jobs: coverage: runs-on: ubuntu-latest - #if: false container: image: xd009642/tarpaulin:develop-nightly options: --security-opt seccomp=unconfined steps: - - uses: actions/checkout@v3 + - name: Checkout + uses: actions/checkout@v3 - name: Rust Cache uses: Swatinem/rust-cache@v2 + - name: Install direnv + run: curl -sfL https://direnv.net/install.sh | bash - name: Install just uses: taiki-e/install-action@just - - name: Generate code coverage + - name: Run tests with coverage uses: nick-fields/retry@v2 env: GITHUB_API_TOKEN: ${{ secrets.RTX_GITHUB_BOT_TOKEN }} @@ -121,10 +126,13 @@ jobs: e2e-linux: runs-on: ubuntu-22.04 needs: [ build-linux ] + container: + image: xd009642/tarpaulin:develop-nightly + options: --security-opt seccomp=unconfined steps: - uses: actions/checkout@v3 - - name: Install zsh/fish - run: sudo apt-get update; sudo apt-get install zsh fish + - name: Install zsh/fish/direnv + run: sudo apt-get update; sudo apt-get install zsh fish direnv - uses: actions/download-artifact@v3 with: name: tarball-x86_64-unknown-linux-gnu diff --git a/.tool-versions b/.tool-versions index a775f9b91..c638108e3 100644 --- a/.tool-versions +++ b/.tool-versions @@ -2,5 +2,4 @@ shellcheck 0.9.0 shfmt 3.6.0 # test comment #nodejs 18.13.0 -nodejs system jq latest diff --git a/Cargo.lock b/Cargo.lock index d8f3fbc4e..7932e5d17 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1028,6 +1028,7 @@ dependencies = [ "rmp-serde", "serde", "serde_derive", + "serde_json", "shell-escape", "simplelog", "tempfile", @@ -1058,6 +1059,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "ryu" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde" + [[package]] name = "scopeguard" version = "1.1.0" @@ -1087,6 +1094,17 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_json" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "serde_spanned" version = "0.6.0" diff --git a/Cargo.toml b/Cargo.toml index 4758dfbc6..3824fd981 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,6 +55,7 @@ toml_edit = "0.18.0" url = "2.3.1" versions = "4.1.0" build-time = "0.1.2" +serde_json = "1.0.91" [target.'cfg(unix)'.dependencies] exec = "0.3.1" diff --git a/e2e/run_all_tests b/e2e/run_all_tests index 379b35dfa..777066559 100755 --- a/e2e/run_all_tests +++ b/e2e/run_all_tests @@ -4,6 +4,7 @@ set -euo pipefail export RTX_MISSING_RUNTIME_BEHAVIOR="autoinstall" export RTX_DATA_DIR="/tmp/rtx" export PATH="$PWD/target/debug:$PATH" +unset GOPATH rtx plugin add nodejs rtx plugin add shfmt diff --git a/e2e/test_local b/e2e/test_local index 983c877b6..7db1c2893 100755 --- a/e2e/test_local +++ b/e2e/test_local @@ -11,7 +11,6 @@ assert "rtx local -p" "#python 3.11.1 3.10.9 # foo shellcheck 0.9.0 shfmt 3.6.0 # test comment #nodejs 18.13.0 -nodejs system jq latest " @@ -19,7 +18,6 @@ assert "rtx local -p shfmt@3.5.0" "#python 3.11.1 3.10.9 # foo shellcheck 0.9.0 shfmt 3.5.0 # test comment #nodejs 18.13.0 -nodejs system jq latest " diff --git a/justfile b/justfile index 7ce9e1c8d..98ce61865 100644 --- a/justfile +++ b/justfile @@ -17,7 +17,6 @@ alias t := test test: test-unit test-e2e test-setup: build - rtx install test-update-snapshots: test-setup cargo insta test --accept @@ -28,11 +27,18 @@ test-unit: test-setup test-e2e: test-setup build ./e2e/run_all_tests -test-coverage: test-setup - rtx --version +test-coverage: clean test-setup cargo +nightly tarpaulin \ --all-features --workspace \ --timeout 120 --out Xml --ignore-tests + # RUSTFLAGS='-Cinstrument-coverage' LLVM_PROFILE_FILE='cargo-test-%p-%m.profraw' cargo test + # grcov . --binary-path ./target/debug/ -s . -t lcov --branch --ignore-not-existing --ignore "/*" -o lcov.info + +clean: + cargo clean + rm -rf target + rm -rf *.profraw + rm -rf coverage lint: cargo clippy diff --git a/src/cli/activate.rs b/src/cli/activate.rs index d7aae7bcb..210f2f7e7 100644 --- a/src/cli/activate.rs +++ b/src/cli/activate.rs @@ -48,11 +48,9 @@ mod test { use crate::assert_cli; - use super::*; - #[test] fn test_activate_zsh() { - let Output { stdout, .. } = assert_cli!("activate", "-s", "zsh"); - assert_display_snapshot!(stdout.content); + let stdout = assert_cli!("activate", "-s", "zsh"); + assert_display_snapshot!(stdout); } } diff --git a/src/cli/alias/ls.rs b/src/cli/alias/ls.rs index 8b758be3d..32792cc12 100644 --- a/src/cli/alias/ls.rs +++ b/src/cli/alias/ls.rs @@ -49,12 +49,11 @@ const AFTER_LONG_HELP: &str = indoc! {r#" mod test { use crate::assert_cli; use crate::cli::test::ensure_plugin_installed; - use crate::output::Output; #[test] fn test_alias_ls() { ensure_plugin_installed("shfmt"); - let Output { stdout, .. } = assert_cli!("aliases"); - assert!(stdout.content.contains("my/alias")); + let stdout = assert_cli!("aliases"); + assert!(stdout.contains("my/alias")); } } diff --git a/src/cli/asdf.rs b/src/cli/asdf.rs index 6c534fba3..a2176cb0d 100644 --- a/src/cli/asdf.rs +++ b/src/cli/asdf.rs @@ -63,19 +63,17 @@ fn list_versions(out: &mut Output, args: &Vec) -> Result<()> { mod test { use crate::assert_cli; - use super::*; - #[test] fn test_fake_asdf() { - let Output { stdout, .. } = assert_cli!("asdf", "-v"); - assert!(stdout.content.starts_with("rtx ")); + let stdout = assert_cli!("asdf", "-v"); + assert!(stdout.starts_with("rtx ")); } #[test] fn test_fake_asdf_list() { assert_cli!("plugin", "install", "shfmt"); assert_cli!("install", "shfmt@2"); - let Output { stdout, .. } = assert_cli!("asdf", "list", "shfmt"); - assert!(stdout.content.contains('2')); + let stdout = assert_cli!("asdf", "list", "shfmt"); + assert!(stdout.contains('2')); } } diff --git a/src/cli/current.rs b/src/cli/current.rs index 6d6dd37c0..6e1ac8a20 100644 --- a/src/cli/current.rs +++ b/src/cli/current.rs @@ -54,23 +54,21 @@ mod test { use crate::assert_cli; - use super::*; - #[test] fn test_current() { assert_cli!("plugin", "add", "shellcheck"); assert_cli!("install"); - let Output { stdout, .. } = assert_cli!("current"); + let stdout = assert_cli!("current"); let re = Regex::new(r"-> shellcheck\s+0\.9\.0\s+").unwrap(); - assert!(re.is_match(&stdout.content)); + assert!(re.is_match(&stdout)); } #[test] fn test_current_with_runtimes() { assert_cli!("plugin", "add", "shfmt"); assert_cli!("install"); - let Output { stdout, .. } = assert_cli!("current", "shfmt"); + let stdout = assert_cli!("current", "shfmt"); let re = Regex::new(r"-> shfmt\s+3\.5\.2\s+").unwrap(); - assert!(re.is_match(&stdout.content)); + assert!(re.is_match(&stdout)); } } diff --git a/src/cli/deactivate.rs b/src/cli/deactivate.rs index 3aa0bcc5f..6bede6091 100644 --- a/src/cli/deactivate.rs +++ b/src/cli/deactivate.rs @@ -42,12 +42,10 @@ mod test { use crate::assert_cli; - use super::*; - #[test] fn test_deactivate_zsh() { std::env::set_var("NO_COLOR", "1"); - let Output { stdout, .. } = assert_cli!("deactivate", "-s", "zsh"); - assert_display_snapshot!(stdout.content); + let stdout = assert_cli!("deactivate", "-s", "zsh"); + assert_display_snapshot!(stdout); } } diff --git a/src/cli/direnv/activate.rs b/src/cli/direnv/activate.rs index 8f0ff10bc..d3ea81ef3 100644 --- a/src/cli/direnv/activate.rs +++ b/src/cli/direnv/activate.rs @@ -20,10 +20,11 @@ impl Command for DirenvActivate { fn run(self, _config: Config, out: &mut Output) -> Result<()> { rtxprintln!( out, + // source_env "$(rtx direnv envrc "$@")" indoc! {r#" ### Do not edit. This was autogenerated by 'rtx direnv' ### use_rtx() {{ - source_env "$(rtx direnv envrc "$@")" + direnv_load rtx direnv exec }} "#} ); diff --git a/src/cli/direnv/envrc.rs b/src/cli/direnv/envrc.rs index 20d33271d..ee209775a 100644 --- a/src/cli/direnv/envrc.rs +++ b/src/cli/direnv/envrc.rs @@ -5,11 +5,11 @@ use std::ops::Deref; use color_eyre::eyre::Result; use crate::cli::command::Command; -use crate::config::settings::MissingRuntimeBehavior::{AutoInstall, Warn}; +use crate::config::settings::MissingRuntimeBehavior::{Prompt, Warn}; use crate::config::Config; -use crate::dirs; use crate::hash::hash_to_str; use crate::output::Output; +use crate::{dirs, env}; /// [internal] This is an internal command that writes an envrc file /// for direnv to consume. @@ -19,11 +19,11 @@ pub struct Envrc {} impl Command for Envrc { fn run(self, mut config: Config, out: &mut Output) -> Result<()> { - if config.settings.missing_runtime_behavior == AutoInstall { + if config.settings.missing_runtime_behavior == Prompt { config.settings.missing_runtime_behavior = Warn; } config.ensure_installed()?; - let envrc_path = dirs::ROOT + let envrc_path = env::RTX_TMP_DIR .join("direnv") .join(hash_to_str(dirs::CURRENT.deref()) + ".envrc"); @@ -67,8 +67,9 @@ mod test { #[test] fn test_direnv_envrc() { - let Output { stdout, .. } = assert_cli!("direnv", "envrc"); - let envrc = fs::read_to_string(stdout.content.trim()).unwrap(); + assert_cli!("install"); + let stdout = assert_cli!("direnv", "envrc"); + let envrc = fs::read_to_string(stdout.trim()).unwrap(); let envrc = envrc.replace(dirs::HOME.to_string_lossy().as_ref(), "~"); assert_display_snapshot!(envrc); } diff --git a/src/cli/direnv/exec.rs b/src/cli/direnv/exec.rs new file mode 100644 index 000000000..aecbbf9ea --- /dev/null +++ b/src/cli/direnv/exec.rs @@ -0,0 +1,59 @@ +use color_eyre::eyre::Result; +use serde_derive::Deserialize; + +use crate::cli::command::Command; +use crate::cmd; +use crate::config::settings::MissingRuntimeBehavior::{Prompt, Warn}; +use crate::config::Config; +use crate::output::Output; + +/// [internal] This is an internal command that writes an envrc file +/// for direnv to consume. +#[derive(Debug, clap::Args)] +#[clap(verbatim_doc_comment, hide = true)] +pub struct DirenvExec {} + +#[derive(Debug, Default, Deserialize)] +struct DirenvWatches { + #[serde(rename(deserialize = "DIRENV_WATCHES"))] + watches: String, +} + +impl Command for DirenvExec { + fn run(self, mut config: Config, out: &mut Output) -> Result<()> { + if config.settings.missing_runtime_behavior == Prompt { + config.settings.missing_runtime_behavior = Warn; + } + config.ensure_installed()?; + let mut cmd = if cfg!(test) { + cmd!("env") + } else { + cmd!("direnv", "dump") + }; + + for (k, v) in config.env()? { + cmd = cmd.env(k, v); + } + cmd = cmd.env("PATH", config.path_env()?); + + let json = cmd!("direnv", "watch", "json", ".tool-versions").read()?; + let w: DirenvWatches = serde_json::from_str(&json)?; + cmd = cmd.env("DIRENV_WATCHES", w.watches); + + rtxprint!(out, "{}", cmd.read()?); + Ok(()) + } +} + +#[cfg(test)] +mod test { + use crate::assert_cli; + use crate::cli::test::grep; + use pretty_assertions::assert_str_eq; + + #[test] + fn test_direnv_exec() { + let stdout = assert_cli!("direnv", "exec"); + assert_str_eq!(grep(stdout, "JDXCODE_TINY="), "JDXCODE_TINY=2.1.0"); + } +} diff --git a/src/cli/direnv/mod.rs b/src/cli/direnv/mod.rs index 57343bd0e..54115c3f1 100644 --- a/src/cli/direnv/mod.rs +++ b/src/cli/direnv/mod.rs @@ -7,6 +7,7 @@ use crate::output::Output; mod activate; mod envrc; +mod exec; /// Output direnv function to use rtx inside direnv /// @@ -25,6 +26,7 @@ pub struct Direnv { #[derive(Debug, Subcommand)] enum Commands { Envrc(envrc::Envrc), + Exec(exec::DirenvExec), Activate(activate::DirenvActivate), } @@ -33,6 +35,7 @@ impl Commands { match self { Self::Activate(cmd) => cmd.run(config, out), Self::Envrc(cmd) => cmd.run(config, out), + Self::Exec(cmd) => cmd.run(config, out), } } } diff --git a/src/cli/direnv/snapshots/rtx__cli__direnv__envrc__test__direnv_envrc.snap b/src/cli/direnv/snapshots/rtx__cli__direnv__envrc__test__direnv_envrc.snap index 0b3065320..915c992ad 100644 --- a/src/cli/direnv/snapshots/rtx__cli__direnv__envrc__test__direnv_envrc.snap +++ b/src/cli/direnv/snapshots/rtx__cli__direnv__envrc__test__direnv_envrc.snap @@ -6,7 +6,9 @@ expression: envrc watch_file ~/cwd/.tool-versions watch_file ~/cwd/.node-version watch_file ~/.tool-versions +export JDXCODE_TINY=2.1.0 PATH_add ~/data/installs/shfmt/3.5.2/bin PATH_add ~/data/installs/jq/1.6/bin +PATH_add ~/data/installs/tiny/2.1.0/bin PATH_add ~/data/installs/shellcheck/0.9.0/bin diff --git a/src/cli/env.rs b/src/cli/env.rs index 7561b5dba..80eb57b27 100644 --- a/src/cli/env.rs +++ b/src/cli/env.rs @@ -56,15 +56,16 @@ Examples: #[cfg(test)] mod test { use crate::assert_cli; + use crate::cli::test::grep; use crate::dirs; - use crate::output::Output; + use pretty_assertions::assert_str_eq; #[test] fn test_env() { assert_cli!("plugin", "add", "shfmt"); assert_cli!("install"); - let Output { stdout, .. } = assert_cli!("env", "-s", "bash"); - assert!(stdout.content.contains( + let stdout = assert_cli!("env", "-s", "bash"); + assert!(stdout.contains( dirs::ROOT .join("installs/shfmt/3.5.2/bin") .to_string_lossy() @@ -76,9 +77,9 @@ mod test { fn test_env_with_runtime_arg() { assert_cli!("plugin", "add", "shfmt"); assert_cli!("install", "shfmt@3.5"); - let Output { stdout, .. } = assert_cli!("env", "shfmt@3.5", "-s", "bash"); + let stdout = assert_cli!("env", "shfmt@3.5", "-s", "bash"); - assert!(stdout.content.contains( + assert!(stdout.contains( dirs::ROOT .join("installs/shfmt/3.5.2/bin") .to_string_lossy() @@ -90,8 +91,8 @@ mod test { fn test_env_alias() { assert_cli!("plugin", "add", "shfmt"); assert_cli!("install", "shfmt@my/alias"); - let Output { stdout, .. } = assert_cli!("env", "shfmt@my/alias", "-s", "bash"); - assert!(stdout.content.contains( + let stdout = assert_cli!("env", "shfmt@my/alias", "-s", "bash"); + assert!(stdout.contains( dirs::ROOT .join("installs/shfmt/3.0.2") .to_string_lossy() @@ -100,10 +101,8 @@ mod test { } #[test] - fn test_env_golang() { - assert_cli!("plugin", "add", "golang"); - assert_cli!("install", "golang"); - let Output { stdout, .. } = assert_cli!("env", "golang", "-s", "bash"); - assert!(stdout.content.contains("GOROOT=")); + fn test_env_tiny() { + let stdout = assert_cli!("env", "tiny@1", "-s", "bash"); + assert_str_eq!(grep(stdout, "JDXCODE"), "export JDXCODE_TINY=1.0.1"); } } diff --git a/src/cli/exec.rs b/src/cli/exec.rs index 68d02473f..00cf8c72e 100644 --- a/src/cli/exec.rs +++ b/src/cli/exec.rs @@ -121,17 +121,13 @@ mod test { #[test] fn test_exec_ok() { - assert_cli!("plugin", "a", "jq"); - assert_cli!("install"); assert_cli!("exec", "--", "jq", "--version"); } #[test] fn test_exec_fail() { - assert_cli!("install"); - assert_cli!("install", "nodejs"); let _ = cli_run( - &vec!["rtx", "exec", "--", "node", "-e", "process.exit(1)"] + &vec!["rtx", "exec", "--", "jq", "-invalid"] .into_iter() .map(String::from) .collect::>(), diff --git a/src/cli/global.rs b/src/cli/global.rs index 03efb0d7d..4b7a51287 100644 --- a/src/cli/global.rs +++ b/src/cli/global.rs @@ -76,7 +76,6 @@ mod test { use insta::assert_snapshot; - use crate::output::Output; use crate::{assert_cli, dirs}; #[test] @@ -86,12 +85,12 @@ mod test { let _ = fs::remove_file(&cf_path); assert_cli!("install", "shfmt@2"); - let Output { stdout, .. } = assert_cli!("global", "shfmt@2"); - assert_snapshot!(stdout.content); - let Output { stdout, .. } = assert_cli!("global", "--fuzzy", "shfmt@2"); - assert_snapshot!(stdout.content); - let Output { stdout, .. } = assert_cli!("global", "--remove", "nodejs"); - assert_snapshot!(stdout.content); + let stdout = assert_cli!("global", "shfmt@2"); + assert_snapshot!(stdout); + let stdout = assert_cli!("global", "--fuzzy", "shfmt@2"); + assert_snapshot!(stdout); + let stdout = assert_cli!("global", "--remove", "nodejs"); + assert_snapshot!(stdout); if let Some(orig) = orig { fs::write(cf_path, orig).unwrap(); diff --git a/src/cli/latest.rs b/src/cli/latest.rs index 1c6e6006d..0f409edab 100644 --- a/src/cli/latest.rs +++ b/src/cli/latest.rs @@ -54,25 +54,23 @@ mod test { use crate::assert_cli; - use super::*; - #[test] fn test_latest() { assert_cli!("plugins", "install", "nodejs"); - let Output { stdout, .. } = assert_cli!("latest", "nodejs@12"); - assert_display_snapshot!(stdout.content); + let stdout = assert_cli!("latest", "nodejs@12"); + assert_display_snapshot!(stdout); } #[test] fn test_latest_ruby() { assert_cli!("plugins", "install", "ruby"); - let Output { stdout, .. } = assert_cli!("latest", "ruby"); - assert!(stdout.content.starts_with("3.")); + let stdout = assert_cli!("latest", "ruby"); + assert!(stdout.starts_with("3.")); } #[test] fn test_latest_asdf_format() { - let Output { stdout, .. } = assert_cli!("latest", "nodejs", "12"); - assert_display_snapshot!(stdout.content); + let stdout = assert_cli!("latest", "nodejs", "12"); + assert_display_snapshot!(stdout); } } diff --git a/src/cli/local.rs b/src/cli/local.rs index a07164d84..49f659fbb 100644 --- a/src/cli/local.rs +++ b/src/cli/local.rs @@ -101,7 +101,7 @@ mod test { use pretty_assertions::assert_str_eq; use crate::cli::test::grep; - use crate::output::Output; + use crate::{assert_cli, dirs}; #[test] @@ -111,15 +111,15 @@ mod test { assert_cli!("plugin", "add", "nodejs"); assert_cli!("install", "shfmt@2"); - let Output { stdout, .. } = assert_cli!("local", "shfmt@2"); - assert_snapshot!(stdout.content); - let Output { stdout, .. } = assert_cli!("local", "--fuzzy", "shfmt@2"); - assert_snapshot!(stdout.content); - let Output { stdout, .. } = assert_cli!("local", "--remove", "nodejs"); - assert_snapshot!(stdout.content); - let Output { stdout, .. } = assert_cli!("ls", "--current"); + let stdout = assert_cli!("local", "shfmt@2"); + assert_snapshot!(stdout); + let stdout = assert_cli!("local", "--fuzzy", "shfmt@2"); + assert_snapshot!(stdout); + let stdout = assert_cli!("local", "--remove", "nodejs"); + assert_snapshot!(stdout); + let stdout = assert_cli!("ls", "--current"); assert_str_eq!( - grep(&stdout, "nodejs").unwrap(), + grep(stdout, "nodejs"), " nodejs 18.0.0 (missing) (set by ~/cwd/.node-version)" ); diff --git a/src/cli/ls.rs b/src/cli/ls.rs index 3c7792abe..dca52d336 100644 --- a/src/cli/ls.rs +++ b/src/cli/ls.rs @@ -153,21 +153,19 @@ mod test { use crate::assert_cli; - use super::*; - #[test] fn test_list() { assert_cli!("install"); assert_cli!("install", "shfmt@3.5.0"); - let Output { stdout, .. } = assert_cli!("list"); + let stdout = assert_cli!("list"); let re = Regex::new(r"-> shellcheck\s+0\.9\.0\s+").unwrap(); - assert!(re.is_match(&stdout.content)); + assert!(re.is_match(&stdout)); let re = Regex::new(r" {3}shfmt\s+3\.5\.0\s+").unwrap(); - assert!(re.is_match(&stdout.content)); + assert!(re.is_match(&stdout)); assert_cli!("uninstall", "shfmt@3.5.2"); - let Output { stdout, .. } = assert_cli!("list"); + let stdout = assert_cli!("list"); let re = Regex::new(r" {3}shfmt\s+3\.5\.2 \(missing\)\s+").unwrap(); - assert!(re.is_match(&stdout.content)); + assert!(re.is_match(&stdout)); } } diff --git a/src/cli/ls_remote.rs b/src/cli/ls_remote.rs index 4705e0989..29b503fb1 100644 --- a/src/cli/ls_remote.rs +++ b/src/cli/ls_remote.rs @@ -44,12 +44,10 @@ mod test { use crate::assert_cli; use crate::cli::test::ensure_plugin_installed; - use super::*; - #[test] fn test_list_remote() { ensure_plugin_installed("nodejs"); - let Output { stdout, .. } = assert_cli!("list-remote", "nodejs"); - assert!(stdout.content.contains("18.0.0")); + let stdout = assert_cli!("list-remote", "nodejs"); + assert!(stdout.contains("18.0.0")); } } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 2bc96ca9f..d8898d47b 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -172,9 +172,11 @@ const AFTER_HELP: &str = indoc! {" #[cfg(test)] pub mod test { + use crate::config::settings::MissingRuntimeBehavior::AutoInstall; use crate::config::settings::Settings; - use crate::output::OutputStream; + use crate::dirs; + use crate::plugins::{Plugin, PluginName}; use super::*; @@ -191,7 +193,7 @@ pub mod test { macro_rules! assert_cli { ($($args:expr),+) => {{ let args = &vec!["rtx".into(), $($args.into()),+]; - $crate::cli::test::cli_run(args).unwrap() + $crate::cli::test::cli_run(args).unwrap().stdout.content }}; } @@ -211,10 +213,12 @@ pub mod test { Plugin::load_ensure_installed(&PluginName::from(name), &settings).unwrap(); } - pub fn grep<'a>(output: &'a OutputStream, pattern: &str) -> Option<&'a str> { + pub fn grep(output: String, pattern: &str) -> String { output - .content .split('\n') .find(|line| line.contains(pattern)) + .map(|line| line.to_string()) + .unwrap_or_default() + .replace(dirs::HOME.to_string_lossy().as_ref(), "~") } } diff --git a/src/cli/plugins/install.rs b/src/cli/plugins/install.rs index f56796bef..a3454e63c 100644 --- a/src/cli/plugins/install.rs +++ b/src/cli/plugins/install.rs @@ -94,12 +94,10 @@ EXAMPLES: #[cfg(test)] mod test { - use insta::assert_display_snapshot; - use pretty_assertions::assert_str_eq; + use insta::{assert_display_snapshot, assert_snapshot}; use crate::assert_cli; - use crate::cli::test::{cli_run, grep}; - use crate::output::Output; + use crate::cli::test::cli_run; #[test] fn test_plugin_install() { @@ -114,12 +112,8 @@ mod test { "-f", "https://github.com/jdxcode/asdf-nodejs" ); - let Output { stdout, .. } = assert_cli!("plugin", "--urls"); - let line = grep(&stdout, "nodejs").unwrap(); - assert_str_eq!( - line, - "nodejs https://github.com/asdf-vm/asdf-nodejs.git" - ); + let stdout = assert_cli!("plugin", "--urls"); + assert_snapshot!(stdout); } #[test] diff --git a/src/cli/plugins/ls.rs b/src/cli/plugins/ls.rs index e0876f5ee..a0d62b63f 100644 --- a/src/cli/plugins/ls.rs +++ b/src/cli/plugins/ls.rs @@ -5,7 +5,6 @@ use crate::cli::command::Command; use crate::cli::plugins::ls_remote::PluginsLsRemote; use crate::config::Config; use crate::output::Output; -use crate::shorthand_repository::ShorthandRepo; /// List installed plugins /// @@ -34,9 +33,7 @@ impl Command for PluginsLs { for plugin in config.ts.list_installed_plugins() { if self.urls { - let shr = ShorthandRepo::new(&config.settings); - let url = shr.lookup(&plugin.name)?; - rtxprintln!(out, "{:29} {}", plugin.name, url); + rtxprintln!(out, "{:29} {}", plugin.name, plugin.get_remote_url()?); } else { rtxprintln!(out, "{}", plugin.name); } @@ -63,31 +60,30 @@ const AFTER_LONG_HELP: &str = indoc! {r#" #[cfg(test)] mod test { use crate::assert_cli; - use crate::cli::test::ensure_plugin_installed; - use crate::output::Output; + use crate::cli::test::grep; + use pretty_assertions::assert_str_eq; #[test] fn test_plugin_list() { - ensure_plugin_installed("nodejs"); - let Output { stdout, .. } = assert_cli!("plugin", "list"); - assert!(stdout.content.contains("nodejs")); + let stdout = assert_cli!("plugin", "list"); + assert_str_eq!(grep(stdout, "nodejs"), "nodejs"); } #[test] fn test_plugin_list_urls() { - ensure_plugin_installed("nodejs"); - let Output { stdout, .. } = assert_cli!("plugin", "list", "--urls"); - assert!(stdout - .content - .contains("https://github.com/asdf-vm/asdf-nodejs.git")); + let stdout = assert_cli!("plugin", "list", "--urls"); + assert_str_eq!( + grep(stdout, "shfmt"), + "shfmt https://github.com/luizm/asdf-shfmt.git" + ); } #[test] fn test_plugin_list_all() { - ensure_plugin_installed("nodejs"); - let Output { stdout, .. } = assert_cli!("plugin", "list", "--all", "--urls"); - assert!(stdout - .content - .contains("https://github.com/asdf-vm/asdf-nodejs.git")); + let stdout = assert_cli!("plugin", "list", "--all", "--urls"); + assert_str_eq!( + grep(stdout, "zephyr"), + "zephyr https://github.com/nsaunders/asdf-zephyr.git" + ); } } diff --git a/src/cli/plugins/ls_remote.rs b/src/cli/plugins/ls_remote.rs index 6b6ad2aa6..ddbe7a712 100644 --- a/src/cli/plugins/ls_remote.rs +++ b/src/cli/plugins/ls_remote.rs @@ -58,12 +58,11 @@ Examples: mod test { use crate::assert_cli; use crate::cli::test::ensure_plugin_installed; - use crate::output::Output; #[test] fn test_plugin_list_remote() { ensure_plugin_installed("nodejs"); - let Output { stdout, .. } = assert_cli!("plugin", "ls-remote"); - assert!(stdout.content.contains("nodejs")); + let stdout = assert_cli!("plugin", "ls-remote"); + assert!(stdout.contains("nodejs")); } } diff --git a/src/cli/plugins/snapshots/rtx__cli__plugins__install__test__plugin_install_url.snap b/src/cli/plugins/snapshots/rtx__cli__plugins__install__test__plugin_install_url.snap new file mode 100644 index 000000000..9af9072cd --- /dev/null +++ b/src/cli/plugins/snapshots/rtx__cli__plugins__install__test__plugin_install_url.snap @@ -0,0 +1,11 @@ +--- +source: src/cli/plugins/install.rs +expression: stdout +--- +jq https://github.com/azmcode/asdf-jq.git +nodejs https://github.com/jdxcode/asdf-nodejs +ruby https://github.com/asdf-vm/asdf-ruby.git +shellcheck https://github.com/luizm/asdf-shellcheck.git +shfmt https://github.com/luizm/asdf-shfmt.git +tiny https://github.com/jdxcode/asdf-tiny + diff --git a/src/cli/plugins/snapshots/rtx__cli__plugins__uninstall__test__plugin_uninstall-2.snap b/src/cli/plugins/snapshots/rtx__cli__plugins__uninstall__test__plugin_uninstall-2.snap index 9f126b122..7cdc3496c 100644 --- a/src/cli/plugins/snapshots/rtx__cli__plugins__uninstall__test__plugin_uninstall-2.snap +++ b/src/cli/plugins/snapshots/rtx__cli__plugins__uninstall__test__plugin_uninstall-2.snap @@ -1,5 +1,4 @@ --- source: src/cli/plugins/uninstall.rs -expression: stdout.content +expression: stdout --- - diff --git a/src/cli/plugins/snapshots/rtx__cli__plugins__uninstall__test__plugin_uninstall.snap b/src/cli/plugins/snapshots/rtx__cli__plugins__uninstall__test__plugin_uninstall.snap index b13c482b8..28674a18e 100644 --- a/src/cli/plugins/snapshots/rtx__cli__plugins__uninstall__test__plugin_uninstall.snap +++ b/src/cli/plugins/snapshots/rtx__cli__plugins__uninstall__test__plugin_uninstall.snap @@ -1,6 +1,5 @@ --- source: src/cli/plugins/uninstall.rs -expression: stdout.content +expression: stdout --- uninstalling plugin: nodejs - diff --git a/src/cli/plugins/uninstall.rs b/src/cli/plugins/uninstall.rs index 392a0aaf5..6920ba04d 100644 --- a/src/cli/plugins/uninstall.rs +++ b/src/cli/plugins/uninstall.rs @@ -49,16 +49,15 @@ mod test { use crate::assert_cli; use crate::cli::test::ensure_plugin_installed; - use crate::output::Output; #[test] fn test_plugin_uninstall() { ensure_plugin_installed("nodejs"); - let Output { stdout, .. } = assert_cli!("plugin", "rm", "nodejs"); - assert_snapshot!(stdout.content); + let stdout = assert_cli!("plugin", "rm", "nodejs"); + assert_snapshot!(stdout); - let Output { stdout, .. } = assert_cli!("plugin", "rm", "nodejs"); - assert_snapshot!(stdout.content); + let stdout = assert_cli!("plugin", "rm", "nodejs"); + assert_snapshot!(stdout); } } diff --git a/src/cli/render_help.rs b/src/cli/render_help.rs index 03bef7d48..58e61a46e 100644 --- a/src/cli/render_help.rs +++ b/src/cli/render_help.rs @@ -667,11 +667,9 @@ fn render_command(parent: Option<&str>, c: &mut clap::Command) -> Option mod tests { use crate::assert_cli; - use super::*; - #[test] fn test_render_help() { - let Output { stdout, .. } = assert_cli!("render-help"); - assert!(stdout.content.contains("Quickstart")); + let stdout = assert_cli!("render-help"); + assert!(stdout.contains("Quickstart")); } } diff --git a/src/cli/settings/get.rs b/src/cli/settings/get.rs index f3aea1a93..19d03ddbc 100644 --- a/src/cli/settings/get.rs +++ b/src/cli/settings/get.rs @@ -37,15 +37,14 @@ const AFTER_LONG_HELP: &str = indoc! {r#" mod test { use insta::{assert_display_snapshot, assert_snapshot}; - use crate::output::Output; use crate::test::reset_config; use crate::{assert_cli, assert_cli_err}; #[test] fn test_settings_get() { reset_config(); - let Output { stdout, .. } = assert_cli!("settings", "get", "legacy_version_file"); - assert_snapshot!(stdout.content, @r###" + let stdout = assert_cli!("settings", "get", "legacy_version_file"); + assert_snapshot!(stdout, @r###" true "###); } diff --git a/src/cli/settings/ls.rs b/src/cli/settings/ls.rs index dcd493d90..c13d93278 100644 --- a/src/cli/settings/ls.rs +++ b/src/cli/settings/ls.rs @@ -35,13 +35,13 @@ mod test { use insta::assert_snapshot; use crate::assert_cli; - use crate::output::Output; + use crate::test::reset_config; #[test] fn test_settings_ls() { reset_config(); - let Output { stdout, .. } = assert_cli!("settings"); - assert_snapshot!(stdout.content); + let stdout = assert_cli!("settings"); + assert_snapshot!(stdout); } } diff --git a/src/cli/settings/set.rs b/src/cli/settings/set.rs index 60d983513..c1c38dd50 100644 --- a/src/cli/settings/set.rs +++ b/src/cli/settings/set.rs @@ -61,14 +61,14 @@ pub mod test { use insta::assert_snapshot; use crate::assert_cli; - use crate::output::Output; + use crate::test::reset_config; #[test] fn test_settings_set() { reset_config(); - let Output { stdout, .. } = assert_cli!("settings"); - assert_snapshot!(stdout.content); + let stdout = assert_cli!("settings"); + assert_snapshot!(stdout); assert_cli!("settings", "set", "missing_runtime_behavior", "warn"); assert_cli!("settings", "set", "legacy_version_file", "false"); @@ -92,8 +92,8 @@ pub mod test { "2" ); - let Output { stdout, .. } = assert_cli!("settings"); - assert_snapshot!(stdout.content); + let stdout = assert_cli!("settings"); + assert_snapshot!(stdout); reset_config(); } } diff --git a/src/cli/settings/snapshots/rtx__cli__settings__ls__test__settings_ls.snap b/src/cli/settings/snapshots/rtx__cli__settings__ls__test__settings_ls.snap index d2a4d7081..20e5a2cf2 100644 --- a/src/cli/settings/snapshots/rtx__cli__settings__ls__test__settings_ls.snap +++ b/src/cli/settings/snapshots/rtx__cli__settings__ls__test__settings_ls.snap @@ -1,6 +1,6 @@ --- source: src/cli/settings/ls.rs -expression: stdout.content +expression: stdout --- missing_runtime_behavior = autoinstall always_keep_download = true @@ -8,4 +8,3 @@ legacy_version_file = true disable_plugin_short_name_repository = false plugin_autoupdate_last_check_duration = 20 plugin_repository_last_check_duration = 20 - diff --git a/src/cli/settings/snapshots/rtx__cli__settings__set__test__settings_set-2.snap b/src/cli/settings/snapshots/rtx__cli__settings__set__test__settings_set-2.snap index 5690f3681..2b3e17b2f 100644 --- a/src/cli/settings/snapshots/rtx__cli__settings__set__test__settings_set-2.snap +++ b/src/cli/settings/snapshots/rtx__cli__settings__set__test__settings_set-2.snap @@ -1,6 +1,6 @@ --- source: src/cli/settings/set.rs -expression: stdout.content +expression: stdout --- missing_runtime_behavior = autoinstall always_keep_download = true @@ -8,4 +8,3 @@ legacy_version_file = false disable_plugin_short_name_repository = true plugin_autoupdate_last_check_duration = 1 plugin_repository_last_check_duration = 2 - diff --git a/src/cli/settings/snapshots/rtx__cli__settings__set__test__settings_set.snap b/src/cli/settings/snapshots/rtx__cli__settings__set__test__settings_set.snap index c4cc31272..a4e7bdcea 100644 --- a/src/cli/settings/snapshots/rtx__cli__settings__set__test__settings_set.snap +++ b/src/cli/settings/snapshots/rtx__cli__settings__set__test__settings_set.snap @@ -1,6 +1,6 @@ --- source: src/cli/settings/set.rs -expression: stdout.content +expression: stdout --- missing_runtime_behavior = autoinstall always_keep_download = true @@ -8,4 +8,3 @@ legacy_version_file = true disable_plugin_short_name_repository = false plugin_autoupdate_last_check_duration = 20 plugin_repository_last_check_duration = 20 - diff --git a/src/cli/settings/unset.rs b/src/cli/settings/unset.rs index 3aba954c6..8d1367da2 100644 --- a/src/cli/settings/unset.rs +++ b/src/cli/settings/unset.rs @@ -34,15 +34,15 @@ mod test { use insta::assert_snapshot; use crate::assert_cli; - use crate::output::Output; + use crate::test::reset_config; #[test] fn test_settings_unset() { reset_config(); - let Output { stdout, .. } = assert_cli!("settings"); - assert_snapshot!(stdout.content, @r###" + let stdout = assert_cli!("settings"); + assert_snapshot!(stdout, @r###" missing_runtime_behavior = autoinstall always_keep_download = true legacy_version_file = true @@ -53,8 +53,8 @@ mod test { assert_cli!("settings", "unset", "legacy_version_file"); - let Output { stdout, .. } = assert_cli!("settings"); - assert_snapshot!(stdout.content, @r###" + let stdout = assert_cli!("settings"); + assert_snapshot!(stdout, @r###" missing_runtime_behavior = autoinstall always_keep_download = true legacy_version_file = true diff --git a/src/cli/snapshots/rtx__cli__activate__test__activate_zsh.snap b/src/cli/snapshots/rtx__cli__activate__test__activate_zsh.snap index 0b23fbdfe..49abb0aba 100644 --- a/src/cli/snapshots/rtx__cli__activate__test__activate_zsh.snap +++ b/src/cli/snapshots/rtx__cli__activate__test__activate_zsh.snap @@ -1,6 +1,6 @@ --- source: src/cli/activate.rs -expression: stdout.content +expression: stdout --- export PATH=":$PATH" _rtx_hook() { @@ -16,4 +16,3 @@ typeset -ag chpwd_functions; if [[ -z "${chpwd_functions[(r)_rtx_hook]+1}" ]]; then chpwd_functions=( _rtx_hook ${chpwd_functions[@]} ) fi - diff --git a/src/cli/snapshots/rtx__cli__deactivate__test__deactivate_zsh.snap b/src/cli/snapshots/rtx__cli__deactivate__test__deactivate_zsh.snap index 4cbd11b51..543b4e5e5 100644 --- a/src/cli/snapshots/rtx__cli__deactivate__test__deactivate_zsh.snap +++ b/src/cli/snapshots/rtx__cli__deactivate__test__deactivate_zsh.snap @@ -1,6 +1,5 @@ --- source: src/cli/deactivate.rs -expression: stdout.content +expression: stdout --- unset _rtx_hook; - diff --git a/src/cli/snapshots/rtx__cli__global__test__global-2.snap b/src/cli/snapshots/rtx__cli__global__test__global-2.snap index fcce8aa36..f11c502dd 100644 --- a/src/cli/snapshots/rtx__cli__global__test__global-2.snap +++ b/src/cli/snapshots/rtx__cli__global__test__global-2.snap @@ -1,6 +1,5 @@ --- source: src/cli/global.rs -expression: stdout.content +expression: stdout --- shfmt 2 - diff --git a/src/cli/snapshots/rtx__cli__global__test__global-3.snap b/src/cli/snapshots/rtx__cli__global__test__global-3.snap index fcce8aa36..f11c502dd 100644 --- a/src/cli/snapshots/rtx__cli__global__test__global-3.snap +++ b/src/cli/snapshots/rtx__cli__global__test__global-3.snap @@ -1,6 +1,5 @@ --- source: src/cli/global.rs -expression: stdout.content +expression: stdout --- shfmt 2 - diff --git a/src/cli/snapshots/rtx__cli__global__test__global.snap b/src/cli/snapshots/rtx__cli__global__test__global.snap index 8f07ac09f..debfb3112 100644 --- a/src/cli/snapshots/rtx__cli__global__test__global.snap +++ b/src/cli/snapshots/rtx__cli__global__test__global.snap @@ -1,6 +1,5 @@ --- source: src/cli/global.rs -expression: stdout.content +expression: stdout --- shfmt 2.6.4 - diff --git a/src/cli/snapshots/rtx__cli__latest__test__latest.snap b/src/cli/snapshots/rtx__cli__latest__test__latest.snap index 2621fa915..77f286cc9 100644 --- a/src/cli/snapshots/rtx__cli__latest__test__latest.snap +++ b/src/cli/snapshots/rtx__cli__latest__test__latest.snap @@ -1,6 +1,5 @@ --- source: src/cli/latest.rs -expression: stdout.content +expression: stdout --- 12.22.12 - diff --git a/src/cli/snapshots/rtx__cli__latest__test__latest_asdf_format.snap b/src/cli/snapshots/rtx__cli__latest__test__latest_asdf_format.snap index 2621fa915..77f286cc9 100644 --- a/src/cli/snapshots/rtx__cli__latest__test__latest_asdf_format.snap +++ b/src/cli/snapshots/rtx__cli__latest__test__latest_asdf_format.snap @@ -1,6 +1,5 @@ --- source: src/cli/latest.rs -expression: stdout.content +expression: stdout --- 12.22.12 - diff --git a/src/cli/snapshots/rtx__cli__local__test__local-2.snap b/src/cli/snapshots/rtx__cli__local__test__local-2.snap index 12220a672..9bb147e50 100644 --- a/src/cli/snapshots/rtx__cli__local__test__local-2.snap +++ b/src/cli/snapshots/rtx__cli__local__test__local-2.snap @@ -1,10 +1,9 @@ --- source: src/cli/local.rs -expression: stdout.content +expression: stdout --- #python 3.11.1 3.10.9 # foo shellcheck 0.9.0 shfmt 2 # test comment #nodejs 18.13.0 nodejs system - diff --git a/src/cli/snapshots/rtx__cli__local__test__local-3.snap b/src/cli/snapshots/rtx__cli__local__test__local-3.snap index d1d937051..a2169959a 100644 --- a/src/cli/snapshots/rtx__cli__local__test__local-3.snap +++ b/src/cli/snapshots/rtx__cli__local__test__local-3.snap @@ -1,9 +1,8 @@ --- source: src/cli/local.rs -expression: stdout.content +expression: stdout --- #python 3.11.1 3.10.9 # foo shellcheck 0.9.0 shfmt 2 # test comment #nodejs 18.13.0 - diff --git a/src/cli/snapshots/rtx__cli__local__test__local.snap b/src/cli/snapshots/rtx__cli__local__test__local.snap index c9b0968cd..ec757848f 100644 --- a/src/cli/snapshots/rtx__cli__local__test__local.snap +++ b/src/cli/snapshots/rtx__cli__local__test__local.snap @@ -1,10 +1,9 @@ --- source: src/cli/local.rs -expression: stdout.content +expression: stdout --- #python 3.11.1 3.10.9 # foo shellcheck 0.9.0 shfmt 2.6.4 # test comment #nodejs 18.13.0 nodejs system - diff --git a/src/cli/where.rs b/src/cli/where.rs index 2cd2f5733..0e15480fe 100644 --- a/src/cli/where.rs +++ b/src/cli/where.rs @@ -64,15 +64,13 @@ mod test { use crate::dirs; use crate::{assert_cli, assert_cli_err}; - use super::*; - #[test] fn test_where() { assert_cli!("plugin", "add", "shfmt"); assert_cli!("install"); - let Output { stdout, .. } = assert_cli!("where", "shfmt"); + let stdout = assert_cli!("where", "shfmt"); assert_str_eq!( - stdout.content.trim(), + stdout.trim(), dirs::ROOT.join("installs/shfmt/3.5.2").to_string_lossy() ); } @@ -81,9 +79,9 @@ mod test { fn test_where_alias() { assert_cli!("plugin", "add", "shfmt"); assert_cli!("install", "shfmt@my/alias"); - let Output { stdout, .. } = assert_cli!("where", "shfmt@my/alias"); + let stdout = assert_cli!("where", "shfmt@my/alias"); assert_str_eq!( - stdout.content.trim(), + stdout.trim(), dirs::ROOT.join("installs/shfmt/3.0.2").to_string_lossy() ); } diff --git a/src/config/toolset.rs b/src/config/toolset.rs index 8b99d82eb..3923dc4af 100644 --- a/src/config/toolset.rs +++ b/src/config/toolset.rs @@ -73,20 +73,24 @@ impl Toolset { } pub fn list_plugins(&self) -> Vec> { - self.plugins.values().map(Arc::clone).collect() + self.plugins + .values() + .sorted_by_cached_key(|p| p.name.clone()) + .map(Arc::clone) + .collect() } pub fn list_installed_plugins(&self) -> Vec> { - self.plugins - .values() + self.list_plugins() + .into_iter() .filter(|p| p.is_installed()) - .map(Arc::clone) .collect() } pub fn list_current_plugins(&self) -> Vec> { self.current_versions .keys() + .sorted() .map(|p| self.find_plugin(p).unwrap()) .collect() } @@ -94,6 +98,7 @@ impl Toolset { pub fn list_installed_versions(&self) -> Vec> { self.installed_versions .iter() + .sorted_by_cached_key(|(plugin_name, _)| plugin_name.to_string()) .flat_map(|(_, versions)| versions.iter().map(|(_, rtv)| rtv.clone())) .collect() } diff --git a/src/env.rs b/src/env.rs index c0997fd6a..29bf6f73b 100644 --- a/src/env.rs +++ b/src/env.rs @@ -48,6 +48,7 @@ lazy_static! { .map(PathBuf::from) .unwrap_or_else(|| XDG_DATA_HOME.join("rtx")) }; + pub static ref RTX_TMP_DIR: PathBuf = temp_dir().join("rtx"); pub static ref PATH: OsString = var_os("PATH").unwrap_or_default(); pub static ref SHELL: String = var("SHELL").unwrap_or_else(|_| "sh".into()); pub static ref RTX_EXE: PathBuf = current_exe().unwrap_or_else(|_| "rtx".into()); diff --git a/src/git.rs b/src/git.rs index 2758918e5..a180cf8d3 100644 --- a/src/git.rs +++ b/src/git.rs @@ -73,6 +73,20 @@ impl Git { debug!("current sha for {}: {}", self.dir.display(), &sha); Ok(sha) } + + pub fn get_remote_url(&self) -> Result { + let url = cmd!( + "git", + "-C", + &self.dir, + "config", + "--get", + "remote.origin.url" + ) + .read()?; + debug!("remote url for {}: {}", self.dir.display(), &url); + Ok(url) + } } #[cfg(test)] diff --git a/src/plugins.rs b/src/plugins.rs index fcdc51c4c..51cbae052 100644 --- a/src/plugins.rs +++ b/src/plugins.rs @@ -93,6 +93,11 @@ impl Plugin { self.plugin_path.exists() } + pub fn get_remote_url(&self) -> Result { + let git = Git::new(self.plugin_path.to_path_buf()); + git.get_remote_url() + } + pub fn install(&self, repository: &String) -> Result<()> { debug!("install {} {:?}", self.name, repository); eprint!( diff --git a/src/test.rs b/src/test.rs index 90ac9bc9e..53d17b564 100644 --- a/src/test.rs +++ b/src/test.rs @@ -2,7 +2,7 @@ use std::fs; use indoc::indoc; -use crate::{cmd, env}; +use crate::{assert_cli, cmd, env}; #[ctor::ctor] fn init() { @@ -20,8 +20,18 @@ fn init() { { warn!("failed to reset test files: {}", err); } - reset_config(); + assert_cli!( + "plugin", + "install", + "tiny", + "https://github.com/jdxcode/asdf-tiny" + ); + assert_cli!("plugin", "install", "shellcheck"); + assert_cli!("plugin", "install", "shfmt"); + assert_cli!("plugin", "install", "nodejs"); + assert_cli!("plugin", "install", "jq"); + assert_cli!("install"); } pub fn reset_config() { diff --git a/test/.tool-versions b/test/.tool-versions index ed79d4100..5712a47c9 100644 --- a/test/.tool-versions +++ b/test/.tool-versions @@ -1,2 +1,3 @@ shfmt 2 jq 1.6 +tiny 2