Commit 267b18dead74106420430f79cde38fd90c17445d
1 parent
b3834bc9
Exists in
master
and in
4 other branches
Add specs for exporting merge requests as diff or patch
Showing
2 changed files
with
99 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,84 @@ |
1 | +require 'spec_helper' | |
2 | + | |
3 | +describe MergeRequestsController do | |
4 | + let(:project) { create(:project) } | |
5 | + let(:user) { create(:user) } | |
6 | + let(:merge_request) { create(:merge_request_with_diffs, project: project) } | |
7 | + | |
8 | + before do | |
9 | + sign_in(user) | |
10 | + | |
11 | + project.add_access(user, :read, :admin) | |
12 | + end | |
13 | + | |
14 | + describe "#show" do | |
15 | + shared_examples "export as" do |format| | |
16 | + it "should generally work" do | |
17 | + get :show, project_id: project.code, id: merge_request.id, format: format | |
18 | + | |
19 | + expect(response).to be_success | |
20 | + end | |
21 | + | |
22 | + it "should generate it" do | |
23 | + MergeRequest.any_instance.should_receive(:"to_#{format}") | |
24 | + | |
25 | + get :show, project_id: project.code, id: merge_request.id, format: format | |
26 | + end | |
27 | + | |
28 | + it "should render it" do | |
29 | + get :show, project_id: project.code, id: merge_request.id, format: format | |
30 | + | |
31 | + expect(response.body).to eq(merge_request.send(:"to_#{format}")) | |
32 | + end | |
33 | + | |
34 | + it "should not escape Html" do | |
35 | + MergeRequest.any_instance.stub(:"to_#{format}").and_return('HTML entities &<>" ') | |
36 | + | |
37 | + get :show, project_id: project.code, id: merge_request.id, format: format | |
38 | + | |
39 | + expect(response.body).to_not include('&') | |
40 | + expect(response.body).to_not include('>') | |
41 | + expect(response.body).to_not include('<') | |
42 | + expect(response.body).to_not include('"') | |
43 | + end | |
44 | + end | |
45 | + | |
46 | + describe "as diff" do | |
47 | + include_examples "export as", :diff | |
48 | + let(:format) { :diff } | |
49 | + | |
50 | + it "should really only be a git diff" do | |
51 | + get :show, project_id: project.code, id: merge_request.id, format: format | |
52 | + | |
53 | + expect(response.body).to start_with("diff --git") | |
54 | + end | |
55 | + end | |
56 | + | |
57 | + describe "as patch" do | |
58 | + include_examples "export as", :patch | |
59 | + let(:format) { :patch } | |
60 | + | |
61 | + it "should really be a git email patch with commit" do | |
62 | + get :show, project_id: project.code, id: merge_request.id, format: format | |
63 | + | |
64 | + expect(response.body[0..100]).to start_with("From #{merge_request.commits.last.id}") | |
65 | + end | |
66 | + | |
67 | + it "should contain as many patches as there are commits" do | |
68 | + get :show, project_id: project.code, id: merge_request.id, format: format | |
69 | + | |
70 | + patch_count = merge_request.commits.count | |
71 | + merge_request.commits.each_with_index do |commit, patch_num| | |
72 | + expect(response.body).to match(/^From #{commit.id}/) | |
73 | + expect(response.body).to match(/^Subject: \[PATCH #{patch_num}\/#{patch_count}\]/) | |
74 | + end | |
75 | + end | |
76 | + | |
77 | + it "should contain git diffs" do | |
78 | + get :show, project_id: project.code, id: merge_request.id, format: format | |
79 | + | |
80 | + expect(response.body).to match(/^diff --git/) | |
81 | + end | |
82 | + end | |
83 | + end | |
84 | +end | ... | ... |
spec/factories.rb
... | ... | @@ -63,7 +63,22 @@ FactoryGirl.define do |
63 | 63 | closed true |
64 | 64 | end |
65 | 65 | |
66 | + # pick 3 commits "at random" (from bcf03b5d~3 to bcf03b5d) | |
67 | + trait :with_diffs do | |
68 | + target_branch "bcf03b5d~3" | |
69 | + source_branch "bcf03b5d" | |
70 | + st_commits do | |
71 | + [Commit.new(project.repo.commit('bcf03b5d')), | |
72 | + Commit.new(project.repo.commit('bcf03b5d~1')), | |
73 | + Commit.new(project.repo.commit('bcf03b5d~2'))] | |
74 | + end | |
75 | + st_diffs do | |
76 | + project.repo.diff("bcf03b5d~3", "bcf03b5d") | |
77 | + end | |
78 | + end | |
79 | + | |
66 | 80 | factory :closed_merge_request, traits: [:closed] |
81 | + factory :merge_request_with_diffs, traits: [:with_diffs] | |
67 | 82 | end |
68 | 83 | |
69 | 84 | factory :note do | ... | ... |