mix sims.gen.config_module (sims v0.2.0)

Copy Markdown View Source

Generate a swappable configuation module

A centeralized configuration module is helpful so configuration can be swapped during async tests via mox.

Example

mix sims.gen.config_module

Generated Modules

This task generates three modules and updates your test helper:

Configuration Module

The main configuration module (e.g., YourApp.Config) acts as the central configuration interface for your application. It delegates to the configured adapter, which can be swapped at runtime (particularly useful for testing).

The adapter is retrieved from application config with a key :config_adapter, falling back to the default adapter in production.

Adapter Behaviour

The behaviour module (e.g., YourApp.Config.Adapter) defines the interface that all adapter implementations must follow. You can add callbacks to this module to define the functions your configuration needs to support.

Default Adapter

The default adapter module (e.g., YourApp.Config.DefaultAdapter) implements the adapter behaviour and is used in production. This is where you'll add your actual configuration logic.

Test Mock

A Mox mock (e.g., YourApp.Config.MockAdapter) is defined in test/test_helper.exs and automatically configured as the adapter for tests. This allows you to mock configuration behavior in your tests using Mox's expect/3 and stub/3 functions.

Options

  • --config-namespace - The module name for the configuration namespace. If not provided, defaults to YourApp.Config.

  • --config-behaviour - The module name for the adapter behaviour that defines the interface that adapters must implement. If not provided, a default name will be generated based on the config namespace.

  • --config-default-adapter - The module name for the default adapter implementation. If not provided, a default name will be generated based on the config namespace.

  • --config-test-adapter - The module name for the test adapter mock. If not provided, a default name will be generated based on the config namespace.

  • --update-test-helper - Whether to automatically update test/test_helper.exs to add the Mox mock definition. Defaults to true. Set to false with --no-update-test-helper to skip this step.

Setting Default Values in config/config.exs

You can configure default values for the options in your config/config.exs file to avoid having to pass them on the command line every time. These defaults will be used when running any sims.gen.* tasks.

Add a configuration block for your application under the :sims key:

# config/config.exs
import Config

config :your_app, :sims,
  config_namespace: "YourApp.Config",
  config_behaviour: "YourApp.Config.Adapter",
  config_default_adapter: "YourApp.Config.DefaultAdapter",
  config_test_adapter: "YourApp.Config.MockAdapter",
  update_test_helper: true

Command-line options will override these configured defaults if provided.