mirror of
https://github.com/ethauvin/sdkman-for-fish.git
synced 2025-04-25 13:27:10 -07:00
Migrate wrapper test to Cucumber.
This commit is contained in:
parent
479fa1e541
commit
56bc601b17
9 changed files with 144 additions and 94 deletions
|
@ -4,10 +4,9 @@ require 'open3'
|
|||
|
||||
module CompletionHelper
|
||||
def complete(cmd)
|
||||
completions = run_fish_command("complete -C\"sdk #{cmd}\"")
|
||||
completions = run_fish_command("complete -C\"sdk #{cmd}\"")[:stdout]
|
||||
|
||||
completions.split("\n") \
|
||||
.map { |line| line.split(/\s+/)[0].strip }
|
||||
completions.map { |line| line.split(/\s+/)[0].strip }
|
||||
# TODO: Why do we get duplicates in the Docker container?
|
||||
end
|
||||
end
|
||||
|
|
39
test/features/step_definitions/wrapper.rb
Normal file
39
test/features/step_definitions/wrapper.rb
Normal file
|
@ -0,0 +1,39 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module WrapperHelper
|
||||
def reject_then_select(lines, exclude, select)
|
||||
lines.select do |e|
|
||||
(exclude.nil? || e !~ exclude) && e =~ select
|
||||
end.sort
|
||||
end
|
||||
|
||||
def compare_env(exclude, include)
|
||||
env_bash = reject_then_select(@response_bash[:env], exclude, include)
|
||||
env_fish = reject_then_select(@response_fish[:env], exclude, include)
|
||||
expect(env_fish).to eq(env_bash)
|
||||
end
|
||||
end
|
||||
World WrapperHelper
|
||||
|
||||
When('we run {string} in Bash and Fish') do |command|
|
||||
@response_bash = run_bash_command(command)
|
||||
@response_fish = run_fish_command(command)
|
||||
end
|
||||
|
||||
Then('the exit code is the same') do
|
||||
expect(@response_fish[:status]).to eq(@response_bash[:status])
|
||||
end
|
||||
|
||||
Then('the output is the same') do
|
||||
%i[stdout stderr].each do |out|
|
||||
expect(@response_fish[out]).to eq(@response_bash[out])
|
||||
end
|
||||
end
|
||||
|
||||
Then('environment variable(s) {env_glob} is/are the same') do |pattern|
|
||||
compare_env(nil, pattern)
|
||||
end
|
||||
|
||||
Then('environment variable(s) {env_glob} is/are the same except for {env_glob}') do |pattern, exclude_pattern|
|
||||
compare_env(exclude_pattern, pattern)
|
||||
end
|
|
@ -1,5 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'fileutils'
|
||||
require 'tmpdir'
|
||||
|
||||
def list_installed_candidates
|
||||
candidates = {}
|
||||
|
||||
|
@ -17,26 +20,70 @@ end
|
|||
|
||||
def installed?(candidate, version = nil)
|
||||
candidates = list_installed_candidates
|
||||
candidates.key?(candidate) && (version.nil? || candidates[candidate].include?(version))
|
||||
candidates.key?(candidate) \
|
||||
&& (version.nil? || candidates[candidate].include?(version))
|
||||
end
|
||||
|
||||
def run_bash_command(cmd)
|
||||
stdout, stderr, status = Open3.capture3("bash -c 'source \"$HOME/.sdkman/bin/sdkman-init.sh\"; #{cmd}'")
|
||||
unless status.success?
|
||||
warn(stderr)
|
||||
raise "Bash command failed: #{stderr}"
|
||||
end
|
||||
Dir.mktmpdir(%w[sdkman-for-fish-test_ _fish]) do |tmp_dir|
|
||||
files = %i[status stdout stderr env].map { |s|
|
||||
[s, FileUtils.touch("#{tmp_dir}/#{s}")[0]]
|
||||
}.to_h
|
||||
|
||||
stdout
|
||||
out, status = Open3.capture2e(<<~BASH
|
||||
bash -c 'source "#{ENV['HOME']}/.sdkman/bin/sdkman-init.sh" && \
|
||||
#{cmd} > #{files[:stdout]} 2> #{files[:stderr]}; \
|
||||
echo "$?" > #{files[:status]}; \
|
||||
env > #{files[:env]}; \
|
||||
'
|
||||
BASH
|
||||
)
|
||||
|
||||
unless status.success?
|
||||
warn(out)
|
||||
raise "Bash command failed: #{out}"
|
||||
end
|
||||
|
||||
{
|
||||
status: File.read(files[:status]).to_i,
|
||||
stdout: File.readlines(files[:stdout]),
|
||||
stderr: File.readlines(files[:stderr]),
|
||||
env: File.readlines(files[:env])
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
# # For nicer diffs: one entry per line, sorted
|
||||
# string split ":" (cat path_bash) | sort > path_bash
|
||||
# string split ":" (cat path_fish) \
|
||||
# | string split " " \
|
||||
# | sort > path_fish
|
||||
# # split by spaces for fish 2.*
|
||||
|
||||
def run_fish_command(cmd)
|
||||
# NB: Fish errors out if we don't set terminal dimensions
|
||||
stdout, stderr, status = Open3.capture3("fish -c 'stty rows 80 columns 80; #{cmd}'")
|
||||
unless status.success?
|
||||
warn(stderr)
|
||||
raise 'Fish command failed'
|
||||
end
|
||||
Dir.mktmpdir(%w[sdkman-for-fish-test_ _fish]) do |tmp_dir|
|
||||
files = %i[status stdout stderr env].map { |s|
|
||||
[s, FileUtils.touch("#{tmp_dir}/#{s}")[0]]
|
||||
}.to_h
|
||||
|
||||
stdout
|
||||
out, status = Open3.capture2e(<<~FISH
|
||||
fish -c '#{cmd} > #{files[:stdout]} ^ #{files[:stderr]}; \
|
||||
echo $status > #{files[:status]}; \
|
||||
env > #{files[:env]}; \
|
||||
'
|
||||
FISH
|
||||
)
|
||||
|
||||
unless status.success?
|
||||
warn(out)
|
||||
raise "Fish command failed: #{out}"
|
||||
end
|
||||
|
||||
{
|
||||
status: File.read(files[:status]).to_i,
|
||||
stdout: File.readlines(files[:stdout]),
|
||||
stderr: File.readlines(files[:stderr]),
|
||||
env: File.readlines(files[:env])
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -19,3 +19,12 @@ ParameterType(
|
|||
end
|
||||
end
|
||||
)
|
||||
|
||||
ParameterType(
|
||||
name: 'env_glob',
|
||||
regexp: /[A-Z_*]+/,
|
||||
type: Regexp,
|
||||
transformer: lambda do |glob|
|
||||
/^#{glob.gsub('*', '[A-Z_]*')}=/
|
||||
end
|
||||
)
|
||||
|
|
30
test/features/wrapper.feature
Normal file
30
test/features/wrapper.feature
Normal file
|
@ -0,0 +1,30 @@
|
|||
Feature: Wrapping of Bash
|
||||
All calls to sdk are performed through Bash;
|
||||
we need to wrap those calls in such a way that
|
||||
the effect sdk has on the Bash environment carries
|
||||
over the current Fish session.
|
||||
|
||||
We verifiy equality of (standard) output, exit code, and environment variables.
|
||||
|
||||
Background:
|
||||
Given SDKMAN! candidate list is up to date
|
||||
And candidate ant is installed at version 1.9.9
|
||||
And candidate ant is installed at version 1.10.1
|
||||
|
||||
Scenario Outline:
|
||||
When we run "<command>" in Bash and Fish
|
||||
Then the exit code is the same
|
||||
And the output is the same
|
||||
And environment variable PATH is the same
|
||||
And environment variables *_HOME are the same
|
||||
And environment variables SDKMAN_* are the same except for SDKMAN_OFFLINE_MODE
|
||||
# NB: SDKMAN_OFFLINE_MODE is not an environment variable in bash, so ignore it here.
|
||||
Examples:
|
||||
| command |
|
||||
| sdk |
|
||||
| sdk version |
|
||||
| sdk list java |
|
||||
| sdk update |
|
||||
| sdk use ant 1.9.9 |
|
||||
| sdk offline enable > /dev/null; sdk install ant foo |
|
||||
| sdk use ant 1.9.9 > /dev/null; sdk broadcast |
|
Loading…
Add table
Add a link
Reference in a new issue