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

Use $callers to use expectations made in the calling process #53

Merged
merged 7 commits into from
Feb 3, 2019

Conversation

axelson
Copy link
Contributor

@axelson axelson commented Feb 1, 2019

My first implementation passed the result of Process.get(:"$callers") in to the server for processing but it felt cleaner to do that call in Mox itself.

Questions:

  • How should we stop the new tests from running when Elixir < 1.8?
    • We could do feature detection by spawning a task and checking if it has $callers set?
      • I didn't like this initially but I'm coming around to it. We could even use the same function in assert_default_raise/2
    • Use an exunit tag and exclude it on some runs?
    • Direct version detection with System.version/0
  • Should we add a test that uses spawn directly and assert that that still fails?
  • Is $callers documented anywhere besides the Elixir 1.8 release notes?

Added to the section of the docs on explicit allowances
Fix travis.yml config to run multiple checks (and remove ignored versions)
@josevalim
Copy link
Member

josevalim commented Feb 1, 2019 via email

@axelson
Copy link
Contributor Author

axelson commented Feb 1, 2019

Okay, updated with a version check to set the tags to exclude.

lib/mox.ex Outdated
defp caller_pid do
case Process.get(:"$callers") do
nil -> self()
pids when is_list(pids) -> List.last(pids)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should actually pass a list to the server and start looking up from head to tail. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay makes sense, I think we can treat self() as the first in the list of $callers and pass that whole list to the server. Then on the server we can check for any allowances set in the list of the pid's, falling back to the last entry in the passed in callers. I'll work on that tonight/this weekend.

@@ -495,7 +540,7 @@ defmodule MoxTest do

{:ok, child_pid} =
Task.start_link(fn ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using assert_default_raise, we should rather change to a spawn_link or call Process.delete(:"$callers").

@josevalim
Copy link
Member

josevalim commented Feb 1, 2019 via email

@axelson
Copy link
Contributor Author

axelson commented Feb 2, 2019

I still need to fix up the assert_default_raise in the tests but I've updated the main check in server.ex to reflect "Although I think we should get the first entry (self()) in case of no matches (the current behavior)."

But isn't defaulting to the last caller (which will be the test process in these cases) cleaner?

    owner_pid = Enum.find_value(caller_pids, List.last(caller_pids), fn caller_pid ->
      state.allowances[caller_pid][mock]
    end)

@josevalim
Copy link
Member

But isn't defaulting to the last caller (which will be the test process in these cases) cleaner?

I don't think it matters in the current code because the only scenario where it will use the default is when we don't have any allowances or expectations and that means both first or last will fail anyway?

Also condense check and add comments
@josevalim josevalim merged commit 96b0fd7 into dashbitco:master Feb 3, 2019
@josevalim
Copy link
Member

❤️ 💚 💙 💛 💜

@axelson axelson deleted the callers-automatic-allow branch February 3, 2019 17:14
@axelson
Copy link
Contributor Author

axelson commented Feb 3, 2019

Awesome, thanks!

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

Successfully merging this pull request may close these issues.

2 participants