-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add package_manager for Composer v1 deprecation warning and unsupport…
…ed error (#10716) * add package_manager for composer. * set composer v1 as deprecated * set composer v1 as unsupported * change fallback version from v1 to v2 when composer v1 is not supported * refactor composer helper improve version determination logic. * regex refined to remove backtracking for performance reasons
- Loading branch information
Showing
6 changed files
with
274 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# typed: strong | ||
# frozen_string_literal: true | ||
|
||
require "sorbet-runtime" | ||
require "dependabot/package_manager" | ||
require "dependabot/composer/version" | ||
|
||
module Dependabot | ||
module Composer | ||
PACKAGE_MANAGER = "composer" | ||
|
||
# Keep versions in ascending order | ||
SUPPORTED_COMPOSER_VERSIONS = T.let([Version.new("2")].freeze, T::Array[Dependabot::Version]) | ||
|
||
DEPRECATED_COMPOSER_VERSIONS = T.let([ | ||
Version.new("1") | ||
].freeze, T::Array[Dependabot::Version]) | ||
|
||
class PackageManager < PackageManagerBase | ||
extend T::Sig | ||
|
||
sig { params(version: T.any(String, Dependabot::Version)).void } | ||
def initialize(version) | ||
@version = T.let(Version.new(version), Dependabot::Version) | ||
@name = T.let(PACKAGE_MANAGER, String) | ||
@deprecated_versions = T.let(DEPRECATED_COMPOSER_VERSIONS, T::Array[Dependabot::Version]) | ||
@supported_versions = T.let(SUPPORTED_COMPOSER_VERSIONS, T::Array[Dependabot::Version]) | ||
end | ||
|
||
sig { override.returns(String) } | ||
attr_reader :name | ||
|
||
sig { override.returns(Dependabot::Version) } | ||
attr_reader :version | ||
|
||
sig { override.returns(T::Array[Dependabot::Version]) } | ||
attr_reader :deprecated_versions | ||
|
||
sig { override.returns(T::Array[Dependabot::Version]) } | ||
attr_reader :supported_versions | ||
|
||
sig { override.returns(T::Boolean) } | ||
def deprecated? | ||
return false if unsupported? | ||
|
||
# Check if the feature flag for Composer v1 deprecation warning is enabled. | ||
return false unless Dependabot::Experiments.enabled?(:composer_v1_deprecation_warning) | ||
|
||
deprecated_versions.include?(version) | ||
end | ||
|
||
sig { override.returns(T::Boolean) } | ||
def unsupported? | ||
# Check if the feature flag for Composer v1 unsupported error is enabled. | ||
return false unless Dependabot::Experiments.enabled?(:composer_v1_unsupported_error) | ||
|
||
supported_versions.all? { |supported| supported > version } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
composer/spec/dependabot/composer/package_manager_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
# typed: false | ||
# frozen_string_literal: true | ||
|
||
require "dependabot/composer/package_manager" | ||
require "dependabot/package_manager" | ||
require "spec_helper" | ||
|
||
RSpec.describe Dependabot::Composer::PackageManager do | ||
let(:package_manager) { described_class.new(version) } | ||
|
||
describe "#initialize" do | ||
context "when version is a String" do | ||
let(:version) { "2" } | ||
|
||
it "sets the version correctly" do | ||
expect(package_manager.version).to eq(Dependabot::Version.new(version)) | ||
end | ||
|
||
it "sets the name correctly" do | ||
expect(package_manager.name).to eq(Dependabot::Composer::PACKAGE_MANAGER) | ||
end | ||
|
||
it "sets the deprecated_versions correctly" do | ||
expect(package_manager.deprecated_versions).to eq(Dependabot::Composer::DEPRECATED_COMPOSER_VERSIONS) | ||
end | ||
|
||
it "sets the supported_versions correctly" do | ||
expect(package_manager.supported_versions).to eq(Dependabot::Composer::SUPPORTED_COMPOSER_VERSIONS) | ||
end | ||
end | ||
|
||
context "when version is a Dependabot::Version" do | ||
let(:version) { Dependabot::Version.new("2") } | ||
|
||
it "sets the version correctly" do | ||
expect(package_manager.version).to eq(version) | ||
end | ||
|
||
it "sets the name correctly" do | ||
expect(package_manager.name).to eq(Dependabot::Composer::PACKAGE_MANAGER) | ||
end | ||
|
||
it "sets the deprecated_versions correctly" do | ||
expect(package_manager.deprecated_versions).to eq(Dependabot::Composer::DEPRECATED_COMPOSER_VERSIONS) | ||
end | ||
|
||
it "sets the supported_versions correctly" do | ||
expect(package_manager.supported_versions).to eq(Dependabot::Composer::SUPPORTED_COMPOSER_VERSIONS) | ||
end | ||
end | ||
end | ||
|
||
describe "SUPPORTED_COMPOSER_VERSIONS" do | ||
it "is in ascending order" do | ||
expect(Dependabot::Composer::SUPPORTED_COMPOSER_VERSIONS) | ||
.to eq(Dependabot::Composer::SUPPORTED_COMPOSER_VERSIONS.sort) | ||
end | ||
end | ||
|
||
describe "#deprecated?" do | ||
before do | ||
allow(Dependabot::Experiments).to receive(:enabled?) | ||
.with(:composer_v1_deprecation_warning) | ||
.and_return(feature_flag_deprecation_enabled) | ||
allow(Dependabot::Experiments).to receive(:enabled?) | ||
.with(:composer_v1_unsupported_error) | ||
.and_return(feature_flag_unsupported_enabled) | ||
end | ||
|
||
context "when feature flag `composer_v1_deprecation_warning` is enabled and version is deprecated" do | ||
let(:version) { "1" } | ||
let(:feature_flag_deprecation_enabled) { true } | ||
let(:feature_flag_unsupported_enabled) { false } | ||
|
||
it "returns true" do | ||
expect(package_manager.deprecated?).to be true | ||
end | ||
end | ||
|
||
context "when feature flag `composer_v1_deprecation_warning` is disabled" do | ||
let(:version) { "1" } | ||
let(:feature_flag_deprecation_enabled) { false } | ||
let(:feature_flag_unsupported_enabled) { false } | ||
|
||
it "returns false" do | ||
expect(package_manager.deprecated?).to be false | ||
end | ||
end | ||
|
||
context "when version is unsupported and takes precedence" do | ||
let(:version) { "0.9" } | ||
let(:feature_flag_deprecation_enabled) { true } | ||
let(:feature_flag_unsupported_enabled) { true } | ||
|
||
it "returns false, as unsupported takes precedence" do | ||
expect(package_manager.deprecated?).to be false | ||
end | ||
end | ||
end | ||
|
||
describe "#unsupported?" do | ||
before do | ||
allow(Dependabot::Experiments).to receive(:enabled?) | ||
.with(:composer_v1_unsupported_error) | ||
.and_return(feature_flag_unsupported_enabled) | ||
end | ||
|
||
context "when feature flag `composer_v1_unsupported_error` is enabled and version is unsupported" do | ||
let(:version) { "0.9" } | ||
let(:feature_flag_unsupported_enabled) { true } | ||
|
||
it "returns true" do | ||
expect(package_manager.unsupported?).to be true | ||
end | ||
end | ||
|
||
context "when feature flag `composer_v1_unsupported_error` is disabled" do | ||
let(:version) { "0.9" } | ||
let(:feature_flag_unsupported_enabled) { false } | ||
|
||
it "returns false" do | ||
expect(package_manager.unsupported?).to be false | ||
end | ||
end | ||
|
||
context "when feature flag is enabled and version is supported" do | ||
let(:version) { "2" } | ||
let(:feature_flag_unsupported_enabled) { true } | ||
|
||
it "returns false" do | ||
expect(package_manager.unsupported?).to be false | ||
end | ||
end | ||
end | ||
|
||
describe "#raise_if_unsupported!" do | ||
before do | ||
allow(Dependabot::Experiments).to receive(:enabled?) | ||
.with(:composer_v1_unsupported_error) | ||
.and_return(feature_flag_enabled) | ||
end | ||
|
||
context "when feature flag is enabled and version is unsupported" do | ||
let(:version) { "0.9" } | ||
let(:feature_flag_enabled) { true } | ||
|
||
it "raises a ToolVersionNotSupported error" do | ||
expect { package_manager.raise_if_unsupported! }.to raise_error(Dependabot::ToolVersionNotSupported) | ||
end | ||
end | ||
|
||
context "when feature flag is disabled" do | ||
let(:version) { "0.9" } | ||
let(:feature_flag_enabled) { false } | ||
|
||
it "does not raise an error" do | ||
expect { package_manager.raise_if_unsupported! }.not_to raise_error | ||
end | ||
end | ||
end | ||
end |