Skip to content

Commit 7122c50

Browse files
committed
[Fix rubocop#2213] Write to cache in binary mode
If the cache file is opened for writing with UTF-8 encoding (for example) there can be an exception when the marshalled data with ASCII-8BIT encoding is written to file.
1 parent cb00a0c commit 7122c50

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* [#2212](https://github.com/bbatsov/rubocop/issues/2212): Handle methods without parentheses in auto-correct. ([@karreiro][])
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][])
10+
* [#2213](https://github.com/bbatsov/rubocop/issues/2213): Write to cache with binary encoding to avoid transcoding exceptions in some locales. ([@jonas054][])
1011

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

lib/rubocop/result_cache.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def load
2626
def save(offenses, disabled_line_ranges, comments)
2727
FileUtils.mkdir_p(File.dirname(@path))
2828
preliminary_path = "#{@path}_#{rand(1_000_000_000)}"
29-
File.open(preliminary_path, 'w') do |f|
29+
File.open(preliminary_path, 'wb') do |f|
3030
# The Hash[x.sort] call is a trick that converts a Hash with a default
3131
# block to a Hash without a default block. Thus making it possible to
3232
# dump.

spec/rubocop/result_cache_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ def abs(path)
8282
end
8383
end
8484

85+
describe '#save' do
86+
context 'when the default internal encoding is UTF-8' do
87+
let(:comments) { ["# Hello \xF0"] }
88+
before(:each) { Encoding.default_internal = Encoding::UTF_8 }
89+
after(:each) { Encoding.default_internal = nil }
90+
91+
it 'writes non UTF-8 encodable data to file with no exception' do
92+
cache.save(offenses, disabled_lines, comments)
93+
end
94+
end
95+
end
96+
8597
describe '.cleanup' do
8698
before do
8799
cfg = { 'AllCops' => { 'MaxFilesInCache' => 1 } }

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