voger

voger

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?

Showing Posts 1 to 4

LostKobrakai

LostKobrakai

voger

voger OP

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.

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…

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

jola
Wrote about how to safely run a globally unique process in an Elixir cluster, and a scary story from the past! Learn about :global for r...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews