Skip to content

Commit

Permalink
fix: Respect globally set SDKMAN_DIR
Browse files Browse the repository at this point in the history
Also, print a warning during shell startup
if a custom install path is set, but
SDKMAN! is not installed there.

fixes #52
  • Loading branch information
reitzig committed Apr 3, 2024
1 parent 5f2ae91 commit 6aa7900
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Added

- Warn on bad custom install path

### Fixes

- Respect `SDKMAN_DIR` if already set (#52; thanks, @xtexChooser!)
- Completions use custom SDKMAN! install path (#48; thanks, @Bryan2333!)

## [2.0.0] - 2023-06-27
Expand Down
19 changes: 13 additions & 6 deletions conf.d/sdk.fish
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,29 @@
# Account for custom install locations
if set -q __sdkman_custom_dir
set -gx SDKMAN_DIR "$__sdkman_custom_dir"
else
# This is the default location:
end
# Guard: SDKMAN! needs to be installed
if set -q SDKMAN_DIR; and not test -f "$SDKMAN_DIR/bin/sdkman-init.sh"
echo "WARNING: SDKMAN! installation path set to $SDKMAN_DIR, but no installation found there"
exit 0
end

# Unless overridden, use the default location:
if not set -q SDKMAN_DIR
set -gx SDKMAN_DIR "$HOME/.sdkman"
end

set __fish_sdkman_init "$SDKMAN_DIR/bin/sdkman-init.sh"

# Copied from https://github.com/jorgebucaran/fisher/blob/main/functions/fisher.fish to be consistent:
set --query fisher_path || set --local fisher_path $__fish_config_dir
set __fish_sdkman_noexport_init "$fisher_path/functions/__sdkman-noexport-init.sh"

# Guard: SDKMAN! needs to be installed
if not test -f "$__fish_sdkman_init"
exit 0
end

# Copied from https://github.com/jorgebucaran/fisher/blob/main/functions/fisher.fish to be consistent:
set --query fisher_path || set --local fisher_path $__fish_config_dir
set __fish_sdkman_noexport_init "$fisher_path/functions/__sdkman-noexport-init.sh"

# Hack for issue #19:
# Create version of sdkman-init that doesn't export any environment variables.
# Refresh if sdkman-init changed.
Expand Down
21 changes: 11 additions & 10 deletions test/features/corner_cases.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ Feature: Corner Cases
When a new Fish shell is launched
Then environment variable SDKMAN_DIR has the original value

Scenario: sdk initialized for another user in this shell
# Use any directory outside of the user's home directory
Given environment variable SDKMAN_DIR is set to "/"
When a new Fish shell is launched
Then environment variable SDKMAN_DIR has the original value
Scenario: Custom installation path via env var
Given SDKMAN! is installed at /tmp/sdkman
And environment variable SDKMAN_DIR is set to "/tmp/sdkman"
When we run "sdk version" in Fish
Then the exit code is 0
And the output contains "SDKMAN!"
And environment variable SDKMAN_DIR has value "/tmp/sdkman"
And environment variable ANT_HOME has value "/tmp/sdkman/candidates/ant/current"

Scenario: Custom installation path
Scenario: Custom installation path via fish config
Given SDKMAN! is installed at /tmp/sdkman
And environment variable SDKMAN_DIR is set to "/something/wicked"
And fish config file config_sdk.fish exists with content
"""
set -g __sdkman_custom_dir /tmp/sdkman
Expand All @@ -25,10 +29,7 @@ Feature: Corner Cases

Scenario Outline: Completions with custom installation path
Given SDKMAN! is installed at /tmp/sdkman
And fish config file config_sdk.fish exists with content
"""
set -g __sdkman_custom_dir /tmp/sdkman
"""
And environment variable SDKMAN_DIR is set to "/tmp/sdkman"
When the user enters "<cmd>" into the prompt
Then completion should propose "<completions>"
Examples:
Expand Down
2 changes: 2 additions & 0 deletions test/features/support/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ def run_fish_command(cmd)
end.to_h

out, status = Open3.capture2e(<<~FISH
#{@command.nil? ? '' : @command}
fish -c '#{cmd} > #{files[:stdout]} 2> #{files[:stderr]}; \
echo $status > #{files[:status]}; \
env > #{files[:env]}; \
'
#{@command.nil? ? '' : ')'}
FISH
)

Expand Down

0 comments on commit 6aa7900

Please sign in to comment.