Skip to content

Commit cd0826d

Browse files
committed
Merge pull request rubocop#2219 from maxjacobson/fix-partially-loading-safe-yaml
Fix error when safe yaml is only partially loaded
2 parents 254317f + 5a39130 commit cd0826d

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* [#2214](https://github.com/bbatsov/rubocop/pull/2214): Fix `File name too long error` when `STDIN` option is provided. ([@mrfoto][])
99
* [#2217](https://github.com/bbatsov/rubocop/issues/2217): Allow block arguments in `Style/SymbolProc`. ([@lumeet][])
1010
* [#2213](https://github.com/bbatsov/rubocop/issues/2213): Write to cache with binary encoding to avoid transcoding exceptions in some locales. ([@jonas054][])
11+
* [#2218](https://github.com/bbatsov/rubocop/issues/2218): Fix loading config error when safe yaml is only partially loaded. ([@maxjacobson][])
1112

1213
## 0.34.0 (05/09/2015)
1314

lib/rubocop/config_loader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def load_yaml_configuration(absolute_path)
148148
def yaml_safe_load(yaml_code)
149149
if YAML.respond_to?(:safe_load) # Ruby 2.1+
150150
if defined?(SafeYAML)
151-
YAML.safe_load(yaml_code, nil, whitelisted_tags: %w(!ruby/regexp))
151+
SafeYAML.load(yaml_code, nil, whitelisted_tags: %w(!ruby/regexp))
152152
else
153153
YAML.safe_load(yaml_code, [Regexp])
154154
end

spec/rubocop/config_loader_spec.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -300,25 +300,25 @@
300300
])
301301
end
302302

303-
it 'de-serializes Regexp class' do
304-
if ::Process.respond_to?(:fork)
305-
# To load SafeYAML in different memory space
306-
pid = ::Process.fork do
307-
# Need to write coverage result under different name
308-
if defined?(SimpleCov)
309-
SimpleCov.command_name "rspec_#{Process.pid}"
310-
SimpleCov.pid = Process.pid
311-
end
312-
313-
require 'safe_yaml'
303+
context 'when it is fully required' do
304+
it 'de-serializes Regexp class' do
305+
in_its_own_process_with('safe_yaml') do
306+
configuration = described_class.load_file('.rubocop.yml')
307+
308+
word_regexp = configuration['Style/WordArray']['WordRegex']
309+
expect(word_regexp).to be_a(::Regexp)
310+
end
311+
end
312+
end
313+
314+
context 'when safe_yaml is required without monkey patching' do
315+
it 'de-serializes Regexp class' do
316+
in_its_own_process_with('safe_yaml/load') do
314317
configuration = described_class.load_file('.rubocop.yml')
315318

316319
word_regexp = configuration['Style/WordArray']['WordRegex']
317320
expect(word_regexp).to be_a(::Regexp)
318321
end
319-
::Process.wait(pid)
320-
else
321-
warn 'Process.fork is not available.'
322322
end
323323
end
324324
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# encoding: utf-8
2+
3+
# RuboCop can be run in contexts where unexpected other libraries are included,
4+
# which may interfere with its normal behavior. In order to test those
5+
# situations, it may be necessary to require another library for the duration
6+
# of one spec
7+
module HostEnvironmentSimulatorHelper
8+
def in_its_own_process_with(*files, &block)
9+
if ::Process.respond_to?(:fork)
10+
pid = ::Process.fork do
11+
# Need to write coverage result under different name
12+
if defined?(SimpleCov)
13+
SimpleCov.command_name "rspec_#{Process.pid}"
14+
SimpleCov.pid = Process.pid
15+
end
16+
17+
files.each { |file| require file }
18+
block.call
19+
end
20+
::Process.wait(pid)
21+
22+
# assert that the block did not fail
23+
expect($CHILD_STATUS).to be_success
24+
else
25+
warn 'Process.fork is not available.'
26+
end
27+
end
28+
end
29+
30+
RSpec.configure do |config|
31+
config.include HostEnvironmentSimulatorHelper
32+
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy