From eff06c0b3aeefb294d610e1d53aefef286402364 Mon Sep 17 00:00:00 2001 From: Kamil Bukum Date: Tue, 20 Aug 2024 11:35:35 -0700 Subject: [PATCH] added specs. fixed problems. --- .../updater/security_update_helpers.rb | 2 +- .../updater/pull_request_helpers_spec.rb | 106 ++++++++++++++---- 2 files changed, 84 insertions(+), 24 deletions(-) diff --git a/updater/lib/dependabot/updater/security_update_helpers.rb b/updater/lib/dependabot/updater/security_update_helpers.rb index eb549cb81f..76083cbb44 100644 --- a/updater/lib/dependabot/updater/security_update_helpers.rb +++ b/updater/lib/dependabot/updater/security_update_helpers.rb @@ -213,7 +213,7 @@ def add_deprecation_notice(notices:, package_manager:) def send_deprecation_notice(package_manager) deprecation_notice = create_deprecation_notice(package_manager) if deprecation_notice - Dependabot::Logger.warn(deprecation_notice.message) + Dependabot.logger.warn(deprecation_notice.message) service.record_update_job_warn( warn_type: deprecation_notice.type, warn_title: deprecation_notice.title, diff --git a/updater/spec/dependabot/updater/pull_request_helpers_spec.rb b/updater/spec/dependabot/updater/pull_request_helpers_spec.rb index 977490748c..36dfdd5762 100644 --- a/updater/spec/dependabot/updater/pull_request_helpers_spec.rb +++ b/updater/spec/dependabot/updater/pull_request_helpers_spec.rb @@ -5,24 +5,49 @@ require "dependabot/updater" require "dependabot/package_manager" require "dependabot/notices" +require "dependabot/service" RSpec.describe Dependabot::Updater::PullRequestHelpers do let(:dummy_class) do Class.new do include Dependabot::Updater::PullRequestHelpers - attr_accessor :notices + attr_accessor :notices, :service - def initialize + def initialize(service = nil) @notices = [] + @service = service end end end - let(:dummy_instance) { dummy_class.new } + let(:dummy_instance) { dummy_class.new(service) } + + let(:service) { instance_double(Dependabot::Service) } + + let(:package_manager) do + Class.new(Dependabot::PackageManagerBase) do + def name + "bundler" + end + + def version + Dependabot::Version.new("1") + end + + def deprecated_versions + [Dependabot::Version.new("1")] + end + + def supported_versions + [Dependabot::Version.new("2"), Dependabot::Version.new("3")] + end + end.new + end before do allow(Dependabot::Experiments).to receive(:enabled?).with(:add_deprecation_warn_to_pr_message).and_return(true) + allow(service).to receive(:record_update_job_warn) end after do @@ -30,26 +55,6 @@ def initialize end describe "#add_deprecation_notice" do - let(:package_manager) do - Class.new(Dependabot::PackageManagerBase) do - def name - "bundler" - end - - def version - Dependabot::Version.new("1") - end - - def deprecated_versions - [Dependabot::Version.new("1")] - end - - def supported_versions - [Dependabot::Version.new("2"), Dependabot::Version.new("3")] - end - end.new - end - context "when package manager is provided and is deprecated" do it "adds a deprecation notice to the notices array" do expect do @@ -100,4 +105,59 @@ def supported_versions end end end + + describe "#send_deprecation_notice" do + context "when deprecation notice is generated" do + let(:deprecation_notice) do + Dependabot::Notice.new( + mode: "WARN", + type: "bundler_deprecated_warn", + package_manager_name: "bundler", + title: "Package manager deprecation notice", + message: "Dependabot will stop supporting `bundler v1`!\n" \ + "Please upgrade to one of the following versions: `v2`, or `v3`.\n", + markdown: "> [!WARNING]\n> Dependabot will stop supporting `bundler v1`!\n>\n" \ + "> Please upgrade to one of the following versions: `v2`, or `v3`.\n>\n" + ) + end + + it "logs the deprecation notice and records it as a warning" do + expect(Dependabot.logger).to receive(:warn).with(deprecation_notice.message) + expect(service).to receive(:record_update_job_warn).with( + warn_type: deprecation_notice.type, + warn_title: deprecation_notice.title, + warn_message: deprecation_notice.message + ) + + dummy_instance.send_deprecation_notice(package_manager) + end + end + + context "when no deprecation notice is generated" do + before do + allow(dummy_instance).to receive(:create_deprecation_notice).and_return(nil) + end + + it "does not log or record any warnings" do + expect(Dependabot.logger).not_to receive(:warn) + expect(service).not_to receive(:record_update_job_warn) + + dummy_instance.send_deprecation_notice(package_manager) + end + end + + context "when an error occurs during sending deprecation notice" do + before do + allow(dummy_instance).to receive(:create_deprecation_notice).and_raise(StandardError, "Unexpected error") + end + + it "logs an error message" do + expect(Dependabot.logger).to receive(:error).with( + "Failed to send package manager deprecation notice warning: Unexpected error" + ) + + dummy_instance.send_deprecation_notice(package_manager) + end + end + end end