feat(test): Tests for custom install dir

ref: issue #34
This commit is contained in:
Raphael Reitzig 2022-03-08 23:12:29 +01:00
parent 7c0ece3dfb
commit 4c15f15ced
8 changed files with 135 additions and 10 deletions

View file

@ -1,6 +1,6 @@
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2018 Raphael Reitzig Copyright (c) 2018-2022 Raphael Reitzig
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View file

@ -26,12 +26,22 @@ _Note:_
- Only compatible with fisher v4 upwards; v3 is no longer supported. - Only compatible with fisher v4 upwards; v3 is no longer supported.
- You have to install [SDKMAN!] separately. - You have to install [SDKMAN!] separately.
- If you have installed SDKMAN! at a custom location, you need to add
```fish
set -g __sdkman_custom_dir /your/path/to/sdkman
```
to a fish config file
[run _before_](https://fishshell.com/docs/current/language.html#configuration-files)
`.config/fish/conf.d/sdk.fish`;
for example, you can use `.config/fish/conf.d/config_sdk.fish`.
## Usage ## Usage
It's all in the background; you should be able to run `sdk` and binaries installed It's all in the background; you should be able to run `sdk` and binaries installed
with `sdk` as you would expect. with `sdk` as you would expect.
## Contribute ## Contribute
When you make changes, When you make changes,
@ -64,6 +74,7 @@ done
wait wait
``` ```
## Acknowledgements ## Acknowledgements
* Completion originally by [Ted Wise](https://github.com/ctwise); see his * Completion originally by [Ted Wise](https://github.com/ctwise); see his

View file

@ -6,8 +6,10 @@ RUN apt-get update \
build-essential \ build-essential \
curl \ curl \
fish \ fish \
nano \
ruby \ ruby \
ruby-dev \ ruby-dev \
tree \
unzip \ unzip \
zip \ zip \
&& apt-get clean \ && apt-get clean \
@ -26,11 +28,13 @@ USER test
RUN curl -s "https://get.sdkman.io" | bash RUN curl -s "https://get.sdkman.io" | bash
# To speed up tests, uncomment this shared setup: # To speed up tests, uncomment this shared setup:
#SHELL ["/bin/bash", "-c"] SHELL ["/bin/bash", "-c"]
#RUN source "$TEST_HOME/.sdkman/bin/sdkman-init.sh" \ RUN source "$TEST_HOME/.sdkman/bin/sdkman-init.sh" \
# && sdk install ant 1.9.9 \ && sdk install ant 1.9.7 \
# && sdk install ant 1.10.1 \ && sdk install ant 1.9.9 \
# && sdk install crash && sdk install ant 1.10.1 \
&& sdk install kscript 1.5.0 \
&& sdk install kscript 1.6.0
# "Install" sdkman-for-fish # "Install" sdkman-for-fish
RUN mkdir -p $TEST_HOME/.config/fish/ RUN mkdir -p $TEST_HOME/.config/fish/

View file

@ -6,14 +6,27 @@ Feature: Corner Cases
Then environment variable SDKMAN_DIR has the original value Then environment variable SDKMAN_DIR has the original value
Scenario: sdk initialized for another user in this shell 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 "/" Given environment variable SDKMAN_DIR is set to "/"
When a new Fish shell is launched When a new Fish shell is launched
Then environment variable SDKMAN_DIR has the original value Then environment variable SDKMAN_DIR has the original value
# TODO: add test that fails if `test` in conf.d/sdk.fish:80 errors (cf issue #28 et al.) Scenario: 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
"""
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"
@pending # cf. issue #10 @pending # cf. issue #10
Scenario: PATH should contain only valid paths Scenario: PATH should contain only valid paths
Given candidate kscript is installed Given candidate kscript is installed
When candidate kscript is uninstalled When candidate kscript is uninstalled
Then environment variable PATH cannot contain "sdkman/candidates/kscript/" Then environment variable PATH cannot contain "sdkman/candidates/kscript/"
# TODO: add test that fails if `test` in conf.d/sdk.fish:80 errors (cf issue #28 et al.)

View file

@ -1,5 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'fileutils'
Given('environment variable {env_name} is not set') do |name| Given('environment variable {env_name} is not set') do |name|
@name = name @name = name
@expect_intermediate_value = false @expect_intermediate_value = false
@ -22,6 +24,26 @@ Given('environment variable {env_name} is set to {string}') do |name, new_value|
BASH BASH
end end
$install_default = "#{ENV['HOME']}/.sdkman"
$install_custom = nil
$install_backup = "#{$install_default}_bak"
Given(/^SDKMAN! is installed at (.*)$/) do |path|
$install_custom = path
FileUtils.cp_r($install_default, $install_custom)
log "Backing up #{$install_default} at #{$install_backup}"
FileUtils.mv($install_default, $install_backup)
end
def _restore_install # called in After hook
unless $install_custom.nil?
log "Removing #{$install_custom}"
FileUtils.rm_rf($install_custom)
$install_custom = nil
log "Restoring #{$install_default} from #{$install_backup}"
FileUtils.mv($install_backup, $install_default)
end
end
When('a new Fish shell is launched') do When('a new Fish shell is launched') do
@command += <<~BASH @command += <<~BASH
fish -lc "env | grep -E \\"^#{@name}=\\"" \\ fish -lc "env | grep -E \\"^#{@name}=\\"" \\

View file

@ -17,11 +17,30 @@ When('we run fish script') do |script_content|
raise "Fish command failed: #{out}" raise "Fish command failed: #{out}"
end end
@response_fish_script = out.strip @output_fish_script = out.strip
end end
Then(/^the output is$/) do |text| Then(/^the output is$/) do |text|
expect(@response_fish_script).to eq(text.strip) expect(@output_fish_script).to eq(text.strip)
end
When('we run {string} in Fish') do |command|
@response = run_fish_command(command)
end
Then('the exit code is {int}') do |code|
expect(@response[:status]).to eq(code)
end
Then(/^environment variable ([A-Z_]+) has value "([^"]+)"$/) do |key, value|
expect(@response[:env][key]).to eq(value)
end
And('the output contains {string}') do |content|
output = @response[:stdout]
.select { |line| !line.blank? }
.map { |line| line.strip }
expect(output).to include(content)
end end
Then(/^file ([a-zA-Z0-9\-_.\/]+) contains$/) do |file,content| Then(/^file ([a-zA-Z0-9\-_.\/]+) contains$/) do |file,content|

View file

@ -47,10 +47,27 @@ Given(/^file ([a-zA-Z0-9\-_.\/]+) exists with content/) do |filename,content|
File.write(filename, content) File.write(filename, content)
end end
$fish_config_files = []
def _remove_fish_configs # called in After hook
$fish_config_files.each do |f|
log "Removing #{f}"
FileUtils.rm_f(f)
end
$fish_config_files = []
end
And(/^fish config file ([a-zA-Z0-9\-_.\/]+) exists with content$/) do |filename,content|
file = "#{ENV['HOME']}/.config/fish/conf.d/#{filename}"
FileUtils.mkdir_p(File.dirname(file))
File.write(file, content)
$fish_config_files << file
end
$config_file = "#{ENV['HOME']}/.sdkman/etc/config" $config_file = "#{ENV['HOME']}/.sdkman/etc/config"
$backup_config_file = nil $backup_config_file = nil
def _restore_config # called in After hook def _restore_config # called in After hook
unless $backup_config_file.nil? unless $backup_config_file.nil?
log "Restoring #{$config_file} from #{$backup_config_file.path}"
FileUtils.mv($backup_config_file, $config_file) FileUtils.mv($backup_config_file, $config_file)
$backup_config_file = nil $backup_config_file = nil
end end
@ -58,7 +75,8 @@ end
Given(/^SDKMAN! config sets ([a-z_]+) to (.*)$/) do |key,value| Given(/^SDKMAN! config sets ([a-z_]+) to (.*)$/) do |key,value|
if $backup_config_file.nil? if $backup_config_file.nil?
$backup_config_file = Tempfile.new('sdkman_config_backup') $backup_config_file = Tempfile.new('sdkman_config_backup_')
log "Backing up #{$config_file} at #{$backup_config_file.path}"
FileUtils.cp($config_file, $backup_config_file) FileUtils.cp($config_file, $backup_config_file)
end end
@ -68,3 +86,37 @@ Given(/^SDKMAN! config sets ([a-z_]+) to (.*)$/) do |key,value|
File.write($config_file, new_config_string) File.write($config_file, new_config_string)
end end
# TODO: create shared helper for both config files
#
$fish_config = "#{ENV['HOME']}/.config/fish/config.fish"
$backup_fish_config = nil
def _restore_fish_config # called in After hook
unless $backup_fish_config.nil?
if $backup_fish_config == :none
log "Deleting #{$fish_config}"
FileUtils.rm($fish_config)
else
log "Restoring #{$fish_config} from #{$backup_fish_config.path}"
FileUtils.mv($backup_fish_config, $fish_config)
end
$backup_fish_config = nil
end
end
And(/^fish config contains `([^`]+)`$/) do |line|
if $backup_fish_config.nil?
if File.exist?($fish_config)
$backup_fish_config = Tempfile.new('fish_config_backup_')
log "Backing up #{$fish_config} at #{$backup_fish_config.path}"
FileUtils.cp($fish_config, $backup_fish_config)
else
$backup_fish_config = :none
end
end
config = File.exist?($fish_config) ? File.readlines($fish_config) : ''
config << "\n\n# Added by sdkman-for-fish test\n#{line}"
File.write($fish_config, config.join("\n"))
end

View file

@ -1,7 +1,11 @@
require_relative '../step_definitions/setup' require_relative '../step_definitions/setup'
require_relative '../step_definitions/corner_cases.rb'
After do |scenario| After do |scenario|
_remove_fish_configs
_restore_fish_config
_restore_config _restore_config
_restore_install
end end
# Uninstall all SDKMAN! candidates # Uninstall all SDKMAN! candidates