Skip to content

Commit

Permalink
Merge pull request #4 from Gazler/test/generators
Browse files Browse the repository at this point in the history
test(Mix.Tasks.Ashes.Generate): tests for controller, model and channel
  • Loading branch information
Nick Gartmann committed Jan 14, 2015
2 parents 29cb8cb + 4762c5c commit eb1dc59
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/deps
erl_crash.dump
*.ez
/tmp
6 changes: 5 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ defmodule Ashes.Mixfile do
#
# Type `mix help deps` for more examples and options
defp deps do
[] end
[
{:phoenix, ">= 0.8.0", only: :test},
{:cowboy, ">= 1.0.0", only: :test}
]
end

defp package do
[
Expand Down
6 changes: 6 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
%{"cowboy": {:hex, :cowboy, "1.0.0"},
"cowlib": {:hex, :cowlib, "1.0.1"},
"phoenix": {:hex, :phoenix, "0.8.0"},
"plug": {:hex, :plug, "0.9.0"},
"poison": {:hex, :poison, "1.3.0"},
"ranch": {:hex, :ranch, "1.0.0"}}
2 changes: 2 additions & 0 deletions test/fixtures/router.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
defmodule PhotoBlog.Router do
end
21 changes: 21 additions & 0 deletions test/mix/mix_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule MixHelper do
import ExUnit.Assertions

def tmp_path do
Path.expand("../../tmp", __DIR__)
end

def assert_file(file) do
assert File.regular?(file), "Expected #{file} to exist, but does not"
end

def assert_file(file, match) do
cond do
Regex.regex?(match) ->
assert_file file, &(assert &1 =~ match)
is_function(match, 1) ->
assert_file(file)
match.(File.read!(file))
end
end
end
60 changes: 60 additions & 0 deletions test/mix/tasks/ashes/generate_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Code.require_file "../../mix_helper.exs", __DIR__

defmodule Mix.Tasks.Ashes.GenerateTest do
use ExUnit.Case

import MixHelper
import ExUnit.CaptureIO

@app_name "photo_blog"
@tmp_path tmp_path()
@project_path Path.join(@tmp_path, @app_name)

setup_all do
templates_path = Path.join([@project_path, "deps", "ashes", "templates"])
root_path = File.cwd!

#Clean up
File.rm_rf @project_path

#Create path for app
File.mkdir_p Path.join(@project_path, "web")

#Copy fixture router into web directory
File.cp! Path.join([root_path, "test", "fixtures", "router.ex"]), Path.join([@project_path, "web", "router.ex"])

#Create path for templates
File.mkdir_p templates_path

#Copy templates into `deps/ashes/templates` to mimic a real Phoenix application
File.cp_r! Path.join(root_path, "templates"), templates_path

#Move into the project directory to run the generator
File.cd! @project_path
end

test "creates controller files and directories" do
Mix.Tasks.Ashes.Generate.run(["controller", "user"])
assert_file "web/controllers/user_controller.ex"

assert File.exists?("web/templates/user")
end

test "creates controller files with --skip-template" do
Mix.Tasks.Ashes.Generate.run(["controller", "photo", "--skip-template"])
assert_file "web/controllers/photo_controller.ex"

refute File.exists?("web/templates/photo")
end

test "creates a channel file" do
Mix.Tasks.Ashes.Generate.run(["channel", "user"])
assert_file "web/channels/user_channel.ex"
end

test "creates model file" do
Mix.Tasks.Ashes.Generate.run(["model", "user"])
assert_file "web/models/user.ex"
end

end

0 comments on commit eb1dc59

Please sign in to comment.