From 06c650886daa2a6eaf4c029cbf686ff401d44c48 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Fri, 17 Dec 2021 08:57:33 -0800 Subject: Simplify file reads This is a new RuboCop cop that moves things to file.read an file.binread vs. an open and then a read. Signed-off-by: Tim Smith --- spec/support/shared/functional/file_resource.rb | 2 +- spec/support/shared/functional/http.rb | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) (limited to 'spec/support/shared/functional') diff --git a/spec/support/shared/functional/file_resource.rb b/spec/support/shared/functional/file_resource.rb index 1385d3dc30..b31982a2d9 100644 --- a/spec/support/shared/functional/file_resource.rb +++ b/spec/support/shared/functional/file_resource.rb @@ -391,7 +391,7 @@ shared_examples_for "a configured file resource" do end def binread(file) - content = File.open(file, "rb", &:read) + content = File.binread(file) content.force_encoding(Encoding::BINARY) if "".respond_to?(:force_encoding) content end diff --git a/spec/support/shared/functional/http.rb b/spec/support/shared/functional/http.rb index ffe52e2148..d3d4531371 100644 --- a/spec/support/shared/functional/http.rb +++ b/spec/support/shared/functional/http.rb @@ -30,7 +30,7 @@ module ChefHTTPShared end def binread(file) - content = File.open(file, "rb", &:read) + content = File.binread(file) content.force_encoding(Encoding::BINARY) if "".respond_to?(:force_encoding) content end @@ -51,19 +51,19 @@ module ChefHTTPShared # just a normal file # (expected_content should be uncompressed) @api.get("/nyan_cat.png", 200) do - File.open(nyan_uncompressed_filename, "rb", &:read) + File.binread(nyan_uncompressed_filename) end # this ends in .gz, we do not uncompress it and drop it on the filesystem as a .gz file (the internet often lies) # (expected_content should be compressed) @api.get("/nyan_cat.png.gz", 200, nil, { "Content-Type" => "application/gzip", "Content-Encoding" => "gzip" } ) do - File.open(nyan_compressed_filename, "rb", &:read) + File.binread(nyan_compressed_filename) end # this is an uncompressed file that was compressed by some mod_gzip-ish webserver thingy, so we will expand it # (expected_content should be uncompressed) @api.get("/nyan_cat_compressed.png", 200, nil, { "Content-Type" => "application/gzip", "Content-Encoding" => "gzip" } ) do - File.open(nyan_compressed_filename, "rb", &:read) + File.binread(nyan_compressed_filename) end # @@ -75,7 +75,7 @@ module ChefHTTPShared { "Content-Length" => nyan_uncompressed_size.to_s, }) do - File.open(nyan_uncompressed_filename, "rb", &:read) + File.binread(nyan_uncompressed_filename) end # (expected_content should be uncompressed) @@ -85,7 +85,7 @@ module ChefHTTPShared "Content-Type" => "application/gzip", "Content-Encoding" => "gzip", }) do - File.open(nyan_compressed_filename, "rb", &:read) + File.binread(nyan_compressed_filename) end # @@ -97,7 +97,7 @@ module ChefHTTPShared { "Content-Length" => (nyan_uncompressed_size + 1).to_s, }) do - File.open(nyan_uncompressed_filename, "rb", &:read) + File.binread(nyan_uncompressed_filename) end # (expected_content should be uncompressed) @@ -107,7 +107,7 @@ module ChefHTTPShared "Content-Type" => "application/gzip", "Content-Encoding" => "gzip", }) do - File.open(nyan_compressed_filename, "rb", &:read) + File.binread(nyan_compressed_filename) end # @@ -120,7 +120,7 @@ module ChefHTTPShared "Content-Length" => (nyan_uncompressed_size + 1).to_s, "Transfer-Encoding" => "anything", }) do - File.open(nyan_uncompressed_filename, "rb", &:read) + File.binread(nyan_uncompressed_filename) end # -- cgit v1.2.1