Fix: always env clear when leaving dir (#878)

* Fix: always env clear when leaving dir

The previous logic did not clear the environment when the user switches between two directories with `.sdmanrc`.
Now we always clear (if an environment was set), and _then_ load the new one, if any.

* Add test for switching auto_env directories.
This commit is contained in:
Raphael
2021-03-04 14:32:25 +01:00
committed by GitHub
parent 52cb0a6c7c
commit bceaaac6c0
2 changed files with 62 additions and 4 deletions

View File

@@ -178,20 +178,22 @@ export PATH
if [[ "$sdkman_auto_env" == "true" ]]; then
if [[ "$zsh_shell" == "true" ]]; then
function sdkman_auto_env() {
if [[ -n $SDKMAN_ENV ]] && [[ ! $PWD =~ ^$SDKMAN_ENV ]]; then
sdk env clear
fi
if [[ -f .sdkmanrc ]]; then
sdk env
elif [[ -n $SDKMAN_ENV ]] && [[ ! $PWD =~ ^$SDKMAN_ENV ]]; then
sdk env clear
fi
}
chpwd_functions+=(sdkman_auto_env)
else
function sdkman_auto_env() {
if [[ -n $SDKMAN_ENV ]] && [[ ! $PWD =~ ^$SDKMAN_ENV ]]; then
sdk env clear
fi
if [[ "$SDKMAN_OLD_PWD" != "$PWD" ]] && [[ -f ".sdkmanrc" ]]; then
sdk env
elif [[ -n $SDKMAN_ENV ]] && [[ ! $PWD =~ ^$SDKMAN_ENV ]]; then
sdk env clear
fi
export SDKMAN_OLD_PWD="$PWD"

View File

@@ -249,6 +249,62 @@ class EnvCommandSpec extends SdkmanEnvSpecification {
bash.output.contains("Restored groovy version to 2.4.6")
}
def "should execute 'sdk env clear; sdk env' when switching to another directory with an .sdkmanrc"() {
given:
new FileTreeBuilder(candidatesDirectory).with {
"groovy" {
"2.4.1" {}
"2.4.6" {}
"2.5.14" {}
}
"ant" {
"1.9.15" {}
"1.10.8" {}
}
}
createSymbolicLink(Paths.get("$candidatesDirectory/groovy/current"), Paths.get("$candidatesDirectory/groovy/2.5.14"))
createSymbolicLink(Paths.get("$candidatesDirectory/ant/current"), Paths.get("$candidatesDirectory/ant/1.10.8"))
bash = sdkmanBashEnvBuilder
.withVersionCache("x.y.z")
.withOfflineMode(true)
.withConfiguration("sdkman_auto_env", "true")
.build()
new FileTreeBuilder(bash.workDir).with {
"projectA" {
".sdkmanrc"("groovy=2.4.1\nant=1.9.15\n")
}
"projectB" {
".sdkmanrc"("groovy=2.4.6\n")
}
}
bash.start()
bash.execute("source $bootstrapScript")
when:
bash.execute("cd projectA")
then:
bash.output.contains('Using groovy version 2.4.1 in this shell')
bash.output.contains('Using ant version 1.9.15 in this shell')
when:
bash.execute("cd ../projectB")
then:
bash.output.contains("Restored ant version to 1.10.8")
bash.output.contains('Using groovy version 2.4.6 in this shell')
when:
bash.execute("cd ..")
then:
bash.output.contains('Restored groovy version to 2.5.14')
!bash.output.contains('ant')
}
def "should not execute 'sdk env clear' when entering a subdirectory within the current active configuration"() {
given:
new FileTreeBuilder(candidatesDirectory).with {