|
| 1 | +require 'completely/commands/base' |
| 2 | + |
| 3 | +module Completely |
| 4 | + module Commands |
| 5 | + class Test < Base |
| 6 | + summary "Test completions" |
| 7 | + |
| 8 | + help <<~EOF |
| 9 | + This command can be used to test that any completion script (either generated by compeltely or not) responds with the right completions. |
| 10 | +
|
| 11 | + In order to test on a completely configuration file other than 'completely.yaml', set the COMPLETELY_CONFIG_PATH environemnt variable. |
| 12 | +
|
| 13 | + In order to test on an arbitrary completions script, set the COMPLETELY_SCRIPT_PATH and COMPLETELY_SCRIPT_FUNCTION environment variables. |
| 14 | + EOF |
| 15 | + |
| 16 | + usage "completely test COMPLINE" |
| 17 | + usage "completely test (-h|--help)" |
| 18 | + |
| 19 | + param "COMPLINE", "The command to test completions for. This will be handled as if a TAB was pressed immediately at the end of it, so the last word is considered the active cursor. If you wish to complete for the next word instead, end your command with a space." |
| 20 | + |
| 21 | + environment "COMPLETELY_CONFIG_PATH", "Path to a completely configuration file [default: completely.yaml]" |
| 22 | + environment "COMPLETELY_SCRIPT_PATH", "Path to a completions script. When set, this script will be tested instead of the completely configuration file" |
| 23 | + environment "COMPLETELY_SCRIPT_FUNCTION", "The main completion function to call when using a custom script" |
| 24 | + |
| 25 | + example %q[completely test "mygit pu"] |
| 26 | + example %q[completely test "mygit pull "] |
| 27 | + example <<~EOF |
| 28 | + COMPLETELY_SCRIPT_PATH=/usr/share/bash-completion/completions/git \\ |
| 29 | + COMPLETELY_SCRIPT_FUNCTION=_git \\ |
| 30 | + completely test "git pu" |
| 31 | + EOF |
| 32 | + |
| 33 | + attr_reader :config, :script_path, :script_function |
| 34 | + |
| 35 | + def run |
| 36 | + set_vars |
| 37 | + puts tester.test(compline).join "\n" |
| 38 | + end |
| 39 | + |
| 40 | + private |
| 41 | + |
| 42 | + def compline |
| 43 | + args['COMPLINE'] |
| 44 | + end |
| 45 | + |
| 46 | + def tester |
| 47 | + if config |
| 48 | + completions = Completions.load config |
| 49 | + completions.tester |
| 50 | + else |
| 51 | + Tester.new script: File.read(script_path), function_name: ENV['COMPLETELY_SCRIPT_FUNCTION'] |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + def set_vars |
| 56 | + if ENV['COMPLETELY_CONFIG_PATH'] |
| 57 | + @config = ENV['COMPLETELY_CONFIG_PATH'] |
| 58 | + elsif ENV['COMPLETELY_SCRIPT_PATH'] and ENV['COMPLETELY_SCRIPT_FUNCTION'] |
| 59 | + @script_path = ENV['COMPLETELY_SCRIPT_PATH'] |
| 60 | + @script_function = ENV['COMPLETELY_SCRIPT_FUNCTION'] |
| 61 | + elsif File.exist? 'completely.yaml' |
| 62 | + @config = 'completely.yaml' |
| 63 | + else |
| 64 | + raise "Please set the proper environment variables or run in a folder with completely.yaml" |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | +end |
0 commit comments