Background
I have a module that reads information from a CSV file and puts it in memory. As with all modules that read things from files, there are a myriad of errors that can occur (file missing, corrupted file, permissions, etc).
This module is critical to the application. Without it, the application does nothing. I need to know of a way to startup the application while being able to test it’s startup.
Code
This is the code as I have envisioned:
defmodule Engine.Application do
use Application
#module that reads data from file and puts it into memory. Nothing special.
alias Engine.Populator
def start(_type, _args) do
children = []
opts = [strategy: :one_for_one, name: Engine.Supervisor]
case Populator.init("super_duper_file.csv") do
{:ok, :start_success} -> Supervisor.start_link(children, opts)
{:error, :reason1} -> IO.puts("Fix it by doing something you dummy!")
{:error, :reason2} -> IO.puts("We could do something, but I'm too lazy")
err -> IO.puts("¯\_(ツ)_/¯")
end
end
end
However, this code can’t be tested. I can’t test what happens if Populator.init fails with :reason2 because to do it I need to create a test that invokes Engine.Application.start, which mix has already invoked to run said test (so it will always fail with :already_started error).
Question
So I take it this logic shouldn’t probably be here. Someone suggested I should turn this into something supervised, and that is what I am trying to do.
But I can’t find a logical way to turn a module that simply reads data from a file into something that is it’s own supervised process. I also want to have decent error messages instead of having the app just blow into my face.
How can I fix this?






















