From 83b73e780608a783b8023ece0de08e91f7dc3b9c Mon Sep 17 00:00:00 2001 From: Julik Tarkhanov Date: Wed, 20 Mar 2024 18:51:10 +0100 Subject: [PATCH] Use standardrb code formatting --- .github/workflows/ci.yml | 2 +- Gemfile | 2 +- Rakefile | 12 ++-- lib/zipline.rb | 10 +-- lib/zipline/zip_handler.rb | 14 ++-- spec/lib/zipline/zip_handler_spec.rb | 96 +++++++++++++++------------- spec/lib/zipline/zipline_spec.rb | 28 ++++---- spec/spec_helper.rb | 28 ++++---- zipline.gemspec | 45 +++++++------ 9 files changed, 125 insertions(+), 112 deletions(-) mode change 100644 => 100755 Rakefile diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fdca5a1..e7962fc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,5 +22,5 @@ jobs: with: ruby-version: ${{ matrix.ruby-version }} bundler-cache: true # 'bundle install' and cache - - name: Run tests + - name: Run tests and standard run: bundle exec rake diff --git a/Gemfile b/Gemfile index 5afb5c1..9e5e083 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ -source 'https://rubygems.org' +source "https://rubygems.org" # Specify your gem's dependencies in zipline.gemspec gemspec diff --git a/Rakefile b/Rakefile old mode 100644 new mode 100755 index b2df96f..54f71f8 --- a/Rakefile +++ b/Rakefile @@ -1,12 +1,8 @@ #!/usr/bin/env rake require "bundler/gem_tasks" +require "standard/rake" +require "rspec/core/rake_task" -begin - require 'rspec/core/rake_task' +RSpec::Core::RakeTask.new(:spec) - RSpec::Core::RakeTask.new(:spec) - - task default: :spec -rescue LoadError - # no rspec available -end +task default: [:spec, :standard] diff --git a/lib/zipline.rb b/lib/zipline.rb index 4e0552f..5dcdb30 100644 --- a/lib/zipline.rb +++ b/lib/zipline.rb @@ -1,7 +1,7 @@ -require 'content_disposition' -require 'zip_kit' -require 'zipline/version' -require 'zipline/zip_handler' +require "content_disposition" +require "zip_kit" +require "zipline/version" +require "zipline/zip_handler" # class MyController < ApplicationController # include Zipline @@ -17,7 +17,7 @@ def self.included(into_controller) super end - def zipline(files, zipname = 'zipline.zip', **kwargs_for_zip_kit_stream) + def zipline(files, zipname = "zipline.zip", **kwargs_for_zip_kit_stream) zip_kit_stream(filename: zipname, **kwargs_for_zip_kit_stream) do |zip_kit_streamer| handler = Zipline::ZipHandler.new(zip_kit_streamer, logger) files.each do |file, name, options = {}| diff --git a/lib/zipline/zip_handler.rb b/lib/zipline/zip_handler.rb index 8c51357..3bd9096 100644 --- a/lib/zipline/zip_handler.rb +++ b/lib/zipline/zip_handler.rb @@ -14,7 +14,7 @@ def handle_file(file, name, options) # Rack bodies, it can be helpful to print the error to the Rails log at least error_message = "zipline: an exception (#{e.inspect}) was raised when serving the ZIP body." error_message += " The error occurred when handling file #{name.inspect}" - @logger.error(error_message) if @logger + @logger&.error(error_message) raise end @@ -51,7 +51,7 @@ def normalize(file) elsif is_url?(file) {url: file} else - raise(ArgumentError, 'Bad File/Stream') + raise(ArgumentError, "Bad File/Stream") end end @@ -71,7 +71,7 @@ def write_file(file, name, options) elsif file[:blob] file[:blob].download { |chunk| writer_for_file << chunk } else - raise(ArgumentError, 'Bad File/Stream') + raise(ArgumentError, "Bad File/Stream") end end end @@ -91,8 +91,12 @@ def is_active_storage_one?(file) end def is_url?(url) - url = URI.parse(url) rescue false - url.kind_of?(URI::HTTP) || url.kind_of?(URI::HTTPS) + url = begin + URI.parse(url) + rescue + false + end + url.is_a?(URI::HTTP) || url.is_a?(URI::HTTPS) end end end diff --git a/spec/lib/zipline/zip_handler_spec.rb b/spec/lib/zipline/zip_handler_spec.rb index 7b396db..6ca5277 100644 --- a/spec/lib/zipline/zip_handler_spec.rb +++ b/spec/lib/zipline/zip_handler_spec.rb @@ -1,17 +1,21 @@ -require 'spec_helper' -require 'tempfile' +require "spec_helper" +require "tempfile" module ActiveStorage class Attached class One < Attached end end + class Attachment; end + class Blob; end + class Filename def initialize(name) @name = name end + def to_s @name end @@ -20,38 +24,44 @@ def to_s describe Zipline::ZipHandler do before { Fog.mock! } - let(:file_attributes){ { - key: 'fog_file_tests', - body: 'some body', - public: true - }} - let(:directory_attributes){{ - key: 'fog_directory' - }} - let(:storage_attributes){{ - aws_access_key_id: 'fake_access_key_id', - aws_secret_access_key: 'fake_secret_access_key', - provider: 'AWS' - }} - let(:storage){ Fog::Storage.new(storage_attributes)} - let(:directory){ storage.directories.create(directory_attributes) } - let(:file){ directory.files.create(file_attributes) } - - describe '.normalize' do - let(:handler){ Zipline::ZipHandler.new(_streamer = double(), _logger = nil)} + let(:file_attributes) { + { + key: "fog_file_tests", + body: "some body", + public: true + } + } + let(:directory_attributes) { + { + key: "fog_directory" + } + } + let(:storage_attributes) { + { + aws_access_key_id: "fake_access_key_id", + aws_secret_access_key: "fake_secret_access_key", + provider: "AWS" + } + } + let(:storage) { Fog::Storage.new(storage_attributes) } + let(:directory) { storage.directories.create(directory_attributes) } + let(:file) { directory.files.create(file_attributes) } + + describe ".normalize" do + let(:handler) { Zipline::ZipHandler.new(_streamer = double, _logger = nil) } context "CarrierWave" do context "Remote" do - let(:file){ CarrierWave::Storage::Fog::File.new(nil,nil,nil) } + let(:file) { CarrierWave::Storage::Fog::File.new(nil, nil, nil) } it "extracts the url" do - allow(file).to receive(:url).and_return('fakeurl') + allow(file).to receive(:url).and_return("fakeurl") expect(File).not_to receive(:open) - expect(handler.normalize(file)).to eq({url: 'fakeurl'}) + expect(handler.normalize(file)).to eq({url: "fakeurl"}) end end context "Local" do - let(:file){ CarrierWave::SanitizedFile.new(Tempfile.new('t')) } + let(:file) { CarrierWave::SanitizedFile.new(Tempfile.new("t")) } it "creates a File" do - allow(file).to receive(:path).and_return('spec/fakefile.txt') + allow(file).to receive(:path).and_return("spec/fakefile.txt") normalized = handler.normalize(file) expect(normalized.keys).to include(:file) expect(normalized[:file]).to be_a File @@ -61,20 +71,20 @@ def to_s let(:uploader) { Class.new(CarrierWave::Uploader::Base).new } context "Remote" do - let(:file){ CarrierWave::Storage::Fog::File.new(nil,nil,nil) } + let(:file) { CarrierWave::Storage::Fog::File.new(nil, nil, nil) } it "extracts the url" do allow(uploader).to receive(:file).and_return(file) - allow(file).to receive(:url).and_return('fakeurl') + allow(file).to receive(:url).and_return("fakeurl") expect(File).not_to receive(:open) - expect(handler.normalize(uploader)).to eq({url: 'fakeurl'}) + expect(handler.normalize(uploader)).to eq({url: "fakeurl"}) end end context "Local" do - let(:file){ CarrierWave::SanitizedFile.new(Tempfile.new('t')) } + let(:file) { CarrierWave::SanitizedFile.new(Tempfile.new("t")) } it "creates a File" do allow(uploader).to receive(:file).and_return(file) - allow(file).to receive(:path).and_return('spec/fakefile.txt') + allow(file).to receive(:path).and_return("spec/fakefile.txt") normalized = handler.normalize(uploader) expect(normalized.keys).to include(:file) expect(normalized[:file]).to be_a File @@ -84,20 +94,20 @@ def to_s end context "Paperclip" do context "Local" do - let(:file){ Paperclip::Attachment.new(:name, :instance) } + let(:file) { Paperclip::Attachment.new(:name, :instance) } it "creates a File" do - allow(file).to receive(:path).and_return('spec/fakefile.txt') + allow(file).to receive(:path).and_return("spec/fakefile.txt") normalized = handler.normalize(file) expect(normalized.keys).to include(:file) expect(normalized[:file]).to be_a File end end context "Remote" do - let(:file){ Paperclip::Attachment.new(:name, :instance, storage: :s3) } + let(:file) { Paperclip::Attachment.new(:name, :instance, storage: :s3) } it "creates a URL" do - allow(file).to receive(:expiring_url).and_return('fakeurl') + allow(file).to receive(:expiring_url).and_return("fakeurl") expect(File).to_not receive(:open) - expect(handler.normalize(file)).to include(url: 'fakeurl') + expect(handler.normalize(file)).to include(url: "fakeurl") end end end @@ -154,7 +164,7 @@ def create_attachment def create_blob blob = ActiveStorage::Blob.new - allow(blob).to receive(:service_url).and_return('fakeurl') + allow(blob).to receive(:service_url).and_return("fakeurl") filename = create_filename allow(blob).to receive(:filename).and_return(filename) blob @@ -162,26 +172,26 @@ def create_blob def create_filename # Rails wraps Blob#filname in this class since Rails 5.2 - ActiveStorage::Filename.new('test') + ActiveStorage::Filename.new("test") end end context "Fog" do it "extracts url" do - allow(file).to receive(:url).and_return('fakeurl') + allow(file).to receive(:url).and_return("fakeurl") expect(File).not_to receive(:open) - expect(handler.normalize(file)).to eq(url: 'fakeurl') + expect(handler.normalize(file)).to eq(url: "fakeurl") end end context "IOStream" do - let(:file){ StringIO.new('passthrough')} + let(:file) { StringIO.new("passthrough") } it "passes through" do expect(handler.normalize(file)).to eq(file: file) end end context "invalid" do - let(:file){ Thread.new{} } + let(:file) { Thread.new {} } it "raises error" do - expect{handler.normalize(file)}.to raise_error(ArgumentError) + expect { handler.normalize(file) }.to raise_error(ArgumentError) end end end diff --git a/spec/lib/zipline/zipline_spec.rb b/spec/lib/zipline/zipline_spec.rb index db2196e..8b7b4a5 100644 --- a/spec/lib/zipline/zipline_spec.rb +++ b/spec/lib/zipline/zipline_spec.rb @@ -1,5 +1,5 @@ -require 'spec_helper' -require 'action_controller' +require "spec_helper" +require "action_controller" describe Zipline do before do @@ -14,7 +14,7 @@ def download_zip [StringIO.new("File content goes here"), "one.txt"], [StringIO.new("Some other content goes here"), "two.txt"] ] - zipline(files, 'myfiles.zip', auto_rename_duplicate_filenames: false) + zipline(files, "myfiles.zip", auto_rename_duplicate_filenames: false) end class FailingIO < StringIO @@ -28,11 +28,11 @@ def download_zip_with_error_during_streaming [StringIO.new("File content goes here"), "one.txt"], [FailingIO.new("This will fail half-way"), "two.txt"] ] - zipline(files, 'myfiles.zip', auto_rename_duplicate_filenames: false) + zipline(files, "myfiles.zip", auto_rename_duplicate_filenames: false) end end - it 'passes keyword parameters to ZipKit::OutputEnumerator' do + it "passes keyword parameters to ZipKit::OutputEnumerator" do fake_rack_env = { "HTTP_VERSION" => "HTTP/1.0", "REQUEST_METHOD" => "GET", @@ -40,16 +40,20 @@ def download_zip_with_error_during_streaming "PATH_INFO" => "/download", "QUERY_STRING" => "", "SERVER_NAME" => "host.example", - "rack.input" => StringIO.new, + "rack.input" => StringIO.new } expect(ZipKit::OutputEnumerator).to receive(:new).with(auto_rename_duplicate_filenames: false).and_call_original status, headers, body = FakeController.action(:download_zip).call(fake_rack_env) - expect(headers['Content-Disposition']).to eq("attachment; filename=\"myfiles.zip\"; filename*=UTF-8''myfiles.zip") + expect(status).to eq(200) + expect(headers["Content-Disposition"]).to eq("attachment; filename=\"myfiles.zip\"; filename*=UTF-8''myfiles.zip") + expect { + body.each {} + }.not_to raise_error end - it 'sends the exception raised in the streaming body to the Rails logger' do + it "sends the exception raised in the streaming body to the Rails logger" do fake_rack_env = { "HTTP_VERSION" => "HTTP/1.0", "REQUEST_METHOD" => "GET", @@ -57,17 +61,17 @@ def download_zip_with_error_during_streaming "PATH_INFO" => "/download", "QUERY_STRING" => "", "SERVER_NAME" => "host.example", - "rack.input" => StringIO.new, + "rack.input" => StringIO.new } - fake_logger = double() + fake_logger = double allow(fake_logger).to receive(:warn) expect(fake_logger).to receive(:error).with(a_string_matching(/when serving the ZIP/)) FakeController.logger = fake_logger expect { - status, headers, body = FakeController.action(:download_zip_with_error_during_streaming).call(fake_rack_env) - body.each { } + _status, _headers, body = FakeController.action(:download_zip_with_error_during_streaming).call(fake_rack_env) + body.each {} }.to raise_error(/Something wonky/) end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b106113..5ccca6c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,22 +1,22 @@ -require 'rspec' -require 'active_support' -require 'active_support/core_ext' -require 'action_dispatch' +require "rspec" +require "active_support" +require "active_support/core_ext" +require "action_dispatch" -require 'zipline' -require 'paperclip' -require 'fog-aws' -require 'carrierwave' +require "zipline" +require "paperclip" +require "fog-aws" +require "carrierwave" -Dir["#{File.expand_path('..', __FILE__)}/support/**/*.rb"].sort.each { |f| require f } +Dir["#{File.expand_path("..", __FILE__)}/support/**/*.rb"].sort.each { |f| require f } CarrierWave.configure do |config| - config.fog_provider = 'fog/aws' + config.fog_provider = "fog/aws" config.fog_credentials = { - provider: 'AWS', - aws_access_key_id: 'dummy', - aws_secret_access_key: 'data', - region: 'us-west-2', + provider: "AWS", + aws_access_key_id: "dummy", + aws_secret_access_key: "data", + region: "us-west-2" } end diff --git a/zipline.gemspec b/zipline.gemspec index 331682c..9cf6cbf 100644 --- a/zipline.gemspec +++ b/zipline.gemspec @@ -1,34 +1,33 @@ -# -*- encoding: utf-8 -*- -require File.expand_path('../lib/zipline/version', __FILE__) +require File.expand_path("../lib/zipline/version", __FILE__) Gem::Specification.new do |gem| - gem.authors = ["Ram Dobson"] - gem.email = ["ram.dobson@solsystemscompany.com"] - gem.description = %q{a module for streaming dynamically generated zip files} - gem.summary = %q{stream zip files from rails} - gem.homepage = "http://github.com/fringd/zipline" + gem.authors = ["Ram Dobson"] + gem.email = ["ram.dobson@solsystemscompany.com"] + gem.description = "a module for streaming dynamically generated zip files" + gem.summary = "stream zip files from rails" + gem.homepage = "http://github.com/fringd/zipline" - gem.files = `git ls-files`.split($\) - %w{.gitignore} - gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } - gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) - gem.name = "zipline" + gem.files = `git ls-files`.split($\) - %w[.gitignore] + gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) } + gem.name = "zipline" gem.require_paths = ["lib"] - gem.version = Zipline::VERSION - gem.licenses = ['MIT'] + gem.version = Zipline::VERSION + gem.licenses = ["MIT"] gem.required_ruby_version = ">= 2.7" - gem.add_dependency 'actionpack', ['>= 6.0', '< 8.0'] - gem.add_dependency 'content_disposition', '~> 1.0' - gem.add_dependency 'zip_kit', ['~> 6', '>= 6.2.0', '< 7'] + gem.add_dependency "actionpack", [">= 6.0", "< 8.0"] + gem.add_dependency "content_disposition", "~> 1.0" + gem.add_dependency "zip_kit", ["~> 6", ">= 6.2.0", "< 7"] - gem.add_development_dependency 'rspec', '~> 3' - gem.add_development_dependency 'fog-aws' - gem.add_development_dependency 'aws-sdk-s3' - gem.add_development_dependency 'carrierwave' - gem.add_development_dependency 'paperclip' - gem.add_development_dependency 'rake' + gem.add_development_dependency "rspec", "~> 3" + gem.add_development_dependency "fog-aws" + gem.add_development_dependency "aws-sdk-s3" + gem.add_development_dependency "carrierwave" + gem.add_development_dependency "paperclip" + gem.add_development_dependency "rake" + gem.add_development_dependency "standard", "1.28.5" # Very specific version of standard for 2.6 with _known_ settings # https://github.com/rspec/rspec-mocks/issues/1457 - gem.add_development_dependency 'rspec-mocks', '~> 3.12' + gem.add_development_dependency "rspec-mocks", "~> 3.12" end