Skip to content

Commit 92817c8

Browse files
authored
Merge pull request #18 from sofakingworld/manual_file_tracking
Manual file tracking function
2 parents 146771f + 7b5bdd1 commit 92817c8

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/temp.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ defmodule Temp do
122122
end
123123
end
124124

125+
@doc """
126+
Function allows add file to tracker, which will be removed on Temp.cleanup
127+
"""
128+
@spec track_file(any) :: {:error, :tracker_not_found} | {:ok, Path.t}
129+
def track_file(path, tracker \\ get_tracker()) do
130+
case is_nil(tracker) do
131+
true -> {:error, :tracker_not_found}
132+
false -> {:ok, register_path(tracker, path)}
133+
end
134+
end
135+
125136
@doc """
126137
Same as `open/1`, but raises an exception on failure.
127138
"""

test/temp_test.exs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,19 @@ defmodule TempTest do
120120
assert_receive {:cleaned, 1}
121121
refute File.exists?(dir)
122122
end
123+
124+
test :track_file do
125+
assert {:ok, tracker} = Temp.track
126+
127+
path_of_tmp_file = "test/tmp_file_created_by_programmer"
128+
File.write!(path_of_tmp_file, "Make Elixir Gr8 Again")
129+
130+
assert File.exists?(path_of_tmp_file)
131+
132+
Temp.track_file(path_of_tmp_file, tracker)
133+
134+
Temp.cleanup(tracker)
135+
136+
refute File.exists?(path_of_tmp_file)
137+
end
123138
end

0 commit comments

Comments
 (0)