Skip to content

Commit

Permalink
Merge pull request #12 from viniciusalonso/improve-tests
Browse files Browse the repository at this point in the history
Move blog_test directory
  • Loading branch information
viniciusalonso committed Nov 6, 2023
2 parents e6cb101 + e374db7 commit 7c463e3
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 27 deletions.
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.
22 changes: 11 additions & 11 deletions test/mix/tasks/simple_blog/compile_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ defmodule Mix.Tasks.SimpleBlog.CompileTest do

describe "run/1" do
setup do
on_exit(fn -> File.rm_rf("output_test") end)
on_exit(fn -> File.rm_rf("test/output") end)
end

test "create a directory to static blog" do
Mix.Tasks.SimpleBlog.Compile.run(["blog_test", "output_test"])
assert File.exists?("output_test")
Mix.Tasks.SimpleBlog.Compile.run(["test/blog", "test/output"])
assert File.exists?("test/output")
end

test "converts posts to html" do
Mix.Tasks.SimpleBlog.Post.run(["My First Blog Post", "blog_test"])
Mix.Tasks.SimpleBlog.Compile.run(["blog_test", "output_test"])
Mix.Tasks.SimpleBlog.Post.run(["My First Blog Post", "test/blog"])
Mix.Tasks.SimpleBlog.Compile.run(["test/blog", "test/output"])

today = Date.utc_today() |> Date.to_string()
dir = SimpleBlog.Post.generate_html_dir(%SimpleBlog.Post{date: today}, "output_test/posts")
dir = SimpleBlog.Post.generate_html_dir(%SimpleBlog.Post{date: today}, "test/output/posts")

assert File.exists?(dir <> "my-first-blog-post.html")

on_exit(fn -> File.rm("blog_test/_posts/#{today}-my-first-blog-post.md") end)
on_exit(fn -> File.rm("test/blog/_posts/#{today}-my-first-blog-post.md") end)
end

test "creates css files" do
Mix.Tasks.SimpleBlog.Compile.run(["blog_test", "output_test"])
css_dir = "output_test/css/"
Mix.Tasks.SimpleBlog.Compile.run(["test/blog", "test/output"])
css_dir = "test/output/css/"

assert File.exists?(css_dir <> "_solarized-light.css")
assert File.exists?(css_dir <> "plain.css")
Expand All @@ -34,8 +34,8 @@ defmodule Mix.Tasks.SimpleBlog.CompileTest do
end

test "creates images files" do
Mix.Tasks.SimpleBlog.Compile.run(["blog_test", "output_test"])
images_dir = "output_test/images/"
Mix.Tasks.SimpleBlog.Compile.run(["test/blog", "test/output"])
images_dir = "test/output/images/"

assert File.exists?(images_dir <> "avatar.png")
end
Expand Down
12 changes: 6 additions & 6 deletions test/mix/tasks/simple_blog/post_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ defmodule Mix.Tasks.SimpleBlog.PostTest do
test "show success message for created blog post" do
message =
capture_io(fn ->
Mix.Tasks.SimpleBlog.Post.run(["My First Blog Post", "blog_test"])
Mix.Tasks.SimpleBlog.Post.run(["My First Blog Post", "test/blog"])
end)

today = Date.utc_today() |> Date.to_string()

assert message == "Blog post created at blog_test/_posts/#{today}-my-first-blog-post.md\n\n"
on_exit(fn -> File.rm("blog_test/_posts/#{today}-my-first-blog-post.md") end)
assert message == "Blog post created at test/blog/_posts/#{today}-my-first-blog-post.md\n\n"
on_exit(fn -> File.rm("test/blog/_posts/#{today}-my-first-blog-post.md") end)
end

test "creates a new markdown file for blog post" do
Mix.Tasks.SimpleBlog.Post.run(["My First Blog Post", "blog_test"])
Mix.Tasks.SimpleBlog.Post.run(["My First Blog Post", "test/blog"])

today = Date.utc_today() |> Date.to_string()

assert File.exists?("blog_test/_posts/#{today}-my-first-blog-post.md")
on_exit(fn -> File.rm("blog_test/_posts/#{today}-my-first-blog-post.md") end)
assert File.exists?("test/blog/_posts/#{today}-my-first-blog-post.md")
on_exit(fn -> File.rm("test/blog/_posts/#{today}-my-first-blog-post.md") end)
end

test "show error message when blog does not exist" do
Expand Down
20 changes: 10 additions & 10 deletions test/simple_blog/reader/posts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ defmodule SimpleBlog.Reader.PostsTest do
use ExUnit.Case

setup do
Mix.Tasks.SimpleBlog.Post.run(["my first job day", "blog_test"])
Mix.Tasks.SimpleBlog.Post.run(["10 tips for a junior develop", "blog_test"])
Mix.Tasks.SimpleBlog.Post.run(["my first job day", "test/blog"])
Mix.Tasks.SimpleBlog.Post.run(["10 tips for a junior develop", "test/blog"])

on_exit(fn ->
{:ok, files} = File.ls("blog_test/_posts/")
{:ok, files} = File.ls("test/blog/_posts/")

full_paths =
files
|> Enum.filter(&String.ends_with?(&1, ".md"))

Enum.each(full_paths, &File.rm("blog_test/_posts/" <> &1))
Enum.each(full_paths, &File.rm("test/blog/_posts/" <> &1))
end)
end

describe "read_from_dir" do
test "returns only markdown content" do
content = SimpleBlog.Reader.Posts.read_from_dir("blog_test")
content = SimpleBlog.Reader.Posts.read_from_dir("test/blog")

today =
Date.utc_today()
Expand All @@ -32,8 +32,8 @@ defmodule SimpleBlog.Reader.PostsTest do
end

test "raises exception when dir not exists" do
assert_raise RuntimeError, "Directory blog_test_v2/_posts/ not found", fn ->
SimpleBlog.Reader.Posts.read_from_dir("blog_test_v2")
assert_raise RuntimeError, "Directory test_v2/blog/_posts/ not found", fn ->
SimpleBlog.Reader.Posts.read_from_dir("test_v2/blog")
end
end
end
Expand All @@ -45,15 +45,15 @@ defmodule SimpleBlog.Reader.PostsTest do
|> Date.to_string()

filename = "#{today}-my-first-job-day.md"
content = SimpleBlog.Reader.Posts.read_post("blog_test", filename)
content = SimpleBlog.Reader.Posts.read_post("test/blog", filename)

assert content ==
"<!---\nfilename: #{filename}\ntitle: my first job day\ndate: #{today}\n--->\n"
end

test "raises exception when dir not exists" do
assert_raise RuntimeError, "Directory blog_test_v2/_posts/ not found", fn ->
SimpleBlog.Reader.Posts.read_post("blog_test_v2", "my first")
assert_raise RuntimeError, "Directory test_v2/blog/_posts/ not found", fn ->
SimpleBlog.Reader.Posts.read_post("test_v2/blog", "my first")
end
end
end
Expand Down

0 comments on commit 7c463e3

Please sign in to comment.