From 4c15f15ced5a67e00b161b2a5e83df7d660405d3 Mon Sep 17 00:00:00 2001 From: Raphael Reitzig <4246780+reitzig@users.noreply.github.com> Date: Tue, 8 Mar 2022 23:12:29 +0100 Subject: [PATCH] feat(test): Tests for custom install dir ref: issue #34 --- LICENSE | 2 +- README.md | 11 ++++ test/Dockerfile | 14 +++-- test/features/corner_cases.feature | 15 +++++- .../features/step_definitions/corner_cases.rb | 22 ++++++++ test/features/step_definitions/general.rb | 23 +++++++- test/features/step_definitions/setup.rb | 54 ++++++++++++++++++- test/features/support/hooks.rb | 4 ++ 8 files changed, 135 insertions(+), 10 deletions(-) diff --git a/LICENSE b/LICENSE index 0b10c1a..fa759ed 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ 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 of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 7e82e95..c9635db 100644 --- a/README.md +++ b/README.md @@ -26,12 +26,22 @@ _Note:_ - Only compatible with fisher v4 upwards; v3 is no longer supported. - 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 It's all in the background; you should be able to run `sdk` and binaries installed with `sdk` as you would expect. + ## Contribute When you make changes, @@ -64,6 +74,7 @@ done wait ``` + ## Acknowledgements * Completion originally by [Ted Wise](https://github.com/ctwise); see his diff --git a/test/Dockerfile b/test/Dockerfile index bc0d1e1..9638acd 100644 --- a/test/Dockerfile +++ b/test/Dockerfile @@ -6,8 +6,10 @@ RUN apt-get update \ build-essential \ curl \ fish \ + nano \ ruby \ ruby-dev \ + tree \ unzip \ zip \ && apt-get clean \ @@ -26,11 +28,13 @@ USER test RUN curl -s "https://get.sdkman.io" | bash # To speed up tests, uncomment this shared setup: -#SHELL ["/bin/bash", "-c"] -#RUN source "$TEST_HOME/.sdkman/bin/sdkman-init.sh" \ -# && sdk install ant 1.9.9 \ -# && sdk install ant 1.10.1 \ -# && sdk install crash +SHELL ["/bin/bash", "-c"] +RUN source "$TEST_HOME/.sdkman/bin/sdkman-init.sh" \ + && sdk install ant 1.9.7 \ + && sdk install ant 1.9.9 \ + && sdk install ant 1.10.1 \ + && sdk install kscript 1.5.0 \ + && sdk install kscript 1.6.0 # "Install" sdkman-for-fish RUN mkdir -p $TEST_HOME/.config/fish/ diff --git a/test/features/corner_cases.feature b/test/features/corner_cases.feature index 23e2c16..a41338c 100644 --- a/test/features/corner_cases.feature +++ b/test/features/corner_cases.feature @@ -6,14 +6,27 @@ Feature: Corner Cases 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 - # 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 Scenario: PATH should contain only valid paths Given candidate kscript is installed When candidate kscript is uninstalled 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.) diff --git a/test/features/step_definitions/corner_cases.rb b/test/features/step_definitions/corner_cases.rb index 198cae4..2ae4120 100644 --- a/test/features/step_definitions/corner_cases.rb +++ b/test/features/step_definitions/corner_cases.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'fileutils' + Given('environment variable {env_name} is not set') do |name| @name = name @expect_intermediate_value = false @@ -22,6 +24,26 @@ Given('environment variable {env_name} is set to {string}') do |name, new_value| BASH 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 @command += <<~BASH fish -lc "env | grep -E \\"^#{@name}=\\"" \\ diff --git a/test/features/step_definitions/general.rb b/test/features/step_definitions/general.rb index 32b1b9a..ca994db 100644 --- a/test/features/step_definitions/general.rb +++ b/test/features/step_definitions/general.rb @@ -17,11 +17,30 @@ When('we run fish script') do |script_content| raise "Fish command failed: #{out}" end - @response_fish_script = out.strip + @output_fish_script = out.strip end 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 Then(/^file ([a-zA-Z0-9\-_.\/]+) contains$/) do |file,content| diff --git a/test/features/step_definitions/setup.rb b/test/features/step_definitions/setup.rb index c82eee1..a295e3b 100644 --- a/test/features/step_definitions/setup.rb +++ b/test/features/step_definitions/setup.rb @@ -47,10 +47,27 @@ Given(/^file ([a-zA-Z0-9\-_.\/]+) exists with content/) do |filename,content| File.write(filename, content) 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" $backup_config_file = nil def _restore_config # called in After hook unless $backup_config_file.nil? + log "Restoring #{$config_file} from #{$backup_config_file.path}" FileUtils.mv($backup_config_file, $config_file) $backup_config_file = nil end @@ -58,7 +75,8 @@ end Given(/^SDKMAN! config sets ([a-z_]+) to (.*)$/) do |key,value| 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) end @@ -68,3 +86,37 @@ Given(/^SDKMAN! config sets ([a-z_]+) to (.*)$/) do |key,value| File.write($config_file, new_config_string) 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 diff --git a/test/features/support/hooks.rb b/test/features/support/hooks.rb index 74a6f90..ea524cb 100644 --- a/test/features/support/hooks.rb +++ b/test/features/support/hooks.rb @@ -1,7 +1,11 @@ require_relative '../step_definitions/setup' +require_relative '../step_definitions/corner_cases.rb' After do |scenario| + _remove_fish_configs + _restore_fish_config _restore_config + _restore_install end # Uninstall all SDKMAN! candidates