From b07ecbb5e37530e7754dc267da4ded1d7d2d736d Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Sun, 19 Jul 2015 19:41:54 -0400 Subject: Simplify AutolinkFilter specs --- spec/features/markdown_spec.rb | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'spec/features/markdown_spec.rb') diff --git a/spec/features/markdown_spec.rb b/spec/features/markdown_spec.rb index b8199aa5e61..b8a846c7015 100644 --- a/spec/features/markdown_spec.rb +++ b/spec/features/markdown_spec.rb @@ -195,45 +195,41 @@ describe 'GitLab Markdown', feature: true do end describe 'AutolinkFilter' do - let(:list) { get_section('autolinkfilter').next_element } + def body + get_section('autolinkfilter').next_element + end - def item(index) - list.at_css("li:nth-child(#{index})") + # Override Capybara's `have_link` matcher to simplify our use case + def have_link(link) + super(link, href: link) end it 'autolinks http://' do - expect(item(1).children.first.name).to eq 'a' - expect(item(1).children.first['href']).to eq 'http://about.gitlab.com/' + expect(body).to have_link('http://about.gitlab.com/') end it 'autolinks https://' do - expect(item(2).children.first.name).to eq 'a' - expect(item(2).children.first['href']).to eq 'https://google.com/' + expect(body).to have_link('https://google.com/') end it 'autolinks ftp://' do - expect(item(3).children.first.name).to eq 'a' - expect(item(3).children.first['href']).to eq 'ftp://ftp.us.debian.org/debian/' + expect(body).to have_link('ftp://ftp.us.debian.org/debian/') end it 'autolinks smb://' do - expect(item(4).children.first.name).to eq 'a' - expect(item(4).children.first['href']).to eq 'smb://foo/bar/baz' + expect(body).to have_link('smb://foo/bar/baz') end it 'autolinks irc://' do - expect(item(5).children.first.name).to eq 'a' - expect(item(5).children.first['href']).to eq 'irc://irc.freenode.net/git' + expect(body).to have_link('irc://irc.freenode.net/git') end it 'autolinks short, invalid URLs' do - expect(item(6).children.first.name).to eq 'a' - expect(item(6).children.first['href']).to eq 'http://localhost:3000' + expect(body).to have_link('http://localhost:3000') end %w(code a kbd).each do |elem| it "ignores links inside '#{elem}' element" do - body = get_section('autolinkfilter') expect(body).not_to have_selector("#{elem} a") end end -- cgit v1.2.1 From 06478ef3ecba80625e5cff70cb3697f442e24cba Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Mon, 20 Jul 2015 22:22:17 -0400 Subject: Minor Markdown feature spec reorganization --- spec/features/markdown_spec.rb | 93 +++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 46 deletions(-) (limited to 'spec/features/markdown_spec.rb') diff --git a/spec/features/markdown_spec.rb b/spec/features/markdown_spec.rb index b8a846c7015..f1be4e49c8a 100644 --- a/spec/features/markdown_spec.rb +++ b/spec/features/markdown_spec.rb @@ -33,39 +33,31 @@ require 'erb' # See the MarkdownFeature class for setup details. describe 'GitLab Markdown', feature: true do - include ActionView::Helpers::TagHelper - include ActionView::Helpers::UrlHelper include Capybara::Node::Matchers include GitlabMarkdownHelper - # `markdown` calls these two methods - def current_user - @feat.user - end - - def user_color_scheme_class - :white - end - # Let's only parse this thing once before(:all) do @feat = MarkdownFeature.new - # `markdown` expects a `@project` variable + # `gfm_with_options` depends on a `@project` variable @project = @feat.project - @md = markdown(@feat.raw_markdown) - @doc = Nokogiri::HTML::DocumentFragment.parse(@md) + @html = markdown(@feat.raw_markdown) end after(:all) do @feat.teardown end + def doc + @doc ||= Nokogiri::HTML::DocumentFragment.parse(@html) + end + # Given a header ID, goes to that element's parent (the header itself), then # its next sibling element (the body). def get_section(id) - @doc.at_css("##{id}").parent.next_element + doc.at_css("##{id}").parent.next_element end # Sometimes it can be useful to see the parsed output of the Markdown document @@ -74,11 +66,11 @@ describe 'GitLab Markdown', feature: true do # # it 'writes to a file' do # File.open(Rails.root.join('tmp/capybara/markdown_spec.html'), 'w') do |file| - # file.puts @md + # file.puts @html # end # end - describe 'Markdown' do + describe 'Redcarpet extensions' do describe 'No Intra Emphasis' do it 'does not parse emphasis inside of words' do body = get_section('no-intra-emphasis') @@ -95,21 +87,21 @@ describe 'GitLab Markdown', feature: true do end it 'allows Markdown in tables' do - expect(@doc.at_css('td:contains("Baz")').children.to_html). + expect(doc.at_css('td:contains("Baz")').children.to_html). to eq 'Baz' end end describe 'Fenced Code Blocks' do it 'parses fenced code blocks' do - expect(@doc).to have_selector('pre.code.highlight.white.c') - expect(@doc).to have_selector('pre.code.highlight.white.python') + expect(doc).to have_selector('pre.code.highlight.white.c') + expect(doc).to have_selector('pre.code.highlight.white.python') end end describe 'Strikethrough' do it 'parses strikethroughs' do - expect(@doc).to have_selector(%{del:contains("and this text doesn't")}) + expect(doc).to have_selector(%{del:contains("and this text doesn't")}) end end @@ -125,28 +117,28 @@ describe 'GitLab Markdown', feature: true do describe 'HTML::Pipeline' do describe 'SanitizationFilter' do it 'uses a permissive whitelist' do - expect(@doc).to have_selector('b:contains("b tag")') - expect(@doc).to have_selector('em:contains("em tag")') - expect(@doc).to have_selector('code:contains("code tag")') - expect(@doc).to have_selector('kbd:contains("s")') - expect(@doc).to have_selector('strike:contains(Emoji)') - expect(@doc).to have_selector('img[src*="smile.png"]') - expect(@doc).to have_selector('br') - expect(@doc).to have_selector('hr') + expect(doc).to have_selector('b:contains("b tag")') + expect(doc).to have_selector('em:contains("em tag")') + expect(doc).to have_selector('code:contains("code tag")') + expect(doc).to have_selector('kbd:contains("s")') + expect(doc).to have_selector('strike:contains(Emoji)') + expect(doc).to have_selector('img[src*="smile.png"]') + expect(doc).to have_selector('br') + expect(doc).to have_selector('hr') end it 'permits span elements' do - expect(@doc).to have_selector('span:contains("span tag")') + expect(doc).to have_selector('span:contains("span tag")') end it 'permits table alignment' do - expect(@doc.at_css('th:contains("Header")')['style']).to eq 'text-align: center' - expect(@doc.at_css('th:contains("Row")')['style']).to eq 'text-align: right' - expect(@doc.at_css('th:contains("Example")')['style']).to eq 'text-align: left' + expect(doc.at_css('th:contains("Header")')['style']).to eq 'text-align: center' + expect(doc.at_css('th:contains("Row")')['style']).to eq 'text-align: right' + expect(doc.at_css('th:contains("Example")')['style']).to eq 'text-align: left' - expect(@doc.at_css('td:contains("Foo")')['style']).to eq 'text-align: center' - expect(@doc.at_css('td:contains("Bar")')['style']).to eq 'text-align: right' - expect(@doc.at_css('td:contains("Baz")')['style']).to eq 'text-align: left' + expect(doc.at_css('td:contains("Foo")')['style']).to eq 'text-align: center' + expect(doc.at_css('td:contains("Bar")')['style']).to eq 'text-align: right' + expect(doc.at_css('td:contains("Baz")')['style']).to eq 'text-align: left' end it 'removes `rel` attribute from links' do @@ -155,12 +147,12 @@ describe 'GitLab Markdown', feature: true do end it "removes `href` from `a` elements if it's fishy" do - expect(@doc).not_to have_selector('a[href*="javascript"]') + expect(doc).not_to have_selector('a[href*="javascript"]') end end describe 'Escaping' do - let(:table) { @doc.css('table').last.at_css('tbody') } + let(:table) { doc.css('table').last.at_css('tbody') } it 'escapes non-tag angle brackets' do expect(table.at_xpath('.//tr[1]/td[3]').inner_html).to eq '1 < 3 & 5' @@ -169,28 +161,28 @@ describe 'GitLab Markdown', feature: true do describe 'Edge Cases' do it 'allows markup inside link elements' do - expect(@doc.at_css('a[href="#link-emphasis"]').to_html). + expect(doc.at_css('a[href="#link-emphasis"]').to_html). to eq %{text} - expect(@doc.at_css('a[href="#link-strong"]').to_html). + expect(doc.at_css('a[href="#link-strong"]').to_html). to eq %{text} - expect(@doc.at_css('a[href="#link-code"]').to_html). + expect(doc.at_css('a[href="#link-code"]').to_html). to eq %{text} end end describe 'EmojiFilter' do it 'parses Emoji' do - expect(@doc).to have_selector('img.emoji', count: 10) + expect(doc).to have_selector('img.emoji', count: 10) end end describe 'TableOfContentsFilter' do it 'creates anchors inside header elements' do - expect(@doc).to have_selector('h1 a#gitlab-markdown') - expect(@doc).to have_selector('h2 a#markdown') - expect(@doc).to have_selector('h3 a#autolinkfilter') + expect(doc).to have_selector('h1 a#gitlab-markdown') + expect(doc).to have_selector('h2 a#markdown') + expect(doc).to have_selector('h3 a#autolinkfilter') end end @@ -249,7 +241,7 @@ describe 'GitLab Markdown', feature: true do describe 'ReferenceFilter' do it 'handles references in headers' do - header = @doc.at_css('#reference-filters-eg-1').parent + header = doc.at_css('#reference-filters-eg-1').parent expect(header.css('a').size).to eq 2 end @@ -304,6 +296,15 @@ describe 'GitLab Markdown', feature: true do end end end + + # `markdown` calls these two methods + def current_user + @feat.user + end + + def user_color_scheme_class + :white + end end # This is a helper class used by the GitLab Markdown feature spec -- cgit v1.2.1 From 3cafa74387d707dac37d0e81bf2bb194e94957e4 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Mon, 20 Jul 2015 22:31:05 -0400 Subject: Use aggregate_failures where appropriate --- spec/features/markdown_spec.rb | 87 +++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 34 deletions(-) (limited to 'spec/features/markdown_spec.rb') diff --git a/spec/features/markdown_spec.rb b/spec/features/markdown_spec.rb index f1be4e49c8a..dadb1c3589f 100644 --- a/spec/features/markdown_spec.rb +++ b/spec/features/markdown_spec.rb @@ -81,9 +81,12 @@ describe 'GitLab Markdown', feature: true do describe 'Tables' do it 'parses table Markdown' do body = get_section('tables') - expect(body).to have_selector('th:contains("Header")') - expect(body).to have_selector('th:contains("Row")') - expect(body).to have_selector('th:contains("Example")') + + aggregate_failures do + expect(body).to have_selector('th:contains("Header")') + expect(body).to have_selector('th:contains("Row")') + expect(body).to have_selector('th:contains("Example")') + end end it 'allows Markdown in tables' do @@ -94,8 +97,10 @@ describe 'GitLab Markdown', feature: true do describe 'Fenced Code Blocks' do it 'parses fenced code blocks' do - expect(doc).to have_selector('pre.code.highlight.white.c') - expect(doc).to have_selector('pre.code.highlight.white.python') + aggregate_failures do + expect(doc).to have_selector('pre.code.highlight.white.c') + expect(doc).to have_selector('pre.code.highlight.white.python') + end end end @@ -108,8 +113,11 @@ describe 'GitLab Markdown', feature: true do describe 'Superscript' do it 'parses superscript' do body = get_section('superscript') - expect(body.to_html).to match('1st') - expect(body.to_html).to match('2nd') + + aggregate_failures do + expect(body.to_html).to match('1st') + expect(body.to_html).to match('2nd') + end end end end @@ -117,14 +125,16 @@ describe 'GitLab Markdown', feature: true do describe 'HTML::Pipeline' do describe 'SanitizationFilter' do it 'uses a permissive whitelist' do - expect(doc).to have_selector('b:contains("b tag")') - expect(doc).to have_selector('em:contains("em tag")') - expect(doc).to have_selector('code:contains("code tag")') - expect(doc).to have_selector('kbd:contains("s")') - expect(doc).to have_selector('strike:contains(Emoji)') - expect(doc).to have_selector('img[src*="smile.png"]') - expect(doc).to have_selector('br') - expect(doc).to have_selector('hr') + aggregate_failures do + expect(doc).to have_selector('b:contains("b tag")') + expect(doc).to have_selector('em:contains("em tag")') + expect(doc).to have_selector('code:contains("code tag")') + expect(doc).to have_selector('kbd:contains("s")') + expect(doc).to have_selector('strike:contains(Emoji)') + expect(doc).to have_selector('img[src*="smile.png"]') + expect(doc).to have_selector('br') + expect(doc).to have_selector('hr') + end end it 'permits span elements' do @@ -132,13 +142,15 @@ describe 'GitLab Markdown', feature: true do end it 'permits table alignment' do - expect(doc.at_css('th:contains("Header")')['style']).to eq 'text-align: center' - expect(doc.at_css('th:contains("Row")')['style']).to eq 'text-align: right' - expect(doc.at_css('th:contains("Example")')['style']).to eq 'text-align: left' - - expect(doc.at_css('td:contains("Foo")')['style']).to eq 'text-align: center' - expect(doc.at_css('td:contains("Bar")')['style']).to eq 'text-align: right' - expect(doc.at_css('td:contains("Baz")')['style']).to eq 'text-align: left' + aggregate_failures do + expect(doc.at_css('th:contains("Header")')['style']).to eq 'text-align: center' + expect(doc.at_css('th:contains("Row")')['style']).to eq 'text-align: right' + expect(doc.at_css('th:contains("Example")')['style']).to eq 'text-align: left' + + expect(doc.at_css('td:contains("Foo")')['style']).to eq 'text-align: center' + expect(doc.at_css('td:contains("Bar")')['style']).to eq 'text-align: right' + expect(doc.at_css('td:contains("Baz")')['style']).to eq 'text-align: left' + end end it 'removes `rel` attribute from links' do @@ -161,14 +173,16 @@ describe 'GitLab Markdown', feature: true do describe 'Edge Cases' do it 'allows markup inside link elements' do - expect(doc.at_css('a[href="#link-emphasis"]').to_html). - to eq %{text} + aggregate_failures do + expect(doc.at_css('a[href="#link-emphasis"]').to_html). + to eq %{text} - expect(doc.at_css('a[href="#link-strong"]').to_html). - to eq %{text} + expect(doc.at_css('a[href="#link-strong"]').to_html). + to eq %{text} - expect(doc.at_css('a[href="#link-code"]').to_html). - to eq %{text} + expect(doc.at_css('a[href="#link-code"]').to_html). + to eq %{text} + end end end @@ -180,9 +194,11 @@ describe 'GitLab Markdown', feature: true do describe 'TableOfContentsFilter' do it 'creates anchors inside header elements' do - expect(doc).to have_selector('h1 a#gitlab-markdown') - expect(doc).to have_selector('h2 a#markdown') - expect(doc).to have_selector('h3 a#autolinkfilter') + aggregate_failures do + expect(doc).to have_selector('h1 a#gitlab-markdown') + expect(doc).to have_selector('h2 a#markdown') + expect(doc).to have_selector('h3 a#autolinkfilter') + end end end @@ -290,9 +306,12 @@ describe 'GitLab Markdown', feature: true do describe 'Task Lists' do it 'generates task lists' do body = get_section('task-lists') - expect(body).to have_selector('ul.task-list', count: 2) - expect(body).to have_selector('li.task-list-item', count: 7) - expect(body).to have_selector('input[checked]', count: 3) + + aggregate_failures do + expect(body).to have_selector('ul.task-list', count: 2) + expect(body).to have_selector('li.task-list-item', count: 7) + expect(body).to have_selector('input[checked]', count: 3) + end end end end -- cgit v1.2.1 From 97cedc5d1b023af56c035cccc5914d11bf6299de Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Mon, 20 Jul 2015 23:57:26 -0400 Subject: Break up SanitizationFilter feature specs --- spec/features/markdown_spec.rb | 53 +++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 16 deletions(-) (limited to 'spec/features/markdown_spec.rb') diff --git a/spec/features/markdown_spec.rb b/spec/features/markdown_spec.rb index dadb1c3589f..5adf19980dd 100644 --- a/spec/features/markdown_spec.rb +++ b/spec/features/markdown_spec.rb @@ -124,29 +124,52 @@ describe 'GitLab Markdown', feature: true do describe 'HTML::Pipeline' do describe 'SanitizationFilter' do - it 'uses a permissive whitelist' do - aggregate_failures do - expect(doc).to have_selector('b:contains("b tag")') - expect(doc).to have_selector('em:contains("em tag")') - expect(doc).to have_selector('code:contains("code tag")') - expect(doc).to have_selector('kbd:contains("s")') - expect(doc).to have_selector('strike:contains(Emoji)') - expect(doc).to have_selector('img[src*="smile.png"]') - expect(doc).to have_selector('br') - expect(doc).to have_selector('hr') - end + it 'permits b elements' do + expect(doc).to have_selector('b:contains("b tag")') + end + + it 'permits em elements' do + expect(doc).to have_selector('em:contains("em tag")') + end + + it 'permits code elements' do + expect(doc).to have_selector('code:contains("code tag")') + end + + it 'permits kbd elements' do + expect(doc).to have_selector('kbd:contains("s")') + end + + it 'permits strike elements' do + expect(doc).to have_selector('strike:contains(Emoji)') + end + + it 'permits img elements' do + expect(doc).to have_selector('img[src*="smile.png"]') + end + + it 'permits br elements' do + expect(doc).to have_selector('br') + end + + it 'permits hr elements' do + expect(doc).to have_selector('hr') end it 'permits span elements' do expect(doc).to have_selector('span:contains("span tag")') end - it 'permits table alignment' do + it 'permits style attribute in th elements' do aggregate_failures do expect(doc.at_css('th:contains("Header")')['style']).to eq 'text-align: center' expect(doc.at_css('th:contains("Row")')['style']).to eq 'text-align: right' expect(doc.at_css('th:contains("Example")')['style']).to eq 'text-align: left' + end + end + it 'permits style attribute in td elements' do + aggregate_failures do expect(doc.at_css('td:contains("Foo")')['style']).to eq 'text-align: center' expect(doc.at_css('td:contains("Bar")')['style']).to eq 'text-align: right' expect(doc.at_css('td:contains("Baz")')['style']).to eq 'text-align: left' @@ -154,8 +177,7 @@ describe 'GitLab Markdown', feature: true do end it 'removes `rel` attribute from links' do - body = get_section('sanitizationfilter') - expect(body).not_to have_selector('a[rel="bookmark"]') + expect(doc).not_to have_selector('a[rel="bookmark"]') end it "removes `href` from `a` elements if it's fishy" do @@ -164,9 +186,8 @@ describe 'GitLab Markdown', feature: true do end describe 'Escaping' do - let(:table) { doc.css('table').last.at_css('tbody') } - it 'escapes non-tag angle brackets' do + table = doc.css('table').last.at_css('tbody') expect(table.at_xpath('.//tr[1]/td[3]').inner_html).to eq '1 < 3 & 5' end end -- cgit v1.2.1 From 590fca0f06c4a21dbac90480bc6622f4da97dc86 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Tue, 21 Jul 2015 00:23:27 -0400 Subject: Move MarkdownFeature to its own support file This file's about to get much bigger and this removes some of the extra noise. --- spec/features/markdown_spec.rb | 115 ----------------------------------------- 1 file changed, 115 deletions(-) (limited to 'spec/features/markdown_spec.rb') diff --git a/spec/features/markdown_spec.rb b/spec/features/markdown_spec.rb index 5adf19980dd..46814fdfb8e 100644 --- a/spec/features/markdown_spec.rb +++ b/spec/features/markdown_spec.rb @@ -346,118 +346,3 @@ describe 'GitLab Markdown', feature: true do :white end end - -# This is a helper class used by the GitLab Markdown feature spec -# -# Because the feature spec only cares about the output of the Markdown, and the -# test setup and teardown and parsing is fairly expensive, we only want to do it -# once. Unfortunately RSpec will not let you access `let`s in a `before(:all)` -# block, so we fake it by encapsulating all the shared setup in this class. -# -# The class renders `spec/fixtures/markdown.md.erb` using ERB, allowing for -# reference to the factory-created objects. -class MarkdownFeature - include FactoryGirl::Syntax::Methods - - def initialize - DatabaseCleaner.start - end - - def teardown - DatabaseCleaner.clean - end - - def user - @user ||= create(:user) - end - - def group - unless @group - @group = create(:group) - @group.add_user(user, Gitlab::Access::DEVELOPER) - end - - @group - end - - # Direct references ---------------------------------------------------------- - - def project - @project ||= create(:project) - end - - def issue - @issue ||= create(:issue, project: project) - end - - def merge_request - @merge_request ||= create(:merge_request, :simple, source_project: project) - end - - def snippet - @snippet ||= create(:project_snippet, project: project) - end - - def commit - @commit ||= project.commit - end - - def commit_range - unless @commit_range - commit2 = project.commit('HEAD~3') - @commit_range = CommitRange.new("#{commit.id}...#{commit2.id}", project) - end - - @commit_range - end - - def simple_label - @simple_label ||= create(:label, name: 'gfm', project: project) - end - - def label - @label ||= create(:label, name: 'awaiting feedback', project: project) - end - - # Cross-references ----------------------------------------------------------- - - def xproject - unless @xproject - namespace = create(:namespace, name: 'cross-reference') - @xproject = create(:project, namespace: namespace) - @xproject.team << [user, :developer] - end - - @xproject - end - - def xissue - @xissue ||= create(:issue, project: xproject) - end - - def xmerge_request - @xmerge_request ||= create(:merge_request, :simple, source_project: xproject) - end - - def xsnippet - @xsnippet ||= create(:project_snippet, project: xproject) - end - - def xcommit - @xcommit ||= xproject.commit - end - - def xcommit_range - unless @xcommit_range - xcommit2 = xproject.commit('HEAD~2') - @xcommit_range = CommitRange.new("#{xcommit.id}...#{xcommit2.id}", xproject) - end - - @xcommit_range - end - - def raw_markdown - fixture = Rails.root.join('spec/fixtures/markdown.md.erb') - ERB.new(File.read(fixture)).result(binding) - end -end -- cgit v1.2.1 From 05f9a6a9c44c094d281d3dc8d80eb30c4e7dff27 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Wed, 22 Jul 2015 21:22:57 -0400 Subject: Update Markdown feature to allow for multiple pipelines --- spec/features/markdown_spec.rb | 216 ++++++++++------------------------------- 1 file changed, 53 insertions(+), 163 deletions(-) (limited to 'spec/features/markdown_spec.rb') diff --git a/spec/features/markdown_spec.rb b/spec/features/markdown_spec.rb index 46814fdfb8e..ddef2317e1c 100644 --- a/spec/features/markdown_spec.rb +++ b/spec/features/markdown_spec.rb @@ -17,16 +17,8 @@ require 'erb' # -> Post-process HTML # -> `gfm_with_options` helper # -> HTML::Pipeline -# -> Sanitize -# -> RelativeLink -# -> Emoji -# -> Table of Contents -# -> Autolinks -# -> Rinku (http, https, ftp) -# -> Other schemes -# -> ExternalLink -# -> References -# -> TaskList +# -> SanitizationFilter +# -> Other filters, depending on pipeline # -> `html_safe` # -> Template # @@ -35,6 +27,7 @@ require 'erb' describe 'GitLab Markdown', feature: true do include Capybara::Node::Matchers include GitlabMarkdownHelper + include MarkdownMatchers # Let's only parse this thing once before(:all) do @@ -42,50 +35,37 @@ describe 'GitLab Markdown', feature: true do # `gfm_with_options` depends on a `@project` variable @project = @feat.project - - @html = markdown(@feat.raw_markdown) end after(:all) do @feat.teardown end - def doc - @doc ||= Nokogiri::HTML::DocumentFragment.parse(@html) + def doc(html = @html) + Nokogiri::HTML::DocumentFragment.parse(html) end - # Given a header ID, goes to that element's parent (the header itself), then - # its next sibling element (the body). - def get_section(id) - doc.at_css("##{id}").parent.next_element + # Sometimes it can be useful to see the parsed output of the Markdown document + # for debugging. Call this method to write the output to + # `tmp/capybara/.html`. + def write_markdown(filename = 'markdown_spec') + File.open(Rails.root.join("tmp/capybara/#{filename}.html"), 'w') do |file| + file.puts @html + end end - # Sometimes it can be useful to see the parsed output of the Markdown document - # for debugging. Uncomment this block to write the output to - # tmp/capybara/markdown_spec.html. - # - # it 'writes to a file' do - # File.open(Rails.root.join('tmp/capybara/markdown_spec.html'), 'w') do |file| - # file.puts @html - # end - # end - - describe 'Redcarpet extensions' do - describe 'No Intra Emphasis' do + # Shared behavior that all pipelines should exhibit + shared_examples 'all pipelines' do + describe 'Redcarpet extensions' do it 'does not parse emphasis inside of words' do - body = get_section('no-intra-emphasis') - expect(body.to_html).not_to match('foobarbaz') + expect(doc.to_html).not_to match('foobarbaz') end - end - describe 'Tables' do it 'parses table Markdown' do - body = get_section('tables') - aggregate_failures do - expect(body).to have_selector('th:contains("Header")') - expect(body).to have_selector('th:contains("Row")') - expect(body).to have_selector('th:contains("Example")') + expect(doc).to have_selector('th:contains("Header")') + expect(doc).to have_selector('th:contains("Row")') + expect(doc).to have_selector('th:contains("Example")') end end @@ -93,36 +73,23 @@ describe 'GitLab Markdown', feature: true do expect(doc.at_css('td:contains("Baz")').children.to_html). to eq 'Baz' end - end - describe 'Fenced Code Blocks' do it 'parses fenced code blocks' do aggregate_failures do expect(doc).to have_selector('pre.code.highlight.white.c') expect(doc).to have_selector('pre.code.highlight.white.python') end end - end - describe 'Strikethrough' do it 'parses strikethroughs' do expect(doc).to have_selector(%{del:contains("and this text doesn't")}) end - end - describe 'Superscript' do it 'parses superscript' do - body = get_section('superscript') - - aggregate_failures do - expect(body.to_html).to match('1st') - expect(body.to_html).to match('2nd') - end + expect(doc).to have_selector('sup', count: 2) end end - end - describe 'HTML::Pipeline' do describe 'SanitizationFilter' do it 'permits b elements' do expect(doc).to have_selector('b:contains("b tag")') @@ -207,133 +174,56 @@ describe 'GitLab Markdown', feature: true do end end - describe 'EmojiFilter' do - it 'parses Emoji' do - expect(doc).to have_selector('img.emoji', count: 10) - end - end - - describe 'TableOfContentsFilter' do - it 'creates anchors inside header elements' do - aggregate_failures do - expect(doc).to have_selector('h1 a#gitlab-markdown') - expect(doc).to have_selector('h2 a#markdown') - expect(doc).to have_selector('h3 a#autolinkfilter') - end - end - end - - describe 'AutolinkFilter' do - def body - get_section('autolinkfilter').next_element - end - - # Override Capybara's `have_link` matcher to simplify our use case - def have_link(link) - super(link, href: link) - end - - it 'autolinks http://' do - expect(body).to have_link('http://about.gitlab.com/') - end - - it 'autolinks https://' do - expect(body).to have_link('https://google.com/') - end - - it 'autolinks ftp://' do - expect(body).to have_link('ftp://ftp.us.debian.org/debian/') - end - - it 'autolinks smb://' do - expect(body).to have_link('smb://foo/bar/baz') - end - - it 'autolinks irc://' do - expect(body).to have_link('irc://irc.freenode.net/git') - end - - it 'autolinks short, invalid URLs' do - expect(body).to have_link('http://localhost:3000') - end - - %w(code a kbd).each do |elem| - it "ignores links inside '#{elem}' element" do - expect(body).not_to have_selector("#{elem} a") - end - end - end - describe 'ExternalLinkFilter' do - let(:links) { get_section('externallinkfilter').next_element } - it 'adds nofollow to external link' do - expect(links.css('a').first.to_html).to match 'nofollow' + link = doc.at_css('a:contains("Google")') + expect(link.attr('rel')).to match 'nofollow' end it 'ignores internal link' do - expect(links.css('a').last.to_html).not_to match 'nofollow' + link = doc.at_css('a:contains("GitLab Root")') + expect(link.attr('rel')).not_to match 'nofollow' end end + end - describe 'ReferenceFilter' do - it 'handles references in headers' do - header = doc.at_css('#reference-filters-eg-1').parent - - expect(header.css('a').size).to eq 2 - end - - it "handles references in Markdown" do - body = get_section('reference-filters-eg-1') - expect(body).to have_selector('em a.gfm-merge_request', count: 1) - end - - it 'parses user references' do - body = get_section('userreferencefilter') - expect(body).to have_selector('a.gfm.gfm-project_member', count: 3) - end + context 'default pipeline' do + before(:all) do + @html = markdown(@feat.raw_markdown) + end - it 'parses issue references' do - body = get_section('issuereferencefilter') - expect(body).to have_selector('a.gfm.gfm-issue', count: 2) - end + it_behaves_like 'all pipelines' - it 'parses merge request references' do - body = get_section('mergerequestreferencefilter') - expect(body).to have_selector('a.gfm.gfm-merge_request', count: 2) - end + it 'includes RelativeLinkFilter' do + expect(doc).to parse_relative_links + end - it 'parses snippet references' do - body = get_section('snippetreferencefilter') - expect(body).to have_selector('a.gfm.gfm-snippet', count: 2) - end + it 'includes EmojiFilter' do + expect(doc).to parse_emoji + end - it 'parses commit range references' do - body = get_section('commitrangereferencefilter') - expect(body).to have_selector('a.gfm.gfm-commit_range', count: 2) - end + it 'includes TableOfContentsFilter' do + expect(doc).to create_header_links + end - it 'parses commit references' do - body = get_section('commitreferencefilter') - expect(body).to have_selector('a.gfm.gfm-commit', count: 2) - end + it 'includes AutolinkFilter' do + expect(doc).to create_autolinks + end - it 'parses label references' do - body = get_section('labelreferencefilter') - expect(body).to have_selector('a.gfm.gfm-label', count: 3) + it 'includes all reference filters' do + aggregate_failures do + expect(doc).to reference_users + expect(doc).to reference_issues + expect(doc).to reference_merge_requests + expect(doc).to reference_snippets + expect(doc).to reference_commit_ranges + expect(doc).to reference_commits + expect(doc).to reference_labels end end - describe 'Task Lists' do - it 'generates task lists' do - body = get_section('task-lists') - - aggregate_failures do - expect(body).to have_selector('ul.task-list', count: 2) - expect(body).to have_selector('li.task-list-item', count: 7) - expect(body).to have_selector('input[checked]', count: 3) - end - end + it 'includes TaskListFilter' do + expect(doc).to parse_task_lists end end -- cgit v1.2.1 From 8c957b54f5a70d07a0c70f15c84cdf9fc7eb0f23 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Tue, 28 Jul 2015 23:27:08 -0400 Subject: Fix setup/teardown for Markdown feature spec Prior, CI seemed to be freezing after running these specs. --- spec/features/markdown_spec.rb | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) (limited to 'spec/features/markdown_spec.rb') diff --git a/spec/features/markdown_spec.rb b/spec/features/markdown_spec.rb index ddef2317e1c..859a62f740f 100644 --- a/spec/features/markdown_spec.rb +++ b/spec/features/markdown_spec.rb @@ -29,22 +29,6 @@ describe 'GitLab Markdown', feature: true do include GitlabMarkdownHelper include MarkdownMatchers - # Let's only parse this thing once - before(:all) do - @feat = MarkdownFeature.new - - # `gfm_with_options` depends on a `@project` variable - @project = @feat.project - end - - after(:all) do - @feat.teardown - end - - def doc(html = @html) - Nokogiri::HTML::DocumentFragment.parse(html) - end - # Sometimes it can be useful to see the parsed output of the Markdown document # for debugging. Call this method to write the output to # `tmp/capybara/.html`. @@ -54,6 +38,10 @@ describe 'GitLab Markdown', feature: true do end end + def doc(html = @html) + Nokogiri::HTML::DocumentFragment.parse(html) + end + # Shared behavior that all pipelines should exhibit shared_examples 'all pipelines' do describe 'Redcarpet extensions' do @@ -189,6 +177,11 @@ describe 'GitLab Markdown', feature: true do context 'default pipeline' do before(:all) do + @feat = MarkdownFeature.new + + # `gfm_with_options` depends on a `@project` variable + @project = @feat.project + @html = markdown(@feat.raw_markdown) end -- cgit v1.2.1