From 0c5bdece5a305cddcef959757230955b551c07c3 Mon Sep 17 00:00:00 2001 From: MrZYX Date: Wed, 25 May 2011 23:39:54 +0200 Subject: [PATCH] warn and exit if there's no config at all --- lib/app_config.rb | 15 +++++++++++++-- spec/lib/app_config_spec.rb | 14 +++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/app_config.rb b/lib/app_config.rb index a23769f22..38da6ba7a 100644 --- a/lib/app_config.rb +++ b/lib/app_config.rb @@ -6,11 +6,16 @@ class AppConfig cattr_accessor :config_vars cattr_accessor :base_file_path + cattr_accessor :file_path def self.base_file_path @@base_file_path || File.join(Rails.root, "config", "app_base.yml") end + def self.file_path + @@file_path || File.join(Rails.root, "config", "app.yml") + end + def self.[](key) config_vars[key] end @@ -38,8 +43,8 @@ class AppConfig $stderr.puts "OH NO! Required file #{base_file_path} doesn't exist! Did you move it?" all_envs = {} end - if File.exist? "#{Rails.root}/config/app.yml" - all_envs_custom = load_config_yaml "#{Rails.root}/config/app.yml" + if File.exist?(file_path) + all_envs_custom = load_config_yaml(file_path) all_envs.deep_merge!(all_envs_custom) elsif File.exist? "#{Rails.root}/config/app_config.yml" all_envs_custom = load_config_yaml "#{Rails.root}/config/app_config.yml" @@ -51,6 +56,12 @@ class AppConfig end end + # Is there a config at all? + unless all_envs['default'] + $stderr.puts "What did you do? There's no config at all!" + Process.exit(false) + end + env = env.to_s if all_envs[env] self.config_vars = all_envs['default'].merge(all_envs[env]).symbolize_keys diff --git a/spec/lib/app_config_spec.rb b/spec/lib/app_config_spec.rb index 4106561e0..f36643f5c 100644 --- a/spec/lib/app_config_spec.rb +++ b/spec/lib/app_config_spec.rb @@ -32,11 +32,23 @@ describe AppConfig do end it "prints error if base file is missing" do AppConfig.base_file_path = "/no/such/file" + AppConfig.file_path = File.join(Rails.root, "config", "app_base.yml") AppConfig.load_config_for_environment(:test) $stderr.rewind $stderr.string.chomp.should_not be_blank end + it "prints error and exits if there's no config at all" do + AppConfig.base_file_path = "/no/such/file" + AppConfig.file_path = "/no/such/file" + + lambda { + AppConfig.load_config_for_environment(:test) + }.should raise_error SystemExit + + $stderr.rewind + $stderr.string.chomp.should_not be_blank + end end describe ".generate_pod_uri" do describe "when pod_url is prefixed with protocol" do @@ -64,4 +76,4 @@ describe AppConfig do end end end -end \ No newline at end of file +end