From b278d886ba65e2d3d438352b6243cd33b1ba4636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Fri, 9 Nov 2018 22:17:43 +0100 Subject: Support both ref and ref-name in protected_for? --- spec/models/project_spec.rb | 68 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 4 deletions(-) (limited to 'spec') diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 1c85411dc3b..a519435322c 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2520,6 +2520,10 @@ describe Project do end context 'when the ref is not protected' do + before do + allow(project).to receive(:protected_for?).with('ref').and_return(false) + end + it 'contains only the CI variables' do is_expected.to contain_exactly(ci_variable) end @@ -2563,7 +2567,13 @@ describe Project do subject { project.protected_for?('ref') } + before do + allow(project).to receive(:resolve_ref).and_return(ref) + end + context 'when the ref is not protected' do + let(:ref) { 'refs/heads/ref' } + before do stub_application_setting( default_branch_protection: Gitlab::Access::PROTECTION_NONE) @@ -2575,9 +2585,9 @@ describe Project do end context 'when the ref is a protected branch' do + let(:ref) { 'refs/heads/ref' } + before do - allow(project).to receive(:repository).and_call_original - allow(project).to receive_message_chain(:repository, :branch_exists?).and_return(true) create(:protected_branch, name: 'ref', project: project) end @@ -2587,9 +2597,9 @@ describe Project do end context 'when the ref is a protected tag' do + let(:ref) { 'refs/tags/ref' } + before do - allow(project).to receive_message_chain(:repository, :branch_exists?).and_return(false) - allow(project).to receive_message_chain(:repository, :tag_exists?).and_return(true) create(:protected_tag, name: 'ref', project: project) end @@ -2778,6 +2788,56 @@ describe Project do end end + describe '#resolve_ref' do + let(:project) { create(:project) } + + subject { project.resolve_ref(ref) } + + context 'when ref is full ref' do + let(:ref) { 'refs/heads/master' } + + it 'returns the unchanged ref' do + is_expected.to eq(ref) + end + end + + context 'when ref is a tag or branch name' do + let(:ref) { 'ref' } + + before do + allow(project.repository).to receive(:tag_exists?).and_return(tag_exists) + allow(project.repository).to receive(:branch_exists?).and_return(branch_exists) + end + + context 'when ref is ambiguous' do + let(:tag_exists) { true } + let(:branch_exists) { true } + + it 'raises an error' do + expect { subject }.to raise_error(described_class::AmbiguousRef) + end + end + + context 'when ref is tag name' do + let(:tag_exists) { true } + let(:branch_exists) { false } + + it 'returns full tag ref path' do + is_expected.to eq('refs/tags/ref') + end + end + + context 'when ref is branch name' do + let(:tag_exists) { false } + let(:branch_exists) { true } + + it 'returns full branch ref path' do + is_expected.to eq('refs/heads/ref') + end + end + end + end + describe '#http_url_to_repo' do let(:project) { create(:project) } -- cgit v1.2.3 From 1bf580688890a3b13e7ac0468f29108dafe08f9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Sat, 10 Nov 2018 14:34:53 +0100 Subject: Use full ref when possible to avoid ambiguity --- spec/models/ci/build_spec.rb | 12 ++++++++---- spec/models/ci/pipeline_spec.rb | 4 ++++ spec/models/concerns/has_ref_spec.rb | 31 +++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 spec/models/concerns/has_ref_spec.rb (limited to 'spec') diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index 89f78f629d4..d4056b4f7f1 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -2385,6 +2385,8 @@ describe Ci::Build do end context 'when protected variable is defined' do + let(:ref) { Gitlab::Git::BRANCH_REF_PREFIX + build.ref } + let(:protected_variable) do { key: 'PROTECTED_KEY', value: 'protected_value', public: false } end @@ -2397,7 +2399,7 @@ describe Ci::Build do context 'when the branch is protected' do before do - allow(build.project).to receive(:protected_for?).with(build.ref).and_return(true) + allow(build.project).to receive(:protected_for?).with(ref).and_return(true) end it { is_expected.to include(protected_variable) } @@ -2405,7 +2407,7 @@ describe Ci::Build do context 'when the tag is protected' do before do - allow(build.project).to receive(:protected_for?).with(build.ref).and_return(true) + allow(build.project).to receive(:protected_for?).with(ref).and_return(true) end it { is_expected.to include(protected_variable) } @@ -2430,6 +2432,8 @@ describe Ci::Build do end context 'when group protected variable is defined' do + let(:ref) { Gitlab::Git::BRANCH_REF_PREFIX + build.ref } + let(:protected_variable) do { key: 'PROTECTED_KEY', value: 'protected_value', public: false } end @@ -2442,7 +2446,7 @@ describe Ci::Build do context 'when the branch is protected' do before do - allow(build.project).to receive(:protected_for?).with(build.ref).and_return(true) + allow(build.project).to receive(:protected_for?).with(ref).and_return(true) end it { is_expected.to include(protected_variable) } @@ -2450,7 +2454,7 @@ describe Ci::Build do context 'when the tag is protected' do before do - allow(build.project).to receive(:protected_for?).with(build.ref).and_return(true) + allow(build.project).to receive(:protected_for?).with(ref).and_return(true) end it { is_expected.to include(protected_variable) } diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index b67c6a4cffa..17f33785fda 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -397,6 +397,10 @@ describe Ci::Pipeline, :mailer do end describe '#protected_ref?' do + before do + pipeline.project = create(:project, :repository) + end + it 'delegates method to project' do expect(pipeline).not_to be_protected_ref end diff --git a/spec/models/concerns/has_ref_spec.rb b/spec/models/concerns/has_ref_spec.rb new file mode 100644 index 00000000000..ec3cf9a8580 --- /dev/null +++ b/spec/models/concerns/has_ref_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe HasRef do + describe '#branch?' do + let(:pipeline) { create(:ci_pipeline) } + + subject { pipeline.branch? } + + context 'is not a tag' do + before do + pipeline.tag = false + end + + it 'return true when tag is set to false' do + is_expected.to be_truthy + end + end + + context 'is not a tag' do + before do + pipeline.tag = true + end + + it 'return false when tag is set to true' do + is_expected.to be_falsey + end + end + end +end -- cgit v1.2.3 From 2edc02143b2d361275fb97ce2904a58e7dc8ff38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Sat, 10 Nov 2018 15:48:22 +0100 Subject: Prevent creating pipelines with ambiguous refs --- spec/lib/gitlab/ci/pipeline/chain/command_spec.rb | 22 ---------------------- spec/lib/gitlab/ci/pipeline/chain/populate_spec.rb | 2 ++ .../ci/pipeline/chain/validate/repository_spec.rb | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 22 deletions(-) (limited to 'spec') diff --git a/spec/lib/gitlab/ci/pipeline/chain/command_spec.rb b/spec/lib/gitlab/ci/pipeline/chain/command_spec.rb index 75a177d2d1f..14b4fbd1f5a 100644 --- a/spec/lib/gitlab/ci/pipeline/chain/command_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/chain/command_spec.rb @@ -160,26 +160,4 @@ describe Gitlab::Ci::Pipeline::Chain::Command do end end end - - describe '#protected_ref?' do - let(:command) { described_class.new(project: project, origin_ref: 'my-branch') } - - subject { command.protected_ref? } - - context 'when a ref is protected' do - before do - expect_any_instance_of(Project).to receive(:protected_for?).with('my-branch').and_return(true) - end - - it { is_expected.to eq(true) } - end - - context 'when a ref is unprotected' do - before do - expect_any_instance_of(Project).to receive(:protected_for?).with('my-branch').and_return(false) - end - - it { is_expected.to eq(false) } - end - end end diff --git a/spec/lib/gitlab/ci/pipeline/chain/populate_spec.rb b/spec/lib/gitlab/ci/pipeline/chain/populate_spec.rb index 284aed91e29..1b014ecfaa4 100644 --- a/spec/lib/gitlab/ci/pipeline/chain/populate_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/chain/populate_spec.rb @@ -14,6 +14,7 @@ describe Gitlab::Ci::Pipeline::Chain::Populate do Gitlab::Ci::Pipeline::Chain::Command.new( project: project, current_user: user, + origin_ref: 'master', seeds_block: nil) end @@ -106,6 +107,7 @@ describe Gitlab::Ci::Pipeline::Chain::Populate do Gitlab::Ci::Pipeline::Chain::Command.new( project: project, current_user: user, + origin_ref: 'master', seeds_block: seeds_block) end diff --git a/spec/lib/gitlab/ci/pipeline/chain/validate/repository_spec.rb b/spec/lib/gitlab/ci/pipeline/chain/validate/repository_spec.rb index fb1b53fc55c..7b30a8381e9 100644 --- a/spec/lib/gitlab/ci/pipeline/chain/validate/repository_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/chain/validate/repository_spec.rb @@ -42,6 +42,27 @@ describe Gitlab::Ci::Pipeline::Chain::Validate::Repository do end end + context 'when ref is ambiguous' do + let(:project) do + p = create(:project, :repository) + p.repository.add_tag(user, 'master', 'master') + p + end + let(:command) do + Gitlab::Ci::Pipeline::Chain::Command.new( + project: project, current_user: user, origin_ref: 'master') + end + + it 'breaks the chain' do + expect(step.break?).to be true + end + + it 'adds an error about missing ref' do + expect(pipeline.errors.to_a) + .to include 'Ref is ambiguous' + end + end + context 'when does not have existing SHA set' do let(:command) do Gitlab::Ci::Pipeline::Chain::Command.new( -- cgit v1.2.3 From 837ab8c5d83243db8efe1cec1c6e001d1cbeb43f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Thu, 15 Nov 2018 21:28:17 +0100 Subject: Make HasRef#git_ref public --- spec/models/concerns/has_ref_spec.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'spec') diff --git a/spec/models/concerns/has_ref_spec.rb b/spec/models/concerns/has_ref_spec.rb index ec3cf9a8580..14db197dfe0 100644 --- a/spec/models/concerns/has_ref_spec.rb +++ b/spec/models/concerns/has_ref_spec.rb @@ -28,4 +28,32 @@ describe HasRef do end end end + + describe '#git_ref' do + subject { pipeline.git_ref } + + context 'when tag is true' do + let(:pipeline) { create(:ci_pipeline, tag: true) } + + it 'returns a tag ref' do + is_expected.to start_with(Gitlab::Git::TAG_REF_PREFIX) + end + end + + context 'when tag is false' do + let(:pipeline) { create(:ci_pipeline, tag: false) } + + it 'returns a branch ref' do + is_expected.to start_with(Gitlab::Git::BRANCH_REF_PREFIX) + end + end + + context 'when tag is nil' do + let(:pipeline) { create(:ci_pipeline, tag: nil) } + + it 'returns a branch ref' do + is_expected.to start_with(Gitlab::Git::BRANCH_REF_PREFIX) + end + end + end end -- cgit v1.2.3 From ce14c20a82af697b927666123443a25f19dab2ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Thu, 15 Nov 2018 21:56:13 +0100 Subject: Avoid using magic variable in spec --- spec/lib/gitlab/ci/pipeline/chain/validate/repository_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'spec') diff --git a/spec/lib/gitlab/ci/pipeline/chain/validate/repository_spec.rb b/spec/lib/gitlab/ci/pipeline/chain/validate/repository_spec.rb index 7b30a8381e9..a7cad423d09 100644 --- a/spec/lib/gitlab/ci/pipeline/chain/validate/repository_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/chain/validate/repository_spec.rb @@ -44,9 +44,9 @@ describe Gitlab::Ci::Pipeline::Chain::Validate::Repository do context 'when ref is ambiguous' do let(:project) do - p = create(:project, :repository) - p.repository.add_tag(user, 'master', 'master') - p + create(:project, :repository).tap do |proj| + proj.repository.add_tag(user, 'master', 'master') + end end let(:command) do Gitlab::Ci::Pipeline::Chain::Command.new( -- cgit v1.2.3 From 855e7c32b9f3541fec085726d338802c8ca9b9f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Thu, 15 Nov 2018 22:54:21 +0100 Subject: Use Gitlab::Git::Ref in Project#resolve_ref Reworks Project#resolve_ref to return Gitlab::Git::Branch, Gitlab::Git::Tag or raise an AmbiguousRef error. --- spec/lib/gitlab/git/branch_spec.rb | 12 +++++++++ spec/lib/gitlab/git/tag_spec.rb | 13 +++++++++ spec/models/project_spec.rb | 55 ++++++++++++++++++++++---------------- spec/models/repository_spec.rb | 28 +++++++++++++++++++ 4 files changed, 85 insertions(+), 23 deletions(-) (limited to 'spec') diff --git a/spec/lib/gitlab/git/branch_spec.rb b/spec/lib/gitlab/git/branch_spec.rb index 0df282d0ae3..1b26b5ef7fd 100644 --- a/spec/lib/gitlab/git/branch_spec.rb +++ b/spec/lib/gitlab/git/branch_spec.rb @@ -124,6 +124,18 @@ describe Gitlab::Git::Branch, :seed_helper do it { expect(repository.branches.size).to eq(SeedRepo::Repo::BRANCHES.size) } + describe '#full_ref' do + subject do + described_class.new(repository, 'master', + repository.commit.sha, + repository.commit).full_ref + end + + it 'returns the full ref' do + is_expected.to eq('refs/heads/master') + end + end + def create_commit params[:message].delete!("\r") Rugged::Commit.create(rugged, params.merge(committer: committer.merge(time: Time.now))) diff --git a/spec/lib/gitlab/git/tag_spec.rb b/spec/lib/gitlab/git/tag_spec.rb index b51e3879f49..f5d0b6af6f0 100644 --- a/spec/lib/gitlab/git/tag_spec.rb +++ b/spec/lib/gitlab/git/tag_spec.rb @@ -69,4 +69,17 @@ describe Gitlab::Git::Tag, :seed_helper do end end end + + describe '#full_ref' do + subject do + described_class.new(repository, { name: 'master', + target: repository.commit.sha, + target_commit: repository.commit }) + .full_ref + end + + it 'returns the full ref' do + is_expected.to eq('refs/tags/master') + end + end end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index a519435322c..e2e8a76ab72 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2563,7 +2563,7 @@ describe Project do end describe '#protected_for?' do - let(:project) { create(:project) } + let(:project) { create(:project, :repository) } subject { project.protected_for?('ref') } @@ -2572,7 +2572,7 @@ describe Project do end context 'when the ref is not protected' do - let(:ref) { 'refs/heads/ref' } + let(:ref) { project.repository.find_branch('master') } before do stub_application_setting( @@ -2585,10 +2585,10 @@ describe Project do end context 'when the ref is a protected branch' do - let(:ref) { 'refs/heads/ref' } + let(:ref) { project.repository.find_branch('master') } before do - create(:protected_branch, name: 'ref', project: project) + create(:protected_branch, name: 'master', project: project) end it 'returns true' do @@ -2597,10 +2597,10 @@ describe Project do end context 'when the ref is a protected tag' do - let(:ref) { 'refs/tags/ref' } + let(:ref) { project.repository.find_tag('v1.0.0') } before do - create(:protected_tag, name: 'ref', project: project) + create(:protected_tag, name: 'v1.0.0', project: project) end it 'returns true' do @@ -2789,29 +2789,36 @@ describe Project do end describe '#resolve_ref' do - let(:project) { create(:project) } + let(:project) { create(:project, :repository) } subject { project.resolve_ref(ref) } context 'when ref is full ref' do - let(:ref) { 'refs/heads/master' } + context 'when ref exists' do + let(:ref) { 'refs/heads/master' } + + it 'returns the ref' do + is_expected.to be_a(Gitlab::Git::Ref) + end + end + + context 'when ref does not exist' do + let(:ref) { 'refs/tags/doesnotexist' } - it 'returns the unchanged ref' do - is_expected.to eq(ref) + it 'returns nil' do + is_expected.to eq(nil) + end end end context 'when ref is a tag or branch name' do let(:ref) { 'ref' } - before do - allow(project.repository).to receive(:tag_exists?).and_return(tag_exists) - allow(project.repository).to receive(:branch_exists?).and_return(branch_exists) - end - context 'when ref is ambiguous' do - let(:tag_exists) { true } - let(:branch_exists) { true } + before do + project.repository.add_tag(project.creator, ref, 'master') + project.repository.add_branch(project.creator, ref, 'master') + end it 'raises an error' do expect { subject }.to raise_error(described_class::AmbiguousRef) @@ -2819,20 +2826,22 @@ describe Project do end context 'when ref is tag name' do - let(:tag_exists) { true } - let(:branch_exists) { false } + before do + project.repository.add_tag(project.creator, ref, 'master') + end it 'returns full tag ref path' do - is_expected.to eq('refs/tags/ref') + is_expected.to be_a(Gitlab::Git::Tag) end end context 'when ref is branch name' do - let(:tag_exists) { false } - let(:branch_exists) { true } + before do + project.repository.add_branch(project.creator, ref, 'master') + end it 'returns full branch ref path' do - is_expected.to eq('refs/heads/ref') + is_expected.to be_a(Gitlab::Git::Branch) end end end diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index f09b4b67061..df0ab7bc0f4 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -1699,6 +1699,34 @@ describe Repository do end end + describe '#find_ref' do + subject { repository.find_ref(ref) } + + context 'when ref is a tag' do + let(:ref) { 'refs/tags/v1.0.0' } + + it 'returns the tag' do + is_expected.to be_a(Gitlab::Git::Tag) + end + end + + context 'when ref is a branch' do + let(:ref) { 'refs/heads/master' } + + it 'returns the branch' do + is_expected.to be_a(Gitlab::Git::Branch) + end + end + + context 'when ref is invalid' do + let(:ref) { 'refs/notvalid/ref' } + + it 'returns nil' do + is_expected.to eq(nil) + end + end + end + describe '#avatar' do it 'returns nil if repo does not exist' do allow(repository).to receive(:root_ref).and_raise(Gitlab::Git::Repository::NoRepository) -- cgit v1.2.3 From b0b5924eb418851ddfab848ab16b6acac27d42e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Thu, 15 Nov 2018 23:31:02 +0100 Subject: Use nil instead of raising AmbiguousRef --- spec/models/project_spec.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'spec') diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index e2e8a76ab72..48cf693b678 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2571,6 +2571,14 @@ describe Project do allow(project).to receive(:resolve_ref).and_return(ref) end + context 'when ref is ambiguous' do + let(:ref) { nil } + + it 'returns false' do + is_expected.to be_falsey + end + end + context 'when the ref is not protected' do let(:ref) { project.repository.find_branch('master') } @@ -2820,8 +2828,8 @@ describe Project do project.repository.add_branch(project.creator, ref, 'master') end - it 'raises an error' do - expect { subject }.to raise_error(described_class::AmbiguousRef) + it 'returns nil' do + is_expected.to eq(nil) end end -- cgit v1.2.3 From b6c8d3ac9f3031f6174dde2f11b3876ab4ac8a20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Thu, 15 Nov 2018 23:38:16 +0100 Subject: Reintroduce Command#protected_ref? --- spec/lib/gitlab/ci/pipeline/chain/command_spec.rb | 22 ++++++++++++++++++++++ spec/lib/gitlab/ci/pipeline/seed/build_spec.rb | 3 ++- spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb | 3 ++- 3 files changed, 26 insertions(+), 2 deletions(-) (limited to 'spec') diff --git a/spec/lib/gitlab/ci/pipeline/chain/command_spec.rb b/spec/lib/gitlab/ci/pipeline/chain/command_spec.rb index 14b4fbd1f5a..75a177d2d1f 100644 --- a/spec/lib/gitlab/ci/pipeline/chain/command_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/chain/command_spec.rb @@ -160,4 +160,26 @@ describe Gitlab::Ci::Pipeline::Chain::Command do end end end + + describe '#protected_ref?' do + let(:command) { described_class.new(project: project, origin_ref: 'my-branch') } + + subject { command.protected_ref? } + + context 'when a ref is protected' do + before do + expect_any_instance_of(Project).to receive(:protected_for?).with('my-branch').and_return(true) + end + + it { is_expected.to eq(true) } + end + + context 'when a ref is unprotected' do + before do + expect_any_instance_of(Project).to receive(:protected_for?).with('my-branch').and_return(false) + end + + it { is_expected.to eq(false) } + end + end end diff --git a/spec/lib/gitlab/ci/pipeline/seed/build_spec.rb b/spec/lib/gitlab/ci/pipeline/seed/build_spec.rb index fffa727c2ed..2cf812b26dc 100644 --- a/spec/lib/gitlab/ci/pipeline/seed/build_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/seed/build_spec.rb @@ -1,7 +1,8 @@ require 'spec_helper' describe Gitlab::Ci::Pipeline::Seed::Build do - let(:pipeline) { create(:ci_empty_pipeline) } + let(:project) { create(:project, :repository) } + let(:pipeline) { create(:ci_empty_pipeline, project: project) } let(:attributes) do { name: 'rspec', diff --git a/spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb b/spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb index 05ce3412fd8..82f741845db 100644 --- a/spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb @@ -1,7 +1,8 @@ require 'spec_helper' describe Gitlab::Ci::Pipeline::Seed::Stage do - let(:pipeline) { create(:ci_empty_pipeline) } + let(:project) { create(:project, :repository) } + let(:pipeline) { create(:ci_empty_pipeline, project: project) } let(:attributes) do { name: 'test', -- cgit v1.2.3 From 2b4883e05e59eff08088e378bf3061d9d8da13dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Fri, 16 Nov 2018 00:27:11 +0100 Subject: Check for Tag/Branch corectness --- spec/models/project_spec.rb | 14 +++++++++++--- spec/models/repository_spec.rb | 12 ++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) (limited to 'spec') diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 48cf693b678..204d611796b 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2805,7 +2805,7 @@ describe Project do context 'when ref exists' do let(:ref) { 'refs/heads/master' } - it 'returns the ref' do + it 'returns a ref' do is_expected.to be_a(Gitlab::Git::Ref) end end @@ -2838,9 +2838,13 @@ describe Project do project.repository.add_tag(project.creator, ref, 'master') end - it 'returns full tag ref path' do + it 'returns a tag' do is_expected.to be_a(Gitlab::Git::Tag) end + + it 'returns the correct tag' do + expect(subject.name).to eq(ref) + end end context 'when ref is branch name' do @@ -2848,9 +2852,13 @@ describe Project do project.repository.add_branch(project.creator, ref, 'master') end - it 'returns full branch ref path' do + it 'returns a branch' do is_expected.to be_a(Gitlab::Git::Branch) end + + it 'returns the correct branch' do + expect(subject.name).to eq(ref) + end end end end diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index df0ab7bc0f4..6f9fdcf4482 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -1705,17 +1705,25 @@ describe Repository do context 'when ref is a tag' do let(:ref) { 'refs/tags/v1.0.0' } - it 'returns the tag' do + it 'returns a tag' do is_expected.to be_a(Gitlab::Git::Tag) end + + it 'returns the correct tag' do + expect(subject.name).to eq('v1.0.0') + end end context 'when ref is a branch' do let(:ref) { 'refs/heads/master' } - it 'returns the branch' do + it 'returns a branch' do is_expected.to be_a(Gitlab::Git::Branch) end + + it 'returns the correct branch' do + expect(subject.name).to eq('master') + end end context 'when ref is invalid' do -- cgit v1.2.3 From 38348aa1215daa2393b7d488c1ca8926d67dc09e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Fri, 16 Nov 2018 13:20:52 +0100 Subject: Remove Gitlab::Git::Ref#full_ref --- spec/lib/gitlab/git/branch_spec.rb | 12 ------------ spec/lib/gitlab/git/tag_spec.rb | 13 ------------- 2 files changed, 25 deletions(-) (limited to 'spec') diff --git a/spec/lib/gitlab/git/branch_spec.rb b/spec/lib/gitlab/git/branch_spec.rb index 1b26b5ef7fd..0df282d0ae3 100644 --- a/spec/lib/gitlab/git/branch_spec.rb +++ b/spec/lib/gitlab/git/branch_spec.rb @@ -124,18 +124,6 @@ describe Gitlab::Git::Branch, :seed_helper do it { expect(repository.branches.size).to eq(SeedRepo::Repo::BRANCHES.size) } - describe '#full_ref' do - subject do - described_class.new(repository, 'master', - repository.commit.sha, - repository.commit).full_ref - end - - it 'returns the full ref' do - is_expected.to eq('refs/heads/master') - end - end - def create_commit params[:message].delete!("\r") Rugged::Commit.create(rugged, params.merge(committer: committer.merge(time: Time.now))) diff --git a/spec/lib/gitlab/git/tag_spec.rb b/spec/lib/gitlab/git/tag_spec.rb index f5d0b6af6f0..b51e3879f49 100644 --- a/spec/lib/gitlab/git/tag_spec.rb +++ b/spec/lib/gitlab/git/tag_spec.rb @@ -69,17 +69,4 @@ describe Gitlab::Git::Tag, :seed_helper do end end end - - describe '#full_ref' do - subject do - described_class.new(repository, { name: 'master', - target: repository.commit.sha, - target_commit: repository.commit }) - .full_ref - end - - it 'returns the full ref' do - is_expected.to eq('refs/tags/master') - end - end end -- cgit v1.2.3 From 96a0a8cfb62d9ea66ee2a22e09a116d64f333703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Fri, 16 Nov 2018 21:03:21 +0100 Subject: Use strings instead of Gitlab::Git::Ref --- spec/models/project_spec.rb | 38 ++++++++++---------------------------- 1 file changed, 10 insertions(+), 28 deletions(-) (limited to 'spec') diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 204d611796b..cbc242308e8 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2580,7 +2580,7 @@ describe Project do end context 'when the ref is not protected' do - let(:ref) { project.repository.find_branch('master') } + let(:ref) { 'refs/heads/master' } before do stub_application_setting( @@ -2593,7 +2593,7 @@ describe Project do end context 'when the ref is a protected branch' do - let(:ref) { project.repository.find_branch('master') } + let(:ref) { 'refs/heads/master' } before do create(:protected_branch, name: 'master', project: project) @@ -2605,7 +2605,7 @@ describe Project do end context 'when the ref is a protected tag' do - let(:ref) { project.repository.find_tag('v1.0.0') } + let(:ref) { 'refs/tags/v1.0.0' } before do create(:protected_tag, name: 'v1.0.0', project: project) @@ -2802,20 +2802,10 @@ describe Project do subject { project.resolve_ref(ref) } context 'when ref is full ref' do - context 'when ref exists' do - let(:ref) { 'refs/heads/master' } + let(:ref) { 'refs/heads/master' } - it 'returns a ref' do - is_expected.to be_a(Gitlab::Git::Ref) - end - end - - context 'when ref does not exist' do - let(:ref) { 'refs/tags/doesnotexist' } - - it 'returns nil' do - is_expected.to eq(nil) - end + it 'returns the ref' do + is_expected.to eq(ref) end end @@ -2838,12 +2828,8 @@ describe Project do project.repository.add_tag(project.creator, ref, 'master') end - it 'returns a tag' do - is_expected.to be_a(Gitlab::Git::Tag) - end - - it 'returns the correct tag' do - expect(subject.name).to eq(ref) + it 'returns the tag ref' do + is_expected.to eq("refs/tags/#{ref}") end end @@ -2852,12 +2838,8 @@ describe Project do project.repository.add_branch(project.creator, ref, 'master') end - it 'returns a branch' do - is_expected.to be_a(Gitlab::Git::Branch) - end - - it 'returns the correct branch' do - expect(subject.name).to eq(ref) + it 'returns the branch ref' do + is_expected.to eq("refs/heads/#{ref}") end end end -- cgit v1.2.3 From e0b5306cb79c7a535f96c0d2d864278b2193d641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Fri, 16 Nov 2018 21:06:58 +0100 Subject: Remove Repository#find_ref --- spec/models/repository_spec.rb | 36 ------------------------------------ 1 file changed, 36 deletions(-) (limited to 'spec') diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index 6f9fdcf4482..f09b4b67061 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -1699,42 +1699,6 @@ describe Repository do end end - describe '#find_ref' do - subject { repository.find_ref(ref) } - - context 'when ref is a tag' do - let(:ref) { 'refs/tags/v1.0.0' } - - it 'returns a tag' do - is_expected.to be_a(Gitlab::Git::Tag) - end - - it 'returns the correct tag' do - expect(subject.name).to eq('v1.0.0') - end - end - - context 'when ref is a branch' do - let(:ref) { 'refs/heads/master' } - - it 'returns a branch' do - is_expected.to be_a(Gitlab::Git::Branch) - end - - it 'returns the correct branch' do - expect(subject.name).to eq('master') - end - end - - context 'when ref is invalid' do - let(:ref) { 'refs/notvalid/ref' } - - it 'returns nil' do - is_expected.to eq(nil) - end - end - end - describe '#avatar' do it 'returns nil if repo does not exist' do allow(repository).to receive(:root_ref).and_raise(Gitlab::Git::Repository::NoRepository) -- cgit v1.2.3 From 24d682254a0ae68dfc605fc077c6a7610b97994a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Tue, 20 Nov 2018 15:07:25 +0100 Subject: Move Project#resolve_ref to Repository --- spec/models/project_spec.rb | 51 +----------------------------------------- spec/models/repository_spec.rb | 47 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 50 deletions(-) (limited to 'spec') diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index cbc242308e8..25560ddea8d 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2568,7 +2568,7 @@ describe Project do subject { project.protected_for?('ref') } before do - allow(project).to receive(:resolve_ref).and_return(ref) + allow(project.repository).to receive(:resolve_ref).and_return(ref) end context 'when ref is ambiguous' do @@ -2796,55 +2796,6 @@ describe Project do end end - describe '#resolve_ref' do - let(:project) { create(:project, :repository) } - - subject { project.resolve_ref(ref) } - - context 'when ref is full ref' do - let(:ref) { 'refs/heads/master' } - - it 'returns the ref' do - is_expected.to eq(ref) - end - end - - context 'when ref is a tag or branch name' do - let(:ref) { 'ref' } - - context 'when ref is ambiguous' do - before do - project.repository.add_tag(project.creator, ref, 'master') - project.repository.add_branch(project.creator, ref, 'master') - end - - it 'returns nil' do - is_expected.to eq(nil) - end - end - - context 'when ref is tag name' do - before do - project.repository.add_tag(project.creator, ref, 'master') - end - - it 'returns the tag ref' do - is_expected.to eq("refs/tags/#{ref}") - end - end - - context 'when ref is branch name' do - before do - project.repository.add_branch(project.creator, ref, 'master') - end - - it 'returns the branch ref' do - is_expected.to eq("refs/heads/#{ref}") - end - end - end - end - describe '#http_url_to_repo' do let(:project) { create(:project) } diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index f09b4b67061..3d6cf2cbc19 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -1005,6 +1005,53 @@ describe Repository do end end + describe '#resolve_ref' do + subject { repository.resolve_ref(ref) } + + context 'when ref is full ref' do + let(:ref) { 'refs/heads/master' } + + it 'returns the ref' do + is_expected.to eq(ref) + end + end + + context 'when ref is a tag or branch name' do + let(:ref) { 'ref' } + + context 'when ref is ambiguous' do + before do + repository.add_tag(project.creator, ref, 'master') + repository.add_branch(project.creator, ref, 'master') + end + + it 'returns nil' do + is_expected.to eq(nil) + end + end + + context 'when ref is tag name' do + before do + repository.add_tag(project.creator, ref, 'master') + end + + it 'returns the tag ref' do + is_expected.to eq("refs/tags/#{ref}") + end + end + + context 'when ref is branch name' do + before do + repository.add_branch(project.creator, ref, 'master') + end + + it 'returns the branch ref' do + is_expected.to eq("refs/heads/#{ref}") + end + end + end + end + describe '#add_branch' do let(:branch_name) { 'new_feature' } let(:target) { 'master' } -- cgit v1.2.3 From 350ea9c55ab6b980ac5ec74d2a34b9cbac915e20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Fri, 23 Nov 2018 00:11:42 +0100 Subject: Implement Repository#ambiguous_ref? This implements Repository#ambiguous_ref? and checks if a ref is ambiguous before trying to resolve the ref in Project#protected_for? --- spec/models/project_spec.rb | 23 +++++++++----- spec/models/repository_spec.rb | 68 +++++++++++++++++++++++++----------------- 2 files changed, 57 insertions(+), 34 deletions(-) (limited to 'spec') diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 25560ddea8d..18ad69a1bda 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2565,14 +2565,23 @@ describe Project do describe '#protected_for?' do let(:project) { create(:project, :repository) } - subject { project.protected_for?('ref') } + subject { project.protected_for?(ref) } - before do - allow(project.repository).to receive(:resolve_ref).and_return(ref) + context 'when ref is nil' do + let(:ref) { nil } + + it 'returns false' do + is_expected.to be_falsey + end end context 'when ref is ambiguous' do - let(:ref) { nil } + let(:ref) { 'ref' } + + before do + project.repository.add_branch(project.creator, ref, 'master') + project.repository.add_tag(project.creator, ref, 'master') + end it 'returns false' do is_expected.to be_falsey @@ -2580,7 +2589,7 @@ describe Project do end context 'when the ref is not protected' do - let(:ref) { 'refs/heads/master' } + let(:ref) { 'master' } before do stub_application_setting( @@ -2593,7 +2602,7 @@ describe Project do end context 'when the ref is a protected branch' do - let(:ref) { 'refs/heads/master' } + let(:ref) { 'master' } before do create(:protected_branch, name: 'master', project: project) @@ -2605,7 +2614,7 @@ describe Project do end context 'when the ref is a protected tag' do - let(:ref) { 'refs/tags/v1.0.0' } + let(:ref) { 'v1.0.0' } before do create(:protected_tag, name: 'v1.0.0', project: project) diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index 3d6cf2cbc19..cdececd49e0 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -1005,7 +1005,36 @@ describe Repository do end end + describe '#ambiguous_ref?' do + let(:ref) { 'ref' } + + subject { repository.ambiguous_ref?(ref) } + + context 'when ref is ambiguous' do + before do + repository.add_tag(project.creator, ref, 'master') + repository.add_branch(project.creator, ref, 'master') + end + + it 'should be true' do + is_expected.to eq(true) + end + end + + context 'when ref is not ambiguous' do + before do + repository.add_tag(project.creator, ref, 'master') + end + + it 'should be false' do + is_expected.to eq(false) + end + end + end + describe '#resolve_ref' do + let(:ref) { 'ref' } + subject { repository.resolve_ref(ref) } context 'when ref is full ref' do @@ -1016,38 +1045,23 @@ describe Repository do end end - context 'when ref is a tag or branch name' do - let(:ref) { 'ref' } - - context 'when ref is ambiguous' do - before do - repository.add_tag(project.creator, ref, 'master') - repository.add_branch(project.creator, ref, 'master') - end - - it 'returns nil' do - is_expected.to eq(nil) - end + context 'when ref is tag name' do + before do + repository.add_tag(project.creator, ref, 'master') end - context 'when ref is tag name' do - before do - repository.add_tag(project.creator, ref, 'master') - end - - it 'returns the tag ref' do - is_expected.to eq("refs/tags/#{ref}") - end + it 'returns the tag ref' do + is_expected.to eq("refs/tags/#{ref}") end + end - context 'when ref is branch name' do - before do - repository.add_branch(project.creator, ref, 'master') - end + context 'when ref is branch name' do + before do + repository.add_branch(project.creator, ref, 'master') + end - it 'returns the branch ref' do - is_expected.to eq("refs/heads/#{ref}") - end + it 'returns the branch ref' do + is_expected.to eq("refs/heads/#{ref}") end end end -- cgit v1.2.3 From a1be58097943f7f568de6a90aa12cf7452ac2f0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Mon, 26 Nov 2018 15:43:46 +0100 Subject: Implement Command#ambiguous_ref? --- spec/lib/gitlab/ci/pipeline/chain/command_spec.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'spec') diff --git a/spec/lib/gitlab/ci/pipeline/chain/command_spec.rb b/spec/lib/gitlab/ci/pipeline/chain/command_spec.rb index 75a177d2d1f..6aa802ce6fd 100644 --- a/spec/lib/gitlab/ci/pipeline/chain/command_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/chain/command_spec.rb @@ -182,4 +182,24 @@ describe Gitlab::Ci::Pipeline::Chain::Command do it { is_expected.to eq(false) } end end + + describe '#ambiguous_ref' do + let(:project) { create(:project, :repository) } + let(:command) { described_class.new(project: project, origin_ref: 'ref') } + + subject { command.ambiguous_ref? } + + context 'when ref is not ambiguous' do + it { is_expected. to eq(false) } + end + + context 'when ref is ambiguous' do + before do + project.repository.add_tag(project.creator, 'ref', 'master') + project.repository.add_branch(project.creator, 'ref', 'master') + end + + it { is_expected. to eq(true) } + end + end end -- cgit v1.2.3 From dfe7f57eef0c5c2319c5c9898ba0721b6a1c9913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Wed, 28 Nov 2018 15:18:14 +0100 Subject: Rename Repository#resolve_ref to expand_ref --- spec/models/repository_spec.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'spec') diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index cdececd49e0..2063b4bbe75 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -1032,16 +1032,16 @@ describe Repository do end end - describe '#resolve_ref' do + describe '#expand_ref' do let(:ref) { 'ref' } - subject { repository.resolve_ref(ref) } + subject { repository.expand_ref(ref) } - context 'when ref is full ref' do + context 'when ref is not tag or branch name' do let(:ref) { 'refs/heads/master' } - it 'returns the ref' do - is_expected.to eq(ref) + it 'returns nil' do + is_expected.to eq(nil) end end -- cgit v1.2.3 From 08942de9b6a3ad361cbae8a83a8e8b7c7e4768ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Wed, 28 Nov 2018 15:43:58 +0100 Subject: Raise an error on ambiguous refs --- spec/models/project_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'spec') diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 18ad69a1bda..a106642c3e5 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2583,8 +2583,8 @@ describe Project do project.repository.add_tag(project.creator, ref, 'master') end - it 'returns false' do - is_expected.to be_falsey + it 'raises an error' do + expect { subject }.to raise_error(Repository::AmbiguousRefError) end end -- cgit v1.2.3 From 0f00c7818bf09e800c4ac6652077fffe7976ed6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Fri, 30 Nov 2018 11:34:59 +0100 Subject: Remove resolving conditional from protected_for --- spec/models/project_spec.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'spec') diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index a106642c3e5..75f1f779bb0 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2624,6 +2624,28 @@ describe Project do is_expected.to be_truthy end end + + context 'when ref name is a full branch ref' do + let(:ref) { 'refs/tags/something' } + + before do + project.repository.add_branch(project.creator, ref, 'master') + end + + it 'returns false' do + is_expected.to be_falsey + end + + context 'when ref is a protected branch' do + before do + create(:protected_branch, name: 'refs/tags/something', project: project) + end + + it 'returns true' do + is_expected.to be_truthy + end + end + end end describe '#update_project_statistics' do -- cgit v1.2.3 From 93d3c61eca258369bda0c28c5519cb14e4ff1457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Fri, 30 Nov 2018 12:18:53 +0100 Subject: Add specs when full ref is passed to protected_for --- spec/models/project_spec.rb | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'spec') diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 75f1f779bb0..33bce0c0823 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2625,7 +2625,27 @@ describe Project do end end - context 'when ref name is a full branch ref' do + context 'when ref is a full ref' do + let(:ref) { 'refs/heads/master' } + + context 'when ref is not protected' do + it 'returns false' do + is_expected.to be_falsey + end + end + + context 'when ref is protected' do + before do + create(:protected_branch, name: 'master', project: project) + end + + it 'returns true' do + is_expected.to be_truthy + end + end + end + + context 'when ref name is a full tag ref' do let(:ref) { 'refs/tags/something' } before do -- cgit v1.2.3 From 86c0558c1c473e26df0a1cd5ea4b647175e8b857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Fri, 30 Nov 2018 12:36:46 +0100 Subject: Refactor Project#protected_for? specs This refactors the Project#protected_for? specs to separate them into two contexts: when the ref is a full ref and when the ref is a ref name. --- spec/models/project_spec.rb | 118 +++++++++++++++++++++++++------------------- 1 file changed, 67 insertions(+), 51 deletions(-) (limited to 'spec') diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 33bce0c0823..b4b9d921ba4 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2567,30 +2567,7 @@ describe Project do subject { project.protected_for?(ref) } - context 'when ref is nil' do - let(:ref) { nil } - - it 'returns false' do - is_expected.to be_falsey - end - end - - context 'when ref is ambiguous' do - let(:ref) { 'ref' } - - before do - project.repository.add_branch(project.creator, ref, 'master') - project.repository.add_tag(project.creator, ref, 'master') - end - - it 'raises an error' do - expect { subject }.to raise_error(Repository::AmbiguousRefError) - end - end - - context 'when the ref is not protected' do - let(:ref) { 'master' } - + shared_examples 'ref is not protected' do before do stub_application_setting( default_branch_protection: Gitlab::Access::PROTECTION_NONE) @@ -2601,9 +2578,7 @@ describe Project do end end - context 'when the ref is a protected branch' do - let(:ref) { 'master' } - + shared_examples 'ref is protected branch' do before do create(:protected_branch, name: 'master', project: project) end @@ -2613,9 +2588,7 @@ describe Project do end end - context 'when the ref is a protected tag' do - let(:ref) { 'v1.0.0' } - + shared_examples 'ref is protected tag' do before do create(:protected_tag, name: 'v1.0.0', project: project) end @@ -2625,44 +2598,87 @@ describe Project do end end - context 'when ref is a full ref' do - let(:ref) { 'refs/heads/master' } + context 'when ref is nil' do + let(:ref) { nil } - context 'when ref is not protected' do - it 'returns false' do - is_expected.to be_falsey - end + it 'returns false' do + is_expected.to be_falsey end + end + + context 'when ref is ref name' do + context 'when ref is ambiguous' do + let(:ref) { 'ref' } - context 'when ref is protected' do before do - create(:protected_branch, name: 'master', project: project) + project.repository.add_branch(project.creator, 'ref', 'master') + project.repository.add_tag(project.creator, 'ref', 'master') end - it 'returns true' do - is_expected.to be_truthy + it 'raises an error' do + expect { subject }.to raise_error(Repository::AmbiguousRefError) end end + + context 'when the ref is not protected' do + let(:ref) { 'master' } + + it_behaves_like 'ref is not protected' + end + + context 'when the ref is a protected branch' do + let(:ref) { 'master' } + + it_behaves_like 'ref is protected branch' + end + + context 'when the ref is a protected tag' do + let(:ref) { 'v1.0.0' } + + it_behaves_like 'ref is protected tag' + end end - context 'when ref name is a full tag ref' do - let(:ref) { 'refs/tags/something' } + context 'when ref is full ref' do + context 'when the ref is not protected' do + let(:ref) { 'refs/heads/master' } - before do - project.repository.add_branch(project.creator, ref, 'master') + it_behaves_like 'ref is not protected' end - it 'returns false' do - is_expected.to be_falsey + context 'when the ref is a protected branch' do + let(:ref) { 'refs/heads/master' } + + it_behaves_like 'ref is protected branch' end - context 'when ref is a protected branch' do + context 'when the ref is a protected tag' do + let(:ref) { 'refs/tags/v1.0.0' } + + it_behaves_like 'ref is protected tag' + end + + context 'when branch ref name is a full tag ref' do + let(:ref) { 'refs/tags/something' } + before do - create(:protected_branch, name: 'refs/tags/something', project: project) + project.repository.add_branch(project.creator, ref, 'master') end - it 'returns true' do - is_expected.to be_truthy + context 'when ref is not protected' do + it 'returns false' do + is_expected.to be_falsey + end + end + + context 'when ref is a protected branch' do + before do + create(:protected_branch, name: 'refs/tags/something', project: project) + end + + it 'returns true' do + is_expected.to be_truthy + end end end end @@ -2883,7 +2899,7 @@ describe Project do it 'shows full error updating an invalid MR' do error_message = 'Failed to replace merge_requests because one or more of the new records could not be saved.'\ - ' Validate fork Source project is not a fork of the target project' + ' Validate fork Source project is not a fork of the target project' expect { project.append_or_update_attribute(:merge_requests, [create(:merge_request)]) } .to raise_error(ActiveRecord::RecordNotSaved, error_message) -- cgit v1.2.3 From 9b4e45c35253a397f3ff79207736280293848bf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Mon, 3 Dec 2018 14:06:07 +0100 Subject: Check for explicit true or false in specs --- spec/models/project_spec.rb | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'spec') diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index b4b9d921ba4..606636e9d58 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2574,7 +2574,7 @@ describe Project do end it 'returns false' do - is_expected.to be_falsey + is_expected.to be false end end @@ -2584,7 +2584,7 @@ describe Project do end it 'returns true' do - is_expected.to be_truthy + is_expected.to be true end end @@ -2594,7 +2594,7 @@ describe Project do end it 'returns true' do - is_expected.to be_truthy + is_expected.to be true end end @@ -2602,7 +2602,7 @@ describe Project do let(:ref) { nil } it 'returns false' do - is_expected.to be_falsey + is_expected.to be false end end @@ -2637,6 +2637,14 @@ describe Project do it_behaves_like 'ref is protected tag' end + + context 'when ref does not exist' do + let(:ref) { 'something' } + + it 'returns false' do + is_expected.to be false + end + end end context 'when ref is full ref' do @@ -2667,7 +2675,7 @@ describe Project do context 'when ref is not protected' do it 'returns false' do - is_expected.to be_falsey + is_expected.to be false end end @@ -2677,10 +2685,18 @@ describe Project do end it 'returns true' do - is_expected.to be_truthy + is_expected.to be true end end end + + context 'when ref does not exist' do + let(:ref) { 'refs/heads/something' } + + it 'returns false' do + is_expected.to be false + end + end end end -- cgit v1.2.3 From 78383c41bb51d50d970f0056e80f698960ed40dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Sat, 8 Dec 2018 20:39:11 +0100 Subject: Use build for testing HasRef --- spec/models/concerns/has_ref_spec.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'spec') diff --git a/spec/models/concerns/has_ref_spec.rb b/spec/models/concerns/has_ref_spec.rb index 14db197dfe0..8aed72d77a4 100644 --- a/spec/models/concerns/has_ref_spec.rb +++ b/spec/models/concerns/has_ref_spec.rb @@ -4,13 +4,13 @@ require 'spec_helper' describe HasRef do describe '#branch?' do - let(:pipeline) { create(:ci_pipeline) } + let(:build) { create(:ci_build) } - subject { pipeline.branch? } + subject { build.branch? } context 'is not a tag' do before do - pipeline.tag = false + build.tag = false end it 'return true when tag is set to false' do @@ -20,7 +20,7 @@ describe HasRef do context 'is not a tag' do before do - pipeline.tag = true + build.tag = true end it 'return false when tag is set to true' do @@ -30,10 +30,10 @@ describe HasRef do end describe '#git_ref' do - subject { pipeline.git_ref } + subject { build.git_ref } context 'when tag is true' do - let(:pipeline) { create(:ci_pipeline, tag: true) } + let(:build) { create(:ci_build, tag: true) } it 'returns a tag ref' do is_expected.to start_with(Gitlab::Git::TAG_REF_PREFIX) @@ -41,7 +41,7 @@ describe HasRef do end context 'when tag is false' do - let(:pipeline) { create(:ci_pipeline, tag: false) } + let(:build) { create(:ci_build, tag: false) } it 'returns a branch ref' do is_expected.to start_with(Gitlab::Git::BRANCH_REF_PREFIX) @@ -49,7 +49,7 @@ describe HasRef do end context 'when tag is nil' do - let(:pipeline) { create(:ci_pipeline, tag: nil) } + let(:build) { create(:ci_build, tag: nil) } it 'returns a branch ref' do is_expected.to start_with(Gitlab::Git::BRANCH_REF_PREFIX) -- cgit v1.2.3 From 426488b7218e85ce69868ae4628801af2322b74a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Sat, 8 Dec 2018 20:39:26 +0100 Subject: Use full ref when creating MR pipeline in specs --- spec/services/ci/create_pipeline_service_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'spec') diff --git a/spec/services/ci/create_pipeline_service_spec.rb b/spec/services/ci/create_pipeline_service_spec.rb index ccc6b0ef1c7..02fde579f6f 100644 --- a/spec/services/ci/create_pipeline_service_spec.rb +++ b/spec/services/ci/create_pipeline_service_spec.rb @@ -667,7 +667,7 @@ describe Ci::CreatePipelineService do stub_ci_pipeline_yaml_file(YAML.dump(config)) end - let(:ref_name) { 'feature' } + let(:ref_name) { 'refs/heads/feature' } context 'when source is merge request' do let(:source) { :merge_request } @@ -696,7 +696,7 @@ describe Ci::CreatePipelineService do let(:merge_request) do create(:merge_request, source_project: project, - source_branch: ref_name, + source_branch: Gitlab::Git.ref_name(ref_name), target_project: project, target_branch: 'master') end @@ -709,7 +709,7 @@ describe Ci::CreatePipelineService do end context 'when ref is tag' do - let(:ref_name) { 'v1.1.0' } + let(:ref_name) { 'refs/tags/v1.1.0' } it 'does not create a merge request pipeline' do expect(pipeline).not_to be_persisted @@ -721,7 +721,7 @@ describe Ci::CreatePipelineService do let(:merge_request) do create(:merge_request, source_project: project, - source_branch: ref_name, + source_branch: Gitlab::Git.ref_name(ref_name), target_project: target_project, target_branch: 'master') end @@ -786,7 +786,7 @@ describe Ci::CreatePipelineService do let(:merge_request) do create(:merge_request, source_project: project, - source_branch: ref_name, + source_branch: Gitlab::Git.ref_name(ref_name), target_project: project, target_branch: 'master') end @@ -839,7 +839,7 @@ describe Ci::CreatePipelineService do let(:merge_request) do create(:merge_request, source_project: project, - source_branch: ref_name, + source_branch: Gitlab::Git.ref_name(ref_name), target_project: project, target_branch: 'master') end -- cgit v1.2.3