Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
reitzig committed Jun 27, 2023
1 parent 7d27858 commit 53d3567
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 33 deletions.
52 changes: 52 additions & 0 deletions .idea/runConfigurations/All_features__mock_install_.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/runConfigurations/Build_Test_Image.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions test/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ USER test
RUN curl -s "https://get.sdkman.io" | bash

# To speed up tests, uncomment this shared setup:
ARG shared_test_setup="false"
ENV SHARED_TEST_SETUP=$shared_test_setup
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
RUN if [ "${shared_test_setup}" = 'true' ]; then \
source "$TEST_HOME/.sdkman/bin/sdkman-init.sh" \
&& sdk update \
&& 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 \
; \
fi

# "Install" sdkman-for-fish
RUN mkdir -p $TEST_HOME/.config/fish/
Expand Down
2 changes: 1 addition & 1 deletion test/features/step_definitions/general.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module GeneralHelper
script_file = Tempfile.new('fish_script')
File.write(script_file, script_content)

out, status = Open3.capture2e("fish #{script_file.path}")
out, status = Open3.capture2e($test_env, "fish #{script_file.path}")
unless status.success?
warn(out)
raise "Fish command failed: #{out}"
Expand Down
6 changes: 3 additions & 3 deletions test/features/step_definitions/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
require 'fileutils'

Given(/^SDKMAN! is not installed$/) do
FileUtils.rm_rf("#{ENV['HOME']}/.sdkman")
FileUtils.rm_rf("#{$test_env['SDKMAN_DIR']}")
end

When('sdk is called and user answers {string}') do |answer|
run_fish_command("echo '#{answer}' | sdk version")
end

Then(/^SDKMAN! is absent$/) do
expect(Dir["#{ENV['HOME']}/.sdkman/*"].count).to eq(0)
expect(Dir["#{$test_env['SDKMAN_DIR']}/*"].count).to eq(0)
response = run_bash_command("sdk version")
expect(response[:status]).to_not eq(0)
end

Then('SDKMAN! is present') do
expect(Dir["#{ENV['HOME']}/.sdkman/*"].count).to be > 1
expect(Dir["#{$test_env['SDKMAN_DIR']}/*"].count).to be > 1
response = run_bash_command("sdk version")
expect(response[:status]).to eq(0)
end
42 changes: 27 additions & 15 deletions test/features/step_definitions/setup.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
require 'fileutils'
require 'tempfile'

def _mock_install(candidate, version)
bin_dir = "#{$test_env['SDKMAN_CANDIDATES_DIR']}/#{candidate}/#{version}/bin"
FileUtils.mkdir_p(bin_dir)
FileUtils.touch("#{bin_dir}/#{candidate}")
FileUtils.ln_s(bin_dir, "#{$test_env['SDKMAN_CANDIDATES_DIR']}/#{candidate}/current", force: true)
end

Given(/^candidate (\w+) is installed at version (\d+(?:\.\d+)*)$/) do |candidate, version|
# TODO: Can we mock-install instead?
# Something like
#
# mkdir -p ${SDKMAN_CANDIDATES_DIR}/${candidate}/{version}/bin \
# && touch ${SDKMAN_CANDIDATES_DIR}/${candidate}/${version}/bin/${candidate} &&
# ln -s ${SDKMAN_CANDIDATES_DIR}/${candidate}/current ${SDKMAN_CANDIDATES_DIR}/${candidate}/${version}
#
# should be quite enough to trick sdk as far as we need it to trick.
# Or is it?
run_bash_command("sdk install #{candidate} #{version}") unless installed?(candidate, version)
if $run_with_real_install
run_bash_command("sdk install #{candidate} #{version}") unless installed?(candidate, version)
else
_mock_install(candidate, version)
end
end

Given(/^candidate (\w+) is installed$/) do |candidate|
run_bash_command("sdk install #{candidate}") unless installed?(candidate)
if $run_with_real_install
run_bash_command("sdk install #{candidate}") unless installed?(candidate)
else
_mock_install(candidate, "1.2.3")
end
end

def _uninstall_candidate_version(candidate_dir)
%r{/([^/]+)/([^/]+)$}.match(candidate_dir) do |match|
candidate = match[1]
version = match[2]
run_bash_command("sdk rm #{candidate} #{version}") unless version == 'current'
if $run_with_real_install
run_bash_command("sdk rm #{candidate} #{version}") unless version == 'current'
else
FileUtils.rm_rf("#{$test_env['SDKMAN_CANDIDATES_DIR']}/#{candidate}/#{version}/")
# TODO: Should we ever use this to uninstall only _one_ version of a candidate,
# we need to re-link current
end
end
end

When(/^candidate (\w+) is uninstalled$/) do |candidate|
log `ls ~/.sdkman/candidates/#{candidate}`
Dir["#{ENV['HOME']}/.sdkman/candidates/#{candidate}/*"].each do |candidate_dir|
Dir["#{$test_env['SDKMAN_CANDIDATES_DIR']}/#{candidate}/*"].each do |candidate_dir|
_uninstall_candidate_version(candidate_dir)
end
log `ls ~/.sdkman/candidates/#{candidate}`
log `ls #{$test_env['SDKMAN_CANDIDATES_DIR']}/#{candidate}`
end

Given(/^file ([a-zA-Z0-9\-_.\/]+) exists with content/) do |filename,content|
Expand All @@ -55,7 +67,7 @@ def _remove_fish_configs # called in After hook
$fish_config_files << file
end

$config_file = "#{ENV['HOME']}/.sdkman/etc/config"
$config_file = "#{$test_env['SDKMAN_DIR']}/etc/config"
$backup_config_file = nil
def _restore_config # called in After hook
unless $backup_config_file.nil?
Expand Down
20 changes: 20 additions & 0 deletions test/features/support/env.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
$run_with_real_install = (ENV['RUN_WITH_REAL_INSTALL'] || 'true') == 'true'
$shared_setup = (ENV['SHARED_TEST_SETUP'] || 'false') == 'true'
$test_env = {}

if $run_with_real_install
# nothing to do except set the flag
puts "Running with real packages installs through SDKMAN! -- beware!"
$test_env = {
'SDKMAN_DIR' => "#{ENV['HOME']}/.sdkman",
'SDKMAN_CANDIDATES_DIR' => "#{ENV['HOME']}/.sdkman/candidates",
}
else
puts "Running with mocked package installs"
# TODO: Put SDKMAN! in a temp dir -- but how to change $SDKMAN_(CANDIDATES_)DIR?
# For now, set the env vars to the defaults so our setup methods work
$test_env = {
'SDKMAN_DIR' => "#{ENV['HOME']}/.sdkman",
'SDKMAN_CANDIDATES_DIR' => "#{ENV['HOME']}/.sdkman/candidates",
}
end
8 changes: 4 additions & 4 deletions test/features/support/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def list_installed_candidates
candidates = {}

Dir["#{ENV['HOME']}/.sdkman/candidates/*/*"].each do |candidate_dir|
Dir["#{$test_env['SDKMAN_CANDIDATES_DIR']}/*/*"].each do |candidate_dir|
%r{/([^/]+)/([^/]+)$}.match(candidate_dir) do |match|
candidate = match[1]
version = match[2]
Expand All @@ -30,8 +30,8 @@ def run_bash_command(cmd)
[s, FileUtils.touch("#{tmp_dir}/#{s}")[0]]
end.to_h

out, status = Open3.capture2e(<<~BASH
bash -c 'source "#{ENV['HOME']}/.sdkman/bin/sdkman-init.sh" && \
out, status = Open3.capture2e($test_env, <<~BASH
bash -c 'source "#{$test_env['SDKMAN_DIR']}/bin/sdkman-init.sh" && \
#{cmd} > #{files[:stdout]} 2> #{files[:stderr]}; \
echo "$?" > #{files[:status]}; \
env > #{files[:env]}; \
Expand Down Expand Up @@ -63,7 +63,7 @@ def run_fish_command(cmd)
[s, FileUtils.touch("#{tmp_dir}/#{s}")[0]]
end.to_h

out, status = Open3.capture2e(<<~FISH
out, status = Open3.capture2e($test_env, <<~FISH
fish -c '#{cmd} > #{files[:stdout]} 2> #{files[:stderr]}; \
echo $status > #{files[:status]}; \
env > #{files[:env]}; \
Expand Down
6 changes: 4 additions & 2 deletions test/features/support/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
require_relative '../step_definitions/corner_cases.rb'

BeforeAll do
run_bash_command('sdk update')
if $run_with_real_install && !$shared_setup
run_bash_command('sdk update')
end
end

After do |scenario|
Expand All @@ -14,7 +16,7 @@

# Uninstall all SDKMAN! candidates
AfterAll do
Dir["#{ENV['HOME']}/.sdkman/candidates/*/*"].each do |candidate_dir|
Dir["#{$test_env['SDKMAN_CANDIDATES_DIR']}/*/*"].each do |candidate_dir|
_uninstall_candidate_version(candidate_dir)
end
end

0 comments on commit 53d3567

Please sign in to comment.