debian-mirror-gitlab/spec/finders/packages/npm/package_finder_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

206 lines
5.6 KiB
Ruby
Raw Permalink Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ::Packages::Npm::PackageFinder do
2022-10-11 01:57:18 +05:30
let_it_be_with_reload(:project) { create(:project) }
2021-06-08 01:23:25 +05:30
let_it_be_with_refind(:package) { create(:npm_package, project: project) }
2021-04-17 20:07:23 +05:30
2020-07-28 23:09:34 +05:30
let(:project) { package.project }
let(:package_name) { package.name }
2021-11-11 11:23:49 +05:30
let(:last_of_each_version) { true }
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
shared_examples 'accepting a namespace for' do |example_name|
before do
project.update!(namespace: namespace)
end
context 'that is a group' do
let_it_be(:namespace) { create(:group) }
it_behaves_like example_name
context 'within another group' do
let_it_be(:subgroup) { create(:group, parent: namespace) }
before do
project.update!(namespace: subgroup)
end
it_behaves_like example_name
end
end
context 'that is a user namespace' do
let_it_be(:user) { create(:user) }
let_it_be(:namespace) { user.namespace }
it_behaves_like example_name
end
end
describe '#execute' do
2021-11-11 11:23:49 +05:30
subject { finder.execute }
2021-04-17 20:07:23 +05:30
shared_examples 'finding packages by name' do
it { is_expected.to eq([package]) }
context 'with unknown package name' do
let(:package_name) { 'baz' }
it { is_expected.to be_empty }
end
2021-06-08 01:23:25 +05:30
context 'with an uninstallable package' do
before do
2022-03-02 08:16:31 +05:30
package.update_column(:status, :error)
2021-06-08 01:23:25 +05:30
end
it { is_expected.to be_empty }
end
2021-04-17 20:07:23 +05:30
end
2021-11-11 11:23:49 +05:30
shared_examples 'handling last_of_each_version' do
include_context 'last_of_each_version setup context'
context 'disabled' do
let(:last_of_each_version) { false }
it { is_expected.to contain_exactly(package1, package2) }
end
context 'enabled' do
it { is_expected.to contain_exactly(package2) }
end
2023-06-20 00:43:36 +05:30
context 'with npm_allow_packages_in_multiple_projects disabled' do
before do
stub_feature_flags(npm_allow_packages_in_multiple_projects: false)
end
it { is_expected.to contain_exactly(package2) }
end
2021-11-11 11:23:49 +05:30
end
2021-04-17 20:07:23 +05:30
context 'with a project' do
2021-11-11 11:23:49 +05:30
let(:finder) { described_class.new(package_name, project: project, last_of_each_version: last_of_each_version) }
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
it_behaves_like 'finding packages by name'
2020-07-28 23:09:34 +05:30
2021-11-11 11:23:49 +05:30
it_behaves_like 'handling last_of_each_version'
2021-04-17 20:07:23 +05:30
context 'set to nil' do
let(:project) { nil }
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
it { is_expected.to be_empty }
end
2020-07-28 23:09:34 +05:30
end
2021-01-29 00:20:46 +05:30
2021-04-17 20:07:23 +05:30
context 'with a namespace' do
2021-11-11 11:23:49 +05:30
let(:finder) { described_class.new(package_name, namespace: namespace, last_of_each_version: last_of_each_version) }
2021-04-17 20:07:23 +05:30
it_behaves_like 'accepting a namespace for', 'finding packages by name'
2021-11-11 11:23:49 +05:30
it_behaves_like 'accepting a namespace for', 'handling last_of_each_version'
2021-04-17 20:07:23 +05:30
context 'set to nil' do
let_it_be(:namespace) { nil }
2021-01-29 00:20:46 +05:30
2021-04-17 20:07:23 +05:30
it { is_expected.to be_empty }
end
2021-01-29 00:20:46 +05:30
end
2020-07-28 23:09:34 +05:30
end
describe '#find_by_version' do
let(:version) { package.version }
2021-04-17 20:07:23 +05:30
subject { finder.find_by_version(version) }
shared_examples 'finding packages by version' do
it { is_expected.to eq(package) }
context 'with unknown version' do
let(:version) { 'foobar' }
it { is_expected.to be_nil }
end
end
2021-11-11 11:23:49 +05:30
shared_examples 'handling last_of_each_version' do
include_context 'last_of_each_version setup context'
context 'enabled' do
it { is_expected.to eq(package2) }
end
end
2021-04-17 20:07:23 +05:30
context 'with a project' do
2021-11-11 11:23:49 +05:30
let(:finder) { described_class.new(package_name, project: project, last_of_each_version: last_of_each_version) }
2021-04-17 20:07:23 +05:30
it_behaves_like 'finding packages by version'
2021-11-11 11:23:49 +05:30
it_behaves_like 'handling last_of_each_version'
2021-04-17 20:07:23 +05:30
end
context 'with a namespace' do
2021-11-11 11:23:49 +05:30
let(:finder) { described_class.new(package_name, namespace: namespace, last_of_each_version: last_of_each_version) }
2021-04-17 20:07:23 +05:30
it_behaves_like 'accepting a namespace for', 'finding packages by version'
2021-11-11 11:23:49 +05:30
it_behaves_like 'accepting a namespace for', 'handling last_of_each_version'
2021-04-17 20:07:23 +05:30
end
end
describe '#last' do
subject { finder.last }
shared_examples 'finding package by last' do
it { is_expected.to eq(package) }
end
2021-11-11 11:23:49 +05:30
shared_examples 'handling last_of_each_version' do
include_context 'last_of_each_version setup context'
context 'disabled' do
let(:last_of_each_version) { false }
it { is_expected.to eq(package2) }
end
context 'enabled' do
it { is_expected.to eq(package2) }
end
end
2021-04-17 20:07:23 +05:30
context 'with a project' do
2021-11-11 11:23:49 +05:30
let(:finder) { described_class.new(package_name, project: project, last_of_each_version: last_of_each_version) }
2021-04-17 20:07:23 +05:30
it_behaves_like 'finding package by last'
2021-11-11 11:23:49 +05:30
it_behaves_like 'handling last_of_each_version'
2021-04-17 20:07:23 +05:30
end
context 'with a namespace' do
let(:finder) { described_class.new(package_name, namespace: namespace) }
it_behaves_like 'accepting a namespace for', 'finding package by last'
2020-07-28 23:09:34 +05:30
2021-11-11 11:23:49 +05:30
it_behaves_like 'accepting a namespace for', 'handling last_of_each_version'
2021-04-17 20:07:23 +05:30
context 'with duplicate packages' do
let_it_be(:namespace) { create(:group) }
let_it_be(:subgroup1) { create(:group, parent: namespace) }
let_it_be(:subgroup2) { create(:group, parent: namespace) }
let_it_be(:project2) { create(:project, namespace: subgroup2) }
let_it_be(:package2) { create(:npm_package, name: package.name, project: project2) }
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
before do
project.update!(namespace: subgroup1)
end
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
# the most recent one is returned
it { is_expected.to eq(package2) }
end
2020-07-28 23:09:34 +05:30
end
end
end