Merge pull request #16690 from reitermarkus/api-tap_from_source_download

Fix `API::tap_from_source_download` for relative paths.
This commit is contained in:
Markus Reiter 2024-02-17 04:47:21 +01:00 committed by GitHub
commit 7eafefbd24
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 0 deletions

View File

@ -175,6 +175,7 @@ module Homebrew
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?("../")

View File

@ -67,4 +67,31 @@ describe Homebrew::API do
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