spec/tracks_checker_spec.rb in danger-dangermattic-1.0.0 vs spec/tracks_checker_spec.rb in danger-dangermattic-1.0.1

- old
+ new

@@ -66,11 +66,11 @@ end it 'does nothing when there are no changes in Tracks-related files' do modified_files = ['MyClass.swift'] allow(@plugin.git_utils).to receive(:all_changed_files).and_return(modified_files) - allow(@plugin.git_utils).to receive(:matching_lines_in_diff_files).with(files: modified_files, line_matcher: kind_of(Proc), change_type: nil).and_return([]) + allow(@plugin.git_utils).to receive(:matching_lines_in_diff_files).with(files: modified_files, line_matcher: kind_of(Proc), change_type: %i[added removed]).and_return([]) @plugin.check_tracks_changes(tracks_files: track_files, tracks_usage_matchers: tracks_matchers, tracks_label: nil) expect(@dangerfile).to not_report end @@ -81,70 +81,88 @@ tracks_label = 'Tracks' allow(@plugin.github).to receive(:pr_labels).and_return([tracks_label]) modified_files = ['MyClass.kt'] allow(@plugin.git_utils).to receive(:all_changed_files).and_return(modified_files) - allow(@plugin.git_utils).to receive(:matching_lines_in_diff_files).with(files: modified_files, line_matcher: kind_of(Proc), change_type: nil) do |args| - analytics_call_in_diff = '- AnalyticsTracker.track("myEvent1")' - expect(args[:line_matcher].call(analytics_call_in_diff)).to be true + allow_diff_match(modified_files: modified_files, diff_line: '- AnalyticsTracker.track("myEvent1")', expect_match: true) - [analytics_call_in_diff] - end - @plugin.check_tracks_changes(tracks_files: track_files, tracks_usage_matchers: tracks_matchers, tracks_label: tracks_label) expect(@dangerfile).to report_messages([TracksChecker::TRACKS_PR_INSTRUCTIONS + format(TracksChecker::TRACKS_NO_LABEL_INSTRUCTION_FORMAT, tracks_label)]) end it 'reports a message with instructions without the label check for review when there are matching changes and no label parameter' do modified_files = ['MyClass.kt'] allow(@plugin.git_utils).to receive(:all_changed_files).and_return(modified_files) - allow(@plugin.git_utils).to receive(:matching_lines_in_diff_files).with(files: modified_files, line_matcher: kind_of(Proc), change_type: nil) do |args| - analytics_call_in_diff = '- AnalyticsTracker.track("evento")' - expect(args[:line_matcher].call(analytics_call_in_diff)).to be true + allow_diff_match(modified_files: modified_files, diff_line: '+ AnalyticsTracker.track("evento")', expect_match: true) - [analytics_call_in_diff] - end - @plugin.check_tracks_changes(tracks_files: track_files, tracks_usage_matchers: tracks_matchers, tracks_label: nil) expect(@dangerfile).to report_messages([TracksChecker::TRACKS_PR_INSTRUCTIONS]) end it 'reports a message with instructions for review and an error when there are matching changes and no label' do allow(@plugin.github).to receive(:pr_labels).and_return([]) modified_files = ['MyClass.kt'] allow(@plugin.git_utils).to receive(:all_changed_files).and_return(modified_files) - allow(@plugin.git_utils).to receive(:matching_lines_in_diff_files).with(files: modified_files, line_matcher: kind_of(Proc), change_type: nil) do |args| - analytics_call_in_diff = '- AnalyticsTracker.track("myEvent1")' - expect(args[:line_matcher].call(analytics_call_in_diff)).to be true + allow_diff_match(modified_files: modified_files, diff_line: '- AnalyticsTracker.track("evento")', expect_match: true) - [analytics_call_in_diff] - end - tracks_label = 'TRACKS PR' @plugin.check_tracks_changes(tracks_files: track_files, tracks_usage_matchers: tracks_matchers, tracks_label: tracks_label) expect_label_checks(tracks_label) end it 'does nothing when there are no matching changes' do modified_files = ['MyClass.kt'] allow(@plugin.git_utils).to receive(:all_changed_files).and_return(modified_files) - allow(@plugin.git_utils).to receive(:matching_lines_in_diff_files).with(files: modified_files, line_matcher: kind_of(Proc), change_type: nil) do |args| - analytics_call_in_diff = '+ AnalyticsHelper.log("event_1")' - expect(args[:line_matcher].call(analytics_call_in_diff)).to be false + allow_diff_match(modified_files: modified_files, diff_line: '- AnalyticsHelper.log("event_1")', expect_match: false) - [] - end + @plugin.check_tracks_changes(tracks_files: track_files, tracks_usage_matchers: tracks_matchers, tracks_label: nil) + expect(@dangerfile).to not_report + end + + it 'does nothing when there are matching changes but in the context parts of the diff' do + modified_file = 'MyClass.kt' + allow(@plugin.git_utils).to receive(:all_changed_files).and_return([modified_file]) + + tracks_diff = <<~STRINGS + diff --git a/MyClass.kt b/MyClass.kt + index 5794d472..772e2b99 100644 + - a/MyClass + + b/MyClass + @@ -1,3 +1,6 @@ + AnalyticsTracker.track("myMagicEvent1") + // call magic + + AnalyticsHelper.log("event_1") + + AnalyticsHelper.log("event_2") + + AnotherUtil.callMagic() + AnalyticsTracker.track("myMagicEvent2") + STRINGS + + diff = GitDiffStruct.new('modified', modified_file, tracks_diff) + + allow(@plugin.git).to receive(:diff_for_file).with(modified_file).and_return(diff) + @plugin.check_tracks_changes(tracks_files: track_files, tracks_usage_matchers: tracks_matchers, tracks_label: nil) expect(@dangerfile).to not_report end + end + end + + def allow_diff_match(modified_files:, diff_line:, expect_match:) + allow(@plugin.git_utils).to receive(:matching_lines_in_diff_files).with(files: modified_files, line_matcher: kind_of(Proc), change_type: %i[added removed]) do |args| + expect(args[:line_matcher].call(diff_line)).to be expect_match + + result = [] + result.append(diff_line) if expect_match + + result end end def expect_label_checks(tracks_label) expect(@dangerfile.status_report[:messages]).to eq [TracksChecker::TRACKS_PR_INSTRUCTIONS + format(TracksChecker::TRACKS_NO_LABEL_INSTRUCTION_FORMAT, tracks_label)]