Skip to content

Commit

Permalink
improvement: Add priv_dir functions to return priv directory (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahryarjb authored Nov 4, 2024
1 parent 5ae3ec1 commit 1d53662
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/igniter/project/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ defmodule Igniter.Project.Application do
end
end

@doc "Returns the path of the application's priv directory."
@spec priv_dir(Igniter.t(), [String.t()]) :: String.t()
def priv_dir(igniter, subpath \\ []) do
igniter
|> app_name()
|> Application.app_dir(["priv"] ++ subpath)
end

@doc "Returns the name of the application module."
def app_module(igniter) do
zipper =
Expand Down
47 changes: 47 additions & 0 deletions test/igniter/project/application_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,51 @@ defmodule Igniter.Project.ApplicationTest do
assert is_nil(Igniter.Project.Application.app_module(igniter))
end
end

describe "priv_dir/1" do
test "it returns the path of the application's priv directory as string" do
igniter = Igniter.new()

assert String.ends_with?(
Igniter.Project.Application.priv_dir(igniter),
"_build/test/lib/igniter/priv"
)
end

test "it returns the path of the application's priv directory and subpath string" do
igniter = Igniter.new()

assert String.ends_with?(
Igniter.Project.Application.priv_dir(igniter, ["test1", ["test2"]]),
"_build/test/lib/igniter/priv/test1/test2"
)
end

test "it raises if the application name can't be resolved as Application priv_dir" do
igniter =
test_project(
files: %{
"mix.exs" => """
defmodule IgniterTest.MixProject do
use Mix.Project
def project do
[
app: @app
]
end
@app :igniter_test
end
"""
}
)

assert_raise RuntimeError, fn -> Igniter.Project.Application.priv_dir(igniter) end

assert_raise RuntimeError, fn ->
Igniter.Project.Application.priv_dir(igniter, ["test1", ["test2"]])
end
end
end
end

0 comments on commit 1d53662

Please sign in to comment.