Follow-up from "Reduce nested #capture_git_error calls on Wiki"
Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.
The following discussion from !157532 (merged) should be addressed:
-
@eugielimpin started a discussion: suggestion (non-blocking) We can reduce the code duplication here by using a
shared_example
.Suggested patch
diff --git a/spec/support/shared_examples/models/wiki_shared_examples.rb b/spec/support/shared_examples/models/wiki_shared_examples.rb index c1f116577e4c7..7d338cc8f3942 100644 --- a/spec/support/shared_examples/models/wiki_shared_examples.rb +++ b/spec/support/shared_examples/models/wiki_shared_examples.rb @@ -160,6 +160,21 @@ end end + shared_examples 'captures Git error and returns the correct value' do + let!(:page) { create(:wiki_page, wiki: wiki, title: 'test page') } + + before do + allow(Gitlab::GitalyClient).to receive(:call) do + raise GRPC::Unavailable, 'Gitaly broken in this spec' + end + end + + it 'returns correct value and sets error_message', :aggregate_failures do + expect(call_method).to eq expected_return + expect(wiki.error_message).to match(/Gitaly broken in this spec/) + end + end + describe '#empty?' do context 'when the wiki repository is empty' do it 'returns true' do @@ -189,17 +204,9 @@ end end - context 'when the repository fails' do - let!(:page) { create(:wiki_page, wiki: subject, title: 'test page') } - - it 'returns true if the repository raise an error' do - allow(Gitlab::GitalyClient).to receive(:call) do - raise GRPC::Unavailable, 'Gitaly broken in this spec' - end - - expect(subject.empty?).to be(true) - expect(subject.error_message).to match(/Gitaly broken in this spec/) - end + it_behaves_like 'captures Git error and returns the correct value' do + let(:call_method) { subject.empty? } + let(:expected_return) { true } end end @@ -315,17 +322,9 @@ end end - context 'when the repository fails to list' do - let!(:page) { create(:wiki_page, wiki: subject, title: 'test page') } - - it 'returns an empty list if the repository raise an error' do - allow(Gitlab::GitalyClient).to receive(:call) do - raise GRPC::Unavailable, 'Gitaly broken in this spec' - end - - expect(wiki_pages.count).to eq(0) - expect(subject.error_message).to match(/Gitaly broken in this spec/) - end + it_behaves_like 'captures Git error and returns the correct value' do + let(:call_method) { wiki_pages.count } + let(:expected_return) { 0 } end end @@ -370,17 +369,9 @@ expect(page).to be_a WikiPage end - context 'when the repository fails to find' do - let!(:page) { create(:wiki_page, wiki: subject, title: 'test page') } - - it 'returns nil if the repository raise an error' do - allow(Gitlab::GitalyClient).to receive(:call) do - raise GRPC::Unavailable, 'Gitaly broken in this spec' - end - - expect(subject.find_page('index page')).to be_nil - expect(subject.error_message).to match(/Gitaly broken in this spec/) - end + it_behaves_like 'captures Git error and returns the correct value' do + let(:call_method) { subject.find_page('index page') } + let(:expected_return) { nil } end # The repository can contain files that were not generated by Gollum Wiki @@ -618,17 +609,9 @@ end end - context 'when the repository fails to find' do - let!(:page) { create(:wiki_page, wiki: subject, title: 'test page') } - - it 'returns nil if the repository raise an error' do - allow(Gitlab::GitalyClient).to receive(:call) do - raise GRPC::Unavailable, 'Gitaly broken in this spec' - end - - expect(subject.find_file('image.png')).to be_nil - expect(subject.error_message).to match(/Gitaly broken in this spec/) - end + it_behaves_like 'captures Git error and returns the correct value' do + let(:call_method) { subject.find_file('image.png') } + let(:expected_return) { nil } end end @@ -802,17 +785,9 @@ end end - context 'when the repository fails to create' do - let!(:page) { create(:wiki_page, wiki: subject, title: 'test page') } - - it 'returns false if the repository raise an error' do - allow(Gitlab::GitalyClient).to receive(:call) do - raise GRPC::Unavailable, 'Gitaly broken in this spec' - end - - expect(subject.create_page('test page', 'content')).to eq(false) - expect(subject.error_message).to match(/Gitaly broken in this spec/) - end + it_behaves_like 'captures Git error and returns the correct value' do + let(:call_method) { subject.create_page('test page', 'content') } + let(:expected_return) { false } end end @@ -969,19 +944,14 @@ def update_page end end + it_behaves_like 'captures Git error and returns the correct value' do + let(:expected_return) { false } + let(:call_method) { subject.update_page(page.page, content: 'new content', format: :markdown) } + end + context 'when the repository fails to update' do let!(:page) { create(:wiki_page, wiki: subject, title: 'test page') } - it 'returns false if the repository raise an error' do - allow(Gitlab::GitalyClient).to receive(:call) do - raise GRPC::Unavailable, 'Gitaly broken in this spec' - end - - expect(subject.update_page(page.page, content: 'new content', format: :markdown)) - .to eq(false) - expect(subject.error_message).to match(/Gitaly broken in this spec/) - end - it 'returns false and sets error message if the repository raise an IndexError', :aggregate_failures do expect(subject.repository) .to receive(:commit_files) @@ -1091,20 +1061,14 @@ def update_page end end - context 'when the repository fails' do + it_behaves_like 'captures Git error and returns the correct value' do + let(:call_method) { subject } + let(:expected_return) { 'main' } + before do wiki.repository.create_if_not_exists wiki.create_page('index', 'test content') end - - it 'returns main branch if the repository raise an error' do - allow(Gitlab::GitalyClient).to receive(:call) do - raise GRPC::Unavailable, 'Gitaly broken in this spec' - end - - expect(subject).to eq 'main' - expect(wiki.error_message).to match(/Gitaly broken in this spec/) - end end end
Edited by 🤖 GitLab Bot 🤖