diff --git a/Library/Homebrew/dev-cmd/bump-formula-pr.rb b/Library/Homebrew/dev-cmd/bump-formula-pr.rb index f98e5052167fa6f0a61d34f1350db9234fee3c89..c5434f73c276ca58e5a16591603d18aa04f780a3 100644 --- a/Library/Homebrew/dev-cmd/bump-formula-pr.rb +++ b/Library/Homebrew/dev-cmd/bump-formula-pr.rb @@ -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 = [] diff --git a/Library/Homebrew/test/utils/tar_spec.rb b/Library/Homebrew/test/utils/tar_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..b3569cad8017d38db88e6791c88318d4d900c45e --- /dev/null +++ b/Library/Homebrew/test/utils/tar_spec.rb @@ -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 diff --git a/Library/Homebrew/utils/tar.rb b/Library/Homebrew/utils/tar.rb new file mode 100644 index 0000000000000000000000000000000000000000..c60c834da41d9c6291ac35087b27a4ebddc38eee --- /dev/null +++ b/Library/Homebrew/utils/tar.rb @@ -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