Skip to content

Commit

Permalink
Be able to set a default :base_dir option for use Tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
preciz committed Jul 13, 2024
1 parent a7ea542 commit a0606f6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ Define your Tmp module:
defmodule MyApp.Tmp do
use Tmp
end

# Or with a custom base directory
defmodule MyApp.CustomTmp do
use Tmp, base_dir: "/path/to/custom/base/dir"
end
```

Add it to your supervision tree:
Expand All @@ -50,7 +55,7 @@ end)
When calling `MyApp.Tmp.dir/2`, you can pass the following options:

- `:prefix` (optional) - Prefix for the temporary directory name, defaults to `nil`
- `:base_dir` (optional) - Base directory for the temporary directory, defaults to `System.tmp_dir()`
- `:base_dir` (optional) - Base directory for the temporary directory, defaults to `System.tmp_dir()` or the value set in `use Tmp`
- `:timeout` (optional) - Timeout in milliseconds, defaults to `:infinity`

### Examples
Expand All @@ -67,6 +72,16 @@ end, prefix: "my_app", base_dir: "/tmp/custom_base")
# => 4
```

Using a custom base directory:

```elixir
MyApp.CustomTmp.dir(fn tmp_dir_path ->
# tmp_dir_path will be within "/path/to/custom/base/dir"
File.touch(Path.join(tmp_dir_path, "file_in_custom_dir"))
# ... other work
end)
```

Error handling:

```elixir
Expand Down
19 changes: 17 additions & 2 deletions lib/tmp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ defmodule Tmp do
use Tmp
end
Or with a custom base directory:
defmodule MyApp.CustomTmp do
use Tmp, base_dir: "/path/to/custom/base/dir"
end
Add it to your supervision tree:
children = [
Expand All @@ -24,12 +30,20 @@ defmodule Tmp do
# then return a value
{:ok, :foobar}
end)
You can also override the base directory for a specific call:
MyApp.Tmp.dir(fn tmp_dir_path ->
# ...
end, base_dir: "/path/to/another/base/dir")
"""

defmacro __using__(_opts) do
defmacro __using__(opts) do
quote do
use Supervisor

@base_dir unquote(Keyword.get(opts, :base_dir))

def start_link(opts) do
Supervisor.start_link(__MODULE__, opts, name: opts[:name])
end
Expand All @@ -44,6 +58,7 @@ defmodule Tmp do
end

def dir(function, options \\ []) when is_function(function, 1) do
options = Keyword.put_new(options, :base_dir, @base_dir)
Tmp.dir(__MODULE__, function, options)
end
end
Expand Down Expand Up @@ -78,7 +93,7 @@ defmodule Tmp do
"""
@spec dir(module(), function(), keyword()) :: term()
def dir(module, function, options \\ []) when is_function(function, 1) do
base_dir = Keyword.get(options, :base_dir, System.tmp_dir())
base_dir = Keyword.get(options, :base_dir) || System.tmp_dir()
prefix = Keyword.get(options, :prefix)
timeout = Keyword.get(options, :timeout, :infinity)
dirname = dirname(prefix)
Expand Down
37 changes: 37 additions & 0 deletions test/tmp_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ defmodule TmpTest do
base_dir: base_dir,
prefix: "test_base_dir"
)

Process.sleep(100)
File.rmdir!(base_dir)
end

test "temporary directory exists in default base dir with correct prefix" do
Expand Down Expand Up @@ -153,4 +156,38 @@ defmodule TmpTest do
)
)
end

test "uses base_dir option when provided" do
custom_base_dir = Path.join(System.tmp_dir(), "custom_base_dir")
File.mkdir_p!(custom_base_dir)

TestTmp.dir(
fn tmp_dir_path ->
assert String.starts_with?(tmp_dir_path, custom_base_dir)
assert File.exists?(tmp_dir_path)
end,
base_dir: custom_base_dir,
prefix: "test_custom_base_dir"
)
end

test "uses base_dir from use Tmp when set" do
defmodule CustomBaseDirTmp do
use Tmp, base_dir: Path.join(System.tmp_dir(), "custom_base_dir_module")
end

start_supervised!({CustomBaseDirTmp, name: CustomBaseDirTmp})

CustomBaseDirTmp.dir(
fn tmp_dir_path ->
assert String.starts_with?(
tmp_dir_path,
Path.join(System.tmp_dir(), "custom_base_dir_module")
)

assert File.exists?(tmp_dir_path)
end,
prefix: "test_custom_base_dir_module"
)
end
end

0 comments on commit a0606f6

Please sign in to comment.