Skip to content

Commit

Permalink
Fix Rubopcop and Sonar style issues (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
otherguy authored Apr 23, 2024
1 parent 8858bfc commit f45ea8a
Show file tree
Hide file tree
Showing 20 changed files with 68 additions and 98 deletions.
10 changes: 10 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ Metrics/MethodLength:

Bundler/OrderedGems:
Enabled: true

RSpec/DescribeClass:
Exclude:
- spec/integration/*

RSpec/ExampleLength:
Max: 25

RSpec/MultipleExpectations:
Max: 10
12 changes: 5 additions & 7 deletions spec/integration/account_info_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "remove_bg"

RSpec.describe "fetching account information" do
let(:api_key) { "test-api-key" }
let(:image_path) do
Expand All @@ -14,15 +12,15 @@
end

expect(account.api).to have_attributes(
free_calls: be_a_kind_of(Numeric),
free_calls: be_a(Numeric),
sizes: "all"
)

expect(account.credits).to have_attributes(
total: be_a_kind_of(Numeric),
subscription: be_a_kind_of(Numeric),
payg: be_a_kind_of(Numeric),
enterprise: be_a_kind_of(Numeric),
total: be_a(Numeric),
subscription: be_a(Numeric),
payg: be_a(Numeric),
enterprise: be_a(Numeric),
)
end
end
2 changes: 0 additions & 2 deletions spec/integration/rate_limit_info_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "remove_bg"

RSpec.describe "rate limit information" do
let(:api_key) { "test-api-key" }
let(:image_path) do
Expand Down
4 changes: 1 addition & 3 deletions spec/integration/remove_from_file_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "remove_bg"

RSpec.describe "removing the background from a file" do
let(:api_key) { "test-api-key" }
let(:image_path) do
Expand All @@ -14,7 +12,7 @@
end

expect(result).to be_a RemoveBg::Result
expect(result.data).to_not be_empty
expect(result.data).not_to be_empty
expect(result.data.encoding).to eq(Encoding::BINARY)
expect(result.type).to eq "person"
expect(result.height).to eq 333
Expand Down
6 changes: 2 additions & 4 deletions spec/integration/remove_from_url_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "remove_bg"

RSpec.describe "removing the background from a URL" do
let(:api_key) { "test-api-key" }
let(:image_url) { "https://static.remove.bg/sample-gallery/people/adult-blue-boy-1438275-thumbnail.jpg" }
Expand All @@ -12,15 +10,15 @@
end

expect(result).to be_a RemoveBg::Result
expect(result.data).to_not be_empty
expect(result.data).not_to be_empty
expect(result.data.encoding).to eq(Encoding::BINARY)
expect(result.type).to eq "person"
expect(result.height).to eq 1080
expect(result.width).to eq 720
expect(result.credits_charged).to be_a(Float).and(be >= 0)
end

context "image doesn't exist" do
context "when image doesn't exist" do
it "raises an error with a helpful message" do
make_request = proc do
VCR.use_cassette("from-url-non-existent-image") do
Expand Down
9 changes: 3 additions & 6 deletions spec/integration/zip_format_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
# frozen_string_literal: true

require "remove_bg"
require "digest"

RSpec.describe "using the ZIP format" do
let(:api_key) { "test-api-key" }
let(:image_path) do
File.expand_path("../fixtures/images/person-in-field.jpg", __dir__)
end

before(:each) { RemoveBg::Configuration.reset }
before { RemoveBg::Configuration.reset }

context "using MiniMagick" do
context "when using MiniMagick" do
it "converts the ZIP to a composite PNG" do
RemoveBg::Configuration.configuration.image_processor = :minimagick

Expand All @@ -27,7 +24,7 @@
end
end

context "using Vips" do
context "when using Vips" do
it "converts the ZIP to a composite PNG" do
RemoveBg::Configuration.configuration.image_processor = :vips

Expand Down
30 changes: 15 additions & 15 deletions spec/remove_bg/api_client_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

require "remove_bg"

RSpec.describe RemoveBg::ApiClient do
subject(:api_client) { described_class.new }

let(:image_path) do
File.expand_path("../fixtures/images/person-in-field.jpg", __dir__)
end
Expand All @@ -13,7 +13,7 @@
it "raises an error with a helpful message" do
make_request = proc do
VCR.use_cassette("from-file-person-in-field-invalid-api-key") do
subject.remove_from_file(image_path, request_options)
api_client.remove_from_file(image_path, request_options)
end
end

Expand All @@ -23,7 +23,7 @@
it "includes the HTTP response for further debugging" do
make_request = proc do
VCR.use_cassette("from-file-person-in-field-invalid-api-key") do
subject.remove_from_file(image_path, request_options)
api_client.remove_from_file(image_path, request_options)
end
end

Expand All @@ -33,16 +33,16 @@
end
end

context "invalid image URL" do
context "with invalid image URL" do
it "raises an error" do
expect { subject.remove_from_url("", build_options) }
expect { api_client.remove_from_url("", build_options) }
.to raise_error RemoveBg::InvalidUrlError
end
end

context "rate limit exceeded", :disable_vcr do
context "when rate limit exceeded", :disable_vcr do
let(:request) do
proc { subject.remove_from_file(image_path, build_options) }
proc { api_client.remove_from_file(image_path, build_options) }
end

it "raises a specific error, to aid rate limit implementations" do
Expand Down Expand Up @@ -87,7 +87,7 @@
)

make_request = proc do
subject.remove_from_url("http://example.image.jpg", build_options)
api_client.remove_from_url("http://example.image.jpg", build_options)
end

expect(&make_request).to raise_error do |exception|
Expand All @@ -103,7 +103,7 @@
stub_request(:post, /api.remove.bg/).to_return(status: 204)

make_request = proc do
subject.remove_from_url("http://example.image.jpg", build_options)
api_client.remove_from_url("http://example.image.jpg", build_options)
end

expect(&make_request).to raise_error RemoveBg::HttpError, /unknown error/
Expand All @@ -114,7 +114,7 @@
it "is included in the request", :disable_vcr do
stub_request(:post, /api.remove.bg/).to_return(status: 200, body: "")

subject.remove_from_url("http://example.image.jpg", build_options)
api_client.remove_from_url("http://example.image.jpg", build_options)

expect(WebMock).to have_requested(:post, /api.remove.bg/)
.with(headers: { "User-Agent" => "remove-bg-ruby-#{RemoveBg::VERSION}" })
Expand All @@ -130,7 +130,7 @@
.to_timeout
.to_return(status: 200, body: "data")

result = subject.remove_from_url(image_url, build_options)
result = api_client.remove_from_url(image_url, build_options)

expect(result.data).to eq "data"
end
Expand All @@ -139,7 +139,7 @@
stub_request(:post, /api.remove.bg/).to_timeout

make_request = proc do
subject.remove_from_url(image_url, build_options)
api_client.remove_from_url(image_url, build_options)
end

expect(&make_request).to raise_error Faraday::ConnectionFailed
Expand All @@ -157,7 +157,7 @@

expect(response_data.encoding).to eq(Encoding::BINARY)

result = subject.remove_from_url(image_url, build_options)
result = api_client.remove_from_url(image_url, build_options)

expect(result.data.encoding).to eq(Encoding::BINARY)
end
Expand All @@ -170,7 +170,7 @@

make_request = proc do
VCR.use_cassette("account-invalid-api-key") do
subject.account_info(request_options)
api_client.account_info(request_options)
end
end

Expand Down
7 changes: 1 addition & 6 deletions spec/remove_bg/composite_result_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
# frozen_string_literal: true

require "remove_bg/composite_result"
require "securerandom"
require "tempfile"
require "tmpdir"

RSpec.describe RemoveBg::CompositeResult do
let(:tmp_dir) { Dir.mktmpdir("remove_bg") }
let(:file_path) { File.join(tmp_dir, "#{SecureRandom.urlsafe_base64}.txt") }
Expand All @@ -16,7 +11,7 @@
file
end

after(:each) do
after do
download.close
download.unlink
FileUtils.rm_rf(tmp_dir)
Expand Down
18 changes: 8 additions & 10 deletions spec/remove_bg/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# frozen_string_literal: true

require "remove_bg"

RSpec.describe "Remove BG configuration" do
before(:each) do
RemoveBg::Configuration.reset
RSpec.describe RemoveBg::Configuration do
before do
described_class.reset
end

describe "::configure" do
Expand All @@ -13,7 +11,7 @@
config.api_key = "an-api-key"
end

expect(RemoveBg::Configuration.configuration.api_key).to eq "an-api-key"
expect(described_class.configuration.api_key).to eq "an-api-key"
end
end

Expand All @@ -22,22 +20,22 @@
config.api_key = "an-api-key"
end

expect { RemoveBg::Configuration.reset }
.to change { RemoveBg::Configuration.configuration.api_key }
expect { described_class.reset }
.to change { described_class.configuration.api_key }
.from("an-api-key").to(nil)
end

describe "image processor" do
it "is automatically configured if there's processing library available" do
expect(RemoveBg::Configuration.configuration.image_processor).to eq :minimagick
expect(described_class.configuration.image_processor).to eq :minimagick
end

it "can be overridden" do
RemoveBg.configure do |config|
config.image_processor = :vips
end

expect(RemoveBg::Configuration.configuration.image_processor).to eq :vips
expect(described_class.configuration.image_processor).to eq :vips
end
end
end
2 changes: 0 additions & 2 deletions spec/remove_bg/http_connection_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "remove_bg/http_connection"

RSpec.describe RemoveBg::HttpConnection do
let(:options) { described_class.build.options }

Expand Down
28 changes: 12 additions & 16 deletions spec/remove_bg/image_composer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
# frozen_string_literal: true

require "remove_bg/configuration"
require "remove_bg/error"
require "remove_bg/image_composer"

RSpec.describe RemoveBg::ImageComposer do
before(:each) { RemoveBg::Configuration.reset }
before { RemoveBg::Configuration.reset }

let(:alpha_file) { instance_double(File, "alpha") }
let(:color_file) { instance_double(File, "color") }
let(:destination_path) { double("Destination path") }
let(:destination_path) { instance_double(Pathname, "Destination path") }

context "configured to use MiniMagick" do
before(:each) { RemoveBg::Configuration.configuration.image_processor = :minimagick }
context "when using MiniMagick" do
before { RemoveBg::Configuration.configuration.image_processor = :minimagick }

it "uses MiniMagick by default" do
require "image_processing/mini_magick"
Expand All @@ -25,8 +21,8 @@
end
end

context "configured to use Vips" do
before(:each) { RemoveBg::Configuration.configuration.image_processor = :vips }
context "when using Vips" do
before { RemoveBg::Configuration.configuration.image_processor = :vips }

it "uses Vips" do
require "image_processing/vips"
Expand All @@ -39,17 +35,17 @@
end
end

context "configured with an unknown processor" do
before(:each) { RemoveBg::Configuration.configuration.image_processor = :foo }
context "when configured with an unknown processor" do
before { RemoveBg::Configuration.configuration.image_processor = :foo }

it "raises an error" do
expect { perform_composition }
.to raise_exception(RemoveBg::Error, "Unsupported image processor: :foo")
end
end

context "not configured" do
before(:each) { RemoveBg::Configuration.configuration.image_processor = nil }
context "when not configured" do
before { RemoveBg::Configuration.configuration.image_processor = nil }

it "raises an error" do
expect { perform_composition }
Expand All @@ -58,7 +54,7 @@
end

describe "::detect_image_processor" do
let(:binary_detector) { double("binary_detector", call: false) }
let(:binary_detector) { instance_double(Proc, "binary_detector", call: false) }

let(:detected) do
described_class.detect_image_processor(detector: binary_detector)
Expand Down Expand Up @@ -90,7 +86,7 @@
private

def spy_on_image_processing(klass)
spy(klass.to_s).tap do |processing_spy|
spy(klass.to_s).tap do |processing_spy| # rubocop:disable RSpec/VerifiedDoubles
allow(klass).to receive(:source).and_return(processing_spy)
end
end
Expand Down
2 changes: 0 additions & 2 deletions spec/remove_bg/rate_limit_info_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "remove_bg/rate_limit_info"

RSpec.describe RemoveBg::RateLimitInfo do
describe "#to_s" do
it "displays the rate limit info in a human readable form" do
Expand Down
Loading

0 comments on commit f45ea8a

Please sign in to comment.