Skip to content

Commit

Permalink
Write the file in binary mode to fix any encoding issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jmortlock committed May 11, 2017
1 parent 6c009a4 commit 60b97fb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/pansophy/local/create_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def initialize(path, body)
def call(options = {})
prevent_overwrite! unless options[:overwrite]
@pathname.dirname.mkpath
::File.write(@pathname, @body)
::File.open(@pathname, 'wb') do |f|
f.write @body
end
end

private
Expand Down
14 changes: 13 additions & 1 deletion spec/synchronizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
let(:local_directory) { Pathname.new(__FILE__).dirname.expand_path.join(local_directory_name) }
let(:local_file) { 'wat.txt' }
let(:file_name) { 'test.yml' }
let(:file_body) { { test: true }.to_yaml }
let(:file_body) { { test: true, message: '用' }.to_yaml }
let(:inner_file_1) { 'sub/inner1.txt' }
let(:inner_file_2) { 'sub/inner2.txt' }
let(:inner_file_1_body) { 'inner file 1' }
Expand Down Expand Up @@ -321,6 +321,18 @@ def get_remote_file(path)
expect(path).not_to exist
end

context 'when creating a file with default internal encoding set to utf-8' do
let(:file_body) { '用'.force_encoding('ASCII-8BIT') }
before do
Encoding.default_internal = Encoding::UTF_8
create_file.call
end

it 'should save the file correctly, and be readable as utf-8' do
expect(path.read).to eq file_body.force_encoding('UTF-8')
end
end

context 'when creating a file' do
before do
create_file.call
Expand Down

0 comments on commit 60b97fb

Please sign in to comment.