diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 10c72e9..89decb3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ jobs: runs-on: ubuntu-latest strategy: - matrix: { ruby: ['2.7', '3.0', '3.1'] } + matrix: { ruby: ['2.7', '3.0', '3.1', head] } steps: - name: Checkout code diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..679ee1c --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,19 @@ +inherit_gem: + rentacop: rentacop.yml + +AllCops: + TargetRubyVersion: 2.7 + Exclude: + - 'debug.rb' + - 'dev/**/*' + +# Allow `set_compline_vars` here, it is more readable +Naming/AccessorMethodName: + Exclude: + - 'lib/completely/tester.rb' + +# Allow long lines in specs and commands +Layout/LineLength: + Exclude: + - 'spec/**/*' + - 'lib/completely/commands/**/*' \ No newline at end of file diff --git a/Gemfile b/Gemfile index aef1e97..664f7d1 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,8 @@ -source "https://rubygems.org" +source 'https://rubygems.org' gem 'byebug' gem 'lp' +gem 'rentacop' gem 'rspec' gem 'rspec_approvals' gem 'runfile' diff --git a/bin/completely b/bin/completely index 3a43e85..dbea409 100755 --- a/bin/completely +++ b/bin/completely @@ -8,9 +8,8 @@ runner = Completely::CLI.runner begin exit runner.run ARGV - rescue => e puts e.backtrace.reverse if ENV['DEBUG'] say! "!undred!#{e.class}!txtrst!\n#{e.message}" exit 1 -end \ No newline at end of file +end diff --git a/bin/console b/bin/console index a1acb98..8bf3add 100755 --- a/bin/console +++ b/bin/console @@ -1,9 +1,9 @@ #!/usr/bin/env ruby -require "irb" -require "irb/completion" -require "completely" +require 'irb' +require 'irb/completion' +require 'completely' include Completely -IRB.start \ No newline at end of file +IRB.start diff --git a/completely.gemspec b/completely.gemspec index 105f027..04ba746 100644 --- a/completely.gemspec +++ b/completely.gemspec @@ -1,22 +1,22 @@ -lib = File.expand_path('../lib', __FILE__) +lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'date' require 'completely/version' Gem::Specification.new do |s| s.name = 'completely' s.version = Completely::VERSION - s.date = Date.today.to_s - s.summary = "Bash Completions Generator" - s.description = "Generate bash completion scripts using simple YAML configuration" - s.authors = ["Danny Ben Shitrit"] + s.summary = 'Bash Completions Generator' + s.description = 'Generate bash completion scripts using simple YAML configuration' + s.authors = ['Danny Ben Shitrit'] s.email = 'db@dannyben.com' s.files = Dir['README.md', 'lib/**/*.*'] s.executables = ['completely'] s.homepage = 'https://github.com/dannyben/completely' s.license = 'MIT' - s.required_ruby_version = ">= 2.7.0" + s.required_ruby_version = '>= 2.7.0' s.add_runtime_dependency 'colsole', '~> 0.6' s.add_runtime_dependency 'mister_bin', '~> 0.7' + + s.metadata['rubygems_mfa_required'] = 'true' end diff --git a/lib/completely/cli.rb b/lib/completely/cli.rb index 45045d3..ef35bde 100644 --- a/lib/completely/cli.rb +++ b/lib/completely/cli.rb @@ -9,8 +9,8 @@ module Completely class CLI def self.runner runner = MisterBin::Runner.new version: Completely::VERSION, - header: "Completely - Bash Completions Generator", - footer: "Run !txtpur!completely COMMAND --help!txtrst! for more information" + header: 'Completely - Bash Completions Generator', + footer: 'Run !txtpur!completely COMMAND --help!txtrst! for more information' runner.route 'init', to: Commands::Init runner.route 'preview', to: Commands::Preview diff --git a/lib/completely/commands/base.rb b/lib/completely/commands/base.rb index 425e2d4..49d60ad 100644 --- a/lib/completely/commands/base.rb +++ b/lib/completely/commands/base.rb @@ -3,22 +3,21 @@ module Completely module Commands class Base < MisterBin::Command - class << self def param_config_path - param "CONFIG_PATH", "Path to the YAML configuration file [default: completely.yaml]\nCan also be set by an environment variable" + param 'CONFIG_PATH', "Path to the YAML configuration file [default: completely.yaml]\nCan also be set by an environment variable" end def option_function - option "-f --function NAME", "Modify the name of the function in the generated script" + option '-f --function NAME', 'Modify the name of the function in the generated script' end def environment_config_path - environment "COMPLETELY_CONFIG_PATH", "Path to a completely configuration file [default: completely.yaml]" + environment 'COMPLETELY_CONFIG_PATH', 'Path to a completely configuration file [default: completely.yaml]' end def environment_debug - environment "COMPLETELY_DEBUG", "It not empty, the generated script will include an additional debugging snippet that outputs the compline and current word to a text file when a completion is requested" + environment 'COMPLETELY_DEBUG', 'It not empty, the generated script will include an additional debugging snippet that outputs the compline and current word to a text file when a completion is requested' end end @@ -46,7 +45,7 @@ def config_basename def syntax_warning say! "\n!txtred!WARNING:\nYour configuration is invalid." - say! "!txtred!All patterns must start with the same word." + say! '!txtred!All patterns must start with the same word.' end end end diff --git a/lib/completely/commands/generate.rb b/lib/completely/commands/generate.rb index 7302f08..42425de 100644 --- a/lib/completely/commands/generate.rb +++ b/lib/completely/commands/generate.rb @@ -3,19 +3,19 @@ module Completely module Commands class Generate < Base - help "Generate the bash completion script to a file" + help 'Generate the bash completion script to a file' - usage "completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]" - usage "completely generate (-h|--help)" + usage 'completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]' + usage 'completely generate (-h|--help)' option_function - option "-w --wrap NAME", "Wrap the completion script inside a function that echos the script. This is useful if you wish to embed it directly in your script" + option '-w --wrap NAME', 'Wrap the completion script inside a function that echos the script. This is useful if you wish to embed it directly in your script' param_config_path - param "OUTPUT_PATH", "Path to the output bash script. When not provided, the name of the input file will be used with a .bash extension\nCan also be set by an environment variable" + param 'OUTPUT_PATH', "Path to the output bash script. When not provided, the name of the input file will be used with a .bash extension\nCan also be set by an environment variable" environment_config_path - environment "COMPLETELY_OUTPUT_PATH", "Path to the output bash script" + environment 'COMPLETELY_OUTPUT_PATH', 'Path to the output bash script' environment_debug def run @@ -31,7 +31,6 @@ def run def wrapper_function(wrapper_name) completions.wrapper_function wrapper_name end - end end end diff --git a/lib/completely/commands/init.rb b/lib/completely/commands/init.rb index 4116ab2..5f06d07 100644 --- a/lib/completely/commands/init.rb +++ b/lib/completely/commands/init.rb @@ -3,21 +3,19 @@ module Completely module Commands class Init < Base - help "Create a new sample YAML configuration file" + help 'Create a new sample YAML configuration file' - usage "completely init [CONFIG_PATH]" - usage "completely init (-h|--help)" + usage 'completely init [CONFIG_PATH]' + usage 'completely init (-h|--help)' param_config_path environment_config_path def run - if File.exist? config_path - raise "File already exists: #{config_path}" - else - File.write config_path, sample - say "Saved !txtpur!#{config_path}" - end + raise "File already exists: #{config_path}" if File.exist? config_path + + File.write config_path, sample + say "Saved !txtpur!#{config_path}" end private @@ -27,9 +25,8 @@ def sample end def sample_path - @sample_path ||= File.expand_path "../templates/sample.yaml", __dir__ + @sample_path ||= File.expand_path '../templates/sample.yaml', __dir__ end - end end end diff --git a/lib/completely/commands/preview.rb b/lib/completely/commands/preview.rb index f4b0123..82f7121 100644 --- a/lib/completely/commands/preview.rb +++ b/lib/completely/commands/preview.rb @@ -3,10 +3,10 @@ module Completely module Commands class Preview < Base - help "Generate the bash completion script to STDOUT" + help 'Generate the bash completion script to STDOUT' - usage "completely preview [CONFIG_PATH --function NAME]" - usage "completely preview (-h|--help)" + usage 'completely preview [CONFIG_PATH --function NAME]' + usage 'completely preview (-h|--help)' option_function param_config_path diff --git a/lib/completely/commands/test.rb b/lib/completely/commands/test.rb index e79dc3f..9493317 100644 --- a/lib/completely/commands/test.rb +++ b/lib/completely/commands/test.rb @@ -3,29 +3,29 @@ module Completely module Commands class Test < Base - summary "Test completions" + summary 'Test completions' - help "This command can be used to test that your completions script responds with the right completions. It works by reading your completely.yaml file, generating a completions script, and generating a temporary testing script." + help 'This command can be used to test that your completions script responds with the right completions. It works by reading your completely.yaml file, generating a completions script, and generating a temporary testing script.' - usage "completely test [--keep] COMPLINE" - usage "completely test (-h|--help)" + usage 'completely test [--keep] COMPLINE' + usage 'completely test (-h|--help)' - option "-k --keep", "Keep the temporary testing script in the current directory" + option '-k --keep', 'Keep the temporary testing script in the current directory' - 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." + 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.' environment_config_path environment_debug - example %q[completely test "mygit pu"] - example %q[completely test "mygit pull "] + example 'completely test "mygit pu"' + example 'completely test "mygit pull "' def run puts tester.test(compline).join "\n" - if args['--keep'] - File.write 'completely-tester.sh', tester_script - puts 'saved completely-tester.sh' - end + return unless args['--keep'] + + File.write 'completely-tester.sh', tester_script + puts 'saved completely-tester.sh' end private @@ -45,7 +45,6 @@ def tester def tester_script @tester_script ||= tester.tester_script compline end - end end end diff --git a/lib/completely/completions.rb b/lib/completely/completions.rb index bf7e87d..b2a1f61 100644 --- a/lib/completely/completions.rb +++ b/lib/completely/completions.rb @@ -3,7 +3,7 @@ module Completely class Completions - attr_reader :config, :function_name + attr_reader :config class << self def load(config_path, function_name: nil) @@ -14,13 +14,14 @@ def load(config_path, function_name: nil) data = YAML.load_file config_path # :nocov: end - + new data, function_name: function_name end end def initialize(config, function_name: nil) - @config, @function_name = config, function_name + @config = config + @function_name = function_name end def patterns @@ -36,11 +37,11 @@ def script end def wrapper_function(name = nil) - name ||= "send_completions" + name ||= 'send_completions' script_lines = script.split("\n").map do |line| clean_line = line.gsub("'") { "\\'" } - %Q[ echo $'#{clean_line}'] + " echo $'#{clean_line}'" end.join("\n") "#{name}() {\n#{script_lines}\n}" @@ -53,13 +54,15 @@ def tester private def patterns! - config.map do |text, completions| + result = config.map do |text, completions| Pattern.new text, completions, pattern_function_name - end.sort_by { |pattern| -pattern.length } + end + + result.sort_by { |pattern| -pattern.length } end def template_path - @template_path ||= File.expand_path("templates/template.erb", __dir__) + @template_path ||= File.expand_path('templates/template.erb', __dir__) end def template @@ -67,7 +70,7 @@ def template end def command - @command ||= config.keys.first.split(' ').first + @command ||= config.keys.first.split.first end def function_name @@ -79,8 +82,7 @@ def pattern_function_name end def pattern_prefixes - patterns.map &:prefix + patterns.map(&:prefix) end - end -end \ No newline at end of file +end diff --git a/lib/completely/pattern.rb b/lib/completely/pattern.rb index 41b22bf..17da567 100644 --- a/lib/completely/pattern.rb +++ b/lib/completely/pattern.rb @@ -17,7 +17,7 @@ def empty? end def words - @words ||= completions.reject { |w| w =~ /^<.*>$/ } + @words ||= completions.grep_v(/^<.*>$/) end def actions @@ -28,21 +28,21 @@ def actions end def prefix - text.split(' ')[0] + text.split[0] end def case_string if text_without_prefix.empty? - "*" - elsif text_without_prefix.include? "*" - %Q['#{text_without_prefix.gsub "*", "'*'"}'] + '*' + elsif text_without_prefix.include? '*' + %['#{text_without_prefix.gsub '*', "'*'"}'] else - %Q['#{text_without_prefix}'*] + "'#{text_without_prefix}'*" end end def text_without_prefix - @text_without_prefix ||= text.split(' ')[1..-1].join ' ' + @text_without_prefix ||= text.split[1..].join ' ' end def compgen @@ -53,9 +53,9 @@ def compgen def compgen! result = [] - result << %Q[#{actions.join ' '}] if actions.any? - result << %Q[-W "$(#{function_name} "#{words.join ' '}")"] if words.any? + result << actions.join(' ').to_s if actions.any? + result << %[-W "$(#{function_name} "#{words.join ' '}")"] if words.any? result.any? ? result.join(' ') : nil end end -end \ No newline at end of file +end diff --git a/lib/completely/tester.rb b/lib/completely/tester.rb index 3f88de6..48d095f 100644 --- a/lib/completely/tester.rb +++ b/lib/completely/tester.rb @@ -5,12 +5,14 @@ module Completely class Tester attr_reader :script, :script_path, :function_name, :cword, :compline - def initialize(script: nil, script_path: nil, function_name: ) - @script, @script_path, @function_name = script, script_path, function_name + def initialize(function_name:, script: nil, script_path: nil) + @script = script + @script_path = script_path + @function_name = function_name end def test(compline) - Tempfile.create "completely-tester" do |f| + Tempfile.create 'completely-tester' do |f| f << tester_script(compline) f.flush `bash #{f.path}` @@ -26,23 +28,20 @@ def tester_script(compline) def set_compline_vars(compline) @compline = compline - @cword = compline.split(' ').size - 1 + @cword = compline.split.size - 1 @cword += 1 if compline.end_with? ' ' end def absolute_script_path - @absolute_script_path ||= begin - script_path ? File.expand_path(script_path) : nil - end + @absolute_script_path ||= script_path ? File.expand_path(script_path) : nil end def template_path - @template_path ||= File.expand_path "templates/tester-template.erb", __dir__ + @template_path ||= File.expand_path 'templates/tester-template.erb', __dir__ end def template @template ||= File.read template_path end - end -end \ No newline at end of file +end diff --git a/lib/completely/version.rb b/lib/completely/version.rb index a129d41..8e0e47e 100644 --- a/lib/completely/version.rb +++ b/lib/completely/version.rb @@ -1,3 +1,3 @@ module Completely - VERSION = "0.5.0" -end \ No newline at end of file + VERSION = '0.5.0' +end diff --git a/spec/completely/bin_spec.rb b/spec/completely/bin_spec.rb index cc866cc..2e28a5c 100644 --- a/spec/completely/bin_spec.rb +++ b/spec/completely/bin_spec.rb @@ -3,12 +3,12 @@ describe 'bin/completely' do subject { CLI.runner } - it "shows list of commands" do - expect{ subject.run }.to output_approval('cli/commands') + it 'shows list of commands' do + expect { subject.run }.to output_approval('cli/commands') end - context "on error" do - it "displays it nicely" do + context 'on error' do + it 'displays it nicely' do expect(`bin/completely preview notfound.yaml 2>&1`).to match_approval('cli/error') end end diff --git a/spec/completely/commands/generate_spec.rb b/spec/completely/commands/generate_spec.rb index 61d68fe..0b28d44 100644 --- a/spec/completely/commands/generate_spec.rb +++ b/spec/completely/commands/generate_spec.rb @@ -2,54 +2,54 @@ describe Commands::Generate do subject { CLI.runner } - before { system "cp lib/completely/templates/sample.yaml completely.yaml" } - after { system "rm -f completely.yaml" } + before { system 'cp lib/completely/templates/sample.yaml completely.yaml' } + after { system 'rm -f completely.yaml' } - context "with --help" do - it "shows long usage" do - expect{ subject.run %w[generate --help] }.to output_approval('cli/generate/help') + context 'with --help' do + it 'shows long usage' do + expect { subject.run %w[generate --help] }.to output_approval('cli/generate/help') end end - context "without arguments" do - it "generates the bash script to completely.bash" do + context 'without arguments' do + it 'generates the bash script to completely.bash' do expect { subject.run %w[generate] }.to output_approval('cli/generate/no-args') - expect(File.read "completely.bash").to match_approval('cli/generated-script') + expect(File.read 'completely.bash').to match_approval('cli/generated-script') end - it "generates a shellcheck compliant script" do + it 'generates a shellcheck compliant script' do expect { subject.run %w[generate] }.to output_approval('cli/generate/no-args') expect(`shellcheck completely.bash 2>&1`).to be_empty end end - context "with CONFIG_PATH" do - it "generates the bash script to completely.bash" do + context 'with CONFIG_PATH' do + it 'generates the bash script to completely.bash' do expect { subject.run %w[generate completely.yaml] }.to output_approval('cli/generate/custom-path') - expect(File.read "completely.bash").to match_approval('cli/generated-script') + expect(File.read 'completely.bash').to match_approval('cli/generated-script') end end - context "with COMPLETELY_CONFIG_PATH env var" do + context 'with COMPLETELY_CONFIG_PATH env var' do before do reset_tmp_dir - system "cp lib/completely/templates/sample.yaml spec/tmp/hello.yml" - system "rm -f completely.yaml" + system 'cp lib/completely/templates/sample.yaml spec/tmp/hello.yml' + system 'rm -f completely.yaml' ENV['COMPLETELY_CONFIG_PATH'] = 'spec/tmp/hello.yml' end after do ENV['COMPLETELY_CONFIG_PATH'] = nil - system "rm -f hello.bash" + system 'rm -f hello.bash' end - it "generates the bash script to hello.bash" do + it 'generates the bash script to hello.bash' do expect { subject.run %w[generate] }.to output_approval('cli/generate/custom-path-env') - expect(File.read "hello.bash").to match_approval('cli/generated-script') + expect(File.read 'hello.bash').to match_approval('cli/generated-script') end end - context "with COMPLETELY_OUTPUT_PATH env var" do + context 'with COMPLETELY_OUTPUT_PATH env var' do let(:outfile) { 'spec/tmp/tada.bash' } before do @@ -61,44 +61,43 @@ ENV['COMPLETELY_OUTPUT_PATH'] = nil end - it "generates the bash script to the requested path" do + it 'generates the bash script to the requested path' do expect { subject.run %w[generate] }.to output_approval('cli/generate/custom-path-env2') expect(File.read outfile).to match_approval('cli/generated-script') end end - context "with CONFIG_PATH OUTPUT_PATH" do + context 'with CONFIG_PATH OUTPUT_PATH' do before { reset_tmp_dir } - it "generates the bash script to the specified path" do + it 'generates the bash script to the specified path' do expect { subject.run %w[generate completely.yaml spec/tmp/out.bash] }.to output_approval('cli/generate/custom-out-path') - expect(File.read "spec/tmp/out.bash").to match_approval('cli/generated-script') + expect(File.read 'spec/tmp/out.bash').to match_approval('cli/generated-script') end end - context "with --function NAME" do - after { system "rm -f completely.bash" } + context 'with --function NAME' do + after { system 'rm -f completely.bash' } - it "uses the provided function name" do + it 'uses the provided function name' do expect { subject.run %w[generate --function _mycomps] }.to output_approval('cli/generate/function') - expect(File.read "completely.bash").to match_approval('cli/generated-script-alt') + expect(File.read 'completely.bash').to match_approval('cli/generated-script-alt') end end - context "with --wrapper NAME" do - after { system "rm -f completely.bash" } + context 'with --wrapper NAME' do + after { system 'rm -f completely.bash' } - it "wraps the script in a function" do + it 'wraps the script in a function' do expect { subject.run %w[generate --wrap give_comps] }.to output_approval('cli/generate/wrapper') - expect(File.read "completely.bash").to match_approval('cli/generated-wrapped-script') + expect(File.read 'completely.bash').to match_approval('cli/generated-wrapped-script') end end - context "with an invalid configuration" do - it "outputs a warning to STDERR" do + context 'with an invalid configuration' do + it 'outputs a warning to STDERR' do expect { subject.run %w[generate spec/fixtures/broken.yaml spec/tmp/out.bash] } .to output_approval('cli/warning').to_stderr end end - end diff --git a/spec/completely/commands/init_spec.rb b/spec/completely/commands/init_spec.rb index 983082c..134cf81 100644 --- a/spec/completely/commands/init_spec.rb +++ b/spec/completely/commands/init_spec.rb @@ -2,34 +2,34 @@ describe Commands::Init do subject { CLI.runner } - before { system "rm -f completely.yaml" } - after { system "rm -f completely.yaml" } - let(:sample) { File.read "lib/completely/templates/sample.yaml" } + before { system 'rm -f completely.yaml' } + after { system 'rm -f completely.yaml' } + let(:sample) { File.read 'lib/completely/templates/sample.yaml' } - context "with --help" do - it "shows long usage" do - expect{ subject.run %w[init --help] }.to output_approval('cli/init/help') + context 'with --help' do + it 'shows long usage' do + expect { subject.run %w[init --help] }.to output_approval('cli/init/help') end end - context "without arguments" do - it "creates a new sample file named completely.yaml" do + context 'without arguments' do + it 'creates a new sample file named completely.yaml' do expect { subject.run %w[init] }.to output_approval('cli/init/no-args') expect(File.read 'completely.yaml').to eq sample end end - context "with CONFIG_PATH" do + context 'with CONFIG_PATH' do before { reset_tmp_dir } - it "creates a new sample file with the requested name" do + it 'creates a new sample file with the requested name' do expect { subject.run %w[init spec/tmp/in.yaml] } .to output_approval('cli/init/custom-path') expect(File.read 'spec/tmp/in.yaml').to eq sample end end - context "with COMPLETELY_CONFIG_PATH env var" do + context 'with COMPLETELY_CONFIG_PATH env var' do before do reset_tmp_dir ENV['COMPLETELY_CONFIG_PATH'] = 'spec/tmp/hello.yml' @@ -37,20 +37,19 @@ after { ENV['COMPLETELY_CONFIG_PATH'] = nil } - it "creates a new sample file with the requested name" do + it 'creates a new sample file with the requested name' do expect { subject.run %w[init] } .to output_approval('cli/init/custom-path-env') expect(File.read 'spec/tmp/hello.yml').to eq sample end end - context "when the config file already exists" do - before { system "cp lib/completely/templates/sample.yaml completely.yaml" } - after { system "rm -f completely.yaml" } - - it "raises an error" do + context 'when the config file already exists' do + before { system 'cp lib/completely/templates/sample.yaml completely.yaml' } + after { system 'rm -f completely.yaml' } + + it 'raises an error' do expect { subject.run %w[init] }.to raise_approval('cli/init/file-exists') end end - end diff --git a/spec/completely/commands/preview_spec.rb b/spec/completely/commands/preview_spec.rb index 60bc8f5..2da25fa 100644 --- a/spec/completely/commands/preview_spec.rb +++ b/spec/completely/commands/preview_spec.rb @@ -2,50 +2,49 @@ describe Commands::Preview do subject { CLI.runner } - before { system "cp lib/completely/templates/sample.yaml completely.yaml" } - after { system "rm -f completely.yaml" } + before { system 'cp lib/completely/templates/sample.yaml completely.yaml' } + after { system 'rm -f completely.yaml' } - context "with --help" do - it "shows long usage" do - expect{ subject.run %w[preview --help] }.to output_approval('cli/preview/help') + context 'with --help' do + it 'shows long usage' do + expect { subject.run %w[preview --help] }.to output_approval('cli/preview/help') end end - context "without arguments" do - it "outputs the generated script to STDOUT" do + context 'without arguments' do + it 'outputs the generated script to STDOUT' do expect { subject.run %w[preview] } .to output_approval('cli/generated-script') end end - context "with CONFIG_PATH" do - it "outputs the generated script to STDOUT" do + context 'with CONFIG_PATH' do + it 'outputs the generated script to STDOUT' do expect { subject.run %w[preview completely.yaml] } .to output_approval('cli/generated-script') end end - context "with COMPLETELY_CONFIG_PATH env var" do + context 'with COMPLETELY_CONFIG_PATH env var' do before do reset_tmp_dir - system "cp lib/completely/templates/sample.yaml spec/tmp/hello.yml" - system "rm -f completely.yaml" + system 'cp lib/completely/templates/sample.yaml spec/tmp/hello.yml' + system 'rm -f completely.yaml' ENV['COMPLETELY_CONFIG_PATH'] = 'spec/tmp/hello.yml' end after { ENV['COMPLETELY_CONFIG_PATH'] = nil } - it "outputs the generated script to STDOUT" do + it 'outputs the generated script to STDOUT' do expect { subject.run %w[preview] } .to output_approval('cli/generated-script') end end - context "with an invalid configuration" do - it "outputs a warning to STDERR" do + context 'with an invalid configuration' do + it 'outputs a warning to STDERR' do expect { subject.run %w[preview spec/fixtures/broken.yaml] } .to output_approval('cli/warning').to_stderr end end - end diff --git a/spec/completely/commands/test_spec.rb b/spec/completely/commands/test_spec.rb index 1eaa19e..58628b5 100644 --- a/spec/completely/commands/test_spec.rb +++ b/spec/completely/commands/test_spec.rb @@ -4,66 +4,65 @@ subject { CLI.runner } before do - system "cp lib/completely/templates/sample.yaml completely.yaml" + system 'cp lib/completely/templates/sample.yaml completely.yaml' ENV['COMPLETELY_CONFIG_PATH'] = nil end - after { system "rm -f completely.yaml" } + after { system 'rm -f completely.yaml' } - context "with --help" do - it "shows long usage" do - expect{ subject.run %w[test --help] }.to output_approval('cli/test/help') + context 'with --help' do + it 'shows long usage' do + expect { subject.run %w[test --help] }.to output_approval('cli/test/help') end end - context "without arguments" do - it "shows a short usage" do + context 'without arguments' do + it 'shows a short usage' do expect { subject.run %w[test] } .to output_approval('cli/test/usage') end end - context "with COMPLINE" do - it "prints completions" do - expect { subject.run ["test", "mygit --"] } + context 'with COMPLINE' do + it 'prints completions' do + expect { subject.run ['test', 'mygit --'] } .to output_approval('cli/test/comps-default') end end - context "with --keep COMPLINE" do + context 'with --keep COMPLINE' do before { system "rm -f #{filename}" } after { system "rm -f #{filename}" } let(:filename) { 'completely-tester.sh' } - it "copies the test script to the current directory" do - expect { subject.run ["test", "--keep", "mygit status --"] } + it 'copies the test script to the current directory' do + expect { subject.run ['test', '--keep', 'mygit status --'] } .to output_approval('cli/test/comps-default-keep') expect(File.read filename).to match_approval('cli/test/completely-tester.sh') end end - context "when COMPLETELY_CONFIG_PATH is set" do + context 'when COMPLETELY_CONFIG_PATH is set' do before do reset_tmp_dir - File.write "spec/tmp/in.yaml", { "play" => ["command", "conquer"] }.to_yaml - ENV['COMPLETELY_CONFIG_PATH'] = "spec/tmp/in.yaml" + File.write 'spec/tmp/in.yaml', { 'play' => %w[command conquer] }.to_yaml + ENV['COMPLETELY_CONFIG_PATH'] = 'spec/tmp/in.yaml' end - it "tests against this completely file" do - expect { subject.run ["test", "play co"] } + it 'tests against this completely file' do + expect { subject.run ['test', 'play co'] } .to output_approval('cli/test/comps-custom-config') end end - context "when there is no compeltely.yaml or COMPLETELY_CONFIG_PATH" do - before { system "rm -f completely.yaml" } - - it "fails gracefully" do - expect { subject.run ["test", "mygit --"] } + context 'when there is no compeltely.yaml or COMPLETELY_CONFIG_PATH' do + before { system 'rm -f completely.yaml' } + + it 'fails gracefully' do + expect { subject.run ['test', 'mygit --'] } .to raise_approval('cli/test/error') end end - end diff --git a/spec/completely/completions_spec.rb b/spec/completely/completions_spec.rb index 0044cb6..e1df6b3 100644 --- a/spec/completely/completions_spec.rb +++ b/spec/completely/completions_spec.rb @@ -3,68 +3,67 @@ describe Completions do subject { described_class.load path } let(:path) { "spec/fixtures/#{file}.yaml" } - let(:file) { "basic" } + let(:file) { 'basic' } describe '#valid?' do - context "when all patterns start with the same word" do - it "returns true" do + context 'when all patterns start with the same word' do + it 'returns true' do expect(subject).to be_valid end end - context "when not all patterns start with the same word" do + context 'when not all patterns start with the same word' do let(:file) { 'broken' } - it "returns false" do + it 'returns false' do expect(subject).not_to be_valid end end end describe '#patterns' do - it "returns an array of Pattern objects" do + it 'returns an array of Pattern objects' do expect(subject.patterns).to be_an Array expect(subject.patterns.first).to be_a Pattern end end describe '#script' do - it "returns a bash completions script" do - expect(subject.script).to match_approval "completions/script" + it 'returns a bash completions script' do + expect(subject.script).to match_approval 'completions/script' end - context "with a configuration file that only includes patterns with spaces" do - let(:file) { "only-spaces" } + context 'with a configuration file that only includes patterns with spaces' do + let(:file) { 'only-spaces' } - it "uses the first word of the first command as the function name" do - expect(subject.script).to match_approval "completions/script-only-spaces" + it 'uses the first word of the first command as the function name' do + expect(subject.script).to match_approval 'completions/script-only-spaces' end end - context "when COMPLETELY_DEBUG is set" do + context 'when COMPLETELY_DEBUG is set' do before { ENV['COMPLETELY_DEBUG'] = '1' } after { ENV['COMPLETELY_DEBUG'] = nil } - it "adds an additional debug snippet to the script" do - expect(subject.script).to match_approval("completions/script-with-debug") + it 'adds an additional debug snippet to the script' do + expect(subject.script).to match_approval('completions/script-with-debug') .except(/case.*/m) - end end end describe '#wrapper_function' do - it "returns the script wrapped inside a function" do - expect(subject.wrapper_function).to match_approval "completions/function" + it 'returns the script wrapped inside a function' do + expect(subject.wrapper_function).to match_approval 'completions/function' end end describe '#tester' do - it "returns a Tester object" do + it 'returns a Tester object' do expect(subject.tester).to be_a Tester end - it "assigns self.script to tester.script" do + it 'assigns self.script to tester.script' do expect(subject.tester.script).to eq subject.script end end diff --git a/spec/completely/integration_spec.rb b/spec/completely/integration_spec.rb index 1aca5fd..c1aaff3 100644 --- a/spec/completely/integration_spec.rb +++ b/spec/completely/integration_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe "generated script" do +describe 'generated script' do subject { Completions.load "#{fixture}.yaml" } let(:response) do diff --git a/spec/completely/pattern_spec.rb b/spec/completely/pattern_spec.rb index e2bed5c..1f46c73 100644 --- a/spec/completely/pattern_spec.rb +++ b/spec/completely/pattern_spec.rb @@ -2,101 +2,101 @@ describe Pattern do subject { described_class.new text, completions, function_name } - let(:text) { "git commit" } + let(:text) { 'git commit' } let(:completions) { %w[--message --help ] } - let(:function_name) { "_filter" } + let(:function_name) { '_filter' } describe '#length' do - it "returns the string length of the pattern text" do + it 'returns the string length of the pattern text' do expect(subject.length).to eq 10 end end describe '#empty?' do - it "returns false" do + it 'returns false' do expect(subject).not_to be_empty end - context "when there are no completions" do + context 'when there are no completions' do let(:completions) { nil } - it "returns true" do + it 'returns true' do expect(subject).to be_empty end end end describe '#words' do - it "returns an array for compgen -W" do + it 'returns an array for compgen -W' do expect(subject.words).to eq %w[--message --help] end end describe '#actions' do - it "returns an array for compgen -A" do - expect(subject.actions).to eq ["-A file", "-A user"] + it 'returns an array for compgen -A' do + expect(subject.actions).to eq ['-A file', '-A user'] end end describe '#prefix' do - it "returns the first word from text" do - expect(subject.prefix).to eq "git" + it 'returns the first word from text' do + expect(subject.prefix).to eq 'git' end end describe '#case_string' do - it "returns the quoted pattern (excluding command name) with a wildcard suffix" do + it 'returns the quoted pattern (excluding command name) with a wildcard suffix' do expect(subject.case_string).to eq "'commit'*" end - context "when the pattern (excluding command name) is empty" do - let(:text) { "git" } + context 'when the pattern (excluding command name) is empty' do + let(:text) { 'git' } it "returns '*'" do - expect(subject.case_string).to eq "*" + expect(subject.case_string).to eq '*' end end - context "when the pattern includes a wildcard" do - let(:text) { "git checkout*--branch" } + context 'when the pattern includes a wildcard' do + let(:text) { 'git checkout*--branch' } - it "returns the quoted pattern (excluding command name) with an unquoted wildcard and without a wildcard suffix" do + it 'returns the quoted pattern (excluding command name) with an unquoted wildcard and without a wildcard suffix' do expect(subject.case_string).to eq "'checkout'*'--branch'" end end end describe '#text_without_prefix' do - it "returns all but the first word from text" do - expect(subject.text_without_prefix).to eq "commit" + it 'returns all but the first word from text' do + expect(subject.text_without_prefix).to eq 'commit' end end describe '#compgen' do - it "returns a line of compgen arguments" do - expect(subject.compgen).to eq %q[-A file -A user -W "$(_filter "--message --help")"] + it 'returns a line of compgen arguments' do + expect(subject.compgen).to eq '-A file -A user -W "$(_filter "--message --help")"' end - context "when there are no words for -W" do + context 'when there are no words for -W' do let(:completions) { %w[ ] } - it "omits the -W argument" do - expect(subject.compgen).to eq %q[-A file -A user] + it 'omits the -W argument' do + expect(subject.compgen).to eq '-A file -A user' end end - context "when there are no actions for -A" do + context 'when there are no actions for -A' do let(:completions) { %w[--message --help] } - it "omits the -A arguments" do - expect(subject.compgen).to eq %q[-W "$(_filter "--message --help")"] + it 'omits the -A arguments' do + expect(subject.compgen).to eq '-W "$(_filter "--message --help")"' end end - context "when there are no completions" do + context 'when there are no completions' do let(:completions) { nil } - it "returns nil" do + it 'returns nil' do expect(subject.compgen).to be_nil end end diff --git a/spec/completely/tester_spec.rb b/spec/completely/tester_spec.rb index b10d81f..c8f19e5 100644 --- a/spec/completely/tester_spec.rb +++ b/spec/completely/tester_spec.rb @@ -5,40 +5,39 @@ let(:function_name) { '_cli_completions' } let(:script_path) { "spec/fixtures/tester/#{fixture}.bash" } let(:fixture) { 'default' } - let(:compline) { "cli co" } + let(:compline) { 'cli co' } before :all do # Create an up to date fixture - comps = Completions.load "spec/fixtures/tester/default.yaml" - File.write "spec/fixtures/tester/default.bash", comps.script + comps = Completions.load 'spec/fixtures/tester/default.yaml' + File.write 'spec/fixtures/tester/default.bash', comps.script end - describe "#tester_script" do - it "sources the script using its absolute path" do - expect(subject.tester_script compline).to match %r[source "/.*spec/fixtures/tester/default.bash"] + describe '#tester_script' do + it 'sources the script using its absolute path' do + expect(subject.tester_script compline).to match %r{source "/.*spec/fixtures/tester/default.bash"} end - it "returns a valid testing script" do - expect(subject.tester_script compline).to match_approval("tester/script_path") + it 'returns a valid testing script' do + expect(subject.tester_script compline).to match_approval('tester/script_path') .except(/source "(.*)"/, 'source ""') end end describe '#test' do - it "returns an array with completions" do - expect(subject.test compline).to eq ["command", "conquer"] + it 'returns an array with completions' do + expect(subject.test compline).to eq %w[command conquer] end end - context "with script instead of script_path" do + context 'with script instead of script_path' do subject { described_class.new script: script, function_name: function_name } - let(:script) { "# some completion script" } + let(:script) { '# some completion script' } describe '#tester_script' do - it "includes the embedded script" do - expect(subject.tester_script compline).to match_approval("tester/script") + it 'includes the embedded script' do + expect(subject.tester_script compline).to match_approval('tester/script') end end end - end diff --git a/spec/completely/zsh_spec.rb b/spec/completely/zsh_spec.rb index 2cf3e66..598ecb6 100644 --- a/spec/completely/zsh_spec.rb +++ b/spec/completely/zsh_spec.rb @@ -1,15 +1,15 @@ require 'spec_helper' -describe "zsh compatibility" do +describe 'zsh compatibility' do let(:completions) { Completely::Completions.load 'spec/fixtures/basic.yaml' } - let(:words) { "completely generate " } + let(:words) { 'completely generate ' } let(:tester_script) { completions.tester.tester_script words } let(:shell) { 'zsh' } before do reset_tmp_dir - system "mkdir -p spec/tmp/somedir" - File.write "spec/tmp/test.sh", tester_script + system 'mkdir -p spec/tmp/somedir' + File.write 'spec/tmp/test.sh', tester_script end subject do @@ -18,18 +18,17 @@ end end - describe "completions script and test script" do - it "returns completions without erroring" do - expect(subject).to eq "somedir" + describe 'completions script and test script' do + it 'returns completions without erroring' do + expect(subject).to eq 'somedir' end - context "on bash" do + context 'on bash' do let(:shell) { 'bash' } - - it "returns the same output" do - expect(subject).to eq "somedir" + + it 'returns the same output' do + expect(subject).to eq 'somedir' end end end - -end \ No newline at end of file +end