Skip to content

Commit

Permalink
Add support for NPM V6 deprecation warning and unsupported error
Browse files Browse the repository at this point in the history
  • Loading branch information
amazimbe committed Dec 13, 2024
1 parent d1608d6 commit 8a2638c
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 4 deletions.
12 changes: 8 additions & 4 deletions npm_and_yarn/lib/dependabot/npm_and_yarn/package_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,13 @@ class NpmPackageManager < Ecosystem::VersionManager

# Keep versions in ascending order
SUPPORTED_VERSIONS = T.let([
Version.new(NPM_V6),
Version.new(NPM_V7),
Version.new(NPM_V8),
Version.new(NPM_V9),
Version.new(NPM_V10)
].freeze, T::Array[Dependabot::Version])

DEPRECATED_VERSIONS = T.let([].freeze, T::Array[Dependabot::Version])
DEPRECATED_VERSIONS = T.let([Version.new(NPM_V6)].freeze, T::Array[Dependabot::Version])

sig do
params(
Expand All @@ -89,12 +88,17 @@ def initialize(raw_version, requirement: nil)

sig { override.returns(T::Boolean) }
def deprecated?
false
return false if unsupported?
return false unless Dependabot::Experiments.enabled?(:npm_v6_deprecation_warning)

deprecated_versions.include?(version)
end

sig { override.returns(T::Boolean) }
def unsupported?
false
return false unless Dependabot::Experiments.enabled?(:npm_v6_unsupported_error)

supported_versions.all? { |supported| supported > version }
end
end

Expand Down
115 changes: 115 additions & 0 deletions npm_and_yarn/spec/dependabot/npm_and_yarn/npm_package_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,44 @@
it "returns false" do
expect(package_manager.deprecated?).to be false
end

context "when npm_v6_deprecation_warning flag is enabled" do
before do
allow(Dependabot::Experiments).to receive(:enabled?)
.with(:npm_v6_deprecation_warning)
.and_return(deprecation_enabled)
allow(Dependabot::Experiments).to receive(:enabled?)
.with(:npm_v6_unsupported_error)
.and_return(unsupported_enabled)
end

context "when feature flag `npm_v6_deprecation_warning` is enabled and version is deprecated" do
let(:deprecation_enabled) { true }
let(:unsupported_enabled) { false }

it "returns true" do
expect(package_manager.deprecated?).to be true
end
end

context "when feature flag `npm_v6_deprecation_warning` is disabled" do
let(:deprecation_enabled) { false }
let(: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(:deprecation_enabled) { true }
let(:unsupported_enabled) { true }

it "returns false, as unsupported takes precedence" do
expect(package_manager.deprecated?).to be false
end
end
end
end

describe "#unsupported?" do
Expand All @@ -46,5 +84,82 @@
it "returns false for supported versions" do
expect(package_manager.unsupported?).to be false
end

context "with unsupported feature flag" do
before do
allow(Dependabot::Experiments).to receive(:enabled?)
.with(:npm_v6_unsupported_error)
.and_return(unsupported_enabled)
end

context "when feature flag `npm_v6_unsupported_error` is enabled and version is unsupported" do
let(:version) { "6" }
let(:unsupported_enabled) { true }

it "returns true" do
expect(package_manager.unsupported?).to be true
end
end

context "when feature flag `npm_v6_unsupported_error` is disabled" do
let(:version) { "6" }
let(: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) { "7" }
let(:unsupported_enabled) { true }

it "returns false" do
expect(package_manager.unsupported?).to be false
end
end
end
end

describe "#deprecated_versions" do
let(:version) { "6" }

it "returns deprecated_versions" do
expect(package_manager.deprecated_versions).to eq(Dependabot::NpmAndYarn::NpmPackageManager::DEPRECATED_VERSIONS)
end
end

describe "#supported_versions" do
let(:version) { "6" }

it "sets the supported_versions correctly" do
expect(package_manager.supported_versions).to eq(Dependabot::NpmAndYarn::NpmPackageManager::SUPPORTED_VERSIONS)
end
end

describe "#raise_if_unsupported!" do
before do
allow(Dependabot::Experiments).to receive(:enabled?)
.with(:npm_v6_unsupported_error)
.and_return(unsupported_enabled)
end

context "when feature flag is enabled and version is unsupported" do
let(:version) { "6" }
let(:unsupported_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) { "6" }
let(:unsupported_enabled) { false }

it "does not raise an error" do
expect { package_manager.raise_if_unsupported! }.not_to raise_error
end
end
end
end

0 comments on commit 8a2638c

Please sign in to comment.