Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Red logs [error] {:error, :invalid_file} in test environment #268

Open
williamweckl opened this issue Oct 6, 2018 · 6 comments
Open

Red logs [error] {:error, :invalid_file} in test environment #268

williamweckl opened this issue Oct 6, 2018 · 6 comments

Comments

@williamweckl
Copy link

Environment

  • Elixir version (elixir -v): 1.7.3
  • Arc version (mix deps): 0.11.0
  • Arc dependencies when applicable (mix deps): arc_ecto 0.11.0
  • Operating system: Ubuntu 18.4

Actual behavior

In my unit tests, when I'm testing arc validations using invalid file types my logs shows [error] {:error, :invalid_file}. All the tests passes but this logs are unnecessary in test environment and gives a false feeling that something is wrong. I'm noticing it since version 0.9.

Expected behavior

Not showing this logs when I'm running application in test environment.

@ghost
Copy link

ghost commented Nov 14, 2018

@williamweckl I see

19:14:04.695 request_id=2ljdu7k9mafurd8gbo00fgj1 [error] {:error, :invalid_file_path}

in my tests and I think it's arc_ecto printing that error here. Here's what I'm calling instead of cast_attachments in my code to avoid that message:

  defp maybe_cast_attachments(struct, %{"file" => %Plug.Upload{path: path}} = params) do
    case File.exists?(path) do
      true ->
        cast_attachments(struct, params, [:file], allow_paths: true)

      _ ->
        struct
    end
  end

  defp maybe_cast_attachments(struct, _params), do: struct

@dbernheisel
Copy link

@williamweckl can you verify if your current tests are using fake files, or nonexistent files? I haven't run into this, but I also usually do something like echo "Hello" > tests/fixtures/test.txt and use that file for any local tests.

@williamweckl
Copy link
Author

@dbernheisel I am using invalid extension files on purpose to test validations.

@dbernheisel
Copy link

gotcha. You might want those logs to appear in real logs then, but not in tests. right?

If so, then what I do to suppress those log outputs is to use ExUnit.CaptureIO and either swallow the logs, or assert the output.

@williamweckl
Copy link
Author

williamweckl commented Dec 1, 2018

@dbernheisel that's the exact behavior I'm expecting :) Could this be the default for the lib? In earlier versions this logs was not shown.

@danadaldos
Copy link

danadaldos commented Dec 6, 2018

As an example of this for future people who find this thread (as I did), you can do something like the following. In our case, the line

conn = post(conn, path, %{"file" => photo_upload, "valuation_id" => valuation.id})

was throwing an arc_ecto error in the test results, but not causing the tests to fail. By doing this:

      ExUnit.CaptureLog.capture_log(fn ->
        conn = post(conn, path, %{"file" => photo_upload, "valuation_id" => valuation.id})
        assert text_response(conn, 422) == "Could not insert photo"
      end)

we were able to wrap the test body in capture_log and swallow the stderror while still allowing the test to run (ExUnit evaluates the assertions correctly and will fail on a mismatch). This wasn't clear from the ExUnit.CaptureIO or ExUnit.CaptureLog docs, which both make it seem like something about the capture itself needs to be asserted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants