From 381e4ea42977bc8c6c2a236db1f65bc1a5707904 Mon Sep 17 00:00:00 2001 From: Jordan Day Date: Mon, 18 Aug 2014 10:59:14 -0500 Subject: [PATCH] Issue #1901: Don't crash if provided config file is empty. Don't return result of YAML.load if false added tests around empty config file update changelog --- Changes.md | 1 + lib/sidekiq/cli.rb | 2 +- test/test_cli.rb | 29 +++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Changes.md b/Changes.md index ef03956c..345a5a81 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,7 @@ ----------- - Add queues list for each process to the Busy page. [davetoxa, #1897] +- Fix for crash caused by empty config file. [jordan0day, #1901] 3.2.2 ----------- diff --git a/lib/sidekiq/cli.rb b/lib/sidekiq/cli.rb index 5d943afe..55a0583c 100644 --- a/lib/sidekiq/cli.rb +++ b/lib/sidekiq/cli.rb @@ -353,7 +353,7 @@ module Sidekiq def parse_config(cfile) opts = {} if File.exist?(cfile) - opts = YAML.load(ERB.new(IO.read(cfile)).result) + opts = YAML.load(ERB.new(IO.read(cfile)).result) || opts opts = opts.merge(opts.delete(environment) || {}) parse_queues(opts, opts.delete(:queues) || []) else diff --git a/test/test_cli.rb b/test/test_cli.rb index e6d03d25..f5b15baa 100644 --- a/test/test_cli.rb +++ b/test/test_cli.rb @@ -241,6 +241,35 @@ class TestCli < Sidekiq::Test end end + describe 'with an empty config file' do + before do + @tmp_file = Tempfile.new('sidekiq-test') + @tmp_path = @tmp_file.path + @tmp_file.close! + end + + after do + File.unlink @tmp_path if File.exist? @tmp_path + end + + it 'takes a path' do + @cli.parse(['sidekiq', '-C', @tmp_path]) + assert_equal @tmp_path, Sidekiq.options[:config_file] + end + + it 'should have an identical options hash, except for config_file' do + @cli.parse(['sidekiq']) + old_options = Sidekiq.options.clone + + @cli.parse(['sidekiq', '-C', @tmp_path]) + new_options = Sidekiq.options.clone + refute_equal old_options, new_options + + new_options.delete(:config_file) + assert_equal old_options, new_options + end + end + describe 'with config file and flags' do before do # We need an actual file here.