From 4bda4fa5249f5fd70df571093115e99f9eb0a6cf Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Mon, 18 Apr 2022 08:25:06 -0400 Subject: [PATCH 1/6] clean up common test setup --- ..._deprecated_references_yml_changes_spec.rb | 238 ++++++++---------- 1 file changed, 110 insertions(+), 128 deletions(-) diff --git a/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb b/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb index d18bc1d..60b4d6c 100644 --- a/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb +++ b/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb @@ -21,8 +21,19 @@ module DangerPackwerk ) end + let(:some_pack_deprecated_references_before) { nil } + before do allow(danger_deprecated_references_yml_changes.git).to receive(:diff).and_return({ 'packs/some_pack/deprecated_references.yml' => double(patch: 'some_fancy_patch') }) + + # After we make the system call to apply the inverse of the deletion patch, we should expect the file + # to be present again, so we write it here as a means of stubbing out that call to `git`. + allow(Open3).to receive(:capture3) do |system_call| + expect(system_call).to include('git apply --reverse ') + patch_file = system_call.gsub('git apply --reverse ', '') + expect(File.read(patch_file)).to eq "some_fancy_patch\n" + some_pack_deprecated_references_before + end end context 'when no deprecated_references.yml files have been added or modified' do @@ -133,25 +144,17 @@ module DangerPackwerk context 'a deprecated_refrences.yml file is deleted (i.e. a pack has all violations removed)' do let(:deleted_files) { ['packs/some_pack/deprecated_references.yml'] } - - before do - expect(Open3).to receive(:capture3) do |system_call| - expect(system_call).to include('git apply --reverse ') - patch_file = system_call.gsub('git apply --reverse ', '') - expect(File.read(patch_file)).to eq "some_fancy_patch\n" - # After we make the system call to apply the inverse of the deletion patch, we should expect the file - # to be present again, so we write it here as a means of stubbing out that call to `git`. - write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) - --- - packs/some_other_pack: - "OtherPackClass": - violations: - - dependency - - privacy - files: - - packs/some_pack/some_class.rb - YML - end + let(:some_pack_deprecated_references_before) do + write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) + --- + packs/some_other_pack: + "OtherPackClass": + violations: + - dependency + - privacy + files: + - packs/some_pack/some_class.rb + YML end it 'calls the before comment input proc' do @@ -187,29 +190,22 @@ module DangerPackwerk [file.to_s] end - before do - expect(Open3).to receive(:capture3) do |system_call| - expect(system_call).to include('git apply --reverse ') - patch_file = system_call.gsub('git apply --reverse ', '') - expect(File.read(patch_file)).to eq "some_fancy_patch\n" - # After we make the system call to apply the inverse of the deletion patch, we should expect the file - # to be present again, so we write it here as a means of stubbing out that call to `git`. - write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) - --- - packs/some_other_pack: - "OtherPackClass": - violations: - - privacy - files: - - packs/some_pack/some_class.rb - "OtherPackClass2": - violations: - - dependency - - privacy - files: - - packs/some_pack/some_class2.rb - YML - end + let(:some_pack_deprecated_references_before) do + write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) + --- + packs/some_other_pack: + "OtherPackClass": + violations: + - privacy + files: + - packs/some_pack/some_class.rb + "OtherPackClass2": + violations: + - dependency + - privacy + files: + - packs/some_pack/some_class2.rb + YML end it 'calls the before comment input proc' do @@ -246,41 +242,34 @@ module DangerPackwerk [file.to_s] end - before do - expect(Open3).to receive(:capture3) do |system_call| - expect(system_call).to include('git apply --reverse ') - patch_file = system_call.gsub('git apply --reverse ', '') - expect(File.read(patch_file)).to eq "some_fancy_patch\n" - # After we make the system call to apply the inverse of the deletion patch, we should expect the file - # to be present again, so we write it here as a means of stubbing out that call to `git`. - write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) - --- - packs/some_other_pack: - "OtherPackClass": - violations: - - privacy - files: - - packs/some_pack/some_class.rb - - packs/some_pack/some_class2.rb - - packs/some_pack/some_class3.rb - - packs/some_pack/some_class4.rb - - packs/some_pack/some_class5.rb - - packs/some_pack/some_class6.rb - - packs/some_pack/some_class7.rb - "OtherPackClass2": - violations: - - dependency - - privacy - files: - - packs/some_pack/some_class2.rb - - packs/some_pack/some_class3.rb - - packs/some_pack/some_class4.rb - - packs/some_pack/some_class5.rb - - packs/some_pack/some_class6.rb - - packs/some_pack/some_class7.rb - - YML - end + let(:some_pack_deprecated_references_before) do + write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) + --- + packs/some_other_pack: + "OtherPackClass": + violations: + - privacy + files: + - packs/some_pack/some_class.rb + - packs/some_pack/some_class2.rb + - packs/some_pack/some_class3.rb + - packs/some_pack/some_class4.rb + - packs/some_pack/some_class5.rb + - packs/some_pack/some_class6.rb + - packs/some_pack/some_class7.rb + "OtherPackClass2": + violations: + - dependency + - privacy + files: + - packs/some_pack/some_class2.rb + - packs/some_pack/some_class3.rb + - packs/some_pack/some_class4.rb + - packs/some_pack/some_class5.rb + - packs/some_pack/some_class6.rb + - packs/some_pack/some_class7.rb + + YML end it 'calls the before comment input proc' do @@ -341,57 +330,50 @@ module DangerPackwerk [file.to_s] end - before do - expect(Open3).to receive(:capture3) do |system_call| - expect(system_call).to include('git apply --reverse ') - patch_file = system_call.gsub('git apply --reverse ', '') - expect(File.read(patch_file)).to eq "some_fancy_patch\n" - # After we make the system call to apply the inverse of the deletion patch, we should expect the file - # to be present again, so we write it here as a means of stubbing out that call to `git`. - write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) - --- - packs/some_other_pack: - "OtherPackClass": - violations: - - privacy - files: - - packs/some_pack/some_class1.rb - - packs/some_pack/some_class2.rb - - packs/some_pack/some_class3.rb - - packs/some_pack/some_class4.rb - - packs/some_pack/some_class5.rb - - packs/some_pack/some_class6.rb - - packs/some_pack/some_class7.rb - - packs/some_pack/some_class8.rb - - packs/some_pack/some_class9.rb - - packs/some_pack/some_class10.rb - - packs/some_pack/some_class11.rb - - packs/some_pack/some_class12.rb - - packs/some_pack/some_class13.rb - - packs/some_pack/some_class14.rb - - packs/some_pack/some_class15.rb - "AnotherPackClass": - violations: - - privacy - - dependency - files: - - packs/some_pack/some_other_class1.rb - - packs/some_pack/some_other_class2.rb - - packs/some_pack/some_other_class3.rb - - packs/some_pack/some_other_class4.rb - - packs/some_pack/some_other_class5.rb - - packs/some_pack/some_other_class6.rb - - packs/some_pack/some_other_class7.rb - - packs/some_pack/some_other_class8.rb - - packs/some_pack/some_other_class9.rb - - packs/some_pack/some_other_class10.rb - - packs/some_pack/some_other_class11.rb - - packs/some_pack/some_other_class12.rb - - packs/some_pack/some_other_class13.rb - - packs/some_pack/some_other_class14.rb - - packs/some_pack/some_other_class15.rb - YML - end + let(:some_pack_deprecated_references_before) do + write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) + --- + packs/some_other_pack: + "OtherPackClass": + violations: + - privacy + files: + - packs/some_pack/some_class1.rb + - packs/some_pack/some_class2.rb + - packs/some_pack/some_class3.rb + - packs/some_pack/some_class4.rb + - packs/some_pack/some_class5.rb + - packs/some_pack/some_class6.rb + - packs/some_pack/some_class7.rb + - packs/some_pack/some_class8.rb + - packs/some_pack/some_class9.rb + - packs/some_pack/some_class10.rb + - packs/some_pack/some_class11.rb + - packs/some_pack/some_class12.rb + - packs/some_pack/some_class13.rb + - packs/some_pack/some_class14.rb + - packs/some_pack/some_class15.rb + "AnotherPackClass": + violations: + - privacy + - dependency + files: + - packs/some_pack/some_other_class1.rb + - packs/some_pack/some_other_class2.rb + - packs/some_pack/some_other_class3.rb + - packs/some_pack/some_other_class4.rb + - packs/some_pack/some_other_class5.rb + - packs/some_pack/some_other_class6.rb + - packs/some_pack/some_other_class7.rb + - packs/some_pack/some_other_class8.rb + - packs/some_pack/some_other_class9.rb + - packs/some_pack/some_other_class10.rb + - packs/some_pack/some_other_class11.rb + - packs/some_pack/some_other_class12.rb + - packs/some_pack/some_other_class13.rb + - packs/some_pack/some_other_class14.rb + - packs/some_pack/some_other_class15.rb + YML end it 'calls the before comment input proc' do From 139159c40e91dbcf863f3840878e0753adf66474 Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Mon, 18 Apr 2022 08:26:24 -0400 Subject: [PATCH 2/6] clean up let blocks --- ..._deprecated_references_yml_changes_spec.rb | 122 +++++++++--------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb b/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb index 60b4d6c..fecaba8 100644 --- a/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb +++ b/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb @@ -56,18 +56,18 @@ module DangerPackwerk describe 'behavior when violations are added or removed' do context 'a deprecated_refrences.yml file is added (i.e. a pack has its first violation)' do let(:added_files) do - file = write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) - --- - packs/some_other_pack: - "OtherPackClass": - violations: - - privacy - - dependency - files: - - packs/some_pack/some_class.rb - YML - - [file.to_s] + [ + write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) + --- + packs/some_other_pack: + "OtherPackClass": + violations: + - privacy + - dependency + files: + - packs/some_pack/some_class.rb + YML + ] end it 'calls the before comment input proc' do @@ -177,17 +177,17 @@ module DangerPackwerk context 'a deprecated_refrences.yml file is modified to remove violations' do let(:modified_files) do - file = write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) - --- - packs/some_other_pack: - "OtherPackClass": - violations: - - privacy - files: - - packs/some_pack/some_class.rb - YML - - [file.to_s] + [ + write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) + --- + packs/some_other_pack: + "OtherPackClass": + violations: + - privacy + files: + - packs/some_pack/some_class.rb + YML + ] end let(:some_pack_deprecated_references_before) do @@ -228,18 +228,18 @@ module DangerPackwerk context 'a deprecated_refrences.yml file is modified to change violations in many files' do let(:modified_files) do - file = write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) - --- - packs/some_other_pack: - "OtherPackClass": - violations: - - privacy - - dependency - files: - - packs/some_pack/some_class.rb - YML - - [file.to_s] + [ + write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) + --- + packs/some_other_pack: + "OtherPackClass": + violations: + - privacy + - dependency + files: + - packs/some_pack/some_class.rb + YML + ] end let(:some_pack_deprecated_references_before) do @@ -302,32 +302,32 @@ module DangerPackwerk context 'a deprecated_refrences.yml file is modified to add 30 and remove 15 violations' do let(:modified_files) do - file = write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) - --- - packs/some_other_pack: - "OtherPackClass": - violations: - - privacy - - dependency - files: - - packs/some_pack/some_class1.rb - - packs/some_pack/some_class2.rb - - packs/some_pack/some_class3.rb - - packs/some_pack/some_class4.rb - - packs/some_pack/some_class5.rb - - packs/some_pack/some_class6.rb - - packs/some_pack/some_class7.rb - - packs/some_pack/some_class8.rb - - packs/some_pack/some_class9.rb - - packs/some_pack/some_class10.rb - - packs/some_pack/some_class11.rb - - packs/some_pack/some_class12.rb - - packs/some_pack/some_class13.rb - - packs/some_pack/some_class14.rb - - packs/some_pack/some_class15.rb - YML - - [file.to_s] + [ + write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) + --- + packs/some_other_pack: + "OtherPackClass": + violations: + - privacy + - dependency + files: + - packs/some_pack/some_class1.rb + - packs/some_pack/some_class2.rb + - packs/some_pack/some_class3.rb + - packs/some_pack/some_class4.rb + - packs/some_pack/some_class5.rb + - packs/some_pack/some_class6.rb + - packs/some_pack/some_class7.rb + - packs/some_pack/some_class8.rb + - packs/some_pack/some_class9.rb + - packs/some_pack/some_class10.rb + - packs/some_pack/some_class11.rb + - packs/some_pack/some_class12.rb + - packs/some_pack/some_class13.rb + - packs/some_pack/some_class14.rb + - packs/some_pack/some_class15.rb + YML + ] end let(:some_pack_deprecated_references_before) do From 8b728d2d2cab0ed9447b3a03265231033fbc6f26 Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Mon, 18 Apr 2022 08:26:52 -0400 Subject: [PATCH 3/6] remove unnecessary context --- ..._deprecated_references_yml_changes_spec.rb | 538 +++++++++--------- 1 file changed, 268 insertions(+), 270 deletions(-) diff --git a/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb b/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb index fecaba8..a7ee0cb 100644 --- a/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb +++ b/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb @@ -53,144 +53,130 @@ module DangerPackwerk end end - describe 'behavior when violations are added or removed' do - context 'a deprecated_refrences.yml file is added (i.e. a pack has its first violation)' do - let(:added_files) do - [ - write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) - --- - packs/some_other_pack: - "OtherPackClass": - violations: - - privacy - - dependency - files: - - packs/some_pack/some_class.rb - YML - ] - end - - it 'calls the before comment input proc' do - expect(slack_notifier).to receive(:notify_slack).with( - { dependency: { minus: 0, plus: 1 }, privacy: { minus: 0, plus: 1 } }, - ['packs/some_pack/deprecated_references.yml'] - ) - - subject - end - - context 'default formatter is used' do - it 'displays a markdown with a reasonable message' do - subject - expect(dangerfile.status_report[:warnings]).to be_empty - expect(dangerfile.status_report[:errors]).to be_empty - actual_markdowns = dangerfile.status_report[:markdowns] - expect(actual_markdowns.count).to eq 1 - actual_markdown = actual_markdowns.first - expected = <<~EXPECTED - Hi! It looks like the pack defining `OtherPackClass` considers this private API, and it's also not in the referencing pack's list of dependencies. - We noticed you ran `bin/packwerk update-deprecations`. Make sure to read through [the docs](https://github.com/Shopify/packwerk/blob/b647594f93c8922c038255a7aaca125d391a1fbf/docs/new_violation_flow_chart.pdf) for other ways to resolve. Could you add some context as a reply here about why we needed to add these violations? - EXPECTED - - expect(actual_markdown.message).to eq expected - expect(actual_markdown.line).to eq 3 - expect(actual_markdown.file).to eq 'packs/some_pack/deprecated_references.yml' - expect(actual_markdown.type).to eq :markdown - end - end - - context 'a offenses formatter is passed in' do - let(:added_offenses_formatter) do - lambda do |added_violations| - <<~MESSAGE - There are #{added_violations.count} new violations, - with class_names #{added_violations.map(&:class_name).uniq.sort}, - with to_package_names #{added_violations.map(&:to_package_name).uniq.sort}, - with types #{added_violations.map(&:type).sort}, - MESSAGE - end - end - - subject do - danger_deprecated_references_yml_changes.check( - added_offenses_formatter: added_offenses_formatter, - before_comment: lambda do |_violation_diff, changed_deprecated_references_ymls| - slack_notifier.notify_slack(changed_deprecated_references_ymls) - end - ) - end - - it 'displays a markdown using the passed in offenses formatter' do - subject - expect(dangerfile.status_report[:warnings]).to be_empty - expect(dangerfile.status_report[:errors]).to be_empty - actual_markdowns = dangerfile.status_report[:markdowns] - expect(actual_markdowns.count).to eq 1 - actual_markdown = actual_markdowns.first - expected = <<~EXPECTED - There are 2 new violations, - with class_names ["OtherPackClass"], - with to_package_names ["packs/some_other_pack"], - with types ["dependency", "privacy"], - EXPECTED - - expect(actual_markdown.message).to eq expected - expect(actual_markdown.line).to eq 3 - expect(actual_markdown.file).to eq 'packs/some_pack/deprecated_references.yml' - expect(actual_markdown.type).to eq :markdown - end - end - end - - context 'a deprecated_refrences.yml file is deleted (i.e. a pack has all violations removed)' do - let(:deleted_files) { ['packs/some_pack/deprecated_references.yml'] } - let(:some_pack_deprecated_references_before) do + context 'a deprecated_refrences.yml file is added (i.e. a pack has its first violation)' do + let(:added_files) do + [ write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) --- packs/some_other_pack: "OtherPackClass": violations: - - dependency - privacy + - dependency files: - packs/some_pack/some_class.rb YML - end + ] + end - it 'calls the before comment input proc' do - expect(slack_notifier).to receive(:notify_slack).with( - { dependency: { minus: 1, plus: 0 }, privacy: { minus: 1, plus: 0 } }, - ['packs/some_pack/deprecated_references.yml'] - ) + it 'calls the before comment input proc' do + expect(slack_notifier).to receive(:notify_slack).with( + { dependency: { minus: 0, plus: 1 }, privacy: { minus: 0, plus: 1 } }, + ['packs/some_pack/deprecated_references.yml'] + ) + + subject + end + context 'default formatter is used' do + it 'displays a markdown with a reasonable message' do subject + expect(dangerfile.status_report[:warnings]).to be_empty + expect(dangerfile.status_report[:errors]).to be_empty + actual_markdowns = dangerfile.status_report[:markdowns] + expect(actual_markdowns.count).to eq 1 + actual_markdown = actual_markdowns.first + expected = <<~EXPECTED + Hi! It looks like the pack defining `OtherPackClass` considers this private API, and it's also not in the referencing pack's list of dependencies. + We noticed you ran `bin/packwerk update-deprecations`. Make sure to read through [the docs](https://github.com/Shopify/packwerk/blob/b647594f93c8922c038255a7aaca125d391a1fbf/docs/new_violation_flow_chart.pdf) for other ways to resolve. Could you add some context as a reply here about why we needed to add these violations? + EXPECTED + + expect(actual_markdown.message).to eq expected + expect(actual_markdown.line).to eq 3 + expect(actual_markdown.file).to eq 'packs/some_pack/deprecated_references.yml' + expect(actual_markdown.type).to eq :markdown + end + end + + context 'a offenses formatter is passed in' do + let(:added_offenses_formatter) do + lambda do |added_violations| + <<~MESSAGE + There are #{added_violations.count} new violations, + with class_names #{added_violations.map(&:class_name).uniq.sort}, + with to_package_names #{added_violations.map(&:to_package_name).uniq.sort}, + with types #{added_violations.map(&:type).sort}, + MESSAGE + end + end + + subject do + danger_deprecated_references_yml_changes.check( + added_offenses_formatter: added_offenses_formatter, + before_comment: lambda do |_violation_diff, changed_deprecated_references_ymls| + slack_notifier.notify_slack(changed_deprecated_references_ymls) + end + ) end - it 'does not display a markdown message' do + it 'displays a markdown using the passed in offenses formatter' do subject expect(dangerfile.status_report[:warnings]).to be_empty expect(dangerfile.status_report[:errors]).to be_empty actual_markdowns = dangerfile.status_report[:markdowns] - expect(actual_markdowns.count).to eq 0 + expect(actual_markdowns.count).to eq 1 + actual_markdown = actual_markdowns.first + expected = <<~EXPECTED + There are 2 new violations, + with class_names ["OtherPackClass"], + with to_package_names ["packs/some_other_pack"], + with types ["dependency", "privacy"], + EXPECTED + + expect(actual_markdown.message).to eq expected + expect(actual_markdown.line).to eq 3 + expect(actual_markdown.file).to eq 'packs/some_pack/deprecated_references.yml' + expect(actual_markdown.type).to eq :markdown end end + end - context 'a deprecated_refrences.yml file is modified to remove violations' do - let(:modified_files) do - [ - write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) - --- - packs/some_other_pack: - "OtherPackClass": - violations: - - privacy - files: - - packs/some_pack/some_class.rb - YML - ] - end + context 'a deprecated_refrences.yml file is deleted (i.e. a pack has all violations removed)' do + let(:deleted_files) { ['packs/some_pack/deprecated_references.yml'] } + let(:some_pack_deprecated_references_before) do + write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) + --- + packs/some_other_pack: + "OtherPackClass": + violations: + - dependency + - privacy + files: + - packs/some_pack/some_class.rb + YML + end + + it 'calls the before comment input proc' do + expect(slack_notifier).to receive(:notify_slack).with( + { dependency: { minus: 1, plus: 0 }, privacy: { minus: 1, plus: 0 } }, + ['packs/some_pack/deprecated_references.yml'] + ) + + subject + end - let(:some_pack_deprecated_references_before) do + it 'does not display a markdown message' do + subject + expect(dangerfile.status_report[:warnings]).to be_empty + expect(dangerfile.status_report[:errors]).to be_empty + actual_markdowns = dangerfile.status_report[:markdowns] + expect(actual_markdowns.count).to eq 0 + end + end + + context 'a deprecated_refrences.yml file is modified to remove violations' do + let(:modified_files) do + [ write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) --- packs/some_other_pack: @@ -199,144 +185,130 @@ module DangerPackwerk - privacy files: - packs/some_pack/some_class.rb - "OtherPackClass2": - violations: - - dependency - - privacy - files: - - packs/some_pack/some_class2.rb YML - end + ] + end - it 'calls the before comment input proc' do - expect(slack_notifier).to receive(:notify_slack).with( - { dependency: { minus: 1, plus: 0 }, privacy: { minus: 1, plus: 0 } }, - ['packs/some_pack/deprecated_references.yml'] - ) + let(:some_pack_deprecated_references_before) do + write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) + --- + packs/some_other_pack: + "OtherPackClass": + violations: + - privacy + files: + - packs/some_pack/some_class.rb + "OtherPackClass2": + violations: + - dependency + - privacy + files: + - packs/some_pack/some_class2.rb + YML + end - subject - end + it 'calls the before comment input proc' do + expect(slack_notifier).to receive(:notify_slack).with( + { dependency: { minus: 1, plus: 0 }, privacy: { minus: 1, plus: 0 } }, + ['packs/some_pack/deprecated_references.yml'] + ) - it 'does not display a markdown message' do - subject - expect(dangerfile.status_report[:warnings]).to be_empty - expect(dangerfile.status_report[:errors]).to be_empty - actual_markdowns = dangerfile.status_report[:markdowns] - expect(actual_markdowns.count).to eq 0 - end + subject end - context 'a deprecated_refrences.yml file is modified to change violations in many files' do - let(:modified_files) do - [ - write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) - --- - packs/some_other_pack: - "OtherPackClass": - violations: - - privacy - - dependency - files: - - packs/some_pack/some_class.rb - YML - ] - end + it 'does not display a markdown message' do + subject + expect(dangerfile.status_report[:warnings]).to be_empty + expect(dangerfile.status_report[:errors]).to be_empty + actual_markdowns = dangerfile.status_report[:markdowns] + expect(actual_markdowns.count).to eq 0 + end + end - let(:some_pack_deprecated_references_before) do + context 'a deprecated_refrences.yml file is modified to change violations in many files' do + let(:modified_files) do + [ write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) --- packs/some_other_pack: "OtherPackClass": violations: - privacy - files: - - packs/some_pack/some_class.rb - - packs/some_pack/some_class2.rb - - packs/some_pack/some_class3.rb - - packs/some_pack/some_class4.rb - - packs/some_pack/some_class5.rb - - packs/some_pack/some_class6.rb - - packs/some_pack/some_class7.rb - "OtherPackClass2": - violations: - dependency - - privacy files: - - packs/some_pack/some_class2.rb - - packs/some_pack/some_class3.rb - - packs/some_pack/some_class4.rb - - packs/some_pack/some_class5.rb - - packs/some_pack/some_class6.rb - - packs/some_pack/some_class7.rb - + - packs/some_pack/some_class.rb YML - end + ] + end - it 'calls the before comment input proc' do - expect(slack_notifier).to receive(:notify_slack).with( - { dependency: { minus: 6, plus: 1 }, privacy: { minus: 12, plus: 0 } }, - ['packs/some_pack/deprecated_references.yml'] - ) + let(:some_pack_deprecated_references_before) do + write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) + --- + packs/some_other_pack: + "OtherPackClass": + violations: + - privacy + files: + - packs/some_pack/some_class.rb + - packs/some_pack/some_class2.rb + - packs/some_pack/some_class3.rb + - packs/some_pack/some_class4.rb + - packs/some_pack/some_class5.rb + - packs/some_pack/some_class6.rb + - packs/some_pack/some_class7.rb + "OtherPackClass2": + violations: + - dependency + - privacy + files: + - packs/some_pack/some_class2.rb + - packs/some_pack/some_class3.rb + - packs/some_pack/some_class4.rb + - packs/some_pack/some_class5.rb + - packs/some_pack/some_class6.rb + - packs/some_pack/some_class7.rb + + YML + end - subject - end + it 'calls the before comment input proc' do + expect(slack_notifier).to receive(:notify_slack).with( + { dependency: { minus: 6, plus: 1 }, privacy: { minus: 12, plus: 0 } }, + ['packs/some_pack/deprecated_references.yml'] + ) - it 'displays a markdown with a reasonable message' do - subject - expect(dangerfile.status_report[:warnings]).to be_empty - expect(dangerfile.status_report[:errors]).to be_empty - actual_markdowns = dangerfile.status_report[:markdowns] - expect(actual_markdowns.count).to eq 1 - actual_markdown = actual_markdowns.first - expected = <<~EXPECTED - Hi! It looks like the pack defining `OtherPackClass` is not in the referencing pack's list of dependencies. - We noticed you ran `bin/packwerk update-deprecations`. Make sure to read through [the docs](https://github.com/Shopify/packwerk/blob/b647594f93c8922c038255a7aaca125d391a1fbf/docs/new_violation_flow_chart.pdf) for other ways to resolve. Could you add some context as a reply here about why we needed to add this violation? - EXPECTED - - expect(actual_markdown.message).to eq expected - expect(actual_markdown.line).to eq 3 - expect(actual_markdown.file).to eq 'packs/some_pack/deprecated_references.yml' - expect(actual_markdown.type).to eq :markdown - end + subject end - context 'a deprecated_refrences.yml file is modified to add 30 and remove 15 violations' do - let(:modified_files) do - [ - write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) - --- - packs/some_other_pack: - "OtherPackClass": - violations: - - privacy - - dependency - files: - - packs/some_pack/some_class1.rb - - packs/some_pack/some_class2.rb - - packs/some_pack/some_class3.rb - - packs/some_pack/some_class4.rb - - packs/some_pack/some_class5.rb - - packs/some_pack/some_class6.rb - - packs/some_pack/some_class7.rb - - packs/some_pack/some_class8.rb - - packs/some_pack/some_class9.rb - - packs/some_pack/some_class10.rb - - packs/some_pack/some_class11.rb - - packs/some_pack/some_class12.rb - - packs/some_pack/some_class13.rb - - packs/some_pack/some_class14.rb - - packs/some_pack/some_class15.rb - YML - ] - end + it 'displays a markdown with a reasonable message' do + subject + expect(dangerfile.status_report[:warnings]).to be_empty + expect(dangerfile.status_report[:errors]).to be_empty + actual_markdowns = dangerfile.status_report[:markdowns] + expect(actual_markdowns.count).to eq 1 + actual_markdown = actual_markdowns.first + expected = <<~EXPECTED + Hi! It looks like the pack defining `OtherPackClass` is not in the referencing pack's list of dependencies. + We noticed you ran `bin/packwerk update-deprecations`. Make sure to read through [the docs](https://github.com/Shopify/packwerk/blob/b647594f93c8922c038255a7aaca125d391a1fbf/docs/new_violation_flow_chart.pdf) for other ways to resolve. Could you add some context as a reply here about why we needed to add this violation? + EXPECTED + + expect(actual_markdown.message).to eq expected + expect(actual_markdown.line).to eq 3 + expect(actual_markdown.file).to eq 'packs/some_pack/deprecated_references.yml' + expect(actual_markdown.type).to eq :markdown + end + end - let(:some_pack_deprecated_references_before) do + context 'a deprecated_refrences.yml file is modified to add 30 and remove 15 violations' do + let(:modified_files) do + [ write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) --- packs/some_other_pack: "OtherPackClass": violations: - privacy + - dependency files: - packs/some_pack/some_class1.rb - packs/some_pack/some_class2.rb @@ -353,55 +325,81 @@ module DangerPackwerk - packs/some_pack/some_class13.rb - packs/some_pack/some_class14.rb - packs/some_pack/some_class15.rb - "AnotherPackClass": - violations: - - privacy - - dependency - files: - - packs/some_pack/some_other_class1.rb - - packs/some_pack/some_other_class2.rb - - packs/some_pack/some_other_class3.rb - - packs/some_pack/some_other_class4.rb - - packs/some_pack/some_other_class5.rb - - packs/some_pack/some_other_class6.rb - - packs/some_pack/some_other_class7.rb - - packs/some_pack/some_other_class8.rb - - packs/some_pack/some_other_class9.rb - - packs/some_pack/some_other_class10.rb - - packs/some_pack/some_other_class11.rb - - packs/some_pack/some_other_class12.rb - - packs/some_pack/some_other_class13.rb - - packs/some_pack/some_other_class14.rb - - packs/some_pack/some_other_class15.rb YML - end + ] + end - it 'calls the before comment input proc' do - expect(slack_notifier).to receive(:notify_slack).with( - { dependency: { minus: 15, plus: 15 }, privacy: { minus: 15, plus: 0 } }, - ['packs/some_pack/deprecated_references.yml'] - ) + let(:some_pack_deprecated_references_before) do + write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) + --- + packs/some_other_pack: + "OtherPackClass": + violations: + - privacy + files: + - packs/some_pack/some_class1.rb + - packs/some_pack/some_class2.rb + - packs/some_pack/some_class3.rb + - packs/some_pack/some_class4.rb + - packs/some_pack/some_class5.rb + - packs/some_pack/some_class6.rb + - packs/some_pack/some_class7.rb + - packs/some_pack/some_class8.rb + - packs/some_pack/some_class9.rb + - packs/some_pack/some_class10.rb + - packs/some_pack/some_class11.rb + - packs/some_pack/some_class12.rb + - packs/some_pack/some_class13.rb + - packs/some_pack/some_class14.rb + - packs/some_pack/some_class15.rb + "AnotherPackClass": + violations: + - privacy + - dependency + files: + - packs/some_pack/some_other_class1.rb + - packs/some_pack/some_other_class2.rb + - packs/some_pack/some_other_class3.rb + - packs/some_pack/some_other_class4.rb + - packs/some_pack/some_other_class5.rb + - packs/some_pack/some_other_class6.rb + - packs/some_pack/some_other_class7.rb + - packs/some_pack/some_other_class8.rb + - packs/some_pack/some_other_class9.rb + - packs/some_pack/some_other_class10.rb + - packs/some_pack/some_other_class11.rb + - packs/some_pack/some_other_class12.rb + - packs/some_pack/some_other_class13.rb + - packs/some_pack/some_other_class14.rb + - packs/some_pack/some_other_class15.rb + YML + end - subject - end + it 'calls the before comment input proc' do + expect(slack_notifier).to receive(:notify_slack).with( + { dependency: { minus: 15, plus: 15 }, privacy: { minus: 15, plus: 0 } }, + ['packs/some_pack/deprecated_references.yml'] + ) - it 'displays a markdown with a reasonable message' do - subject - expect(dangerfile.status_report[:warnings]).to be_empty - expect(dangerfile.status_report[:errors]).to be_empty - actual_markdowns = dangerfile.status_report[:markdowns] - expect(actual_markdowns.count).to eq 1 - actual_markdown = actual_markdowns.first - expected = <<~EXPECTED - Hi! It looks like the pack defining `OtherPackClass` is not in the referencing pack's list of dependencies. - We noticed you ran `bin/packwerk update-deprecations`. Make sure to read through [the docs](https://github.com/Shopify/packwerk/blob/b647594f93c8922c038255a7aaca125d391a1fbf/docs/new_violation_flow_chart.pdf) for other ways to resolve. Could you add some context as a reply here about why we needed to add these violations? - EXPECTED + subject + end - expect(actual_markdown.message).to eq expected - expect(actual_markdown.line).to eq 3 - expect(actual_markdown.file).to eq 'packs/some_pack/deprecated_references.yml' - expect(actual_markdown.type).to eq :markdown - end + it 'displays a markdown with a reasonable message' do + subject + expect(dangerfile.status_report[:warnings]).to be_empty + expect(dangerfile.status_report[:errors]).to be_empty + actual_markdowns = dangerfile.status_report[:markdowns] + expect(actual_markdowns.count).to eq 1 + actual_markdown = actual_markdowns.first + expected = <<~EXPECTED + Hi! It looks like the pack defining `OtherPackClass` is not in the referencing pack's list of dependencies. + We noticed you ran `bin/packwerk update-deprecations`. Make sure to read through [the docs](https://github.com/Shopify/packwerk/blob/b647594f93c8922c038255a7aaca125d391a1fbf/docs/new_violation_flow_chart.pdf) for other ways to resolve. Could you add some context as a reply here about why we needed to add these violations? + EXPECTED + + expect(actual_markdown.message).to eq expected + expect(actual_markdown.line).to eq 3 + expect(actual_markdown.file).to eq 'packs/some_pack/deprecated_references.yml' + expect(actual_markdown.type).to eq :markdown end end end From 9f265c9a35ea5582f9e1139ce86745336c432f0e Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Mon, 18 Apr 2022 08:31:12 -0400 Subject: [PATCH 4/6] Add some test cases --- .../private/default_offenses_formatter.rb | 3 +- ..._deprecated_references_yml_changes_spec.rb | 187 +++++++++++++++++- 2 files changed, 187 insertions(+), 3 deletions(-) diff --git a/lib/danger-packwerk/private/default_offenses_formatter.rb b/lib/danger-packwerk/private/default_offenses_formatter.rb index 61064ef..836c8ca 100644 --- a/lib/danger-packwerk/private/default_offenses_formatter.rb +++ b/lib/danger-packwerk/private/default_offenses_formatter.rb @@ -28,8 +28,7 @@ def self.format(violations) else # violations.any?(&:privacy?) <<~MESSAGE Hi! It looks like the pack defining `#{constant_name}` considers this private API. - #{disclaimer} - #{request_to_add_context} + #{disclaimer}#{request_to_add_context} MESSAGE end end diff --git a/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb b/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb index a7ee0cb..f8f4a6b 100644 --- a/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb +++ b/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb @@ -174,7 +174,7 @@ module DangerPackwerk end end - context 'a deprecated_refrences.yml file is modified to remove violations' do + context 'a deprecated_refrences.yml file is modified to remove some, but not all, violations' do let(:modified_files) do [ write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) @@ -225,6 +225,191 @@ module DangerPackwerk end end + context 'a deprecated_refrences.yml file is modified to add a new violation against a new constant in an existing file' do + let(:modified_files) do + [ + write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) + --- + packs/some_other_pack: + "OtherPackClass": + violations: + - privacy + files: + - packs/some_pack/some_class.rb + "OtherPackClass2": + violations: + - privacy + - dependency + files: + - packs/some_pack/some_class.rb + YML + ] + end + + let(:some_pack_deprecated_references_before) do + write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) + --- + packs/some_other_pack: + "OtherPackClass": + violations: + - privacy + files: + - packs/some_pack/some_class.rb + YML + end + + it 'calls the before comment input proc' do + expect(slack_notifier).to receive(:notify_slack).with( + { dependency: { minus: 0, plus: 1 }, privacy: { minus: 0, plus: 1 } }, + ['packs/some_pack/deprecated_references.yml'] + ) + + subject + end + + it 'displays a markdown with a reasonable message' do + subject + expect(dangerfile.status_report[:warnings]).to be_empty + expect(dangerfile.status_report[:errors]).to be_empty + actual_markdowns = dangerfile.status_report[:markdowns] + expect(actual_markdowns.count).to eq 1 + actual_markdown = actual_markdowns.first + expected = <<~EXPECTED + Hi! It looks like the pack defining `OtherPackClass2` considers this private API, and it's also not in the referencing pack's list of dependencies. + We noticed you ran `bin/packwerk update-deprecations`. Make sure to read through [the docs](https://github.com/Shopify/packwerk/blob/b647594f93c8922c038255a7aaca125d391a1fbf/docs/new_violation_flow_chart.pdf) for other ways to resolve. Could you add some context as a reply here about why we needed to add these violations? + EXPECTED + + expect(actual_markdown.message).to eq expected + expect(actual_markdown.line).to eq 8 + expect(actual_markdown.file).to eq 'packs/some_pack/deprecated_references.yml' + expect(actual_markdown.type).to eq :markdown + end + end + + context 'a deprecated_refrences.yml file is modified to add a new reference against an existing constant in an existing file' do + let(:modified_files) do + [ + write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) + --- + packs/some_other_pack: + "OtherPackClass": + violations: + - privacy + files: + - packs/some_pack/some_class.rb + - packs/some_pack/some_other_class.rb + YML + ] + end + + let(:some_pack_deprecated_references_before) do + write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) + --- + packs/some_other_pack: + "OtherPackClass": + violations: + - privacy + files: + - packs/some_pack/some_class.rb + YML + end + + it 'calls the before comment input proc' do + expect(slack_notifier).to receive(:notify_slack).with( + { dependency: { minus: 0, plus: 0 }, privacy: { minus: 0, plus: 1 } }, + ['packs/some_pack/deprecated_references.yml'] + ) + + subject + end + + it 'displays a markdown with a reasonable message' do + subject + expect(dangerfile.status_report[:warnings]).to be_empty + expect(dangerfile.status_report[:errors]).to be_empty + actual_markdowns = dangerfile.status_report[:markdowns] + expect(actual_markdowns.count).to eq 1 + actual_markdown = actual_markdowns.first + expected = <<~EXPECTED + Hi! It looks like the pack defining `OtherPackClass` considers this private API. + We noticed you ran `bin/packwerk update-deprecations`. Make sure to read through [the docs](https://github.com/Shopify/packwerk/blob/b647594f93c8922c038255a7aaca125d391a1fbf/docs/new_violation_flow_chart.pdf) for other ways to resolve. Could you add some context as a reply here about why we needed to add this violation? + EXPECTED + + expect(actual_markdown.message).to eq expected + expect(actual_markdown.line).to eq 3 + expect(actual_markdown.file).to eq 'packs/some_pack/deprecated_references.yml' + expect(actual_markdown.type).to eq :markdown + end + end + + context 'a deprecated_refrences.yml file is modified to add a reference (that already exists in `deprecated_references.yml`) against an existing constant in an existing file' do + let(:modified_files) do + [ + write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) + --- + packs/some_other_pack: + "OtherPackClass": + violations: + - privacy + files: + - packs/some_pack/some_class.rb + - packs/some_pack/some_other_class.rb + "SomeOtherPackClass": + violations: + - privacy + files: + - packs/some_pack/some_class.rb + - packs/some_pack/some_other_class.rb + YML + ] + end + + let(:some_pack_deprecated_references_before) do + write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) + --- + packs/some_other_pack: + "OtherPackClass": + violations: + - privacy + files: + - packs/some_pack/some_class.rb + - packs/some_pack/some_other_class.rb + "SomeOtherPackClass": + violations: + - privacy + files: + - packs/some_pack/some_class.rb + YML + end + + it 'calls the before comment input proc' do + expect(slack_notifier).to receive(:notify_slack).with( + { dependency: { minus: 0, plus: 0 }, privacy: { minus: 0, plus: 1 } }, + ['packs/some_pack/deprecated_references.yml'] + ) + + subject + end + + it 'displays a markdown with a reasonable message' do + subject + expect(dangerfile.status_report[:warnings]).to be_empty + expect(dangerfile.status_report[:errors]).to be_empty + actual_markdowns = dangerfile.status_report[:markdowns] + expect(actual_markdowns.count).to eq 1 + actual_markdown = actual_markdowns.first + expected = <<~EXPECTED + Hi! It looks like the pack defining `SomeOtherPackClass` considers this private API. + We noticed you ran `bin/packwerk update-deprecations`. Make sure to read through [the docs](https://github.com/Shopify/packwerk/blob/b647594f93c8922c038255a7aaca125d391a1fbf/docs/new_violation_flow_chart.pdf) for other ways to resolve. Could you add some context as a reply here about why we needed to add this violation? + EXPECTED + + expect(actual_markdown.message).to eq expected + expect(actual_markdown.line).to eq 9 + expect(actual_markdown.file).to eq 'packs/some_pack/deprecated_references.yml' + expect(actual_markdown.type).to eq :markdown + end + end + context 'a deprecated_refrences.yml file is modified to change violations in many files' do let(:modified_files) do [ From 404e0c7863d2fd09c65bc7b3536d38b20c8997bd Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Mon, 18 Apr 2022 10:45:05 -0400 Subject: [PATCH 5/6] Update spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb Co-authored-by: Josh Nichols --- .../danger_deprecated_references_yml_changes_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb b/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb index f8f4a6b..0d0463b 100644 --- a/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb +++ b/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb @@ -53,7 +53,7 @@ module DangerPackwerk end end - context 'a deprecated_refrences.yml file is added (i.e. a pack has its first violation)' do + context 'a deprecated_references.yml file is added (i.e. a pack has its first violation)' do let(:added_files) do [ write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip) From 1603dc6f8a2f85660a36070ff2045d248f671f8d Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Mon, 18 Apr 2022 10:45:13 -0400 Subject: [PATCH 6/6] Update spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb Co-authored-by: Josh Nichols --- .../danger_deprecated_references_yml_changes_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb b/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb index 0d0463b..41436aa 100644 --- a/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb +++ b/spec/danger_packwerk/danger_deprecated_references_yml_changes_spec.rb @@ -141,7 +141,7 @@ module DangerPackwerk end end - context 'a deprecated_refrences.yml file is deleted (i.e. a pack has all violations removed)' do + context 'a deprecated_references.yml file is deleted (i.e. a pack has all violations removed)' do let(:deleted_files) { ['packs/some_pack/deprecated_references.yml'] } let(:some_pack_deprecated_references_before) do write_file('packs/some_pack/deprecated_references.yml', <<~YML.strip)