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

Add FakeNif #121

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ if Mix.env in [:test, :test_phoenix, :test_no_nif] do
handle_sasl_reports: false

config :appsignal, appsignal_system: Appsignal.FakeSystem
config :appsignal, appsignal_nif: Appsignal.FakeNif
end
5 changes: 5 additions & 0 deletions lib/appsignal/nif.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
defmodule Appsignal.NifBehaviour do
@callback loaded?() :: boolean()
end

defmodule Appsignal.Nif do
@behaviour Appsignal.NifBehaviour
@moduledoc """

It's a NIF! Oh no!
Expand Down
3 changes: 2 additions & 1 deletion lib/mix/tasks/appsignal.diagnose.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Mix.Tasks.Appsignal.Diagnose do
use Mix.Task

@system Application.get_env(:appsignal, :appsignal_system, Appsignal.System)
@nif Application.get_env(:appsignal, :appsignal_nif, Appsignal.Nif)

@shortdoc "Starts and tests AppSignal while validating the configuration."

Expand Down Expand Up @@ -44,7 +45,7 @@ defmodule Mix.Tasks.Appsignal.Diagnose do

agent_info = Poison.decode!(File.read!(Path.expand("../../../agent.json", __DIR__)))
IO.puts " Agent version: #{agent_info["version"]}"
IO.puts " Nif loaded: #{yes_or_no(Appsignal.Nif.loaded?)}"
IO.puts " Nif loaded: #{yes_or_no(@nif.loaded?)}"
end

defp host_information do
Expand Down
7 changes: 6 additions & 1 deletion test/mix/tasks/appsignal_diagnose_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ defmodule Mix.Tasks.Appsignal.DiagnoseTest do
import Mock

@system Application.get_env(:appsignal, :appsignal_system, Appsignal.System)
@nif Application.get_env(:appsignal, :appsignal_nif, Appsignal.Nif)

defp run do
capture_io(fn -> Mix.Tasks.Appsignal.Diagnose.run(nil) end)
end

setup do
@system.start_link
@nif.start_link

original_config = appsignal_config()

# By default, Push API key is valid
Expand Down Expand Up @@ -56,7 +59,9 @@ defmodule Mix.Tasks.Appsignal.DiagnoseTest do
end

describe "when Nif is not loaded" do
test_with_mock "outputs that the Nif is not loaded", Appsignal.Nif, [:passthrough], [loaded?: fn -> false end] do
setup do: @nif.set(:loaded?, false)

test "outputs that the Nif is not loaded" do
output = run()
assert String.contains? output, "Nif loaded: no"
end
Expand Down
19 changes: 19 additions & 0 deletions test/support/fake_nif.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Appsignal.FakeNif do
@behaviour Appsignal.NifBehaviour

def start_link do
Agent.start_link(fn -> %{} end, name: __MODULE__)
end

def set(key, value) do
Agent.update(__MODULE__, &Map.put(&1, key, value))
end

def loaded? do
Agent.get(__MODULE__, &Map.get(&1, :loaded?, true))
end

def running_in_container? do
Agent.get(__MODULE__, &Map.get(&1, :running_in_container?, true))
end
end