# encoding: utf-8 require 'spec_helper' require 'fedux_org_stdlib/app_config' RSpec.describe AppConfig do context 'options' do it 'creates readers and writers' do with_environment 'HOME' => working_directory do config_klass = Class.new(AppConfig) do option :opt1, nil def class_name 'TestConfig' end def module_name 'MyApplication' end end config = config_klass.new expect(config.opt1).to be_nil config.opt1 = 'blub' expect(config.opt1).to eq 'blub' end end it 'creates readers' do with_environment 'HOME' => working_directory do config_klass = Class.new(AppConfig) do option_reader :opt1, nil def class_name 'TestConfig' end def module_name 'MyApplication' end end config = config_klass.new expect(config.opt1).to be_nil expect { config.opt1 = 'blub' }.to raise_error NoMethodError end end end context 'config files' do it 'looks at ~/.test.yaml' do with_environment 'HOME' => working_directory do create_file '.tests.yaml', <<-EOS.strip_heredoc --- opt1: hello world EOS config_klass = Class.new(AppConfig) do option_reader :opt1, nil def class_name 'TestConfig' end def module_name 'MyApplication' end end config = config_klass.new expect(config.opt1).to eq 'hello world' end end it 'looks at ~/.config/my_application/tests.yaml' do with_environment 'HOME' => working_directory do create_file '.config/my_application/tests.yaml', <<-EOS.strip_heredoc --- opt1: hello world EOS config_klass = Class.new(AppConfig) do option_reader :opt1, nil def class_name 'TestConfig' end def module_name 'MyApplication' end end config = config_klass.new expect(config.opt1).to eq 'hello world' end end end it 'looks at ~/.my_application/tests.yaml' do with_environment 'HOME' => working_directory do create_file '.my_application/tests.yaml', <<-EOS.strip_heredoc --- opt1: hello world EOS config_klass = Class.new(AppConfig) do option_reader :opt1, nil def class_name 'TestConfig' end def module_name 'MyApplication' end end config = config_klass.new expect(config.opt1).to eq 'hello world' end end it 'looks at ~/.tests.yaml' do with_environment 'HOME' => working_directory do create_file '.tests.yaml', <<-EOS.strip_heredoc --- opt1: hello world EOS config_klass = Class.new(AppConfig) do option_reader :opt1, nil def class_name 'TestConfig' end def module_name 'MyApplication' end end config = config_klass.new expect(config.opt1).to eq 'hello world' end end it 'looks at ~/.testsrc' do with_environment 'HOME' => working_directory do create_file '.testsrc', <<-EOS.strip_heredoc --- opt1: hello world EOS config_klass = Class.new(AppConfig) do option_reader :opt1, nil def class_name 'TestConfig' end def module_name 'MyApplication' end end config = config_klass.new expect(config.opt1).to eq 'hello world' end end context '#to_s' do it 'outputs a list of variables' do with_environment 'HOME' => working_directory do config_klass = Class.new(AppConfig) do option :opt1, 'test1' option :opt2, 'test2' def class_name 'TestConfig' end def module_name 'MyApplication' end end config = config_klass.new expect(config.to_s).to eq <<-EOS.strip_heredoc.chomp option | value -------------------- + -------------------------------------------------------------------------------- opt1 | test1 opt2 | test2 EOS end end end end