Skip to content
Snippets Groups Projects
Unverified Commit b94f04b7 authored by Seeker's avatar Seeker Committed by GitHub
Browse files

Merge pull request #8611 from SeekingMeaning/bump-formula-pr/validate_tar_download

utils: add `tar`
parents 08ab2d73 15af7189
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@
require "formula"
require "cli/parser"
require "utils/pypi"
require "utils/tar"
module Homebrew
module_function
......@@ -205,19 +206,8 @@ module Homebrew
end
check_closed_pull_requests(formula, tap_full_name, url: new_url, args: args) unless new_version
resource_path, forced_version = fetch_resource(formula, new_version, new_url)
tar_file_extensions = %w[.tar .tb2 .tbz .tbz2 .tgz .tlz .txz .tZ]
if tar_file_extensions.any? { |extension| new_url.include? extension }
gnu_tar_gtar_path = HOMEBREW_PREFIX/"opt/gnu-tar/bin/gtar"
gnu_tar_gtar = gnu_tar_gtar_path if gnu_tar_gtar_path.executable?
tar = which("gtar") || gnu_tar_gtar || which("tar")
if Utils.popen_read(tar, "-tf", resource_path).match?(%r{/.*\.})
new_hash = resource_path.sha256
else
odie "#{resource_path} is not a valid tar file!"
end
else
new_hash = resource_path.sha256
end
Utils::Tar.validate_file(resource_path)
new_hash = resource_path.sha256
end
replacement_pairs = []
......
# frozen_string_literal: true
require "utils/tar"
describe Utils::Tar do
before do
described_class.clear_executable_cache
end
describe ".available?" do
it "returns true if tar or gnu-tar is available" do
if described_class.executable.present?
expect(described_class).to be_available
else
expect(described_class).not_to be_available
end
end
end
describe ".validate_file" do
it "does not raise an error when tar and gnu-tar are unavailable" do
allow(described_class).to receive(:available?).and_return false
expect { described_class.validate_file "blah" }.not_to raise_error
end
context "when tar or gnu-tar is available" do
let(:testball_resource) { "#{TEST_FIXTURE_DIR}/tarballs/testball-0.1.tbz" }
let(:invalid_resource) { "#{TEST_TMPDIR}/invalid.tgz" }
before do
allow(described_class).to receive(:available?).and_return true
end
it "does not raise an error if file is not a tar file" do
expect { described_class.validate_file "blah" }.not_to raise_error
end
it "does not raise an error if file is valid tar file" do
expect { described_class.validate_file testball_resource }.not_to raise_error
end
it "raises an error if file is an invalid tar file" do
FileUtils.touch invalid_resource
expect { described_class.validate_file invalid_resource }.to raise_error SystemExit
FileUtils.rm_f invalid_resource
end
end
end
end
# frozen_string_literal: true
module Utils
# Helper functions for interacting with tar files.
#
# @api private
module Tar
module_function
TAR_FILE_EXTENSIONS = %w[.tar .tb2 .tbz .tbz2 .tgz .tlz .txz .tZ].freeze
def available?
executable.present?
end
def executable
return @executable if defined?(@executable)
gnu_tar_gtar_path = HOMEBREW_PREFIX/"opt/gnu-tar/bin/gtar"
gnu_tar_gtar = gnu_tar_gtar_path if gnu_tar_gtar_path.executable?
@executable = which("gtar") || gnu_tar_gtar || which("tar")
end
def validate_file(path)
return unless available?
path = Pathname.new(path)
return unless TAR_FILE_EXTENSIONS.include? path.extname
return if Utils.popen_read(executable, "-tf", path).match?(%r{/.*\.})
odie "#{path} is not a valid tar file!"
end
def clear_executable_cache
remove_instance_variable(:@executable) if defined?(@executable)
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment