require 'spec_helper' describe GitlabMarkdownHelper do include ApplicationHelper let!(:project) { create(:project) } let(:user) { create(:user, username: 'gfm') } let(:commit) { project.commit } 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) } # Helper expects a current_user method. let(:current_user) { user } before do # Helper expects a @project instance variable @project = project end describe "#gfm" do it "should forward HTML options to links" do expect(gfm("Fixed in #{commit.id}", @project, class: 'foo')). to have_selector('a.gfm.foo') end describe "referencing multiple objects" do let(:actual) { "!#{merge_request.iid} -> #{commit.id} -> ##{issue.iid}" } it "should link to the merge request" do 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 = namespace_project_commit_path(project.namespace, project, commit) expect(gfm(actual)).to match(expected) end it "should link to the issue" do expected = namespace_project_issue_path(project.namespace, project, issue) expect(gfm(actual)).to match(expected) end end context 'parse_tasks: true' do before(:all) do @source_text_asterisk = <<-EOT.strip_heredoc * [ ] valid unchecked task * [x] valid lowercase checked task * [X] valid uppercase checked task * [ ] valid unchecked nested task * [x] valid checked nested task [ ] not an unchecked task - no list item [x] not a checked task - no list item * [ ] not an unchecked task - too many spaces * [x ] not a checked task - too many spaces * [] not an unchecked task - no spaces * Not a task [ ] - not at beginning EOT @source_text_dash = <<-EOT.strip_heredoc - [ ] valid unchecked task - [x] valid lowercase checked task - [X] valid uppercase checked task - [ ] valid unchecked nested task - [x] valid checked nested task EOT end it 'should render checkboxes at beginning of asterisk list items' do rendered_text = markdown(@source_text_asterisk, parse_tasks: true) expect(rendered_text).to match(/Working around ##{issue.iid}}) expect(markdown(actual, no_header_anchors: true)). to match(%r{Apply !#{merge_request.iid}}) end it "should handle references in " do actual = "Apply _!#{merge_request.iid}_ ASAP" expect(markdown(actual)). to match(%r{Apply !#{merge_request.iid}}) end # CODE BLOCKS ------------------------------------------------------------- it "should leave code blocks untouched" do allow(helper).to receive(:current_user).and_return(user) allow(helper).to receive(:user_color_scheme_class).and_return(:white) target_html = "
some code from $#{snippet.id}\nhere too\n
\n" expect(markdown("\n some code from $#{snippet.id}\n here too\n")). to eq(target_html) expect(markdown("\n```\nsome code from $#{snippet.id}\nhere too\n```\n")). to eq(target_html) end it "should leave inline code untouched" do expect(markdown("Don't use `$#{snippet.id}` here.")). to eq "

Don't use $#{snippet.id} here.

\n" end # REF-LIKE AUTOLINKS? ----------------------------------------------------- # Basically: Don't parse references inside `` tags. it "should leave ref-like autolinks untouched" do expect(markdown("look at http://example.tld/#!#{merge_request.iid}")).to eq("

look at http://example.tld/#!#{merge_request.iid}

\n") end it "should leave ref-like href of 'manual' links untouched" do expect(markdown("why not [inspect !#{merge_request.iid}](http://example.tld/#!#{merge_request.iid})")).to eq("

why not inspect !#{merge_request.iid}

\n") end it "should leave ref-like src of images untouched" do expect(markdown("screen shot: ![some image](http://example.tld/#!#{merge_request.iid})")).to eq("

screen shot: \"some

\n") end # RELATIVE URLS ----------------------------------------------------------- # TODO (rspeicher): These belong in a relative link filter spec context 'relative links' do context 'with a valid repository' do before do @repository = project.repository @ref = 'markdown' end it "should handle relative urls for a file in master" do actual = "[GitLab API doc](doc/api/README.md)\n" expected = "

GitLab API doc

\n" 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 = "

GitLab API doc

\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 = "

GitLab API doc

\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 = "

GitLab API doc

\n" expect(markdown(actual)).to match(expected) end it "should handle absolute urls" do actual = "[GitLab](https://www.gitlab.com)\n" expected = "

GitLab

\n" 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 = "

GitLab API doc

\n" 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 = "

GitLab API doc directory

\n" 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 = "" expect(markdown(actual)).to match(expected) end end context 'with an empty repository' do before do @project = create(:empty_project) @repository = @project.repository end it "should not touch relative urls" do actual = "[GitLab API doc][GitLab readme]\n [GitLab readme]: doc/api/README.md\n" expected = "

GitLab API doc

\n" expect(markdown(actual)).to match(expected) end end end end describe '#render_wiki_content' do before do @wiki = double('WikiPage') allow(@wiki).to receive(:content).and_return('wiki content') end it "should use GitLab Flavored Markdown for markdown files" do allow(@wiki).to receive(:format).and_return(:markdown) 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 allow(@wiki).to receive(:format).and_return(:rdoc) formatted_content_stub = double('formatted_content') 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 end end