Skip to content

Commit

Permalink
Move integration tests to bash script
Browse files Browse the repository at this point in the history
  • Loading branch information
danielberkompas committed Apr 26, 2018
1 parent eccf8db commit 716db46
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 108 deletions.
7 changes: 6 additions & 1 deletion bin/test
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@
MIX_ENV=test mix compile --warnings-as-errors --force || { echo 'Please fix all compiler warnings.'; exit 1; }
MIX_ENV=test mix credo --strict --ignore design,consistency || { echo 'Elixir code failed Credo linting. See warnings above.'; exit 1; }
mix docs || { echo 'Elixir HTML docs were not generated!'; exit 1; }
mix test || { echo 'Elixir tests failed!'; exit 1; }
mix test || { echo 'Elixir tests on Torch failed!'; exit 1; }

# Run integration tests
cd test/support/apps/example && {
bin/test || { echo 'Integration tests on regular Phoenix project failed!'; exit 1; }
}
32 changes: 0 additions & 32 deletions test/mix/tasks/torch.gen.html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,8 @@ defmodule Mix.Tasks.Torch.Gen.HtmlTest do
use Torch.MixCase, async: false

describe ".run/1" do
setup [:prepare_example_apps, :clean_generated_files]

test_mix_config_errors("torch.gen.html")

@tag :integration
test "generates valid context, controllers, and tests" do
# Install torch layout
System.cmd("mix", ["torch.install"], cd: @project_dir)

# TODO: support slim
for format <- [:eex] do
System.cmd(
"mix",
["torch.gen.html", "Blog", "Post", "posts", "title:string", "--format", "#{format}"],
cd: @project_dir
)

# Overwrite the router to include the generated link as
# shown in the instructions
File.write!(
"#{@project_dir}/lib/example_web/router.ex",
File.read!("test/support/routers/modified.ex")
)

# Run tests in the example app, ensure they are passing
assert {response, status} = System.cmd("mix", ["test"], cd: @project_dir)
assert status == 0, response
assert response =~ "20 tests, 0 failures"

# Clean up generated files so they don't get committed by accident
clean_generated_files([])
end
end

# TODO: Add integration test for umbrella app
end
end
15 changes: 0 additions & 15 deletions test/mix/tasks/torch.install_test.exs
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
defmodule Mix.Tasks.Torch.InstallTest do
use Torch.MixCase

setup_all :prepare_example_apps

describe ".run/1" do
setup :clean_generated_files

test_mix_config_errors("torch.install")

@tag :integration
test "injects layout template into regular project" do
for format <- @formats do
System.cmd("mix", ["torch.install", "--format", "#{format}"], cd: @project_dir)

assert File.exists?(
"#{@project_dir}/lib/example_web/templates/layout/torch.html.#{format}"
)
end
end

# TODO: add test for umbrella project
end
end
18 changes: 0 additions & 18 deletions test/mix/tasks/torch.uninstall_test.exs
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
defmodule Mix.Tasks.Torch.UninstallTest do
use Torch.MixCase

setup_all :prepare_example_apps

describe ".run/1" do
setup :clean_generated_files

test_mix_config_errors("torch.uninstall")

@tag :integration
test "removes all torch files from regular project if they exist" do
for format <- @formats do
System.cmd("mix", ["torch.install", "--format", "#{format}"], cd: @project_dir)
System.cmd("mix", ["torch.uninstall", "--format", "#{format}"], cd: @project_dir)

refute File.exists?("#{@project_dir}/priv/templates/phx.gen.html/")

refute File.exists?(
"#{@project_dir}/lib/example_web/templates/layout/torch.html.#{format}"
)
end
end

# TODO: add test for umbrella project
end
end
23 changes: 23 additions & 0 deletions test/support/apps/example/bin/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

mix deps.get || { echo 'Dependencies could not be fetched!'; exit 1; }
MIX_ENV=test mix ecto.drop || { echo 'Database could not be dropped'; exit 1; }
mix torch.install || { echo 'Torch could not be installed!'; exit 1; }
mix torch.gen.html Blog Post posts title:string || { echo 'Torch files not generated!'; exit 1; }
cp ../../routers/modified.ex lib/example_web/router.ex
mix test || { echo 'Tests failed!'; exit 1; }

# Clean up generated files
rm -rf priv/templates
rm -rf priv/repo/migrations/*.exs
rm -rf lib/example/blog
rm -rf test/example/blog
rm -rf lib/example_web/controllers/post_controller.ex
rm -rf lib/example_web/templates/layout/torch.html.eex
rm -rf lib/example_web/templates/post/
rm -rf lib/example_web/views/post_view.ex
rm -rf test/example_web/controllers/post_controller_test.exs
cp ../../routers/original.ex lib/example_web/router.ex

echo 'Tests succeeded!'
exit 0
42 changes: 0 additions & 42 deletions test/support/cases/mix_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,7 @@ defmodule Torch.MixCase do
using do
quote do
import Torch.MixCase

@project_dir "test/support/apps/example"
@formats [:eex, :slim]
end
end

@project_dir "test/support/apps/example"
@formats [:eex, :slim]

@doc false
def prepare_example_apps(_) do
System.cmd("mix", ["deps.get"], cd: @project_dir)
System.cmd("mix", ["ecto.drop"], cd: @project_dir, env: [{"MIX_ENV", "test"}])

:ok
end

@doc false
def clean_generated_files(_) do
for format <- @formats do
files = [
"#{@project_dir}/priv/templates/",
"#{@project_dir}/priv/repo/migrations/",
"#{@project_dir}/lib/example/blog/",
"#{@project_dir}/test/example/blog/",
"#{@project_dir}/lib/example_web/controllers/post_controller.ex",
"#{@project_dir}/lib/example_web/templates/layout/torch.html.#{format}",
"#{@project_dir}/lib/example_web/templates/post/",
"#{@project_dir}/lib/example_web/views/post_view.ex",
"#{@project_dir}/test/example_web/controllers/post_controller_test.exs"
]

Enum.each(files, &File.rm_rf/1)

File.write!(
"#{@project_dir}/lib/example_web/router.ex",
File.read!("test/support/routers/original.ex")
)

File.mkdir("#{@project_dir}/priv/repo/migrations/")
end

:ok
end

@doc false
Expand Down

0 comments on commit 716db46

Please sign in to comment.