Skip to content

Commit

Permalink
Add Ruby 3.2.x support (#37)
Browse files Browse the repository at this point in the history
Remove Ruby 2.6.x support
Fix various code lints
  • Loading branch information
abrom authored Jan 22, 2023
1 parent a140536 commit ca6b541
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 51 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['2.6', '2.7', '3.0', '3.1']
ruby-version: ['2.7', '3.0', '3.1', '3.2']

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Set up Ruby
uses: ruby/setup-ruby@v1
Expand All @@ -32,6 +32,6 @@ jobs:
run: bundle exec rspec

- name: Test & publish code coverage
uses: paambaati/codeclimate-action@v3.0.0
uses: paambaati/codeclimate-action@v3.2.0
env:
CC_TEST_REPORTER_ID: bb96c1ff9dc66724c38fb4eb54486dd72dc88a7fd6e727c034b9cf8d747d069e
13 changes: 12 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
require:
- rubocop-performance
- rubocop-rake
- rubocop-rspec

AllCops:
NewCops: enable
TargetRubyVersion: 2.6
TargetRubyVersion: 2.7

Layout/EmptyLinesAroundAttributeAccessor:
Enabled: true
Expand Down Expand Up @@ -30,6 +35,12 @@ Metrics/BlockLength:
Metrics/MethodLength:
Max: 15

RSpec/ExampleLength:
Max: 12

RSpec/MultipleExpectations:
Max: 4

Style/ClassVars:
Enabled: false

Expand Down
5 changes: 2 additions & 3 deletions henkei.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require 'henkei/version'

Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
Gem::Specification.new do |spec|
spec.name = 'henkei'
spec.version = Henkei::VERSION
spec.authors = ['Erol Fornoles', 'Andrew Bromwich']
Expand All @@ -15,7 +15,7 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
'(.doc, .docx, .pages, .odt, .rtf, .pdf) using Apache Tika toolkit'
spec.homepage = 'https://github.com/abrom/henkei'
spec.license = 'MIT'
spec.required_ruby_version = ['>= 2.6.0', '< 3.2.0']
spec.required_ruby_version = ['>= 2.7.0', '< 3.3.0']

# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
# delete this section to allow pushing this gem to any host.
Expand All @@ -37,7 +37,6 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
spec.add_development_dependency 'rspec', '~> 3.7'
spec.add_development_dependency 'rubocop', '~> 1.26'
spec.add_development_dependency 'rubocop-performance', '~> 1.13'
spec.add_development_dependency 'rubocop-rails', '~> 2.14'
spec.add_development_dependency 'rubocop-rake', '~> 0.6'
spec.add_development_dependency 'rubocop-rspec', '~> 2.9'
spec.add_development_dependency 'simplecov', '~> 0.15', '< 0.18'
Expand Down
13 changes: 6 additions & 7 deletions lib/henkei.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class Henkei # rubocop:disable Metrics/ClassLength

def self.mimetype(content_type)
if Henkei.configuration.mime_library == 'mime/types' && defined?(MIME::Types)
warn '[DEPRECATION] `mime/types` is deprecated. Please use `mini_mime` instead.'\
' Use Henkei.configure and assign "mini_mime" to `mime_library`.'
warn '[DEPRECATION] `mime/types` is deprecated. Please use `mini_mime` instead. ' \
'Use Henkei.configure and assign "mini_mime" to `mime_library`.'
MIME::Types[content_type].first
else
MiniMime.lookup_by_content_type(content_type).tap do |object|
Expand Down Expand Up @@ -78,7 +78,7 @@ def initialize(input)
if input.is_a? String
if File.exist? input
@path = input
elsif input =~ URI::DEFAULT_PARSER.make_regexp
elsif input&.match?(URI::DEFAULT_PARSER.make_regexp)
@uri = URI.parse input
else
raise Errno::ENOENT, "missing file or invalid URI - #{input}"
Expand Down Expand Up @@ -265,7 +265,7 @@ def self.server_read(data)
# tell Tika that we're done sending data
s.shutdown(Socket::SHUT_WR)

resp = String.new ''
resp = +''
loop do
chunk = s.recv(65_536)
break if chunk.empty? || !chunk
Expand Down Expand Up @@ -300,9 +300,8 @@ def self.switch_for_type(type)
# Internal helper to remove erroneous output
#
def self.filter_response(response)
response.gsub(
/\AWARNING: sun\.reflect\.Reflection\.getCallerClass is not supported\. This will impact performance\.\n/,
''
response.delete_prefix(
"WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance.\n"
)
end
private_class_method :filter_response
Expand Down
75 changes: 38 additions & 37 deletions spec/henkei_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@

describe '.read' do
it 'reads text' do
text = Henkei.read :text, data
text = described_class.read :text, data

expect(text).to include 'The quick brown fox jumped over the lazy cat.'
end

it 'reads metadata' do
metadata = Henkei.read :metadata, data
metadata = described_class.read :metadata, data

expect(metadata['Content-Type']).to(
eq 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
Expand All @@ -30,13 +30,13 @@

it 'reads metadata values with colons as strings' do
data = File.read 'spec/samples/sample-metadata-values-with-colons.doc'
metadata = Henkei.read :metadata, data
metadata = described_class.read :metadata, data

expect(metadata['dc:title']).to eq 'problem: test'
end

it 'reads mimetype' do
mimetype = Henkei.read :mimetype, data
mimetype = described_class.read :mimetype, data

expect(mimetype.content_type).to(
eq 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
Expand All @@ -48,7 +48,7 @@
let(:data) { File.read 'spec/samples/pipe-error.png' }

it 'returns an empty result' do
text = Henkei.read :text, data
text = described_class.read :text, data

expect(text).to eq ''
end
Expand All @@ -57,35 +57,35 @@

describe '.new' do
it 'requires parameters' do
expect { Henkei.new }.to raise_error ArgumentError
expect { described_class.new }.to raise_error ArgumentError
end

it 'accepts a root path' do
henkei = Henkei.new 'spec/samples/sample.pages'
henkei = described_class.new File.join(Henkei::GEM_PATH, 'spec/samples/sample.pages')

expect(henkei).to be_path
expect(henkei).not_to be_uri
expect(henkei).not_to be_stream
end

it 'accepts a relative path' do
henkei = Henkei.new 'spec/samples/sample.pages'
henkei = described_class.new 'spec/samples/sample.pages'

expect(henkei).to be_path
expect(henkei).not_to be_uri
expect(henkei).not_to be_stream
end

it 'accepts a path with spaces' do
henkei = Henkei.new 'spec/samples/sample filename with spaces.pages'
henkei = described_class.new 'spec/samples/sample filename with spaces.pages'

expect(henkei).to be_path
expect(henkei).not_to be_uri
expect(henkei).not_to be_stream
end

it 'accepts a URI' do
henkei = Henkei.new 'http://svn.apache.org/repos/asf/poi/trunk/test-data/document/sample.docx'
henkei = described_class.new 'http://svn.apache.org/repos/asf/poi/trunk/test-data/document/sample.docx'

expect(henkei).to be_uri
expect(henkei).not_to be_path
Expand All @@ -94,7 +94,7 @@

it 'accepts a stream or object that can be read' do
File.open 'spec/samples/sample.pages', 'r' do |file|
henkei = Henkei.new file
henkei = described_class.new file

expect(henkei).to be_stream
expect(henkei).not_to be_path
Expand All @@ -103,37 +103,38 @@
end

it 'refuses a path to a missing file' do
expect { Henkei.new 'test/sample/missing.pages' }.to raise_error Errno::ENOENT
expect { described_class.new 'test/sample/missing.pages' }.to raise_error Errno::ENOENT
end

it 'refuses other objects' do
[nil, 1, 1.1].each do |object|
expect { Henkei.new object }.to raise_error TypeError
expect { described_class.new object }.to raise_error TypeError
end
end
end

describe '.creation_date' do
let(:henkei) { Henkei.new 'spec/samples/sample.pages' }
it 'should return Time' do
let(:henkei) { described_class.new 'spec/samples/sample.pages' }

it 'returns Time' do
expect(henkei.creation_date).to be_a Time
end
end

describe '.java' do
specify 'with no specified JAVA_HOME' do
expect(Henkei.send(:java_path)).to eq 'java'
expect(described_class.send(:java_path)).to eq 'java'
end

specify 'with a specified JAVA_HOME' do
ENV['JAVA_HOME'] = '/path/to/java/home'

expect(Henkei.send(:java_path)).to eq '/path/to/java/home/bin/java'
expect(described_class.send(:java_path)).to eq '/path/to/java/home/bin/java'
end
end

context 'initialized with a given path' do
let(:henkei) { Henkei.new 'spec/samples/sample.pages' }
context 'when initialized with a given path' do
let(:henkei) { described_class.new 'spec/samples/sample.pages' }

specify '#text reads text' do
expect(henkei.text).to include 'The quick brown fox jumped over the lazy cat.'
Expand All @@ -144,7 +145,7 @@
end

context 'when passing in the `pipe-error.png` test file' do
let(:henkei) { Henkei.new 'spec/samples/pipe-error.png' }
let(:henkei) { described_class.new 'spec/samples/pipe-error.png' }

it '#text returns an empty result' do
expect(henkei.text).to eq ''
Expand All @@ -161,8 +162,8 @@
end
end

context 'initialized with a given URI' do
let(:henkei) { Henkei.new 'http://svn.apache.org/repos/asf/poi/trunk/test-data/document/sample.docx' }
context 'when initialized with a given URI' do
let(:henkei) { described_class.new 'http://svn.apache.org/repos/asf/poi/trunk/test-data/document/sample.docx' }

specify '#text reads text' do
expect(henkei.text).to include 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.'
Expand All @@ -175,8 +176,8 @@
end
end

context 'initialized with a given stream' do
let(:henkei) { Henkei.new File.open('spec/samples/sample.pages', 'rb') }
context 'when initialized with a given stream' do
let(:henkei) { described_class.new File.open('spec/samples/sample.pages', 'rb') }

specify '#text reads text' do
expect(henkei.text).to include 'The quick brown fox jumped over the lazy cat.'
Expand All @@ -188,7 +189,7 @@
end

context 'when source is a remote PDF' do
let(:henkei) { Henkei.new 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf' }
let(:henkei) { described_class.new 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf' }

specify '#text reads text' do
expect(henkei.text).to include 'Dummy PDF file'
Expand All @@ -199,35 +200,35 @@
end
end

context 'working as server mode' do
context 'when working as server mode' do
specify '#starts and kills server' do
Henkei.server(:text)
expect(Henkei.class_variable_get(:@@server_pid)).not_to be_nil
expect(Henkei.class_variable_get(:@@server_port)).not_to be_nil
described_class.server(:text)
expect(described_class.class_variable_get(:@@server_pid)).not_to be_nil
expect(described_class.class_variable_get(:@@server_port)).not_to be_nil

s = TCPSocket.new('localhost', Henkei.class_variable_get(:@@server_port))
s = TCPSocket.new('localhost', described_class.class_variable_get(:@@server_port))
expect(s).to be_a TCPSocket
s.close
ensure
port = Henkei.class_variable_get(:@@server_port)
Henkei.kill_server!
port = described_class.class_variable_get(:@@server_port)
described_class.kill_server!
sleep 2
expect { TCPSocket.new('localhost', port) }.to raise_error Errno::ECONNREFUSED
end

specify '#runs samples through server mode' do
Henkei.server(:text)
expect(Henkei.new('spec/samples/sample.pages').text).to(
described_class.server(:text)
expect(described_class.new('spec/samples/sample.pages').text).to(
include 'The quick brown fox jumped over the lazy cat.'
)
expect(Henkei.new('spec/samples/sample filename with spaces.pages').text).to(
expect(described_class.new('spec/samples/sample filename with spaces.pages').text).to(
include 'The quick brown fox jumped over the lazy cat.'
)
expect(Henkei.new('spec/samples/sample.docx').text).to(
expect(described_class.new('spec/samples/sample.docx').text).to(
include 'The quick brown fox jumped over the lazy cat.'
)
ensure
Henkei.kill_server!
described_class.kill_server!
end
end
end

0 comments on commit ca6b541

Please sign in to comment.