Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.10.1 fix issue #268 #269

Merged
merged 2 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

# SmarterCSV 1.x Change Log

## 1.10.1 (2024-01-07)
* fix incorrect warning about UTF-8 (issue #268, thanks hirowatari)

## 1.10.0 (2023-12-31) ⚡ BREAKING ⚡

* BREAKING CHANGES:
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ A Big Thank you to everyone who filed issues, sent comments, and who contributed
* [Rahul Chaudhary](https://github.com/rahulch95)
* [Alessandro Fazzi](https://github.com/pioneerskies)
* [JP Camara](https://github.com/jpcamara)
* [Hiro Watari](https://github.com/hirowatari)
2 changes: 1 addition & 1 deletion lib/smarter_csv/smarter_csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def SmarterCSV.process(input, given_options = {}, &block) # rubocop:disable Lint
begin
fh = input.respond_to?(:readline) ? input : File.open(input, "r:#{options[:file_encoding]}")

if @enforce_utf8 && (fh.respond_to?(:external_encoding) && fh.external_encoding != Encoding.find('UTF-8') || fh.respond_to?(:encoding) && fh.encoding != Encoding.find('UTF-8'))
if (options[:force_utf8] || options[:file_encoding] =~ /utf-8/i) && (fh.respond_to?(:external_encoding) && fh.external_encoding != Encoding.find('UTF-8') || fh.respond_to?(:encoding) && fh.encoding != Encoding.find('UTF-8'))
puts 'WARNING: you are trying to process UTF-8 input, but did not open the input with "b:utf-8" option. See README file "NOTES about File Encodings".'
end

Expand Down
2 changes: 1 addition & 1 deletion lib/smarter_csv/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module SmarterCSV
VERSION = "1.10.0"
VERSION = "1.10.1"
end
64 changes: 64 additions & 0 deletions spec/smarter_csv/file_encoding_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe SmarterCSV do
describe 'encoding warning message' do
let(:file_path) { 'path/to/csvfile.csv' }
let(:file_content) { "some,content\nwith,lines" }
let(:file_double) { StringIO.new(file_content) }

before do
allow(File).to receive(:open).with(file_path, anything).and_return(file_double)
end

context 'with force_utf8 option and non-UTF-8 file encoding' do
let(:options) { { force_utf8: true } }

before do
allow(file_double).to receive(:external_encoding).and_return(Encoding.find('ISO-8859-1'))
end

it 'prints a warning about UTF-8 processing' do
expect { described_class.process(file_path, options) }.to output(/WARNING: you are trying to process UTF-8 input/).to_stdout
end
end

context 'with utf-8 file_encoding option and non-UTF-8 file encoding' do
let(:options) { { file_encoding: 'utf-8' } }

before do
allow(file_double).to receive(:external_encoding).and_return(Encoding.find('ISO-8859-1'))
end

it 'prints a warning about UTF-8 processing' do
expect { described_class.process(file_path, options) }.to output(/WARNING: you are trying to process UTF-8 input/).to_stdout
end
end

context 'with non-matching file_encoding option and non-UTF-8 file encoding' do
let(:options) { { file_encoding: 'other-encoding' } }

before do
allow(file_double).to receive(:external_encoding).and_return(Encoding.find('ISO-8859-1'))
end

it 'does not print a warning about UTF-8 processing' do
expect { described_class.process(file_path, options) }.not_to output(/WARNING: you are trying to process UTF-8 input/).to_stdout
end
end

context 'with force_utf8 option and UTF-8 file encoding' do
let(:options) { { force_utf8: true } }

before do
allow(file_double).to receive(:external_encoding).and_return(Encoding.find('UTF-8'))
end

it 'does not print a warning about UTF-8 processing' do
expect { described_class.process(file_path, options) }.not_to output(/WARNING: you are trying to process UTF-8 input/).to_stdout
end
end
end
end