From 15af7189eb706026727a8d1bb38e01d650028bb3 Mon Sep 17 00:00:00 2001 From: Seeker <meaningseeking@protonmail.com> Date: Fri, 4 Sep 2020 14:13:43 -0700 Subject: [PATCH] utils: add `tar` Includes `validate_file` method to be used by `bump-formula-pr` --- Library/Homebrew/dev-cmd/bump-formula-pr.rb | 16 ++----- Library/Homebrew/test/utils/tar_spec.rb | 49 +++++++++++++++++++++ Library/Homebrew/utils/tar.rb | 38 ++++++++++++++++ 3 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 Library/Homebrew/test/utils/tar_spec.rb create mode 100644 Library/Homebrew/utils/tar.rb diff --git a/Library/Homebrew/dev-cmd/bump-formula-pr.rb b/Library/Homebrew/dev-cmd/bump-formula-pr.rb index f98e505216..c5434f73c2 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 0000000000..b3569cad80 --- /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 0000000000..c60c834da4 --- /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 -- GitLab