我们正试图在一个完全干净的dets环境中运行每个测试,每个测试都负责自己的设置。我们遇到了无法在测试之间完全删除目录的问题。
我们如何重置dets
?我们错误地假设吹走整个目录并重新启动应用程序是否足够设置?在运行之间删除所有记录是否更好?
假设:
async: true
未设置任何测试。dets
是必需的示例代码:
setup do
#clean up anything that wasn't already clean (failed teardown, etc)
TestSetup.teardown_dets(:myappatom, "dets")
on_exit fn ->
TestSetup.teardown_dets(:myappatom, "dets")
Application.ensure_all_started(:myappatom)
Process.sleep(300)
end
end
拆解功能的胆量......
def teardown_dets(application_atom, directory) when is_bitstring(directory) do
teardown_dets(application_atom, [directory])
end
def teardown_dets(application_atom, directories)
when is_atom(application_atom) and is_list(directories)
do
#stop the applications completely
Application.stop(application_atom)
#blow away all dets while the application is stopped
try do
directories
|> Enum.map(&File.rm_rf(&1))
rescue
error in File.Error -> IO.puts("First file removal failure. Re-attempting. Initial error information #{error.message}")
after
Process.sleep(3000) #wait for whatever it was to get out of the way
directories
|> Enum.map(&File.rm_rf!(&1))
end
end
运行mix test
时的示例错误是stdout显示:
dets: file "dets/event_listener_stream_positions_test" not properly closed, repairing ...
但不是错误,只是定期输出到控制台。
然后是测试中的实际失败:
** (File.Error) could not remove files and directories recursively from "dets": file already exists