Mocking PWD in testing env

I’m trying to come up with a way to test configuration files for my app. The app config has several optional attributes for specifying location such as latitude and longitude coordinates or city/region/country names/codes. If no location is specified it has a fallback method for finding location of local machine based on ipinfo.io.

I have a default configuration in place but this only allows me to test the attribute set in that file. I’d like to have the ability to specify the path in the testing env such that I could use a different config file to test each of the optional attributes, including the situation where no location attribute is set.

My current configuration API just calls MyApp.Config.load_config() with no arguments. The reason for this is I have a cascading set of configuration sources: default config file (. /config/config.toml), system ENV variables, installed system-wide config file (/etc/myapp/config.toml in Linux), installed user config file (/home/$USER/.config/myapp/config.toml). Each source overwrites any previously set attributes.

Could you use the tmp_dir tag?

2 Likes

Can you inject current_path to the function being tested?

Something like this

def my_config_function(cwd \\ File.cwd!()) do
  # read files from cwd folder
end

At runtime, just ignore the optional arg and rely on the default.
At test time, use the tmp_dir tag mentioned by @srowley and pass it as the optional arg value.

1 Like

Those are great ideas. I just saw this blog post today that mentions handling runtime configuration for different environments (dev, prod, testing). While adding a parameter with a “production” default would probably be the easiest solutin, I think the ideas in that blog post are probably the "right way"to do it.

1 Like