diff --git a/CHANGELOG.md b/CHANGELOG.md index 529eb58d..eea4d891 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * [CHANGE] Fix test warning related to `cucumber_opts` declaration (by [@faisal][]) * [BUGFIX] Stop using long-deprecated MiniTest module name, removed in 5.19.0 (by [@faisal][]) * [CHANGE] Disable VERBOSE warnings in test stubs (by [@fbuys][]) +* [BUGFIX] Raise error when the same branches are compared (by [@rishijain][]) # v4.8.1 / 2023-05-17 [(commits)](https://github.com/whitesmith/rubycritic/compare/v4.8.0...v4.8.1) diff --git a/lib/rubycritic/commands/compare.rb b/lib/rubycritic/commands/compare.rb index 929e6f25..b0e55452 100644 --- a/lib/rubycritic/commands/compare.rb +++ b/lib/rubycritic/commands/compare.rb @@ -18,6 +18,10 @@ def initialize(options) end def execute + if Config.send(:base_branch) == Config.send(:feature_branch) + raise('The branch you are on and are comparing with are the same. + Please switch to a different branch or choose a different branch to compare.') + end compare_branches status_reporter.score = Config.feature_branch_score status_reporter diff --git a/test/lib/rubycritic/commands/compare_test.rb b/test/lib/rubycritic/commands/compare_test.rb index ab52e643..3019a6d6 100644 --- a/test/lib/rubycritic/commands/compare_test.rb +++ b/test/lib/rubycritic/commands/compare_test.rb @@ -33,6 +33,22 @@ def abort(str); end RubyCritic::SourceControlSystem::Git.stubs(:modified_files).returns('test/samples/compare_file.rb') end + describe 'comparing the branches with the compare option' do + context 'when same branches are compared' do + it 'it aborts with the error message' do + options = ['-b', 'feature_branch'] + options = RubyCritic::Cli::Options.new(options).parse.to_h + RubyCritic::Config.set(options) + comparison = RubyCritic::Command::Compare.new(options) + + assert_raises('The branch you are on and are comparing with are the same. + Please switch to a different branch or choose a different branch to compare.') do + comparison.execute + end + end + end + end + describe 'comparing the same file for two different branches' do after do # clear file contents after tests @@ -65,6 +81,7 @@ def abort(str); end options = ['-b', 'feature_branch', '-t', '0', 'test/samples/compare_file.rb'] options = RubyCritic::Cli::Options.new(options).parse.to_h RubyCritic::Config.set(options) + RubyCritic::Config.set(base_branch: 'base_branch') copy_proc = proc do |_| FileUtils.cp 'test/samples/base_branch_file.rb', 'test/samples/compare_file.rb' end