Skip to content

Commit

Permalink
Ensure that docker-api works with frozen strings
Browse files Browse the repository at this point in the history
I think after Ruby 3.4 is released[^1], more people will start expecting
libraries to be functional with eg the `--enable-frozen-string-literal`
option enabled.

This commit adds the `frozen_string_literal` header to all files and
fixes failing tests.

[^1]:ruby/ruby@12be40a
  • Loading branch information
bquorning committed Oct 30, 2024
1 parent 15b5bf3 commit c416cfd
Show file tree
Hide file tree
Showing 34 changed files with 80 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .simplecov
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

SimpleCov.start do
add_group 'Library', 'lib'
add_group 'Specs', 'spec'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'http://rubygems.org'

gemspec
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'bundler/setup'

ENV['PATH'] = "/opt/docker/:#{ENV['PATH']}" if ENV['CI'] == 'true'
Expand Down
2 changes: 2 additions & 0 deletions docker-api.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- encoding: utf-8 -*-
# frozen_string_literal: true

require File.expand_path('../lib/docker/version', __FILE__)

Gem::Specification.new do |gem|
Expand Down
2 changes: 2 additions & 0 deletions lib/docker-api.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# frozen_string_literal: true

require 'docker'
2 changes: 2 additions & 0 deletions lib/docker.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'cgi'
require 'multi_json'
require 'excon'
Expand Down
2 changes: 2 additions & 0 deletions lib/docker/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# This class is a base class for Docker Container and Image.
# It is implementing accessor methods for the models attributes.
module Docker::Base
Expand Down
2 changes: 2 additions & 0 deletions lib/docker/connection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# This class represents a Connection to a Docker server. The Connection is
# immutable in that once the url and options is set they cannot be changed.
class Docker::Connection
Expand Down
2 changes: 2 additions & 0 deletions lib/docker/container.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# This class represents a Docker Container. It's important to note that nothing
# is cached so that the information is always up to date.
class Docker::Container
Expand Down
2 changes: 2 additions & 0 deletions lib/docker/error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# This module holds the Errors for the gem.
module Docker::Error

Expand Down
2 changes: 2 additions & 0 deletions lib/docker/event.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# This class represents a Docker Event.
class Docker::Event
include Docker::Error
Expand Down
2 changes: 2 additions & 0 deletions lib/docker/exec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# This class represents a Docker Exec Instance.
class Docker::Exec
include Docker::Base
Expand Down
16 changes: 9 additions & 7 deletions lib/docker/image.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# This class represents a Docker Image.
class Docker::Image
include Docker::Base
Expand Down Expand Up @@ -28,7 +30,7 @@ def push(creds = nil, options = {}, &block)
repo, tag = Docker::Util.parse_repo_tag(repo_tag)
raise ArgumentError, "Image does not have a name to push." if repo.nil?

body = ""
body = +""
credentials = creds || Docker.creds || {}
headers = Docker::Util.build_auth_header(credentials)
opts = {:tag => tag}.merge(options)
Expand Down Expand Up @@ -119,7 +121,7 @@ class << self
def create(opts = {}, creds = nil, conn = Docker.connection, &block)
credentials = creds.nil? ? Docker.creds : MultiJson.dump(creds)
headers = credentials && Docker::Util.build_auth_header(credentials) || {}
body = ''
body = +''
conn.post(
'/images/create',
opts,
Expand Down Expand Up @@ -168,7 +170,7 @@ def save(names, filename = nil, conn = Docker.connection)
end
nil
else
string = ''
string = +''
save_stream(names, {}, conn, &response_block_for_save(string))
string
end
Expand Down Expand Up @@ -196,7 +198,7 @@ def save_stream(names, opts = {}, conn = Docker.connection, &block)
def load(tar, opts = {}, conn = Docker.connection, creds = nil, &block)
headers = build_headers(creds)
io = tar.is_a?(String) ? File.open(tar, 'rb') : tar
body = ""
body = +""
conn.post(
'/images/load',
opts,
Expand Down Expand Up @@ -267,7 +269,7 @@ def import_stream(options = {}, connection = Docker.connection, &block)

# Given a Dockerfile as a string, builds an Image.
def build(commands, opts = {}, connection = Docker.connection, &block)
body = ""
body = +""
connection.post(
'/build', opts,
:body => Docker::Util.create_tar('Dockerfile' => commands),
Expand All @@ -288,7 +290,7 @@ def build_from_tar(tar, opts = {}, connection = Docker.connection,
headers = build_headers(creds)

# The response_block passed to Excon will build up this body variable.
body = ""
body = +""
connection.post(
'/build', opts,
:headers => headers,
Expand Down Expand Up @@ -340,7 +342,7 @@ def path_for(resource)
# Convience method to get the Dockerfile for a file hash and a path to
# output to.
def dockerfile_for(file_hash, output_path)
dockerfile = "from #{self.id}\n"
dockerfile = +"from #{self.id}\n"

file_hash.keys.each do |basename|
dockerfile << "add #{basename} #{output_path}\n"
Expand Down
2 changes: 2 additions & 0 deletions lib/docker/messages.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# This class represents all the messages either received by chunks from attach
class Docker::Messages

Expand Down
2 changes: 2 additions & 0 deletions lib/docker/messages_stack.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# This class represents a messages stack
class Docker::MessagesStack

Expand Down
2 changes: 2 additions & 0 deletions lib/docker/network.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# This class represents a Docker Network.
class Docker::Network
include Docker::Base
Expand Down
2 changes: 2 additions & 0 deletions lib/docker/rake_task.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# This class allows image-based tasks to be created.
class Docker::ImageTask < Rake::Task
def self.scope_name(_scope, task_name)
Expand Down
2 changes: 2 additions & 0 deletions lib/docker/util.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'set'

# This module holds shared logic that doesn't really belong anywhere else in the
Expand Down
2 changes: 2 additions & 0 deletions lib/docker/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Docker
# The version of the docker-api gem.
VERSION = '2.3.0'
Expand Down
2 changes: 2 additions & 0 deletions lib/docker/volume.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# class represents a Docker Volume
class Docker::Volume
include Docker::Base
Expand Down
2 changes: 2 additions & 0 deletions lib/excon/middlewares/hijack.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Excon
module Middleware
# Hijack is an Excon middleware which parses response headers and then
Expand Down
2 changes: 2 additions & 0 deletions spec/cov_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

SingleCov.not_covered!
Expand Down
2 changes: 2 additions & 0 deletions spec/docker/connection_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

SingleCov.covered! uncovered: 12
Expand Down
2 changes: 2 additions & 0 deletions spec/docker/container_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

SingleCov.covered! uncovered: 39
Expand Down
2 changes: 2 additions & 0 deletions spec/docker/event_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

SingleCov.covered! uncovered: 5
Expand Down
2 changes: 2 additions & 0 deletions spec/docker/exec_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

SingleCov.covered! uncovered: 5
Expand Down
12 changes: 7 additions & 5 deletions spec/docker/image_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

SingleCov.covered! uncovered: 16
Expand Down Expand Up @@ -464,7 +466,7 @@
end

context 'with a block capturing create output' do
let(:create_output) { "" }
let(:create_output) { +"" }
let(:block) { Proc.new { |chunk| create_output << chunk } }

before do
Expand Down Expand Up @@ -537,7 +539,7 @@
let(:non_streamed) do
Docker.connection.get('/images/get', 'names' => image)
end
let(:streamed) { '' }
let(:streamed) { +'' }
let(:tar_files) do
proc do |string|
Gem::Package::TarReader
Expand Down Expand Up @@ -723,7 +725,7 @@
end

context 'with a block capturing build output' do
let(:build_output) { "" }
let(:build_output) { +"" }
let(:block) { Proc.new { |chunk| build_output << chunk } }
let!(:image) { subject.build("FROM debian:stable\n", &block) }

Expand Down Expand Up @@ -775,7 +777,7 @@
end

context 'with a block capturing build output' do
let(:build_output) { "" }
let(:build_output) { +"" }
let(:block) { Proc.new { |chunk| build_output << chunk } }

it 'calls the block and passes build output' do
Expand All @@ -784,7 +786,7 @@
end

context 'uses a cached version the second time' do
let(:build_output_two) { "" }
let(:build_output_two) { +"" }
let(:block_two) { Proc.new { |chunk| build_output_two << chunk } }
let(:image_two) { subject.build_from_dir(dir, opts, &block_two) }

Expand Down
2 changes: 2 additions & 0 deletions spec/docker/messages_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

SingleCov.covered! uncovered: 4
Expand Down
2 changes: 2 additions & 0 deletions spec/docker/messages_stack.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

SingleCov.covered!
Expand Down
2 changes: 2 additions & 0 deletions spec/docker/network_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

unless ::Docker.podman?
Expand Down
2 changes: 2 additions & 0 deletions spec/docker/util_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'
require 'tempfile'
require 'fileutils'
Expand Down
2 changes: 2 additions & 0 deletions spec/docker/volume_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

SingleCov.covered! uncovered: 1
Expand Down
2 changes: 2 additions & 0 deletions spec/docker_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

SingleCov.covered! uncovered: 8
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'bundler/setup'

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
Expand Down

0 comments on commit c416cfd

Please sign in to comment.