beam-community / bamboo

Testable, composable, and adapter based Elixir email library for devs that love piping.
https://hex.pm/packages/bamboo
MIT License
1.91k stars 345 forks source link

Fix Regex match failure on `refute_email_delivered_with/2` #645

Open Stamates opened 2 years ago

Stamates commented 2 years ago

We often have dynamic content (and emojis) in the subject line of emails such that we normally use a Regex pattern match. Example: assert_email_delivered_with(subject: ~r/Request/)

As a testing pattern, we'll have a second test that is expected to NOT send the same email based on other failing conditions. For test readability, it would be great to just use refute_email_delivered_with(subject: ~r/Request/), unfortunately, that returns the following error:

** (FunctionClauseError) no function clause matching in Regex.match?/2

The following arguments were given to Regex.match?/2:

   # 1
   ~r/Request/

   # 2
   nil

Attempted function clauses (showing 1 out of 1):
   def match?(%Regex{} = regex, string) when is_binary(string)

code: refute_email_delivered_with(subject: ~r/Request/)
stacktrace:
     (elixir 1.13.4) lib/regex.ex:282: Regex.match?/2
     (elixir 1.13.4) lib/enum.ex:3973: Enum.any_list/2

I believe this is due to a bug in the refute_email_delivered_with function: The received_email_params is being set to [] if no email is sent, but the if check is is_nil(received_email_params) which will be false instead of true like it should be if no email was sent. This causes else condition to be called which results in a failure because [][:some_key] == nil.

The same Regex error occurs if you try to refute_email_delivered_with using an invalid email key when an email is actually received. Seems like the solve might be adding do_match(nil, _, _), do: refute false as a first function head (if we expect providing an invalid email key for refute_email_delivered_with to pass when an email is delivered).

github-actions[bot] commented 1 month ago

This issue has been automatically marked as "stale:discard". We are sorry that we haven't been able to prioritize it yet. If this issue still relevant, please leave any comment if you have any new additional information that helps to solve this issue. We encourage you to create a pull request, if you can. We are happy to help you with that.