From a6a6a74c485593a5ee956bded76227efb4a67496 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 16 Feb 2024 23:19:39 +0100 Subject: [PATCH] Fix `API::tap_from_source_download` for relative paths. --- Library/Homebrew/api.rb | 1 + Library/Homebrew/test/api_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/Library/Homebrew/api.rb b/Library/Homebrew/api.rb index a6071c0031017..df733d69dd193 100644 --- a/Library/Homebrew/api.rb +++ b/Library/Homebrew/api.rb @@ -175,6 +175,7 @@ def self.write_names_file(names, type, regenerate:) sig { params(path: Pathname).returns(T.nilable(Tap)) } def self.tap_from_source_download(path) + path = path.expand_path source_relative_path = path.relative_path_from(Homebrew::API::HOMEBREW_CACHE_API_SOURCE) return if source_relative_path.to_s.start_with?("../") diff --git a/Library/Homebrew/test/api_spec.rb b/Library/Homebrew/test/api_spec.rb index 3bfece9f5e748..4b6c121c6958f 100644 --- a/Library/Homebrew/test/api_spec.rb +++ b/Library/Homebrew/test/api_spec.rb @@ -67,4 +67,31 @@ def mock_curl_download(stdout:) end.to raise_error(SystemExit) end end + + describe "::tap_from_source_download" do + let(:api_cache_root) { Homebrew::API::HOMEBREW_CACHE_API_SOURCE } + let(:cache_path) do + api_cache_root/"Homebrew"/"homebrew-core"/"cf5c386c1fa2cb54279d78c0990dd7a0fa4bc327"/"Formula"/"foo.rb" + end + + context "when given a path inside the API source cache" do + it "returns the corresponding tap" do + expect(described_class.tap_from_source_download(cache_path)).to eq CoreTap.instance + end + end + + context "when given a path that is not inside the API source cache" do + let(:api_cache_root) { mktmpdir } + + it "returns nil" do + expect(described_class.tap_from_source_download(cache_path)).to be_nil + end + end + + context "when given a relative path that is not inside the API source cache" do + it "returns nil" do + expect(described_class.tap_from_source_download(Pathname("../foo.rb"))).to be_nil + end + end + end end