Skip to content

Optimize html/epub tests by making them asyncronous by using tmp_dir tag #1557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ ex_doc-*.tar
node_modules/
/test/fixtures/umbrella/_build/
/test/tmp/
/tmp/
/npm-debug.log

# Ignore artifacts from non-production builds
Expand Down
120 changes: 56 additions & 64 deletions test/ex_doc/formatter/epub_test.exs
Original file line number Diff line number Diff line change
@@ -1,114 +1,103 @@
defmodule ExDoc.Formatter.EPUBTest do
use ExUnit.Case
use ExUnit.Case, async: true

setup do
File.rm_rf(output_dir())
File.mkdir_p!(output_dir())
end

defp output_dir do
Path.expand("../../tmp/epub", __DIR__)
end

defp beam_dir do
Path.expand("../../tmp/beam", __DIR__)
end
@moduletag :tmp_dir

@before_closing_head_tag_content_epub "UNIQUE:<dont-escape>&copy;BEFORE-CLOSING-HEAD-TAG-HTML</dont-escape>"
@before_closing_body_tag_content_epub "UNIQUE:<dont-escape>&copy;BEFORE-CLOSING-BODY-TAG-HTML</dont-escape>"

defp before_closing_head_tag(:epub), do: @before_closing_head_tag_content_epub
defp before_closing_body_tag(:epub), do: @before_closing_body_tag_content_epub

defp doc_config do
defp doc_config(%{tmp_dir: tmp_dir} = _context) do
[
app: :elixir,
project: "Elixir",
version: "1.0.1",
formatter: "epub",
output: output_dir(),
source_beam: beam_dir(),
output: tmp_dir <> "/epub",
source_beam: "test/tmp/beam",
extras: ["test/fixtures/README.md"],
skip_undefined_reference_warnings_on: ["Warnings"]
]
end

defp doc_config(config) do
Keyword.merge(doc_config(), config)
defp doc_config(context, config) when is_map(context) and is_list(config) do
Keyword.merge(doc_config(context), config)
end

defp generate_docs(config) do
ExDoc.generate_docs(config[:project], config[:version], config)
end

defp generate_docs_and_unzip(options) do
generate_docs(options)
unzip_dir = String.to_charlist("#{doc_config()[:output]}")
defp generate_docs_and_unzip(context, config) do
generate_docs(config)
unzip_dir = String.to_charlist("#{doc_config(context)[:output]}")

"#{doc_config()[:output]}/#{doc_config()[:project]}.epub"
"#{doc_config(context)[:output]}/#{doc_config(context)[:project]}.epub"
|> String.to_charlist()
|> :zip.unzip(cwd: unzip_dir)
end

test "generates headers for module pages" do
generate_docs_and_unzip(doc_config(main: "RandomError"))
test "generates headers for module pages", %{tmp_dir: tmp_dir} = context do
generate_docs_and_unzip(context, doc_config(context, main: "RandomError"))

content = File.read!("#{output_dir()}/OEBPS/RandomError.xhtml")
content = File.read!(tmp_dir <> "/epub/OEBPS/RandomError.xhtml")
assert content =~ ~r{<html.*lang="en".*xmlns:epub="http://www.idpf.org/2007/ops">}ms
assert content =~ ~r{<meta charset="utf-8" />}ms
assert content =~ ~r{<meta name="generator" content="ExDoc v[^"]+" />}
assert content =~ ~r{<title>RandomError - Elixir v1.0.1</title>}
end

test "allows to set the primary language of the document" do
generate_docs_and_unzip(doc_config(main: "RandomError", language: "fr"))
test "allows to set the primary language of the document", %{tmp_dir: tmp_dir} = context do
generate_docs_and_unzip(context, doc_config(context, main: "RandomError", language: "fr"))

content = File.read!("#{output_dir()}/OEBPS/RandomError.xhtml")
content = File.read!(tmp_dir <> "/epub/OEBPS/RandomError.xhtml")
assert content =~ ~r{<html.*lang="fr".*xmlns:epub="http://www.idpf.org/2007/ops">}ms
end

test "allows to set the authors of the book" do
generate_docs_and_unzip(doc_config(authors: ["John Doe", "Jane Doe"]))
test "allows to set the authors of the book", %{tmp_dir: tmp_dir} = context do
generate_docs_and_unzip(context, doc_config(context, authors: ["John Doe", "Jane Doe"]))

content = File.read!("#{output_dir()}/OEBPS/content.opf")
content = File.read!(tmp_dir <> "/epub/OEBPS/content.opf")
assert content =~ ~r{<dc:creator id="author1">John Doe</dc:creator>}
assert content =~ ~r{<dc:creator id="author2">Jane Doe</dc:creator>}
end

test "raises when assets are invalid" do
test "raises when assets are invalid", context do
File.mkdir_p!("test/tmp/epub_assets/hello")
File.touch!("test/tmp/epub_assets/hello/world.pdf")

assert_raise(
RuntimeError,
~s{asset with extension ".pdf" is not supported by EPUB format},
fn -> generate_docs(doc_config(assets: "test/tmp/epub_assets")) end
fn -> generate_docs(doc_config(context, assets: "test/tmp/epub_assets")) end
)
after
File.rm_rf!("test/tmp/epub_assets")
end

test "generates an EPUB file in the default directory" do
generate_docs(doc_config())
assert File.regular?("#{output_dir()}/#{doc_config()[:project]}.epub")
test "generates an EPUB file in the default directory", %{tmp_dir: tmp_dir} = context do
generate_docs(doc_config(context))
assert File.regular?(tmp_dir <> "/epub/#{doc_config(context)[:project]}.epub")
end

test "generates an EPUB file with erlang as proglang" do
generate_docs(Keyword.put(doc_config(), :proglang, :erlang))
assert File.regular?("#{output_dir()}/#{doc_config()[:project]}.epub")
test "generates an EPUB file with erlang as proglang", %{tmp_dir: tmp_dir} = context do
generate_docs(Keyword.put(doc_config(context), :proglang, :erlang))
assert File.regular?(tmp_dir <> "/epub/#{doc_config(context)[:project]}.epub")
end

test "generates an EPUB file in specified output directory" do
config = doc_config(output: "#{output_dir()}/another_dir", main: "RandomError")
test "generates an EPUB file in specified output directory", %{tmp_dir: tmp_dir} = context do
config = doc_config(context, output: tmp_dir <> "/epub/another_dir", main: "RandomError")
generate_docs(config)

assert File.regular?("#{output_dir()}/another_dir/#{doc_config()[:project]}.epub")
assert File.regular?(tmp_dir <> "/epub/another_dir/#{doc_config(context)[:project]}.epub")
end

test "generates an EPUB file with a standardized structure" do
generate_docs_and_unzip(doc_config())
test "generates an EPUB file with a standardized structure", %{tmp_dir: tmp_dir} = context do
generate_docs_and_unzip(context, doc_config(context))

root_dir = "#{output_dir()}"
root_dir = tmp_dir <> "/epub"
meta_dir = "#{root_dir}/META-INF"
oebps_dir = "#{root_dir}/OEBPS"
dist_dir = "#{oebps_dir}/dist"
Expand All @@ -126,9 +115,9 @@ defmodule ExDoc.Formatter.EPUBTest do
assert [_] = Path.wildcard("#{dist_dir}/elixir*.css")
end

test "generates all listing files" do
generate_docs_and_unzip(doc_config())
content = File.read!("#{output_dir()}/OEBPS/content.opf")
test "generates all listing files", %{tmp_dir: tmp_dir} = context do
generate_docs_and_unzip(context, doc_config(context))
content = File.read!(tmp_dir <> "/epub/OEBPS/content.opf")

assert content =~ ~r{.*"CompiledWithDocs\".*}ms
assert content =~ ~r{.*"CompiledWithDocs.Nested\".*}ms
Expand All @@ -139,11 +128,11 @@ defmodule ExDoc.Formatter.EPUBTest do
assert content =~ ~r{.*"Mix\.Tasks\.TaskWithDocs\".*}ms
end

test "generates the readme file" do
config = doc_config(main: "README")
generate_docs_and_unzip(config)
test "generates the readme file", %{tmp_dir: tmp_dir} = context do
config = doc_config(context, main: "README")
generate_docs_and_unzip(context, config)

content = File.read!("#{output_dir()}/OEBPS/readme.xhtml")
content = File.read!(tmp_dir <> "/epub/OEBPS/readme.xhtml")
assert content =~ ~r{<title>README [^<]*</title>}
assert content =~ ~r{<a href="RandomError.xhtml"><code(\sclass="inline")?>RandomError</code>}

Expand All @@ -153,14 +142,14 @@ defmodule ExDoc.Formatter.EPUBTest do
assert content =~
~r{<a href="TypesAndSpecs.Sub.xhtml"><code(\sclass="inline")?>TypesAndSpecs.Sub</code></a>}

content = File.read!("#{output_dir()}/OEBPS/nav.xhtml")
content = File.read!(tmp_dir <> "/epub/OEBPS/nav.xhtml")
assert content =~ ~r{<li><a href="readme.xhtml">README</a></li>}
end

test "uses samp as highlight tag for markdown" do
generate_docs_and_unzip(doc_config())
test "uses samp as highlight tag for markdown", %{tmp_dir: tmp_dir} = context do
generate_docs_and_unzip(context, doc_config(context))

assert File.read!("#{output_dir()}/OEBPS/CompiledWithDocs.xhtml") =~
assert File.read!(tmp_dir <> "/epub/OEBPS/CompiledWithDocs.xhtml") =~
"<samp class=\"nc\">CompiledWithDocs<\/samp>"
end

Expand All @@ -174,15 +163,17 @@ defmodule ExDoc.Formatter.EPUBTest do
"CompiledWithDocs.Nested.xhtml"
]

test "before_closing_*_tags required by the user are in the right place" do
test "before_closing_*_tags required by the user are in the right place",
%{tmp_dir: tmp_dir} = context do
generate_docs_and_unzip(
doc_config(
context,
doc_config(context,
before_closing_head_tag: &before_closing_head_tag/1,
before_closing_body_tag: &before_closing_body_tag/1
)
)

oebps_dir = "#{output_dir()}/OEBPS"
oebps_dir = tmp_dir <> "/epub/OEBPS"

for basename <- @example_basenames do
content = File.read!(Path.join(oebps_dir, basename))
Expand All @@ -191,21 +182,22 @@ defmodule ExDoc.Formatter.EPUBTest do
end
end

test "assets required by the user end up in the right place" do
test "assets required by the user end up in the right place", %{tmp_dir: tmp_dir} = context do
File.mkdir_p!("test/tmp/epub_assets/hello")
File.touch!("test/tmp/epub_assets/hello/world.png")

generate_docs_and_unzip(
doc_config(
context,
doc_config(context,
assets: "test/tmp/epub_assets",
logo: "test/fixtures/elixir.png",
cover: "test/fixtures/elixir.png"
)
)

assert File.regular?("#{output_dir()}/OEBPS/assets/hello/world.png")
assert File.regular?("#{output_dir()}/OEBPS/assets/logo.png")
assert File.regular?("#{output_dir()}/OEBPS/assets/cover.png")
assert File.regular?(tmp_dir <> "/epub/OEBPS/assets/hello/world.png")
assert File.regular?(tmp_dir <> "/epub/OEBPS/assets/logo.png")
assert File.regular?(tmp_dir <> "/epub/OEBPS/assets/cover.png")
after
File.rm_rf!("test/tmp/epub_assets")
end
Expand Down
Loading