|
1 | | -Code.require_file("support/file_helpers.exs", __DIR__) |
| 1 | +# Copied from https://github.com/elixir-ecto/ecto/blob/v2.2/integration_test/support/file_helpers.exs |
| 2 | +defmodule Support.FileHelpers do |
| 3 | + import ExUnit.Assertions |
| 4 | + |
| 5 | + @doc """ |
| 6 | + Returns the `tmp_path` for tests. |
| 7 | + """ |
| 8 | + def tmp_path do |
| 9 | + Path.expand("../../tmp", __DIR__) |
| 10 | + end |
| 11 | + |
| 12 | + @doc """ |
| 13 | + Executes the given function in a temp directory |
| 14 | + tailored for this test case and test. |
| 15 | + """ |
| 16 | + defmacro in_tmp(fun) do |
| 17 | + path = |
| 18 | + Path.join([ |
| 19 | + tmp_path(), |
| 20 | + "#{__CALLER__.module}", |
| 21 | + "#{elem(__CALLER__.function || raise("no function"), 0)}" |
| 22 | + ]) |
| 23 | + |
| 24 | + quote do |
| 25 | + path = unquote(path) |
| 26 | + File.rm_rf!(path) |
| 27 | + File.mkdir_p!(path) |
| 28 | + File.cd!(path, fn -> unquote(fun).(path) end) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + @doc """ |
| 33 | + Asserts a file was generated. |
| 34 | + """ |
| 35 | + def assert_file(file) do |
| 36 | + assert File.regular?(file), "Expected #{file} to exist, but does not" |
| 37 | + end |
| 38 | + |
| 39 | + @doc """ |
| 40 | + Asserts a file was generated and that it matches a given pattern. |
| 41 | + """ |
| 42 | + def assert_file(file, callback) when is_function(callback, 1) do |
| 43 | + assert_file(file) |
| 44 | + callback.(File.read!(file)) |
| 45 | + end |
| 46 | + |
| 47 | + def assert_file(file, match) do |
| 48 | + assert_file(file, &assert(&1 =~ match)) |
| 49 | + end |
| 50 | +end |
| 51 | + |
2 | 52 | Mix.shell(Mix.Shell.Process) |
3 | 53 | ExUnit.start() |
0 commit comments