# frozen_string_literal: true
require 'spec_helper'
describe EnvironmentsFinder do
describe '#execute' do
let(:project) { create(:project, :repository) }
let(:user) { project.creator }
let(:environment) { create(:environment, project: project) }
before do
project.add_maintainer(user)
end
context 'tagged deployment' do
create(:deployment, :success, environment: environment, ref: 'v1.1.0', tag: true, sha: project.commit.id)
it 'returns environment when with_tags is set' do
expect(described_class.new(project, user, ref: 'master', commit: project.commit, with_tags: true).execute)
.to contain_exactly(environment)
it 'does not return environment when no with_tags is set' do
expect(described_class.new(project, user, ref: 'master', commit: project.commit).execute)
.to be_empty
it 'does not return environment when commit is not part of deployment' do
expect(described_class.new(project, user, ref: 'master', commit: project.commit('feature')).execute)
context 'branch deployment' do
create(:deployment, :success, environment: environment, ref: 'master', sha: project.commit.id)
it 'returns environment when ref is set' do
it 'does not environment when ref is different' do
expect(described_class.new(project, user, ref: 'feature', commit: project.commit).execute)
it 'returns environment when commit constraint is not set' do
expect(described_class.new(project, user, ref: 'master').execute)
context 'commit deployment' do
it 'returns environment' do
expect(described_class.new(project, user, commit: project.commit).execute)
context 'recently updated' do
context 'when last deployment to environment is the most recent one' do
create(:deployment, :success, environment: environment, ref: 'feature')
it 'finds recently updated environment' do
expect(described_class.new(project, user, ref: 'feature', recently_updated: true).execute)
context 'when last deployment to environment is not the most recent' do
create(:deployment, :success, environment: environment, ref: 'master')
it 'does not find environment' do
context 'when there are two environments that deploy to the same branch' do
let(:second_environment) { create(:environment, project: project) }
create(:deployment, :success, environment: second_environment, ref: 'feature')
it 'finds both environments' do
.to contain_exactly(environment, second_environment)