Skip to content

Commit

Permalink
Handle Psych::SyntaxError issues when parsing invalid YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjrice committed Jan 16, 2013
1 parent ac7a6e9 commit 06af52e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/asset_sync/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ def yml_exists?
end

def yml
@yml ||= YAML.load(ERB.new(IO.read(yml_path)).result)[Rails.env] rescue nil || {}
begin
@yml ||= YAML.load(ERB.new(IO.read(yml_path)).result)[Rails.env] rescue nil || {}
rescue Psych::SyntaxError
@yml = {}
end
end

def yml_path
Expand Down
24 changes: 24 additions & 0 deletions spec/fixtures/with_invalid_yml/config/asset_sync.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defaults: &defaults
fog_provider= "AWS"
aws_access_key_id: "xxxx"
aws_secret_access_key: "zzzz"
region: "eu-west-1"

development:
<<: *defaults
fog_directory: "rails_app_development"
existing_remote_files: keep

test:
<<: *defaults
fog_directory: "rails_app_test"
existing_remote_files: keep

production:
<<: *defaults
fog_directory: "rails_app_production"
existing_remote_files: delete

hybrid:
<<: *defaults
enabled: false
18 changes: 18 additions & 0 deletions spec/unit/asset_sync_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,22 @@
end
end

describe 'with invalid yml' do

before(:each) do
set_rails_root('with_invalid_yml')
AssetSync.config = AssetSync::Config.new
end

it "config should be invalid" do
AssetSync.config.valid?.should be_false
end

it "should raise a config invalid error" do
lambda{ AssetSync.sync }.should raise_error(AssetSync::Config::Invalid)
end


end

end

0 comments on commit 06af52e

Please sign in to comment.