Skip to content

Commit

Permalink
Update code base for Ruby v3.2 (#392)
Browse files Browse the repository at this point in the history
* `File.exists?` is removed in ruby 3.2

* Fix deprecated rspec expectation syntax

* Fix `Socket.gethostbyname` deprecation

* Test on ruby 3.2 and rack 2.
  • Loading branch information
Vasfed authored Jan 31, 2023
1 parent 2d75699 commit 829c3aa
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 17 deletions.
26 changes: 16 additions & 10 deletions .github/workflows/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ jobs:
test:
runs-on: ${{matrix.os}}-latest
continue-on-error: ${{matrix.experimental}}

strategy:
matrix:
experimental: [false]

os:
- ubuntu
- macos

ruby:
- 2.5
- 2.6
- 2.7

- 3.2

include:
- experimental: true
os: ubuntu
Expand All @@ -30,17 +31,22 @@ jobs:
env: BUNDLE_GEMFILE=gems/rack-v1.rb
- experimental: true
os: ubuntu
ruby: 2.7
env: BUNDLE_GEMFILE=gems/rack-head.rb

ruby: 3.2
env: BUNDLE_GEMFILE=gems/rack-v2.rb
# enable when rack v3 is supported
# - experimental: true
# os: ubuntu
# ruby: 3.2
# env: BUNDLE_GEMFILE=gems/rack-head.rb

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{matrix.ruby}}

- name: Install dependencies
run: ${{matrix.env}} bundle install

- name: Run tests
run: ${{matrix.env}} bundle exec rake
16 changes: 16 additions & 0 deletions gems/rack-v2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec path: "../"

gem 'rack', '~> 2.0'

group :development do
gem "rake-compiler"
end

group :test do
gem "rake", ">= 12.3.3"
gem "rspec", "~> 3.5"
end
12 changes: 11 additions & 1 deletion lib/thin/backends/swiftiply_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,17 @@ def swiftiply_handshake(key)

# For some reason Swiftiply request the current host
def host_ip
Socket.gethostbyname(@backend.host)[3].unpack('CCCC') rescue [0, 0, 0, 0]
begin
if defined?(Addrinfo)
# ruby 2.0+
# TODO: ipv6 support here?
Addrinfo.getaddrinfo(@backend.host, @backend.port, :PF_INET, :STREAM).first.ip_address.split('.').map(&:to_i)
else
Socket.gethostbyname(@backend.host)[3].unpack('CCCC')
end
rescue
[0, 0, 0, 0]
end
end
end
end
4 changes: 2 additions & 2 deletions lib/thin/daemonizing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def change_privilege(user, group=user)

if uid != target_uid || gid != target_gid
# Change PID file ownership
File.chown(target_uid, target_gid, @pid_file) if File.exists?(@pid_file)
File.chown(target_uid, target_gid, @pid_file) if File.exist?(@pid_file)

# Change process ownership
Process.initgroups(user, target_gid)
Expand Down Expand Up @@ -174,7 +174,7 @@ def read_pid_file(file)

protected
def remove_pid_file
File.delete(@pid_file) if @pid_file && File.exists?(@pid_file)
File.delete(@pid_file) if @pid_file && File.exist?(@pid_file)
end

def write_pid_file
Expand Down
2 changes: 1 addition & 1 deletion spec/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
end

it "should process at most once when request is larger than expected" do
@connection.should_receive(:process).at_most(1)
expect(@connection).to receive(:process).at_most(1)
@connection.receive_data("POST / HTTP/1.1\r\nHost: localhost:3000\r\nContent-Length: 300\r\n\r\n")
10.times { @connection.receive_data('X' * 1_000) }
end
Expand Down
2 changes: 1 addition & 1 deletion spec/rails_app/config/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def vendor_rails?

# FIXME : Ruby 1.9
def preinitialize
load(preinitializer_path) if File.exists?(preinitializer_path)
load(preinitializer_path) if File.exist?(preinitializer_path)
end

def preinitializer_path
Expand Down
4 changes: 2 additions & 2 deletions spec/request/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,15 @@
end

it "should fail when total request vastly exceeds specified CONTENT_LENGTH" do
proc do
expect do
R(<<-EOS, true)
POST / HTTP/1.1
Host: localhost:3000
Content-Length: 300
#{'X' * 300_000}
EOS
end.should raise_error(InvalidRequest)
end.to raise_error(InvalidRequest)
end

it "should default SERVER_NAME to localhost" do
Expand Down

0 comments on commit 829c3aa

Please sign in to comment.