Skip to content

Commit

Permalink
passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benbalter committed Oct 11, 2013
1 parent acc35cb commit fa2fb03
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 56 deletions.
28 changes: 14 additions & 14 deletions lib/html/proofer/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,32 +95,32 @@ def self.subclasses

def resolve_path(path)

#Strip anchor, not needed to resolve file
path = path.split('#').first

if path =~ /^\// #path relative to root
base = @src
else #relative links
elsif File.exist? File.join Dir.pwd, @path #relative links, path is a file
base = File.dirname(@path)
else #relative link, path is a directory
base = @path
end

if path =~ /^#/ #anchor link, no trailing slash
path = "#{base}#{path}"
else # relative path, resolve trailing slashes automatically
path = File.join File.dirname(base), path
path = File.join base, path
end

begin
parts = URI.parse URI.encode path

# implicit /index.html support, with support for tailing slashes
parts.path = File.join parts.path, "index.html" if File.directory? parts.path
# implicit /index.html support, with support for tailing slashes
path = File.join path, "index.html" if File.directory? File.join Dir.pwd, path

# strip anchor which isn't necessary to resolve path
parts.fragment = nil
path
end

path = parts.to_s
rescue
self.add_issue("#{@path}".blue + ": #{path} is not a valid URI")
end
URI.decode path
# checks if a file exists relative to the current pwd
def file?(path)
File.exist? File.join Dir.pwd, resolve_path(path)
end
end
end
2 changes: 1 addition & 1 deletion lib/html/proofer/checks/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def run
# check image sources
if src && src.length > 0
if !external_href?(src)
self.add_issue("#{@path}".blue + ": internal image #{src} does not exist") unless File.exist?( resolve_path(src))
self.add_issue("#{@path}".blue + ": internal image #{src} does not exist") unless file? src
else
validate_url(src, "#{@path}".blue + ": external image #{src} does not exist")
end
Expand Down
10 changes: 4 additions & 6 deletions lib/html/proofer/checks/links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ def run

# it's not an internal hash; it's pointing to some other file
if href_file.length > 0
href_location = resolve_path href
if !File.exist?(href_location)
self.add_issue("#{@path}".blue + ": internal link #{href_location} does not exist")
if !file?(href_file)
self.add_issue("#{@path}".blue + ": internal link #{href_file} does not exist")
else
href_html = HTML::Proofer.create_nokogiri(href_location)
href_html = HTML::Proofer.create_nokogiri(resolve_path(href_file))
found_hash_match = false
unless hash_check(href_html, href_hash)
self.add_issue("#{@path}".blue + ": linking to #{href}, but #{href_hash} does not exist")
Expand All @@ -45,8 +44,7 @@ def run
end
# internal link, no hash
else
href_location = resolve_path href
self.add_issue("#{@path}".blue + ": internally linking to #{href_location}, which does not exist") unless File.exist?(href_location)
self.add_issue("#{@path}".blue + ": internally linking to #{href}, which does not exist") unless file?(href)
end
else
validate_url(href, "#{@path}".blue + ": externally linking to #{href}, which does not exist")
Expand Down
2 changes: 1 addition & 1 deletion spec/html/proofer/fixtures/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
root.
<h1 id="anchor">root.</a>
9 changes: 0 additions & 9 deletions spec/html/proofer/fixtures/missingImageDirPrefix.html

This file was deleted.

4 changes: 4 additions & 0 deletions spec/html/proofer/fixtures/relativeLinks.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<a href="/index.html">Relative to root</a>
<a href="index.html">Relative to self</a>
<a href="folder/">Folder relative to self</a>
<a href="index.html#anchor">Anchor relative to self</a>
21 changes: 7 additions & 14 deletions spec/html/proofer/images_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,28 @@
describe "Image tests" do
it "passes for existing external images" do
externalImageFilepath = "#{FIXTURES_DIR}/existingImageExternal.html"
@imageCheck = Images.new("/", externalImageFilepath, HTML::Proofer.create_nokogiri(externalImageFilepath))
@imageCheck = Images.new("#{FIXTURES_DIR}", externalImageFilepath, HTML::Proofer.create_nokogiri(externalImageFilepath))
@imageCheck.run
@imageCheck.issues[0].should eq(nil)
end

it "fails for image without alt attribute" do
missingAltFilepath = "#{FIXTURES_DIR}/missingImageAlt.html"
@imageCheck = Images.new("/", missingAltFilepath, HTML::Proofer.create_nokogiri(missingAltFilepath))
@imageCheck = Images.new("#{FIXTURES_DIR}", missingAltFilepath, HTML::Proofer.create_nokogiri(missingAltFilepath))
@imageCheck.run
@imageCheck.issues[0].should eq("spec/html/proofer/fixtures/missingImageAlt.html".blue + ": image ./gpl.png does not have an alt attribute")
end

it "fails for image with an empty alt attribute" do
missingAltFilepath = "#{FIXTURES_DIR}/missingImageAltText.html"
@imageCheck = Images.new("/", missingAltFilepath, HTML::Proofer.create_nokogiri(missingAltFilepath))
@imageCheck = Images.new("#{FIXTURES_DIR}", missingAltFilepath, HTML::Proofer.create_nokogiri(missingAltFilepath))
@imageCheck.run
@imageCheck.issues[0].should eq("spec/html/proofer/fixtures/missingImageAltText.html".blue + ": image ./gpl.png does not have an alt attribute")
end

it "fails for image without a dir prefix" do
missingImageDirPrefixFilepath = "#{FIXTURES_DIR}/missingImageDirPrefix.html"
@imageCheck = Images.new("/", missingImageDirPrefixFilepath, HTML::Proofer.create_nokogiri(missingImageDirPrefixFilepath))
@imageCheck.run
@imageCheck.issues[0].should eq("spec/html/proofer/fixtures/missingImageDirPrefix.html".blue + ": internal image /gpl.png does not exist")
end

it "fails for missing external images" do
externalImageFilepath = "#{FIXTURES_DIR}/missingImageExternal.html"
@imageCheck = Images.new("/", externalImageFilepath, HTML::Proofer.create_nokogiri(externalImageFilepath))
@imageCheck = Images.new("#{FIXTURES_DIR}", externalImageFilepath, HTML::Proofer.create_nokogiri(externalImageFilepath))
@imageCheck.run
@imageCheck.hydra.run
@imageCheck.issues[0].sub!(/ #<Typhoeus::Response:[\w]+>/, "")
Expand All @@ -40,21 +33,21 @@

it "fails for missing internal images" do
internalImageFilepath = "#{FIXTURES_DIR}/missingImageInternal.html"
@imageCheck = Images.new("/", internalImageFilepath, HTML::Proofer.create_nokogiri(internalImageFilepath))
@imageCheck = Images.new("#{FIXTURES_DIR}", internalImageFilepath, HTML::Proofer.create_nokogiri(internalImageFilepath))
@imageCheck.run
@imageCheck.issues[0].should eq("spec/html/proofer/fixtures/missingImageInternal.html".blue + ": internal image ./doesnotexist.png does not exist")
end

it "fails for image with no src" do
imageSrcFilepath = "#{FIXTURES_DIR}/missingImageSrc.html"
@imageCheck = Images.new("/", imageSrcFilepath, HTML::Proofer.create_nokogiri(imageSrcFilepath))
@imageCheck = Images.new("#{FIXTURES_DIR}", imageSrcFilepath, HTML::Proofer.create_nokogiri(imageSrcFilepath))
@imageCheck.run
@imageCheck.issues[0].should eq("spec/html/proofer/fixtures/missingImageSrc.html".blue + ": image has no src attribute")
end

it "fails for image with default mac filename" do
terribleImageName = "#{FIXTURES_DIR}/terribleImageName.html"
@imageCheck = Images.new("/", terribleImageName, HTML::Proofer.create_nokogiri(terribleImageName))
@imageCheck = Images.new("#{FIXTURES_DIR}", terribleImageName, HTML::Proofer.create_nokogiri(terribleImageName))
@imageCheck.run
@imageCheck.issues[0].should eq("spec/html/proofer/fixtures/terribleImageName.html".blue + ": image has a terrible filename (./Screen Shot 2012-08-09 at 7.51.18 AM.png)")
end
Expand Down
29 changes: 18 additions & 11 deletions spec/html/proofer/links_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

it "fails for broken external hash (even if the file exists)" do
brokenHashExternalFilepath = "#{FIXTURES_DIR}/brokenHashExternal.html"
@linkCheck = Links.new("/", brokenHashExternalFilepath, HTML::Proofer.create_nokogiri(brokenHashExternalFilepath))
@linkCheck = Links.new("#{FIXTURES_DIR}", brokenHashExternalFilepath, HTML::Proofer.create_nokogiri(brokenHashExternalFilepath))
@linkCheck.run
@linkCheck.issues[1].should eq("spec/html/proofer/fixtures/brokenHashExternal.html".blue + ": linking to ./missingImageAlt.html#asdfasfdkafl, but asdfasfdkafl does not exist")
end

it "fails for broken internal hash" do
brokenHashInternalFilepath = "#{FIXTURES_DIR}/brokenHashInternal.html"
@linkCheck = Links.new("/", brokenHashInternalFilepath, HTML::Proofer.create_nokogiri(brokenHashInternalFilepath))
@linkCheck = Links.new("#{FIXTURES_DIR}", brokenHashInternalFilepath, HTML::Proofer.create_nokogiri(brokenHashInternalFilepath))
@linkCheck.run
@linkCheck.issues[0].should eq("spec/html/proofer/fixtures/brokenHashInternal.html".blue + ": linking to an internal hash called noHash that does not exist")
end

it "fails for broken external links" do
brokenLinkExternalFilepath = "#{FIXTURES_DIR}/brokenLinkExternal.html"
@linkCheck = Links.new("/", brokenLinkExternalFilepath, HTML::Proofer.create_nokogiri(brokenLinkExternalFilepath))
@linkCheck = Links.new("#{FIXTURES_DIR}", brokenLinkExternalFilepath, HTML::Proofer.create_nokogiri(brokenLinkExternalFilepath))
@linkCheck.run
@linkCheck.hydra.run
@linkCheck.issues[0].sub!(/ #<Typhoeus::Response:[\w]+>/, "")
Expand All @@ -27,49 +27,56 @@

it "fails for broken internal links" do
brokenLinkInternalFilepath = "#{FIXTURES_DIR}/brokenLinkInternal.html"
@linkCheck = Links.new("/", brokenLinkInternalFilepath, HTML::Proofer.create_nokogiri(brokenLinkInternalFilepath))
@linkCheck = Links.new("#{FIXTURES_DIR}", brokenLinkInternalFilepath, HTML::Proofer.create_nokogiri(brokenLinkInternalFilepath))
@linkCheck.run
@linkCheck.issues[0].should eq("spec/html/proofer/fixtures/brokenLinkInternal.html".blue + ": internally linking to spec/html/proofer/fixtures/./notreal.html, which does not exist")
@linkCheck.issues[0].should eq("spec/html/proofer/fixtures/brokenLinkInternal.html".blue + ": internally linking to ./notreal.html, which does not exist")
end

it "fails for link with no href" do
missingLinkHrefFilepath = "#{FIXTURES_DIR}/missingLinkHref.html"
@linkCheck = Links.new("/", missingLinkHrefFilepath, HTML::Proofer.create_nokogiri(missingLinkHrefFilepath))
@linkCheck = Links.new("#{FIXTURES_DIR}", missingLinkHrefFilepath, HTML::Proofer.create_nokogiri(missingLinkHrefFilepath))
@linkCheck.run
@linkCheck.issues[0].should eq("spec/html/proofer/fixtures/missingLinkHref.html".blue + ": link has no href attribute")
end

it "should follow redirects" do
linkWithRedirectFilepath = "#{FIXTURES_DIR}/linkWithRedirect.html"
@linkCheck = Links.new("/", linkWithRedirectFilepath, HTML::Proofer.create_nokogiri(linkWithRedirectFilepath))
@linkCheck = Links.new("#{FIXTURES_DIR}", linkWithRedirectFilepath, HTML::Proofer.create_nokogiri(linkWithRedirectFilepath))
@linkCheck.run
@linkCheck.issues[0].should eq(nil)
end

it "should understand https" do
linkWithHttpsFilepath = "#{FIXTURES_DIR}/linkWithHttps.html"
@linkCheck = Links.new("/", linkWithHttpsFilepath, HTML::Proofer.create_nokogiri(linkWithHttpsFilepath))
@linkCheck = Links.new("#{FIXTURES_DIR}", linkWithHttpsFilepath, HTML::Proofer.create_nokogiri(linkWithHttpsFilepath))
@linkCheck.run
@linkCheck.issues[0].should eq(nil)
end

it "fails for broken hash links with status code numbers" do
brokenLinkWithNumberFilepath = "#{FIXTURES_DIR}/brokenLinkWithNumber.html"
@linkCheck = Links.new("/", brokenLinkWithNumberFilepath, HTML::Proofer.create_nokogiri(brokenLinkWithNumberFilepath))
@linkCheck = Links.new("#{FIXTURES_DIR}", brokenLinkWithNumberFilepath, HTML::Proofer.create_nokogiri(brokenLinkWithNumberFilepath))
@linkCheck.run
@linkCheck.issues[0].should eq(nil)
end

it 'properly resolves implicit /index.html in link paths' do
linkToFolder = "#{FIXTURES_DIR}/linkToFolder.html"
@linkCheck = Links.new("/", linkToFolder, HTML::Proofer.create_nokogiri(linkToFolder))
@linkCheck = Links.new("#{FIXTURES_DIR}", linkToFolder, HTML::Proofer.create_nokogiri(linkToFolder))
@linkCheck.run
@linkCheck.issues[0].should eq(nil)
end

it 'properly checks links to root' do
rootLink = "#{FIXTURES_DIR}/rootLink.html"
@linkCheck = Links.new("/", rootLink, HTML::Proofer.create_nokogiri(rootLink))
@linkCheck = Links.new("#{FIXTURES_DIR}", rootLink, HTML::Proofer.create_nokogiri(rootLink))
@linkCheck.run
@linkCheck.issues[0].should eq(nil)
end

it 'properly checks relative links' do
relativeLinks = "#{FIXTURES_DIR}/relativeLinks.html"
@linkCheck = Links.new("#{FIXTURES_DIR}", relativeLinks, HTML::Proofer.create_nokogiri(relativeLinks))
@linkCheck.run
@linkCheck.issues[0].should eq(nil)
end
Expand Down

0 comments on commit fa2fb03

Please sign in to comment.