From e13d3f2f50742c21b6cdfa125f725bc422332d23 Mon Sep 17 00:00:00 2001 From: Raphael Reitzig <4246780+reitzig@users.noreply.github.com> Date: Sun, 15 Jul 2018 12:27:48 +0200 Subject: [PATCH] Adds completion tests. Resolves issue #7. --- .travis.yml | 7 +- test/completion.rb | 160 ++++++++++++++++++++++++++++++++++++++++++ test/prepare_tests.sh | 10 +++ test/wrapper.fish | 4 ++ 4 files changed, 177 insertions(+), 4 deletions(-) create mode 100644 test/completion.rb create mode 100644 test/prepare_tests.sh diff --git a/.travis.yml b/.travis.yml index a478138..e985a02 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,10 +4,7 @@ before_install: - sudo apt-get update - sudo apt-get -y install fish - curl -s "https://get.sdkman.io" | bash - - source "${HOME}"/.sdkman/bin/sdkman-init.sh - - sdk install ant 1.9.9 - - echo "y" | sdk install ant 1.10.1 - - sdk default ant 1.10.1 + - bash test/prepare_tests.sh install: - mkdir -p "${HOME}"/.config/fish/{completions,conf.d,functions} @@ -17,6 +14,8 @@ install: jobs: include: + - stage: Completion Tests + script: ruby test/completion.rb - stage: General Wrapper Tests script: fish test/wrapper.fish - stage: Ticket-specific tests diff --git a/test/completion.rb b/test/completion.rb new file mode 100644 index 0000000..d1d835c --- /dev/null +++ b/test/completion.rb @@ -0,0 +1,160 @@ +#!/usr/bin/env ruby + +# Includes completion examples with the intent to cover +# all sdk commands +# Beware, pasta follows. + +test_cases = { + # Basic commands (in the order of `sdk help`): + "i" => ["i", "install"], + "u" => ["u", "ug", "uninstall", "update", "upgrade", "use"], + "r" => ["rm"], + "l" => ["list", "ls"], + "d" => ["d", "default"], + "c" => ["c", "current"], + "v" => ["v", "version"], + "b" => ["b", "broadcast"], + "h" => ["h", "help"], + "o" => ["offline"], + "s" => ["selfupdate"], + "f" => ["flush"], + # Currently uncovered; include to catch new upstream commands: + "a" => [], + "e" => [], + "g" => [], + "j" => [], + "k" => [], + "m" => [], + "n" => [], + "p" => [], + "q" => [], + "t" => [], + "w" => [], + "x" => [], + "y" => [], + "z" => [], + + # Second parameters complete correctly + "install an" => ["ant"], + "install xyz" => [], + "install 1." => [], + + "uninstall " => ["ant"], + "uninstall an" => ["ant"], + "uninstall j" => [], + "uninstall 1." => [], + + "list an" => ["ant"], + "list xyz" => [], + "list 1." => [], + + "use " => ["ant"], + "use an" => ["ant"], + "use j" => [], + "use 1." => [], + + "default " => ["ant"], + "default an" => ["ant"], + "default j" => [], + "default 1." => [], + + "current an" => ["ant"], + "current xyz" => [], + # "default j" => [], # TODO: should uninstalled candidates complete here? + "current 1." => [], + + "upgrade " => ["ant"], + "upgrade an" => ["ant"], + "upgrade j" => [], + "upgrade 1." => [], + + "version " => [], + "version a" => [], + + "broadcast " => [], + "broadcast a" => [], + + "help " => [], + "help a" => [], + + "offline " => ["enable", "disable"], + "offline a" => [], + + "selfupdate " => ["force"], + "selfupdate a" => [], + + "update " => [], + "update a" => [], + + "flush " => ["broadcast", "archives", "temp"], + "flush a" => [], + + # Third parameters complete correctly + #"install ant 1.10." => ["1.10.0", "1.10.1"], # TODO: issue #4 + "uninstall ant 1.10." => ["1.10.1"], + "list " => [], + "use ant " => ["1.9.9", "1.10.1"], + "default ant " => ["1.9.9", "1.10.1"], + "current " => [], + "upgrade " => [], + "offline " => [], + "selfupdate " => [], + "flush " => [], + + # Fourth parameters complete correctly + "install ant 1.10.2 " => [], + "uninstall ant 1.10.1 " => [], + "use ant 1.10.1 " => [], + "default ant 1.10.1 " => [], + + # Lists of all candidates work + "install gr" => ["gradle", "grails", "groovy", "groovyserv"], + "install grad" => ["gradle"], + "install gradk" => [], + + # Lists of installed candidates work + "uninstall an" => ["ant"], + "uninstall gr" => [], + "uninstall xyz" => [], + + # List of all versions work + # TODO + + # List of installed versions work + "uninstall ant 1" => ["1.9.9", "1.10.1"], + "uninstall ant 2" => [], + "uninstall vertx " => [], + "uninstall an " => [] +} + +def fish_command(prompt) + # Fish errors out if we don't set terminal dimensions + "fish -c 'stty rows 80 columns 80; complete -C\"sdk #{prompt}\"'" +end + +def extract(completion_output) + completion_output.split("\n").map { |line| + line.split(/\s+/)[0].strip + } +end + +puts "Testing sdk completions" +failures = 0 +test_cases.each { |prompt, results| + results.sort! + + print " Test: 'sdk #{prompt}'" + completions = extract(`#{fish_command(prompt)}`).sort + if completions != results + puts " -- bad!" + puts " - Expected: #{results}" + puts " - Actual: #{completions}" + failures += 1 + else + puts " -- ok!" + end +} + +puts "Ran #{test_cases.size} checks each." +puts "#{failures}/#{test_cases.size} checks failed." +exit failures > 0 ? 1 : 0 \ No newline at end of file diff --git a/test/prepare_tests.sh b/test/prepare_tests.sh new file mode 100644 index 0000000..981d4e0 --- /dev/null +++ b/test/prepare_tests.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +source "${HOME}"/.sdkman/bin/sdkman-init.sh + +# Set up an SDK with two installed versions +# --> test of `sdk use` in wrapper.fish +# --> tests in completion.rb +sdk install ant 1.9.9 +echo "y" | sdk install ant 1.10.1 +sdk default ant 1.10.1 \ No newline at end of file diff --git a/test/wrapper.fish b/test/wrapper.fish index 8a0d4d3..630df77 100644 --- a/test/wrapper.fish +++ b/test/wrapper.fish @@ -1,3 +1,7 @@ +# Test that a couple of commands have the same effect when run through +# the fish wrapper and directly. +# Verifies equality of (standard) output, exit code, and PATH. + set test_commands \ "sdk" \ "sdk version" \