From 3fad55bf5e95f674cfccc0362f77a47605b9ba50 Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Tue, 6 Feb 2018 11:09:18 -0500 Subject: [PATCH] break command tests out into individual files --- spec/bin_spec.rb | 94 ++------------------- spec/licensee/commands/detect_spec.rb | 86 +++++++++++++++++++ spec/licensee/commands/license_path_spec.rb | 21 +++++ spec/licensee/commands/version_spec.rb | 21 +++++ 4 files changed, 133 insertions(+), 89 deletions(-) create mode 100644 spec/licensee/commands/detect_spec.rb create mode 100644 spec/licensee/commands/license_path_spec.rb create mode 100644 spec/licensee/commands/version_spec.rb diff --git a/spec/bin_spec.rb b/spec/bin_spec.rb index 80f4f95..7a11847 100644 --- a/spec/bin_spec.rb +++ b/spec/bin_spec.rb @@ -1,5 +1,5 @@ RSpec.describe 'command line invocation' do - let(:command) { ['bundle', 'exec', 'bin/licensee'].push(subcommand) } + let(:command) { ['bundle', 'exec', 'bin/licensee', 'help'] } let(:arguments) { [] } let(:output) do Dir.chdir project_root do @@ -10,96 +10,12 @@ RSpec.describe 'command line invocation' do let(:stdout) { output[0] } let(:stderr) { output[1] } let(:status) { output[2] } - let(:hash) { '46cdc03462b9af57968df67b450cc4372ac41f53' } - context 'detection' do - let(:subcommand) { 'detect' } - - let(:expected) do - { - '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' - } - } - end - - { - '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 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 + it 'Returns a zero exit code' do + expect(status.exitstatus).to eql(0) 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 - - context 'json' do - let(:subcommand) { 'detect' } - let(:arguments) { ['--json'] } - let(:expected) { JSON.parse fixture_contents('detect.json') } - - it 'Returns a zero exit code' do - expect(status.exitstatus).to eql(0) - end - - it 'returns valid JSON' do - expect { JSON.parse(stdout) }.to_not raise_error - end - - it 'returns the expected output' do - expect(JSON.parse(stdout)).to eql(expected) - end + it 'returns the help text' do + expect(stdout).to include('Licensee commands:') end end diff --git a/spec/licensee/commands/detect_spec.rb b/spec/licensee/commands/detect_spec.rb new file mode 100644 index 0000000..7eec2f4 --- /dev/null +++ b/spec/licensee/commands/detect_spec.rb @@ -0,0 +1,86 @@ +RSpec.describe 'detect command' do + let(:command) { ['bundle', 'exec', 'bin/licensee', 'detect'] } + let(:arguments) { [] } + let(:output) do + Dir.chdir project_root do + Open3.capture3(*[command, arguments].flatten) + end + end + let(:parsed_output) { YAML.safe_load(stdout) } + let(:stdout) { output[0] } + let(:stderr) { output[1] } + let(:status) { output[2] } + let(:hash) { '46cdc03462b9af57968df67b450cc4372ac41f53' } + let(:expected) do + { + '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' + } + } + end + + { + '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 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 + + context 'json' do + let(:arguments) { ['--json'] } + let(:expected) { JSON.parse fixture_contents('detect.json') } + + it 'Returns a zero exit code' do + expect(status.exitstatus).to eql(0) + end + + it 'returns valid JSON' do + expect { JSON.parse(stdout) }.to_not raise_error + end + + it 'returns the expected output' do + expect(JSON.parse(stdout)).to eql(expected) + end + end + + context 'the default command' do + let(:command) { ['bundle', 'exec', 'bin/licensee'] } + + it 'Returns a zero exit code' do + expect(status.exitstatus).to eql(0) + end + + it 'returns the exected values' do + expect(parsed_output).to eql(expected) + end + end +end diff --git a/spec/licensee/commands/license_path_spec.rb b/spec/licensee/commands/license_path_spec.rb new file mode 100644 index 0000000..0882ea5 --- /dev/null +++ b/spec/licensee/commands/license_path_spec.rb @@ -0,0 +1,21 @@ +RSpec.describe 'license-path command' do + let(:command) { ['bundle', 'exec', 'bin/licensee', 'license-path'] } + let(:arguments) { [project_root] } + let(:output) do + Dir.chdir project_root do + Open3.capture3(*[command, arguments].flatten) + end + end + let(:parsed_output) { YAML.safe_load(stdout) } + let(:stdout) { output[0] } + let(:stderr) { output[1] } + let(:status) { output[2] } + + 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 diff --git a/spec/licensee/commands/version_spec.rb b/spec/licensee/commands/version_spec.rb new file mode 100644 index 0000000..3bd5bc5 --- /dev/null +++ b/spec/licensee/commands/version_spec.rb @@ -0,0 +1,21 @@ +RSpec.describe 'version command' do + let(:command) { ['bundle', 'exec', 'bin/licensee', 'version'] } + let(:arguments) { [] } + let(:output) do + Dir.chdir project_root do + Open3.capture3(*[command, arguments].flatten) + end + end + let(:parsed_output) { YAML.safe_load(stdout) } + let(:stdout) { output[0] } + let(:stderr) { output[1] } + let(:status) { output[2] } + + 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