file_helpers.exs (1020B)
1 defmodule Support.FileHelpers do 2 import ExUnit.Assertions 3 4 @doc """ 5 Returns the `tmp_path` for tests. 6 """ 7 def tmp_path do 8 Path.expand("../../tmp", __DIR__) 9 end 10 11 @doc """ 12 Executes the given function in a temp directory 13 tailored for this test case and test. 14 """ 15 defmacro in_tmp(fun) do 16 path = Path.join([tmp_path(), "#{__CALLER__.module}", "#{elem(__CALLER__.function, 0)}"]) 17 quote do 18 path = unquote(path) 19 File.rm_rf!(path) 20 File.mkdir_p!(path) 21 File.cd!(path, fn -> unquote(fun).(path) end) 22 end 23 end 24 25 @doc """ 26 Asserts a file was generated. 27 """ 28 def assert_file(file) do 29 assert File.regular?(file), "Expected #{file} to exist, but does not" 30 end 31 32 @doc """ 33 Asserts a file was generated and that it matches a given pattern. 34 """ 35 def assert_file(file, callback) when is_function(callback, 1) do 36 assert_file(file) 37 callback.(File.read!(file)) 38 end 39 40 def assert_file(file, match) do 41 assert_file file, &(assert &1 =~ match) 42 end 43 end