From 6edbbe0a8e4957ec868e0b6aa37aceede6bd044e Mon Sep 17 00:00:00 2001 From: Haroon Ahmed Date: Fri, 31 May 2019 08:12:29 +0100 Subject: [PATCH] use RACK_ENV --- lib/gel/config.rb | 14 +++----------- test/config_test.rb | 30 ++++++++---------------------- 2 files changed, 11 insertions(+), 33 deletions(-) diff --git a/lib/gel/config.rb b/lib/gel/config.rb index e4d4821..e05c127 100644 --- a/lib/gel/config.rb +++ b/lib/gel/config.rb @@ -2,7 +2,7 @@ class Gel::Config def initialize - @root = File.expand_path("~/.config/gel") + @root = ENV.fetch('GEL_CONFIG') { File.expand_path("~/.config/gel") } @path = File.join(@root, "config") @config = nil end @@ -15,14 +15,6 @@ def all config end - def config_exists? - File.exist?(@path) - end - - def config_file - File.read(@path) - end - def [](group = nil, key) if group.nil? group, key = key.split(".", 2) @@ -45,9 +37,9 @@ def []=(group = nil, key, value) def read result = {} - if config_exists? + if File.exist?(@path) context = nil - config_file.each_line do |line| + File.read(@path).each_line do |line| line.chomp! if line =~ /\A(\S[^:]*):\z/ context = result[$1] = {} diff --git a/test/config_test.rb b/test/config_test.rb index 0a33904..cd65daf 100644 --- a/test/config_test.rb +++ b/test/config_test.rb @@ -4,34 +4,20 @@ class ConfigTest < Minitest::Test def test_reading_single_item_from_config_successully - config = Class.new(Gel::Config) do - define_method(:config_file) do - File.open(fixture_file("config")).read - end - - define_method(:config_exists?) do - true - end - end - - klass = config.new + prev_gel_config = ENV['GEL_CONFIG'] + ENV['GEL_CONFIG'] = fixture_file('') + klass = Gel::Config.new assert_equal '1', klass['jobs'] assert_equal 'true', klass['gem.mit'] + ENV['GEL_CONFIG'] = prev_gel_config end def test_reading_all_from_config_successully - config = Class.new(Gel::Config) do - define_method(:config_file) do - File.open(fixture_file("config")).read - end - - define_method(:config_exists?) do - true - end - end - - output = config.new.all + prev_gel_config = ENV['GEL_CONFIG'] + ENV['GEL_CONFIG'] = fixture_file('') + output = Gel::Config.new.all assert output['jobs'] == { '' => '1' } assert output['gem'] == { 'mit' => 'true' } + ENV['GEL_CONFIG'] = prev_gel_config end end