Test for autoenv setting

This commit is contained in:
Raphael Reitzig 2021-01-29 13:26:07 +01:00
parent 7b17fcc4b7
commit 61f1cf8efc
7 changed files with 154 additions and 12 deletions

View file

@ -60,7 +60,7 @@ jobs:
- name: Run Tests - name: Run Tests
working-directory: test working-directory: test
run: | run: |
cucumber --publish-quiet cucumber --publish-quiet --tags "not @pending"
confirm-installation: confirm-installation:
name: "Check fisher install" name: "Check fisher install"

View file

@ -11,7 +11,7 @@ Also adds binaries from installed SDKs to the PATH.
Version 2.0.0 has been tested with Version 2.0.0 has been tested with
- fish 3.1.2, and - fish 3.1.2, and
- SDKMAN! 5.9.2, on - SDKMAN! 5.11.0, on
- Ubuntu 20.04 LTS and macOS 10.15 - Ubuntu 20.04 LTS and macOS 10.15
## Install ## Install

View file

@ -40,4 +40,4 @@ RUN ls -R $TEST_HOME/.config/fish/
# Run tests # Run tests
COPY test ./ COPY test ./
ENTRYPOINT ["cucumber"] ENTRYPOINT ["cucumber"]
CMD ["--publish-quiet"] CMD ["--publish-quiet", "--tags", "not @pending"]

View file

@ -0,0 +1,85 @@
Feature: Support autoenv setting
When the user sets `sdkman_auto_env=true`, we should always `use` the
candidates specified in `./.sdkmanrc`, if any.
Background:
Given SDKMAN! candidate list is up to date
And candidate ant is installed at version 1.9.7
And candidate ant is installed at version 1.9.9
And candidate ant is installed at version 1.10.1
And candidate crash is installed at version 1.2.11
And candidate crash is installed at version 1.3.0
Scenario: No action if autoenv turned off
Given SDKMAN! config sets sdkman_auto_env to false
And file /tmp/autoenv-test/.sdkmanrc exists with content
"""
ant=1.9.9
"""
When we run fish script
"""
echo (basename $ANT_HOME) > /tmp/autoenv-test.log
cd /tmp/autoenv-test
echo (basename $ANT_HOME) >> /tmp/autoenv-test.log
cd ..
echo (basename $ANT_HOME) >> /tmp/autoenv-test.log
"""
Then file /tmp/autoenv-test.log contains
"""
current
current
current
"""
Scenario: .sdkmanrc loaded if autoenv turned on
Given SDKMAN! config sets sdkman_auto_env to true
And file /tmp/autoenv-test/.sdkmanrc exists with content
"""
ant=1.9.9
"""
When we run fish script
"""
echo (basename $ANT_HOME) > /tmp/autoenv-test.log
cd /tmp/autoenv-test
echo (basename $ANT_HOME) >> /tmp/autoenv-test.log
cd ..
echo (basename $ANT_HOME) >> /tmp/autoenv-test.log
"""
Then file /tmp/autoenv-test.log contains
"""
current
1.9.9
1.10.1
"""
# TODO: we would expect `current` in the last line -- but that's what sdk currently does!
# TODO: This one doesn't work due to a bug in sdkman. Track: https://github.com/sdkman/sdkman-cli/pull/878
@pending
Scenario: Switching between directories with .sdkmanrc
Given SDKMAN! config sets sdkman_auto_env to true
And file /tmp/autoenv-test-1/.sdkmanrc exists with content
"""
ant=1.9.9
crash=1.2.11
"""
And file /tmp/autoenv-test-2/.sdkmanrc exists with content
"""
ant=1.9.7
"""
When we run fish script
"""
echo (basename $ANT_HOME),(basename $CRASH_HOME) > /tmp/autoenv-test.log
cd /tmp/autoenv-test-1
echo (basename $ANT_HOME),(basename $CRASH_HOME) >> /tmp/autoenv-test.log
cd ../autoenv-test-2
echo (basename $ANT_HOME),(basename $CRASH_HOME) >> /tmp/autoenv-test.log
cd ..
echo (basename $ANT_HOME),(basename $CRASH_HOME) >> /tmp/autoenv-test.log
"""
Then file /tmp/autoenv-test.log contains
"""
current,current
1.9.9,1.2.11
1.9.7,current
current,current
"""

View file

@ -0,0 +1,29 @@
# frozen_string_literal: true
require 'fileutils'
require 'tempfile'
module GeneralHelper
end
World GeneralHelper
When('we run fish script') do |script_content|
script_file = Tempfile.new('fish_script')
File.write(script_file, script_content)
out, status = Open3.capture2e("fish #{script_file.path}")
unless status.success?
warn(out)
raise "Fish command failed: #{out}"
end
@response_fish_script = out.strip
end
Then(/^the output is$/) do |text|
expect(@response_fish_script).to eq(text.strip)
end
Then(/^file ([a-zA-Z0-9-_.\/]+) contains$/) do |file,content|
expect(File.readlines(file).join("").strip).to eq(content.strip)
end

View file

@ -1,4 +1,5 @@
require 'fileutils' require 'fileutils'
require 'tempfile'
$index_updated = false # TODO: Hack since Cucumber doesn't have Feature-level hooks $index_updated = false # TODO: Hack since Cucumber doesn't have Feature-level hooks
Given(/^SDKMAN! candidate list is up to date$/) do Given(/^SDKMAN! candidate list is up to date$/) do
@ -46,14 +47,24 @@ Given(/^file ([a-zA-Z0-9\-_.\/]+) exists with content/) do |filename,content|
File.write(filename, content) File.write(filename, content)
end end
# Uninstall all SDKMAN! candidates $config_file = "#{ENV['HOME']}/.sdkman/etc/config"
# TODO: Run after every scenario, this makes tests very slow. $backup_config_file = nil
# Currently, Cucumber doesn't have Feature-level hooks, so we have to work around: def _restore_config # called in After hook
# --> install only if not already installed; unless $backup_config_file.nil?
# if the test needs a candidate to _not_ be there, make it explicit. FileUtils.mv($backup_config_file, $config_file)
# --> clean up after _all_ features at least $backup_config_file = nil
at_exit do
Dir["#{ENV['HOME']}/.sdkman/candidates/*/*"].each do |candidate_dir|
_uninstall_candidate_version(candidate_dir)
end end
end end
Given(/^SDKMAN! config sets ([a-z_]+) to (.*)$/) do |key,value|
if $backup_config_file.nil?
$backup_config_file = Tempfile.new('sdkman_config_backup')
FileUtils.cp($config_file, $backup_config_file)
end
config = File.readlines($config_file).map { |line| line.split("=").map { |v| v.strip } }.to_h
config[key] = value
new_config_string = config.map { |k,v| "#{k}=#{v}" }.join("\n")
File.write($config_file, new_config_string)
end

View file

@ -0,0 +1,17 @@
require_relative '../step_definitions/setup'
After do |scenario|
_restore_config
end
# Uninstall all SDKMAN! candidates
# TODO: Run after every scenario, this makes tests very slow.
# Currently, Cucumber doesn't have Feature-level hooks, so we have to work around:
# --> install only if not already installed;
# if the test needs a candidate to _not_ be there, make it explicit.
# --> clean up after _all_ features at least
at_exit do
Dir["#{ENV['HOME']}/.sdkman/candidates/*/*"].each do |candidate_dir|
_uninstall_candidate_version(candidate_dir)
end
end