From ad6f1301c4c5a87bb877d70ff7bd984a0fa53344 Mon Sep 17 00:00:00 2001 From: preciz Date: Sat, 20 Jul 2024 23:10:13 +0200 Subject: [PATCH] Add more tests --- lib/tmp.ex | 8 +++++--- test/tmp_test.exs | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/tmp.ex b/lib/tmp.ex index 52ad86d..2a975bf 100644 --- a/lib/tmp.ex +++ b/lib/tmp.ex @@ -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) diff --git a/test/tmp_test.exs b/test/tmp_test.exs index 114ee4b..1fbfdc9 100644 --- a/test/tmp_test.exs +++ b/test/tmp_test.exs @@ -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