Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
preciz committed Jul 20, 2024
1 parent 0f1f175 commit ad6f130
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/tmp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ defmodule Tmp do
Tmp.Worker.execute(monitor, path, function, timeout)
end

defp dirname(_prefix = nil), do: rand_dirname()
defp dirname(prefix), do: prefix <> "-" <> rand_dirname()
@doc false
def dirname(_prefix = nil), do: rand_dirname()
def dirname(prefix), do: prefix <> "-" <> rand_dirname()

defp rand_dirname do
@doc false
def rand_dirname do
sec = :os.system_time(:second) |> Integer.to_string()
rand = :crypto.strong_rand_bytes(5) |> Base.encode16(case: :lower)

Expand Down
15 changes: 15 additions & 0 deletions test/tmp_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,19 @@ defmodule TmpTest do
prefix: "test_custom_base_dir_module"
)
end

test "dirname generates correct format with nil prefix" do
dirname = Tmp.dirname(nil)
assert String.match?(dirname, ~r/^\d+-[a-f0-9]{10}$/)
end

test "dirname generates correct format with non-nil prefix" do
dirname = Tmp.dirname("test")
assert String.match?(dirname, ~r/^test-\d+-[a-f0-9]{10}$/)
end

test "rand_dirname generates unique names" do
names = for _ <- 1..100, do: Tmp.rand_dirname()
assert length(Enum.uniq(names)) == 100
end
end

0 comments on commit ad6f130

Please sign in to comment.