utils: add tar
Includes `validate_file` method to be used by `bump-formula-pr`
This commit is contained in:
parent
17a9b58b59
commit
15af7189eb
@ -3,6 +3,7 @@
|
|||||||
require "formula"
|
require "formula"
|
||||||
require "cli/parser"
|
require "cli/parser"
|
||||||
require "utils/pypi"
|
require "utils/pypi"
|
||||||
|
require "utils/tar"
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
module_function
|
module_function
|
||||||
@ -205,19 +206,8 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
check_closed_pull_requests(formula, tap_full_name, url: new_url, args: args) unless new_version
|
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)
|
resource_path, forced_version = fetch_resource(formula, new_version, new_url)
|
||||||
tar_file_extensions = %w[.tar .tb2 .tbz .tbz2 .tgz .tlz .txz .tZ]
|
Utils::Tar.validate_file(resource_path)
|
||||||
if tar_file_extensions.any? { |extension| new_url.include? extension }
|
new_hash = resource_path.sha256
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
replacement_pairs = []
|
replacement_pairs = []
|
||||||
|
49
Library/Homebrew/test/utils/tar_spec.rb
Normal file
49
Library/Homebrew/test/utils/tar_spec.rb
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
# 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
|
38
Library/Homebrew/utils/tar.rb
Normal file
38
Library/Homebrew/utils/tar.rb
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
# 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
|
Loading…
x
Reference in New Issue
Block a user