voger

voger

How to test config options?

I have a library that uses option parser to format the output. The input is a string coming from the code itself, not a shell.

I am using this code to merge the switches given from the user with some predefined switches. The predefined switches are:

  • The default hard coded in the module
  • If are there any coming from config.exs merge them with the hard coded

If the user provides own switches, they will be merged to the predefined switches

defmodule Clouseau.Switches do
  #...
  @predefined_switches [
    file: true,
    full_path: false,
    module: true,
    line: true,
    text: true,
    border: false,
    colors: false
  ]

  #...
  @default_switches Keyword.merge(
                      @predefined_switches,
                      Application.get_env(
                        :clouseau, 
                        :default_switches, 
                        @predefined_switches
                        )
                    )

 #...
  defp apply(switches) do
    Keyword.merge(@default_switches, switches)
  end
end

The @default_switches module attribute is set during compile and Application.put_env(:clouseau, :default_switches, file: false) has no effect.

How can I test that changing the config options changes the behavior of my libray?

So far I came up with this test file.

defmodule SwitchesTest do
  use ClouseauCase

  setup_all  do
    # Save current config if exists
    env_switches = Application.get_env(:clousau, :default_switches)
    # Put new config 
    Application.put_env(:clouseau, :default_switches, file: false)
    # Recompile the file to refresh the @default_switches module attribute
    Kernel.ParallelCompiler.compile(["lib/switches.ex"])

    # On exit revert :default_switches config
    on_exit(fn ->
      # If it existed before put back the old one
      if env_switches do
        Application.put_env(:clouseau, :default_switches, env_switches)
      # if didn't exist before just delete the current one
      else
        Application.delete_env(:clouseau, :default_switches)
      end
      
      # Regenerate the @default_switches module attribute
      Kernel.ParallelCompiler.compile(["lib/switches.ex"])
    end)
  end
end

In the setup_all block I save the current config, recompile the switches module, and on_exit I revert back whatever config was there and recompile once again the switches module.

This works so far but it shows warnings during the test

warning: redefining module Clouseau.Switches (current version loaded from _build/test/lib/clouseau/ebin/Elixir.Clouseau.Switches.beam)
  lib/switches.ex:1

.warning: redefining module Clouseau.Switches (current version defined in memory)
  lib/switches.ex:1

Is there any better way to test the config options? Or maybe a better way to design the switches module but avoiding merging the hard coded switches with the configured every time in runtime?

Most Liked

LostKobrakai

LostKobrakai

I can see why keeping config in compile time can be desired, but if you can build your library to be configurable at runtime you can test it without being dependent on compile time. If you really want to stay at compile time you might want to look at ex_cldr, which does a bunch of compile time optimazations as well.

OvermindDL1

OvermindDL1

Yeah that’s my issue with the general config style. I still think we need a staged configuration system like I’ve used in other languages. Essentially you have global configuration, which only affects code-gen and acts as defaults for the later stages. Then there is the load-time configs, which should only affect things that happen at load time, in addition to acting as defaults for later stages. Then run-time configuration, which when changed then updates the configuration (users of this should either access it every time they need it, or for something like a database link it should receive an event when it changes so it can update as necessary), which of course acts as defaults for later stages. And finally there is the scoped stage which is where you pass in the configuration overrides to a specific call or instance of something.

This is similar to how I treat my TaskAfter library. I keep meaning to make a StagedConfiguration library for Elixir sometime, it would simplify a lot of my own work…

voger

voger

I have read that post and I read it again now. Unfortunately it looks like it is no easy way to put custom config at runtime while avoiding merges during runtime.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

Other popular topics Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement