From df0737334c691dccf6a1f50f3f94f7b24d38a906 Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Tue, 6 Feb 2018 10:46:58 -0500 Subject: [PATCH] test bin --- lib/licensee/commands/detect.rb | 9 ++- lib/licensee/commands/diff.rb | 9 ++- spec/bin_spec.rb | 98 ++++++++++++++++++++++----------- 3 files changed, 80 insertions(+), 36 deletions(-) diff --git a/lib/licensee/commands/detect.rb b/lib/licensee/commands/detect.rb index d61a949..60396ba 100644 --- a/lib/licensee/commands/detect.rb +++ b/lib/licensee/commands/detect.rb @@ -22,13 +22,16 @@ class LicenseeCLI < Thor rows = [] rows << if project.license ['License:', project.license.name] - elsif project.licenses - ['Licenses:', project.licenses.map(&:name)] + elsif !project.licenses.empty? + ["Licenses:", project.licenses.map(&:name)] else ['License:', set_color('None', :red)] end - rows << ['Matched files:', project.matched_files.map(&:filename).join(', ')] + unless project.matched_files.empty? + rows << ['Matched files:', project.matched_files.map(&:filename).join(', ')] + end + print_table rows project.matched_files.each do |matched_file| diff --git a/lib/licensee/commands/diff.rb b/lib/licensee/commands/diff.rb index 64e990a..94a906d 100644 --- a/lib/licensee/commands/diff.rb +++ b/lib/licensee/commands/diff.rb @@ -52,10 +52,15 @@ class LicenseeCLI < Thor end def expected_license - @expected_license ||= Licensee::License.find options[:license] + @expected_license ||= Licensee::License.find options[:license] if options[:license] return @expected_license if @expected_license - error "#{options[:license]} is not a valid license" + if options[:license] + error "#{options[:license]} is not a valid license" + else + error "You must provide an expected license" + end + error "Valid licenses: #{Licensee::License.all(hidden: true).map(&:key).join(', ')}" exit 1 end diff --git a/spec/bin_spec.rb b/spec/bin_spec.rb index 054979b..609949c 100644 --- a/spec/bin_spec.rb +++ b/spec/bin_spec.rb @@ -1,8 +1,8 @@ RSpec.describe 'command line invocation' do - let(:command) { ['bundle', 'exec', 'bin/licensee'] } + let(:command) { ['bundle', 'exec', 'bin/licensee'].push(subcommand) } + let(:arguments) { [] } let(:output) do Dir.chdir project_root do - puts [command, arguments].flatten.inspect Open3.capture3(*[command, arguments].flatten) end end @@ -11,41 +11,77 @@ RSpec.describe 'command line invocation' do let(:stderr) { output[1] } let(:status) { output[2] } let(:hash) { '46cdc03462b9af57968df67b450cc4372ac41f53' } - let(:expected) { - { - "License" => "MIT License", - "Matched files" => "LICENSE.md, licensee.gemspec", - "LICENSE.md" => { - "Content hash" => hash, - "Attribution" => "Copyright (c) 2014-2017 Ben Balter", - "Confidence" => "100.00%", - "Matcher" => "Licensee::Matchers::Exact", - "License" => "MIT License" - }, - "licensee.gemspec" => { - "Confidence" => "90.00%", - "Matcher" => "Licensee::Matchers::Gemspec", - "License" => "MIT License" + + context "detection" do + let(:subcommand) { 'detect' } + + let(:expected) { + { + "License" => "MIT License", + "Matched files" => "LICENSE.md, licensee.gemspec", + "LICENSE.md" => { + "Content hash" => hash, + "Attribution" => "Copyright (c) 2014-2017 Ben Balter", + "Confidence" => "100.00%", + "Matcher" => "Licensee::Matchers::Exact", + "License" => "MIT License" + }, + "licensee.gemspec" => { + "Confidence" => "90.00%", + "Matcher" => "Licensee::Matchers::Gemspec", + "License" => "MIT License" + } } } - } - { - "No arguments" => [], - "Project root" => [project_root], - "License path" => [File.expand_path('LICENSE.md', project_root)] - }.each do |name, args| - context "When given #{name}" do - let(:arguments) { args } + { + "No arguments" => [], + "Project root" => [project_root], + "License path" => [File.expand_path('LICENSE.md', project_root)] + }.each do |name, args| + context "When given #{name}" do + let(:arguments) { args } - it 'Returns a zero exit code' do - expect(status.exitstatus).to eql(0) - end + it 'Returns a zero exit code' do + expect(status.exitstatus).to eql(0) + end - it "returns the exected values" do - puts stdout - expect(parsed_output).to eql(expected), stdout + stderr + it "returns the exected values" do + hash = expected.dup + + if name == "License path" + hash.delete("licensee.gemspec") + hash["Matched files"] = "LICENSE.md" + end + + expect(parsed_output).to eql(hash) + end end end end + + context "version" do + let(:subcommand) { 'version' } + + it "returns the version" do + expect(stdout).to include(Licensee::VERSION) + end + + it 'Returns a zero exit code' do + expect(status.exitstatus).to eql(0) + end + end + + context "license-path" do + let(:subcommand) { 'license-path' } + let(:arguments) { [project_root] } + + it "returns the license path" do + expect(stdout).to match(File.join(project_root, "LICENSE.md")) + end + + it 'Returns a zero exit code' do + expect(status.exitstatus).to eql(0) + end + end end