diff options
author | Vinnie Okada <vokada@mrvinn.com> | 2015-03-17 20:53:09 -0600 |
---|---|---|
committer | Vinnie Okada <vokada@mrvinn.com> | 2015-03-17 20:53:09 -0600 |
commit | feeffc442618d92040cd1cc38158b689a09988fd (patch) | |
tree | b19c0ac2ddae23d830bbc69b99d920eec1f81363 /spec/helpers/gitlab_markdown_helper_spec.rb | |
parent | 1a9c2ddc55cf563ea42d67811a19b2693d7a44e9 (diff) | |
parent | 5bbc70da9cb439342bdbe022988e4e734d891f44 (diff) | |
download | gitlab-ce-feeffc442618d92040cd1cc38158b689a09988fd.tar.gz |
Merge branch 'master' into markdown-tags
Use the latest HTML pipeline gem
Diffstat (limited to 'spec/helpers/gitlab_markdown_helper_spec.rb')
-rw-r--r-- | spec/helpers/gitlab_markdown_helper_spec.rb | 367 |
1 files changed, 240 insertions, 127 deletions
diff --git a/spec/helpers/gitlab_markdown_helper_spec.rb b/spec/helpers/gitlab_markdown_helper_spec.rb index c75773ad321..3a884d39bf1 100644 --- a/spec/helpers/gitlab_markdown_helper_spec.rb +++ b/spec/helpers/gitlab_markdown_helper_spec.rb @@ -1,4 +1,4 @@ -require "spec_helper" +require 'spec_helper' describe GitlabMarkdownHelper do include ApplicationHelper @@ -9,6 +9,7 @@ describe GitlabMarkdownHelper do let(:user) { create(:user, username: 'gfm') } let(:commit) { project.repository.commit } + let(:earlier_commit){ project.repository.commit("HEAD~2") } let(:issue) { create(:issue, project: project) } let(:merge_request) { create(:merge_request, source_project: project, target_project: project) } let(:snippet) { create(:project_snippet, project: project) } @@ -23,71 +24,120 @@ describe GitlabMarkdownHelper do @project = project @ref = 'markdown' @repository = project.repository + @request.host = Gitlab.config.gitlab.host end describe "#gfm" do it "should return unaltered text if project is nil" do actual = "Testing references: ##{issue.iid}" - gfm(actual).should_not == actual + expect(gfm(actual)).not_to eq(actual) @project = nil - gfm(actual).should == actual + expect(gfm(actual)).to eq(actual) end it "should not alter non-references" do actual = expected = "_Please_ *stop* 'helping' and all the other b*$#%' you do." - gfm(actual).should == expected + expect(gfm(actual)).to eq(expected) end it "should not touch HTML entities" do - @project.issues.stub(:where).with(id: '39').and_return([issue]) + allow(@project.issues).to receive(:where). + with(id: '39').and_return([issue]) actual = 'We'll accept good pull requests.' - gfm(actual).should == "We'll accept good pull requests." + expect(gfm(actual)).to eq("We'll accept good pull requests.") end it "should forward HTML options to links" do - gfm("Fixed in #{commit.id}", @project, class: 'foo'). - should have_selector('a.gfm.foo') + expect(gfm("Fixed in #{commit.id}", @project, class: 'foo')). + to have_selector('a.gfm.foo') + end + + describe "referencing a commit range" do + let(:expected) { namespace_project_compare_path(project.namespace, project, from: earlier_commit.id, to: commit.id) } + + it "should link using a full id" do + actual = "What happened in #{earlier_commit.id}...#{commit.id}" + expect(gfm(actual)).to match(expected) + end + + it "should link using a short id" do + actual = "What happened in #{earlier_commit.short_id}...#{commit.short_id}" + expected = namespace_project_compare_path(project.namespace, project, from: earlier_commit.short_id, to: commit.short_id) + expect(gfm(actual)).to match(expected) + end + + it "should link inclusively" do + actual = "What happened in #{earlier_commit.id}..#{commit.id}" + expected = namespace_project_compare_path(project.namespace, project, from: "#{earlier_commit.id}^", to: commit.id) + expect(gfm(actual)).to match(expected) + end + + it "should link with adjacent text" do + actual = "(see #{earlier_commit.id}...#{commit.id})" + expect(gfm(actual)).to match(expected) + end + + it "should keep whitespace intact" do + actual = "Changes #{earlier_commit.id}...#{commit.id} dramatically" + expected = /Changes <a.+>#{earlier_commit.id}...#{commit.id}<\/a> dramatically/ + expect(gfm(actual)).to match(expected) + end + + it "should not link with an invalid id" do + actual = expected = "What happened in #{earlier_commit.id.reverse}...#{commit.id.reverse}" + expect(gfm(actual)).to eq(expected) + end + + it "should include a title attribute" do + actual = "What happened in #{earlier_commit.id}...#{commit.id}" + expect(gfm(actual)).to match(/title="Commits #{earlier_commit.id} through #{commit.id}"/) + end + + it "should include standard gfm classes" do + actual = "What happened in #{earlier_commit.id}...#{commit.id}" + expect(gfm(actual)).to match(/class="\s?gfm gfm-commit_range\s?"/) + end end describe "referencing a commit" do - let(:expected) { project_commit_path(project, commit) } + let(:expected) { namespace_project_commit_path(project.namespace, project, commit) } it "should link using a full id" do actual = "Reverts #{commit.id}" - gfm(actual).should match(expected) + expect(gfm(actual)).to match(expected) end it "should link using a short id" do - actual = "Backported from #{commit.short_id(6)}" - gfm(actual).should match(expected) + actual = "Backported from #{commit.short_id}" + expect(gfm(actual)).to match(expected) end it "should link with adjacent text" do actual = "Reverted (see #{commit.id})" - gfm(actual).should match(expected) + expect(gfm(actual)).to match(expected) end it "should keep whitespace intact" do actual = "Changes #{commit.id} dramatically" expected = /Changes <a.+>#{commit.id}<\/a> dramatically/ - gfm(actual).should match(expected) + expect(gfm(actual)).to match(expected) end it "should not link with an invalid id" do actual = expected = "What happened in #{commit.id.reverse}" - gfm(actual).should == expected + expect(gfm(actual)).to eq(expected) end it "should include a title attribute" do actual = "Reverts #{commit.id}" - gfm(actual).should match(/title="#{commit.link_title}"/) + expect(gfm(actual)).to match(/title="#{commit.link_title}"/) end it "should include standard gfm classes" do actual = "Reverts #{commit.id}" - gfm(actual).should match(/class="\s?gfm gfm-commit\s?"/) + expect(gfm(actual)).to match(/class="\s?gfm gfm-commit\s?"/) end end @@ -100,37 +150,37 @@ describe GitlabMarkdownHelper do end it "should link using a simple name" do - gfm(actual).should match(expected) + expect(gfm(actual)).to match(expected) end it "should link using a name with dots" do user.update_attributes(name: "alphA.Beta") - gfm(actual).should match(expected) + expect(gfm(actual)).to match(expected) end it "should link using name with underscores" do user.update_attributes(name: "ping_pong_king") - gfm(actual).should match(expected) + expect(gfm(actual)).to match(expected) end it "should link with adjacent text" do actual = "Mail the admin (@#{user.username})" - gfm(actual).should match(expected) + expect(gfm(actual)).to match(expected) end it "should keep whitespace intact" do actual = "Yes, @#{user.username} is right." expected = /Yes, <a.+>@#{user.username}<\/a> is right/ - gfm(actual).should match(expected) + expect(gfm(actual)).to match(expected) end it "should not link with an invalid id" do actual = expected = "@#{user.username.reverse} you are right." - gfm(actual).should == expected + expect(gfm(actual)).to eq(expected) end it "should include standard gfm classes" do - gfm(actual).should match(/class="\s?gfm gfm-team_member\s?"/) + expect(gfm(actual)).to match(/class="\s?gfm gfm-project_member\s?"/) end end @@ -144,40 +194,41 @@ describe GitlabMarkdownHelper do # Currently limited to Snippets, Issues and MergeRequests shared_examples 'referenced object' do let(:actual) { "Reference to #{reference}" } - let(:expected) { polymorphic_path([project, object]) } + let(:expected) { polymorphic_path([project.namespace, project, object]) } it "should link using a valid id" do - gfm(actual).should match(expected) + expect(gfm(actual)).to match(expected) end it "should link with adjacent text" do # Wrap the reference in parenthesis - gfm(actual.gsub(reference, "(#{reference})")).should match(expected) + expect(gfm(actual.gsub(reference, "(#{reference})"))).to match(expected) # Append some text to the end of the reference - gfm(actual.gsub(reference, "#{reference}, right?")).should match(expected) + expect(gfm(actual.gsub(reference, "#{reference}, right?"))). + to match(expected) end it "should keep whitespace intact" do actual = "Referenced #{reference} already." expected = /Referenced <a.+>[^\s]+<\/a> already/ - gfm(actual).should match(expected) + expect(gfm(actual)).to match(expected) end it "should not link with an invalid id" do # Modify the reference string so it's still parsed, but is invalid reference.gsub!(/^(.)(\d+)$/, '\1' + ('\2' * 2)) - gfm(actual).should == actual + expect(gfm(actual)).to eq(actual) end it "should include a title attribute" do title = "#{object.class.to_s.titlecase}: #{object.title}" - gfm(actual).should match(/title="#{title}"/) + expect(gfm(actual)).to match(/title="#{title}"/) end it "should include standard gfm classes" do css = object.class.to_s.underscore - gfm(actual).should match(/class="\s?gfm gfm-#{css}\s?"/) + expect(gfm(actual)).to match(/class="\s?gfm gfm-#{css}\s?"/) end end @@ -196,34 +247,33 @@ describe GitlabMarkdownHelper do let(:actual) { "Reference to #{full_reference}" } let(:expected) do if object.is_a?(Commit) - project_commit_path(@other_project, object) + namespace_project_commit_path(@other_project.namespace, @other_project, object) else - polymorphic_path([@other_project, object]) + polymorphic_path([@other_project.namespace, @other_project, object]) end end it 'should link using a valid id' do - gfm(actual).should match( + expect(gfm(actual)).to match( /#{expected}.*#{Regexp.escape(full_reference)}/ ) end it 'should link with adjacent text' do # Wrap the reference in parenthesis - gfm(actual.gsub(full_reference, "(#{full_reference})")).should( + expect(gfm(actual.gsub(full_reference, "(#{full_reference})"))).to( match(expected) ) # Append some text to the end of the reference - gfm(actual.gsub(full_reference, "#{full_reference}, right?")).should( - match(expected) - ) + expect(gfm(actual.gsub(full_reference, "#{full_reference}, right?"))). + to(match(expected)) end it 'should keep whitespace intact' do actual = "Referenced #{full_reference} already." expected = /Referenced <a.+>[^\s]+<\/a> already/ - gfm(actual).should match(expected) + expect(gfm(actual)).to match(expected) end it 'should not link with an invalid id' do @@ -233,7 +283,7 @@ describe GitlabMarkdownHelper do else reference.gsub!(/^(.)(\d+)$/, '\1' + ('\2' * 2)) end - gfm(actual).should == actual + expect(gfm(actual)).to eq(actual) end it 'should include a title attribute' do @@ -242,12 +292,12 @@ describe GitlabMarkdownHelper do else title = "#{object.class.to_s.titlecase}: #{object.title}" end - gfm(actual).should match(/title="#{title}"/) + expect(gfm(actual)).to match(/title="#{title}"/) end it 'should include standard gfm classes' do css = object.class.to_s.underscore - gfm(actual).should match(/class="\s?gfm gfm-#{css}\s?"/) + expect(gfm(actual)).to match(/class="\s?gfm gfm-#{css}\s?"/) end end @@ -296,43 +346,47 @@ describe GitlabMarkdownHelper do let(:reference) { "JIRA-#{issue.iid}" } before do - issue_tracker_config = { "jira" => { "title" => "JIRA tracker", "issues_url" => "http://jira.example/browse/:id" } } - Gitlab.config.stub(:issues_tracker).and_return(issue_tracker_config) - @project.stub(:issues_tracker).and_return("jira") - @project.stub(:issues_tracker_id).and_return("JIRA") + jira = @project.create_jira_service if @project.jira_service.nil? + properties = {"title"=>"JIRA tracker", "project_url"=>"http://jira.example/issues/?jql=project=A", "issues_url"=>"http://jira.example/browse/:id", "new_issue_url"=>"http://jira.example/secure/CreateIssue.jspa"} + jira.update_attributes(properties: properties, active: true) + end + + after do + @project.jira_service.destroy! unless @project.jira_service.nil? end it "should link using a valid id" do - gfm(actual).should match(expected) + expect(gfm(actual)).to match(expected) end it "should link with adjacent text" do # Wrap the reference in parenthesis - gfm(actual.gsub(reference, "(#{reference})")).should match(expected) + expect(gfm(actual.gsub(reference, "(#{reference})"))).to match(expected) # Append some text to the end of the reference - gfm(actual.gsub(reference, "#{reference}, right?")).should match(expected) + expect(gfm(actual.gsub(reference, "#{reference}, right?"))). + to match(expected) end it "should keep whitespace intact" do actual = "Referenced #{reference} already." expected = /Referenced <a.+>[^\s]+<\/a> already/ - gfm(actual).should match(expected) + expect(gfm(actual)).to match(expected) end it "should not link with an invalid id" do # Modify the reference string so it's still parsed, but is invalid invalid_reference = actual.gsub(/(\d+)$/, "r45") - gfm(invalid_reference).should == invalid_reference + expect(gfm(invalid_reference)).to eq(invalid_reference) end it "should include a title attribute" do title = "Issue in JIRA tracker" - gfm(actual).should match(/title="#{title}"/) + expect(gfm(actual)).to match(/title="#{title}"/) end it "should include standard gfm classes" do - gfm(actual).should match(/class="\s?gfm gfm-issue\s?"/) + expect(gfm(actual)).to match(/class="\s?gfm gfm-issue\s?"/) end end @@ -347,40 +401,40 @@ describe GitlabMarkdownHelper do let(:object) { snippet } let(:reference) { "$#{snippet.id}" } let(:actual) { "Reference to #{reference}" } - let(:expected) { project_snippet_path(project, object) } + let(:expected) { namespace_project_snippet_path(project.namespace, project, object) } it "should link using a valid id" do - gfm(actual).should match(expected) + expect(gfm(actual)).to match(expected) end it "should link with adjacent text" do # Wrap the reference in parenthesis - gfm(actual.gsub(reference, "(#{reference})")).should match(expected) + expect(gfm(actual.gsub(reference, "(#{reference})"))).to match(expected) # Append some text to the end of the reference - gfm(actual.gsub(reference, "#{reference}, right?")).should match(expected) + expect(gfm(actual.gsub(reference, "#{reference}, right?"))).to match(expected) end it "should keep whitespace intact" do actual = "Referenced #{reference} already." expected = /Referenced <a.+>[^\s]+<\/a> already/ - gfm(actual).should match(expected) + expect(gfm(actual)).to match(expected) end it "should not link with an invalid id" do # Modify the reference string so it's still parsed, but is invalid reference.gsub!(/^(.)(\d+)$/, '\1' + ('\2' * 2)) - gfm(actual).should == actual + expect(gfm(actual)).to eq(actual) end it "should include a title attribute" do title = "Snippet: #{object.title}" - gfm(actual).should match(/title="#{title}"/) + expect(gfm(actual)).to match(/title="#{title}"/) end it "should include standard gfm classes" do css = object.class.to_s.underscore - gfm(actual).should match(/class="\s?gfm gfm-snippet\s?"/) + expect(gfm(actual)).to match(/class="\s?gfm gfm-snippet\s?"/) end end @@ -389,70 +443,70 @@ describe GitlabMarkdownHelper do let(:actual) { "!#{merge_request.iid} -> #{commit.id} -> ##{issue.iid}" } it "should link to the merge request" do - expected = project_merge_request_path(project, merge_request) - gfm(actual).should match(expected) + expected = namespace_project_merge_request_path(project.namespace, project, merge_request) + expect(gfm(actual)).to match(expected) end it "should link to the commit" do - expected = project_commit_path(project, commit) - gfm(actual).should match(expected) + expected = namespace_project_commit_path(project.namespace, project, commit) + expect(gfm(actual)).to match(expected) end it "should link to the issue" do - expected = project_issue_path(project, issue) - gfm(actual).should match(expected) + expected = namespace_project_issue_path(project.namespace, project, issue) + expect(gfm(actual)).to match(expected) end end describe "emoji" do it "matches at the start of a string" do - gfm(":+1:").should match(/<img/) + expect(gfm(":+1:")).to match(/<img/) end it "matches at the end of a string" do - gfm("This gets a :-1:").should match(/<img/) + expect(gfm("This gets a :-1:")).to match(/<img/) end it "matches with adjacent text" do - gfm("+1 (:+1:)").should match(/<img/) + expect(gfm("+1 (:+1:)")).to match(/<img/) end it "has a title attribute" do - gfm(":-1:").should match(/title=":-1:"/) + expect(gfm(":-1:")).to match(/title=":-1:"/) end it "has an alt attribute" do - gfm(":-1:").should match(/alt=":-1:"/) + expect(gfm(":-1:")).to match(/alt=":-1:"/) end it "has an emoji class" do - gfm(":+1:").should match('class="emoji"') + expect(gfm(":+1:")).to match('class="emoji"') end it "sets height and width" do actual = gfm(":+1:") - actual.should match(/width="20"/) - actual.should match(/height="20"/) + expect(actual).to match(/width="20"/) + expect(actual).to match(/height="20"/) end it "keeps whitespace intact" do - gfm('This deserves a :+1: big time.'). - should match(/deserves a <img.+> big time/) + expect(gfm('This deserves a :+1: big time.')). + to match(/deserves a <img.+> big time/) end it "ignores invalid emoji" do - gfm(":invalid-emoji:").should_not match(/<img/) + expect(gfm(":invalid-emoji:")).not_to match(/<img/) end it "should work independent of reference links (i.e. without @project being set)" do @project = nil - gfm(":+1:").should match(/<img/) + expect(gfm(":+1:")).to match(/<img/) end end end describe "#link_to_gfm" do - let(:commit_path) { project_commit_path(project, commit) } + let(:commit_path) { namespace_project_commit_path(project.namespace, project, commit) } let(:issues) { create_list(:issue, 2, project: project) } it "should handle references nested in links with all the text" do @@ -463,60 +517,67 @@ describe GitlabMarkdownHelper do groups = actual.split("</a>") # Leading commit link - groups[0].should match(/href="#{commit_path}"/) - groups[0].should match(/This should finally fix $/) + expect(groups[0]).to match(/href="#{commit_path}"/) + expect(groups[0]).to match(/This should finally fix $/) # First issue link - groups[1].should match(/href="#{project_issue_url(project, issues[0])}"/) - groups[1].should match(/##{issues[0].iid}$/) + expect(groups[1]). + to match(/href="#{namespace_project_issue_path(project.namespace, project, issues[0])}"/) + expect(groups[1]).to match(/##{issues[0].iid}$/) # Internal commit link - groups[2].should match(/href="#{commit_path}"/) - groups[2].should match(/ and /) + expect(groups[2]).to match(/href="#{commit_path}"/) + expect(groups[2]).to match(/ and /) # Second issue link - groups[3].should match(/href="#{project_issue_url(project, issues[1])}"/) - groups[3].should match(/##{issues[1].iid}$/) + expect(groups[3]). + to match(/href="#{namespace_project_issue_path(project.namespace, project, issues[1])}"/) + expect(groups[3]).to match(/##{issues[1].iid}$/) # Trailing commit link - groups[4].should match(/href="#{commit_path}"/) - groups[4].should match(/ for real$/) + expect(groups[4]).to match(/href="#{commit_path}"/) + expect(groups[4]).to match(/ for real$/) end it "should forward HTML options" do actual = link_to_gfm("Fixed in #{commit.id}", commit_path, class: 'foo') - actual.should have_selector 'a.gfm.gfm-commit.foo' + expect(actual).to have_selector 'a.gfm.gfm-commit.foo' end it "escapes HTML passed in as the body" do actual = "This is a <h1>test</h1> - see ##{issues[0].iid}" - link_to_gfm(actual, commit_path).should match('<h1>test</h1>') + expect(link_to_gfm(actual, commit_path)). + to match('<h1>test</h1>') end end describe "#markdown" do it "should handle references in paragraphs" do actual = "\n\nLorem ipsum dolor sit amet. #{commit.id} Nam pulvinar sapien eget.\n" - expected = project_commit_path(project, commit) - markdown(actual).should match(expected) + expected = namespace_project_commit_path(project.namespace, project, commit) + expect(markdown(actual)).to match(expected) end it "should handle references in headers" do actual = "\n# Working around ##{issue.iid}\n## Apply !#{merge_request.iid}" - markdown(actual, {no_header_anchors:true}).should match(%r{<h1[^<]*>Working around <a.+>##{issue.iid}</a></h1>}) - markdown(actual, {no_header_anchors:true}).should match(%r{<h2[^<]*>Apply <a.+>!#{merge_request.iid}</a></h2>}) + expect(markdown(actual, no_header_anchors: true)). + to match(%r{<h1[^<]*>Working around <a.+>##{issue.iid}</a></h1>}) + expect(markdown(actual, no_header_anchors: true)). + to match(%r{<h2[^<]*>Apply <a.+>!#{merge_request.iid}</a></h2>}) end it "should add ids and links to headers" do # Test every rule except nested tags. text = '..Ab_c-d. e..' id = 'ab_c-d-e' - markdown("# #{text}").should match(%r{<h1 id="#{id}">#{text}<a href="[^"]*##{id}"></a></h1>}) - markdown("# #{text}", {no_header_anchors:true}).should == "<h1>#{text}</h1>" + expect(markdown("# #{text}")). + to match(%r{<h1 id="#{id}">#{text}<a href="[^"]*##{id}"></a></h1>}) + expect(markdown("# #{text}", {no_header_anchors:true})). + to eq("<h1>#{text}</h1>") id = 'link-text' - markdown("# [link text](url) ![img alt](url)").should match( + expect(markdown("# [link text](url) ![img alt](url)")).to match( %r{<h1 id="#{id}"><a href="[^"]*url">link text</a> <img[^>]*><a href="[^"]*##{id}"></a></h1>} ) end @@ -526,14 +587,37 @@ describe GitlabMarkdownHelper do actual = "\n* dark: ##{issue.iid}\n* light by @#{member.user.username}" - markdown(actual).should match(%r{<li>dark: <a.+>##{issue.iid}</a></li>}) - markdown(actual).should match(%r{<li>light by <a.+>@#{member.user.username}</a></li>}) + expect(markdown(actual)). + to match(%r{<li>dark: <a.+>##{issue.iid}</a></li>}) + expect(markdown(actual)). + to match(%r{<li>light by <a.+>@#{member.user.username}</a></li>}) + end + + it "should not link the apostrophe to issue 39" do + project.team << [user, :master] + allow(project.issues). + to receive(:where).with(iid: '39').and_return([issue]) + + actual = "Yes, it is @#{member.user.username}'s task." + expected = /Yes, it is <a.+>@#{member.user.username}<\/a>'s task/ + expect(markdown(actual)).to match(expected) + end + + it "should not link the apostrophe to issue 39 in code blocks" do + project.team << [user, :master] + allow(project.issues). + to receive(:where).with(iid: '39').and_return([issue]) + + actual = "Yes, `it is @#{member.user.username}'s task.`" + expected = /Yes, <code>it is @gfm\'s task.<\/code>/ + expect(markdown(actual)).to match(expected) end it "should handle references in <em>" do actual = "Apply _!#{merge_request.iid}_ ASAP" - markdown(actual).should match(%r{Apply <em><a.+>!#{merge_request.iid}</a></em>}) + expect(markdown(actual)). + to match(%r{Apply <em><a.+>!#{merge_request.iid}</a></em>}) end it "should handle tables" do @@ -542,77 +626,106 @@ describe GitlabMarkdownHelper do | cell 1 | cell 2 | | cell 3 | cell 4 |} - markdown(actual).should match(/\A<table/) + expect(markdown(actual)).to match(/\A<table/) end it "should leave code blocks untouched" do - helper.stub(:user_color_scheme_class).and_return(:white) + allow(helper).to receive(:user_color_scheme_class).and_return(:white) - target_html = "\n<div class=\"highlighted-data white\">\n <div class=\"highlight\">\n <pre><code class=\"\">some code from $#{snippet.id}\nhere too\n</code></pre>\n </div>\n</div>\n\n" + target_html = "<pre class=\"code highlight white plaintext\"><code>some code from $#{snippet.id}\nhere too\n</code></pre>\n" - helper.markdown("\n some code from $#{snippet.id}\n here too\n").should == target_html - helper.markdown("\n```\nsome code from $#{snippet.id}\nhere too\n```\n").should == target_html + expect(helper.markdown("\n some code from $#{snippet.id}\n here too\n")). + to eq(target_html) + expect(helper.markdown("\n```\nsome code from $#{snippet.id}\nhere too\n```\n")). + to eq(target_html) end it "should leave inline code untouched" do - markdown("\nDon't use `$#{snippet.id}` here.\n").should == + expect(markdown("\nDon't use `$#{snippet.id}` here.\n")).to eq( "<p>Don't use <code>$#{snippet.id}</code> here.</p>\n" + ) end it "should leave ref-like autolinks untouched" do - markdown("look at http://example.tld/#!#{merge_request.iid}").should == "<p>look at <a href=\"http://example.tld/#!#{merge_request.iid}\">http://example.tld/#!#{merge_request.iid}</a></p>\n" + expect(markdown("look at http://example.tld/#!#{merge_request.iid}")).to eq("<p>look at <a href=\"http://example.tld/#!#{merge_request.iid}\">http://example.tld/#!#{merge_request.iid}</a></p>\n") end it "should leave ref-like href of 'manual' links untouched" do - markdown("why not [inspect !#{merge_request.iid}](http://example.tld/#!#{merge_request.iid})").should == "<p>why not <a href=\"http://example.tld/#!#{merge_request.iid}\">inspect </a><a class=\"gfm gfm-merge_request \" href=\"#{project_merge_request_url(project, merge_request)}\" title=\"Merge Request: #{merge_request.title}\">!#{merge_request.iid}</a><a href=\"http://example.tld/#!#{merge_request.iid}\"></a></p>\n" + expect(markdown("why not [inspect !#{merge_request.iid}](http://example.tld/#!#{merge_request.iid})")).to eq("<p>why not <a href=\"http://example.tld/#!#{merge_request.iid}\">inspect </a><a class=\"gfm gfm-merge_request \" href=\"#{namespace_project_merge_request_url(project.namespace, project, merge_request)}\" title=\"Merge Request: #{merge_request.title}\">!#{merge_request.iid}</a><a href=\"http://example.tld/#!#{merge_request.iid}\"></a></p>\n") end it "should leave ref-like src of images untouched" do - markdown("screen shot: ![some image](http://example.tld/#!#{merge_request.iid})").should == "<p>screen shot: <img src=\"http://example.tld/#!#{merge_request.iid}\" alt=\"some image\"></p>\n" + expect(markdown("screen shot: ![some image](http://example.tld/#!#{merge_request.iid})")).to eq("<p>screen shot: <img src=\"http://example.tld/#!#{merge_request.iid}\" alt=\"some image\"></p>\n") end it "should generate absolute urls for refs" do - markdown("##{issue.iid}").should include(project_issue_url(project, issue)) + expect(markdown("##{issue.iid}")).to include(namespace_project_issue_path(project.namespace, project, issue)) end it "should generate absolute urls for emoji" do - markdown(":smile:").should include("src=\"#{url_helper('emoji/smile')}") + expect(markdown(':smile:')).to( + include(%(src="#{Gitlab.config.gitlab.url}/assets/emoji/#{Emoji.emoji_filename('smile')}.png)) + ) + end + + it "should generate absolute urls for emoji if relative url is present" do + allow(Gitlab.config.gitlab).to receive(:url).and_return('http://localhost/gitlab/root') + expect(markdown(":smile:")).to include("src=\"http://localhost/gitlab/root/assets/emoji/#{Emoji.emoji_filename('smile')}.png") + end + + it "should generate absolute urls for emoji if asset_host is present" do + allow(Gitlab::Application.config).to receive(:asset_host).and_return("https://cdn.example.com") + ActionView::Base.any_instance.stub_chain(:config, :asset_host).and_return("https://cdn.example.com") + expect(markdown(":smile:")).to include("src=\"https://cdn.example.com/assets/emoji/#{Emoji.emoji_filename('smile')}.png") end + it "should handle relative urls for a file in master" do actual = "[GitLab API doc](doc/api/README.md)\n" expected = "<p><a href=\"/#{project.path_with_namespace}/blob/#{@ref}/doc/api/README.md\">GitLab API doc</a></p>\n" - markdown(actual).should match(expected) + expect(markdown(actual)).to match(expected) + end + + it "should handle relative urls for a file in master with an anchor" do + actual = "[GitLab API doc](doc/api/README.md#section)\n" + expected = "<p><a href=\"/#{project.path_with_namespace}/blob/#{@ref}/doc/api/README.md#section\">GitLab API doc</a></p>\n" + expect(markdown(actual)).to match(expected) + end + + it "should not handle relative urls for the current file with an anchor" do + actual = "[GitLab API doc](#section)\n" + expected = "<p><a href=\"#section\">GitLab API doc</a></p>\n" + expect(markdown(actual)).to match(expected) end it "should handle relative urls for a directory in master" do actual = "[GitLab API doc](doc/api)\n" expected = "<p><a href=\"/#{project.path_with_namespace}/tree/#{@ref}/doc/api\">GitLab API doc</a></p>\n" - markdown(actual).should match(expected) + expect(markdown(actual)).to match(expected) end it "should handle absolute urls" do actual = "[GitLab](https://www.gitlab.com)\n" expected = "<p><a href=\"https://www.gitlab.com\">GitLab</a></p>\n" - markdown(actual).should match(expected) + expect(markdown(actual)).to match(expected) end it "should handle relative urls in reference links for a file in master" do actual = "[GitLab API doc][GitLab readme]\n [GitLab readme]: doc/api/README.md\n" expected = "<p><a href=\"/#{project.path_with_namespace}/blob/#{@ref}/doc/api/README.md\">GitLab API doc</a></p>\n" - markdown(actual).should match(expected) + expect(markdown(actual)).to match(expected) end it "should handle relative urls in reference links for a directory in master" do actual = "[GitLab API doc directory][GitLab readmes]\n [GitLab readmes]: doc/api/\n" expected = "<p><a href=\"/#{project.path_with_namespace}/tree/#{@ref}/doc/api\">GitLab API doc directory</a></p>\n" - markdown(actual).should match(expected) + expect(markdown(actual)).to match(expected) end it "should not handle malformed relative urls in reference links for a file in master" do actual = "[GitLab readme]: doc/api/README.md\n" expected = "" - markdown(actual).should match(expected) + expect(markdown(actual)).to match(expected) end it 'should allow whitelisted HTML tags from the user' do @@ -655,29 +768,29 @@ describe GitlabMarkdownHelper do it "should not touch relative urls" do actual = "[GitLab API doc][GitLab readme]\n [GitLab readme]: doc/api/README.md\n" expected = "<p><a href=\"doc/api/README.md\">GitLab API doc</a></p>\n" - markdown(actual).should match(expected) + expect(markdown(actual)).to match(expected) end end describe "#render_wiki_content" do before do @wiki = double('WikiPage') - @wiki.stub(:content).and_return('wiki content') + allow(@wiki).to receive(:content).and_return('wiki content') end it "should use GitLab Flavored Markdown for markdown files" do - @wiki.stub(:format).and_return(:markdown) + allow(@wiki).to receive(:format).and_return(:markdown) - helper.should_receive(:markdown).with('wiki content') + expect(helper).to receive(:markdown).with('wiki content') helper.render_wiki_content(@wiki) end it "should use the Gollum renderer for all other file types" do - @wiki.stub(:format).and_return(:rdoc) + allow(@wiki).to receive(:format).and_return(:rdoc) formatted_content_stub = double('formatted_content') - formatted_content_stub.should_receive(:html_safe) - @wiki.stub(:formatted_content).and_return(formatted_content_stub) + expect(formatted_content_stub).to receive(:html_safe) + allow(@wiki).to receive(:formatted_content).and_return(formatted_content_stub) helper.render_wiki_content(@wiki) end |