Skip to content

Commit

Permalink
fix: prepend virtualenv path rather than append (#1751)
Browse files Browse the repository at this point in the history
prepend all virtualenv PATHs so the most nested directory is at the
front of the vector.

otherwise the most globally configured virtualenv takes precedence

add unit tests and e2e tests to verify this change
  • Loading branch information
kalvinnchau authored Mar 15, 2024
1 parent 8dcf9d2 commit 5c9e82e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
17 changes: 17 additions & 0 deletions e2e/test_python
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ assert_contains "mise x [email protected] -- python --version" "Python 3.12.0"
assert_contains "mise env -s bash | grep VIRTUAL_ENV" "$MISE_DATA_DIR/venv"
assert "mise x -- which python" "$MISE_DATA_DIR/venv/bin/python"

# verify nested virtualenv is used
mkdir -p subdir
cat >subdir/.e2e.mise.toml <<EOF
[env._.python]
venv = {path = "{{env.MISE_DATA_DIR}}/subvenv", create=true}
[tools]
python = "{{exec(command='echo 3.12.0')}}"
EOF

cd subdir
mise i
assert_contains "mise x [email protected] -- python --version" "Python 3.12.0"
assert_contains "mise env -s bash | grep VIRTUAL_ENV" "$MISE_DATA_DIR/subvenv"
assert "mise x -- which python" "$MISE_DATA_DIR/subvenv/bin/python"

rm -rf subdir

if [ "${TEST_ALL:-}" != 1 ]; then
exit
fi
Expand Down
37 changes: 36 additions & 1 deletion src/config/env_directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl EnvResults {
}
}
if venv.exists() {
r.env_paths.push(venv.join("bin"));
r.env_paths.insert(0, venv.join("bin"));
env.insert(
"VIRTUAL_ENV".into(),
(venv.to_string_lossy().to_string(), Some(source.clone())),
Expand Down Expand Up @@ -279,4 +279,39 @@ mod tests {
"###
);
}

#[test]
fn test_venv_path() {
let env = HashMap::new();
let results = EnvResults::resolve(
&env,
vec![
(
EnvDirective::PythonVenv {
path: PathBuf::from("/"),
create: false,
},
Default::default(),
),
(
EnvDirective::PythonVenv {
path: PathBuf::from("./"),
create: false,
},
Default::default(),
),
],
)
.unwrap();
// expect order to be reversed as it processes directives from global to dir specific
assert_debug_snapshot!(
results.env_paths.into_iter().map(|p| replace_path(&p.display().to_string())).collect::<Vec<_>>(),
@r###"
[
"~/cwd/bin",
"/bin",
]
"###
);
}
}

0 comments on commit 5c9e82e

Please sign in to comment.