From bceaaac6c06b8e4b868657de5ff0dbda791228ac Mon Sep 17 00:00:00 2001 From: Raphael <4246780+reitzig@users.noreply.github.com> Date: Thu, 4 Mar 2021 14:32:25 +0100 Subject: [PATCH] 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. --- src/main/bash/sdkman-init.sh | 10 ++-- .../groovy/sdkman/specs/EnvCommandSpec.groovy | 56 +++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/src/main/bash/sdkman-init.sh b/src/main/bash/sdkman-init.sh index c6283c1d..345f5716 100644 --- a/src/main/bash/sdkman-init.sh +++ b/src/main/bash/sdkman-init.sh @@ -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" diff --git a/src/test/groovy/sdkman/specs/EnvCommandSpec.groovy b/src/test/groovy/sdkman/specs/EnvCommandSpec.groovy index 6bbd6b1e..eb834405 100644 --- a/src/test/groovy/sdkman/specs/EnvCommandSpec.groovy +++ b/src/test/groovy/sdkman/specs/EnvCommandSpec.groovy @@ -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 {