Skip to content

Commit

Permalink
bug: fix order of env and path when using multiple versions (jdx#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx authored Feb 21, 2023
1 parent 12d0257 commit a801565
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 20 deletions.
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ shellcheck 0.9.0
shfmt 3.6.0 # test comment
#nodejs 18.13.0
jq latest
tiny 2 1 3
6 changes: 3 additions & 3 deletions e2e/cd/test_bash
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ assert_path() {
}

test "$(node -v)" = "v18.0.0"
assert_path "~/.rtx/installs/nodejs/18.0.0/bin:~/.rtx/installs/jq/1.6/bin:~/.rtx/installs/shfmt/3.6.0/bin:~/.rtx/installs/shellcheck/0.9.0/bin"
assert_path "~/.rtx/installs/nodejs/18.0.0/bin:~/.rtx/installs/shellcheck/0.9.0/bin:~/.rtx/installs/shfmt/3.6.0/bin:~/.rtx/installs/jq/1.6/bin"

cd 16 && _rtx_hook
test "$(node -v)" = "v16.0.0"
assert_path "~/.rtx/installs/nodejs/16.0.0/bin:~/.rtx/installs/jq/1.6/bin:~/.rtx/installs/shfmt/3.6.0/bin:~/.rtx/installs/shellcheck/0.9.0/bin"
assert_path "~/.rtx/installs/nodejs/16.0.0/bin:~/.rtx/installs/shellcheck/0.9.0/bin:~/.rtx/installs/shfmt/3.6.0/bin:~/.rtx/installs/jq/1.6/bin"

cd .. && _rtx_hook
test "$(node -v)" = "v18.0.0"
assert_path "~/.rtx/installs/nodejs/18.0.0/bin:~/.rtx/installs/jq/1.6/bin:~/.rtx/installs/shfmt/3.6.0/bin:~/.rtx/installs/shellcheck/0.9.0/bin"
assert_path "~/.rtx/installs/nodejs/18.0.0/bin:~/.rtx/installs/shellcheck/0.9.0/bin:~/.rtx/installs/shfmt/3.6.0/bin:~/.rtx/installs/jq/1.6/bin"
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ watch_file ~/cwd/.test-tool-versions
watch_file ~/cwd/.node-version
watch_file ~/.test-tool-versions
export JDXCODE_TINY=2.1.0
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
PATH_add ~/data/installs/jq/1.6/bin
PATH_add ~/data/installs/shfmt/3.5.1/bin
PATH_add ~/data/installs/shellcheck/0.9.0/bin

4 changes: 2 additions & 2 deletions src/cli/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ mod tests {

#[test]
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");
let stdout = assert_cli!("env", "tiny@2", "tiny@1", "tiny@3", "-s", "bash");
assert_str_eq!(grep(stdout, "JDXCODE"), "export JDXCODE_TINY=2.1.0");
}

#[test]
Expand Down
7 changes: 7 additions & 0 deletions src/cli/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ mod tests {
});
}
#[test]
fn test_local_multiple_versions() {
run_test(|| {
assert_cli_snapshot!("local", "tiny@2", "tiny@1", "tiny@3");
assert_cli_snapshot!("bin-paths");
});
}
#[test]
fn test_local_output_current_version() {
run_test(|| {
assert_cli!("local", "tiny", "2");
Expand Down
1 change: 1 addition & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ pub mod tests {
let args = &vec!["rtx".into(), $($args.into()),+];
let output = $crate::cli::tests::cli_run(args).unwrap().stdout.content;
let output = console::strip_ansi_codes(&output).to_string();
let output = output.replace($crate::dirs::HOME.to_string_lossy().as_ref(), "~");
assert_snapshot!(output);
}};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: src/cli/local.rs
expression: output
---
~/data/installs/shellcheck/0.9.0/bin
~/data/installs/shfmt/3.5.1/bin
~/data/installs/tiny/2.1.0/bin
~/data/installs/tiny/1.0.1/bin
~/data/installs/tiny/3.1.0/bin
~/data/installs/jq/1.6/bin

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: src/cli/local.rs
expression: output
---
#python 3.11.1 3.10.9 # foo
shellcheck 0.9.0
shfmt 3.5.1 # test comment
#nodejs 18.13.0
nodejs system
tiny 2 1 3

26 changes: 13 additions & 13 deletions src/toolset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use color_eyre::eyre::Result;
use console::style;
use indexmap::IndexMap;
use itertools::Itertools;

use rayon::prelude::*;
use rayon::ThreadPoolBuilder;

Expand All @@ -22,7 +21,6 @@ use crate::env;
use crate::plugins::{InstallType, Plugin, PluginName};
use crate::runtimes::RuntimeVersion;
use crate::toolset::tool_version_list::ToolVersionList;

use crate::ui::multi_progress_report::MultiProgressReport;
use crate::ui::prompt;

Expand Down Expand Up @@ -61,13 +59,13 @@ impl Toolset {
.or_insert_with(|| ToolVersionList::new(self.source.clone().unwrap()));
versions.add_version(version);
}
pub fn merge(&mut self, other: Toolset) {
for plugin in other.versions.keys() {
self.versions.shift_remove(plugin);
}
for (plugin, versions) in other.versions {
self.versions.insert(plugin, versions);
pub fn merge(&mut self, mut other: Toolset) {
for (plugin, versions) in self.versions.clone() {
if !other.versions.contains_key(&plugin) {
other.versions.insert(plugin, versions);
}
}
self.versions = other.versions; // swap to use other's first
self.source = other.source;
}
pub fn resolve(&mut self, config: &Config) {
Expand Down Expand Up @@ -247,19 +245,22 @@ impl Toolset {
.collect()
}
pub fn env(&self) -> IndexMap<String, String> {
let mut entries: Vec<(String, String)> = self
let mut entries: IndexMap<String, String> = self
.list_current_installed_versions()
.into_par_iter()
.flat_map(|p| match p.exec_env() {
.flat_map(|v| match v.exec_env() {
Ok(env) => env.into_iter().collect(),
Err(e) => {
warn!("Error running exec-env: {}", e);
Vec::new()
}
})
.collect::<Vec<(String, String)>>()
.into_iter()
.rev()
.collect();
entries.par_sort();
entries.into_iter().collect()
entries.sort_keys();
entries
}
pub fn path_env(&self) -> String {
let installs = self.list_paths();
Expand All @@ -271,7 +272,6 @@ impl Toolset {
pub fn list_paths(&self) -> Vec<PathBuf> {
self.list_current_installed_versions()
.into_par_iter()
.rev()
.flat_map(|rtv| match rtv.list_bin_paths() {
Ok(paths) => paths,
Err(e) => {
Expand Down

0 comments on commit a801565

Please sign in to comment.