I am trying to use anyway_config for the first time in a plain Ruby program with RSpec tests. The docs do not give as much guidance as I would like. The end of this posting shows the pressing issue that I need to solve, plus a related bonus question that would be so helpful to me.
My config class is GitTree::GTConfig.
require "anyway"
require "anyway/testing"
# See https://github.com/palkan/anyway_config?tab=readme-ov-file#usage
module GitTree
# Enables loading config from `treeconfig.yml` and `treeconfig.local.yml` files.
# By default, Anyway! looks for yml files in
# - `./config/treeconfig.yml`
# - `./.treeconfig.yml`
# - `~/.config/treeconfig.yml`
# - `~/.treeconfig.yml`
class GTConfig < Anyway::Config
config_name :treeconfig
Anyway::Settings.default_config_path = "config/treeconfig.yml"
# All environment variables will be prefixed with `GIT_TREE_`
env_prefix :git_tree
# Add required attributes with default values
attr_config git_timeout: 300,
verbosity: ::Logging::NORMAL,
default_roots: %w[sites sitesUbuntu work]
# See https://github.com/palkan/anyway_config?tab=readme-ov-file#required-options
required :git_timeout,
:verbosity,
:default_roots
# See https://github.com/palkan/anyway_config?tab=readme-ov-file#multi-env-configuration
Anyway::Settings.future.use :unwrap_known_environments
# See https://github.com/palkan/anyway_config?tab=readme-ov-file#source-tracing
# Anyway::Settings.enable_source_tracing
# On_load validators must not accept any arguments
on_load :validate_environment
on_load :log_environment
private
# Raise RuntimeError if a configuration error
def validate_environment
raise "The Anyway::Settings environment is not set" unless Anyway::Settings.current_environment
end
def log_environment
$stdout.puts "Current environment: #{Anyway::Settings.current_environment}" # if Anyway::Settings.verbosity >= ::Logging::VERBOSE
end
end
end
config/treeconfig.yml:
test:
git_timeout: 7
verbosity: NORMAL
default_roots:
development:
git_timeout: 7
verbosity: NORMAL
default_roots:
production:
git_timeout: 300
verbosity: NORMAL
default_roots: [sites, sitesUbuntu, work]
This is my RSpec code:
require "spec_helper"
require "anyway/testing"
describe GitTree::GTConfig, type: :config do
include Anyway::Testing::Helpers
let(:config) { described_class.new }
# Reset to default config before each example
around do |ex|
with_config(default_roots: %w[a b]) do
x = ex.run
puts "x is a #{x.class.name}"
end
end
context "when the environment is not set" do
it "raises an error" do
Anyway::Settings.current_environment = nil
expect { config }.to raise_error(RuntimeError, /Anyway::Settings environment/)
end
end
context "when the environment is set" do
before do
Anyway::Settings.current_environment = "test"
end
after do
Anyway::Settings.current_environment = nil
end
it "does not raise an error" do
expect { config }.not_to raise_error
end
context "with yaml configuration" do
it "loads configuration from a YAML file" do
with_config(default_roots: %w[c d]) do
expect(config.default_roots).to eq(%w[c d])
end
end
it "loads configuration from a YAML file with a specific location" do
with_config_path("spec/fixtures/treeconfig.yml") do
expect(config.default_roots).to eq(%w[e f])
end
end
end
context "with environment variables" do
it "loads configuration from environment variables" do
with_env("GIT_TREE_DEFAULT_ROOTS" => "g h") do
expect(config.default_roots).to eq(%w[g h])
end
end
end
context "with source tracing" do
it "traces the source of the configuration" do
with_config(default_roots: %w[c d]) do
expect(config.to_source_trace["default_roots"]).to eq(default: %w[a b], test: { "default_roots" => %w[c d] })
end
end
end
end
end
The tests all have similar complaints:
NoMethodError:
undefined method 'with_config' for #<RSpec::ExampleGroups::GitTreeGTConfig::WhenTheEnvironmentIsNotSet:0x000078d34db7aa90>
# ./spec/git_tree/gt_config_spec.rb:11:in 'block (2 levels) in <top (required)>'
# ./binstub/rspec:16:in 'Kernel#load'
# ./binstub/rspec:16:in '<main>'
Furthermore, my program has integer constants defined for verbosity; NORMAL has value 1. How can the text values ("NORMAL") specified in treeconfig.yml be converted to integers when read?
with_configdoes not appear to be a method anywhere in the code base. Did you possibly meanwith_env? Additionallyinclude Anyway::Testing::Helpersis not needed asrequire "anyway/testing"will handle this inclusion for you.with_configis mentioned. How can I troubleshoot this? The erb idea is cool!