Skip to content

Commit

Permalink
Merge pull request #75 from JuliaComputing/mp/fix-nothing-error
Browse files Browse the repository at this point in the history
fix: error when meta-refresh tag is missing
  • Loading branch information
pfitzseb authored Oct 29, 2024
2 parents 9a64f03 + e437e78 commit ccf1c89
Show file tree
Hide file tree
Showing 27 changed files with 58 additions and 15 deletions.
12 changes: 5 additions & 7 deletions src/documentertools/canonical_urls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,11 @@ function update_canonical_links(docs_directory::AbstractString; canonical::Abstr
docs_directory = abspath(docs_directory)
isdir(docs_directory) || throw(ArgumentError("No such directory: $(docs_directory)"))

redirect_index_html_path = joinpath(docs_directory, "index.html")
canonical_path = if isfile(redirect_index_html_path)
redirect_url = get_meta_redirect_url(redirect_index_html_path)
splitpath(normpath(redirect_url))
else
canonical_path = canonical_directory_from_redirect_index_html(docs_directory)
if isnothing(canonical_path)
# canonical_version_from_versions_js returns just a string, but we want to splat
# canonical_path later, so we turn this into a single-element tuple
(canonical_version_from_versions_js(docs_directory),)
canonical_path = (canonical_version_from_versions_js(docs_directory),)
end
canonical_full_root = joinurl(canonical, canonical_path...)
# If we have determined which version should be the canonical version, we can actually
Expand Down Expand Up @@ -132,6 +129,7 @@ function canonical_directory_from_redirect_index_html(docs_directory::AbstractSt
redirect_index_html_path = joinpath(docs_directory, "index.html")
isfile(redirect_index_html_path) || return nothing
redirect_url = get_meta_redirect_url(redirect_index_html_path)
isnothing(redirect_url) && return nothing
splitpath(normpath(redirect_url))
end

Expand Down Expand Up @@ -189,7 +187,7 @@ function canonical_version_from_versions_js(docs_directory)
# We'll filter out a couple of potential bad cases and issue warnings
filter(versions) do vi
if !vi.path_exists
@warn "update_canonical_links: path does not exists or is not a directory" docs_directory vi
@warn "update_canonical_links: path does not exist or is not a directory" docs_directory vi
return false
end
return true
Expand Down
35 changes: 27 additions & 8 deletions test/documentertools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ normalize_newlines(s::AbstractString) = replace(s, "\r\n" => "\n")

@testset "walkdocs" begin
let fileinfos = DocumenterTools.FileInfo[]
rs = DocumenterTools.walkdocs(joinpath(FIXTURES, "pre")) do fileinfo
rs = DocumenterTools.walkdocs(joinpath(FIXTURES, "simple.pre")) do fileinfo
push!(fileinfos, fileinfo)
@test isabspath(fileinfo.root)
@test isabspath(fileinfo.fullpath)
Expand All @@ -20,7 +20,7 @@ normalize_newlines(s::AbstractString) = replace(s, "\r\n" => "\n")

let fileinfos = []
rs = DocumenterTools.walkdocs(
joinpath(FIXTURES, "pre"),
joinpath(FIXTURES, "simple.pre"),
DocumenterTools.isdochtml,
) do fileinfo
push!(fileinfos, fileinfo)
Expand All @@ -33,7 +33,10 @@ normalize_newlines(s::AbstractString) = replace(s, "\r\n" => "\n")
@test length(fileinfos) == 6
end

let rs = DocumenterTools.walkdocs(joinpath(FIXTURES, "pre"), collect = true) do fileinfo
let rs = DocumenterTools.walkdocs(
joinpath(FIXTURES, "simple.pre"),
collect = true,
) do fileinfo
fileinfo.root
end
@test length(rs) == 9
Expand Down Expand Up @@ -115,16 +118,16 @@ end
end
end

@testset "update_canonical_links" begin
@testset "update_canonical_links: simple" begin
out = tempname()
cp(joinpath(FIXTURES, "pre"), out)
cp(joinpath(FIXTURES, "simple.pre"), out)
@test DocumenterTools.canonical_directory_from_redirect_index_html(out) ==
["stable"]
DocumenterTools.update_canonical_links(
out;
canonical = "https://example.org/this-is-test",
)
DocumenterTools.walkdocs(joinpath(FIXTURES, "post")) do fileinfo
DocumenterTools.walkdocs(joinpath(FIXTURES, "simple.post")) do fileinfo
post = normalize_newlines(read(fileinfo.fullpath, String))
changed = normalize_newlines(read(joinpath(out, fileinfo.relpath), String))
if changed != post
Expand All @@ -137,7 +140,7 @@ end
# directory, remove index.html and instead use versions.js to determine the stable link
# For that we also need to make sure that stable/ is a symlink
out = tempname()
cp(joinpath(FIXTURES, "pre"), out)
cp(joinpath(FIXTURES, "simple.pre"), out)
rm(joinpath(out, "index.html"))
rm(joinpath(out, "stable"), recursive = true)
symlink(joinpath(out, "v0.5.0"), joinpath(out, "stable"))
Expand All @@ -158,7 +161,7 @@ end
out;
canonical = "https://example.org/this-is-test",
)
DocumenterTools.walkdocs(joinpath(FIXTURES, "post")) do fileinfo
DocumenterTools.walkdocs(joinpath(FIXTURES, "simple.post")) do fileinfo
# We removed the root /index.html redirect file, so we skip testing it
(fileinfo.relpath == "index.html") && return
# We also don't check the stable/ symlink.
Expand All @@ -173,4 +176,20 @@ end
@test changed == post
end
end

# Testing the case where index.html does not have a meta redirect
@testset "update_canonical_links: nometa" begin
out = tempname()
cp(joinpath(FIXTURES, "nometa.pre"), out)
@test DocumenterTools.canonical_directory_from_redirect_index_html(out) === nothing
if !Sys.iswindows()
# These two tests depend on symlinks, so they do not work on Windows
@test DocumenterTools.canonical_version_from_versions_js(out) == "stable"
# Just tests that the function runs
@test DocumenterTools.update_canonical_links(
out;
canonical = "https://example.org/this-is-test",
) === nothing
end
end
end
2 changes: 2 additions & 0 deletions test/fixtures/nometa.pre/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!--This file is automatically generated by Documenter.jl-->
<!--The meta tag is missing.-->
1 change: 1 addition & 0 deletions test/fixtures/nometa.pre/stable
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions test/fixtures/nometa.pre/versions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var DOC_VERSIONS = [
"stable",
"v0.5.0"
];
var DOCUMENTER_NEWEST = "v0.5.0";
var DOCUMENTER_STABLE = "stable";
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/fixtures/simple.pre/v0.5.0/asset.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.
7 changes: 7 additions & 0 deletions test/fixtures/simple.pre/v0.5.0/foo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE >
<HTML>
<head></head>
<body>
nothing much in here
</body>
</HTML>
9 changes: 9 additions & 0 deletions test/fixtures/simple.pre/v0.5.0/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE >
<HTML>
<head>
<link rel="canonical"
</head>
<body>
nothing much in here
</body>
</HTML>
Empty file.

0 comments on commit ccf1c89

Please sign in to comment.