Skip to content

Commit

Permalink
[WIP] Create test for dead links
Browse files Browse the repository at this point in the history
  • Loading branch information
yui-knk committed Feb 28, 2015
1 parent 35b338b commit 618bfb5
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--require spec_helper
--require turnip/rspec
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ gem 'nokogiri'
# Monitoring tools
gem 'newrelic_rpm'

# for test
gem 'capybara'
gem 'rack'
gem 'rspec'
gem 'turnip'
33 changes: 33 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ GEM
tzinfo (~> 1.1)
blankslate (2.1.2.4)
builder (3.2.2)
capybara (2.4.4)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
celluloid (0.16.0)
timers (~> 4.0.0)
classifier-reborn (2.0.1)
Expand All @@ -35,10 +41,13 @@ GEM
execjs
coffee-script-source (1.8.0)
colorator (0.1)
diff-lcs (1.2.5)
erubis (2.7.0)
execjs (2.2.1)
fast-stemmer (1.0.2)
ffi (1.9.3)
gherkin (2.12.2)
multi_json (~> 1.3)
hitimes (1.2.2)
i18n (0.6.11)
jekyll (2.4.0)
Expand Down Expand Up @@ -73,8 +82,10 @@ GEM
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
mercenary (0.3.4)
mime-types (2.4.3)
mini_portile (0.6.0)
minitest (5.4.1)
multi_json (1.10.1)
newrelic_rpm (3.9.5.251)
nokogiri (1.6.3.1)
mini_portile (= 0.6.0)
Expand All @@ -93,19 +104,37 @@ GEM
rb-inotify (0.9.5)
ffi (>= 0.5.0)
redcarpet (3.1.2)
rspec (3.2.0)
rspec-core (~> 3.2.0)
rspec-expectations (~> 3.2.0)
rspec-mocks (~> 3.2.0)
rspec-core (3.2.1)
rspec-support (~> 3.2.0)
rspec-expectations (3.2.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.2.0)
rspec-mocks (3.2.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.2.0)
rspec-support (3.2.2)
safe_yaml (1.0.3)
sass (3.4.4)
thread_safe (0.3.4)
timers (4.0.1)
hitimes
toml (0.1.1)
parslet (~> 1.5.0)
turnip (1.2.4)
gherkin (>= 2.5)
rspec (>= 2.14.0, < 4.0)
tzinfo (1.2.2)
thread_safe (~> 0.1)
unicorn (4.8.3)
kgio (~> 2.6)
rack
raindrops (~> 0.7)
xpath (2.0.0)
nokogiri (~> 1.3)
yajl-ruby (1.1.0)

PLATFORMS
Expand All @@ -115,9 +144,13 @@ DEPENDENCIES
actionpack
activesupport
bundler
capybara
jekyll
newrelic_rpm
nokogiri
rack
rack-jekyll!
rake
rspec
turnip
unicorn
10 changes: 10 additions & 0 deletions spec/features/no-dead-link.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# language: ja

フィーチャ: あるバージョンを読んでる最中、突然他のバージョンに移動することはないし、本の中のリンクはしなない
シナリオ: トップページにアクセスしてログインする
前提 "asset_pipeline" を表示する
ならば 表示されている内部リンクは、すべて有効である

シナリオ: トップページにアクセスしてログインする
前提 "4_1_release_notes" を表示する
ならば 表示されている内部リンクは、すべて有効である
13 changes: 13 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'rack/file'
require 'capybara/rspec'
require 'turnip'
require 'turnip/capybara'

Dir.glob("spec/**/*steps.rb") { |f| load f, true }
Dir.glob("spec/support/**/*.rb") { |f| load f, true }

RSpec.configure do |config|
config.color = true
end

Capybara.app = Rack::File.new(File.expand_path('../../guides/output', __FILE__))
18 changes: 18 additions & 0 deletions spec/steps/dead_link_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
step '表示されている内部リンクは、すべて有効である' do
_, internal_links = all('a').partition {|link| link[:href].start_with? 'http' }
fragment_links = internal_links.select {|link| link[:href] =~ /#/ }

# ["#active-record-enums"], ["/asset_pipeline.html#css%E3%81%A8erb"]
same_pages, other_pages = fragment_links.partition {|link| link[:href] =~ /\A#/ }

same_pages.each do |link|
expect(link).to_not be_dead_fragment(page)
end

other_pages.each do |link|
file, _ = link[:href].split("#", 2)
original_page_path = page.current_path
visit file
expect(link).to_not be_dead_fragment(page, original_page_path)
end
end
4 changes: 4 additions & 0 deletions spec/steps/visit_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
step ":file を表示する" do |file|
# visit '/4_1_release_notes.html'
visit "#{file}.html"
end
33 changes: 33 additions & 0 deletions spec/support/matchers/be_dead_link.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'uri'

# cf: https://gist.github.com/hanachin/9968957
RSpec::Matchers.define :be_dead_fragment do |page, original_page_path=nil|
define_method :id_from_link do |link|
_, fragment = link[:href].split("#", 2)
id = URI.unescape(fragment)
end

match do |link|
begin
!page.find_by_id(id_from_link(link))
rescue
true
end
end

failure_message_when_negated do |link|
if original_page_path
"Link(id: #{id_from_link(link)}, text: #{link.text}, original_page_path: #{original_page_path}) is dead fragment"
else
"Link(id: #{id_from_link(link)}, text: #{link.text}) is dead fragment"
end
end

failure_message do |link|
if original_page_path
"Link(id: #{id_from_link(link)}, text: #{link.text}, original_page_path: #{original_page_path}) is not dead fragment"
else
"Link(id: #{id_from_link(link)}, text: #{link.text}) is not dead fragment"
end
end
end

0 comments on commit 618bfb5

Please sign in to comment.