beam-community / bamboo

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

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

Open Stamates opened 1 year ago

Stamates commented 1 year 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).