Skip to content

Commit

Permalink
Fix API::tap_from_source_download for relative paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Feb 16, 2024
1 parent fa23636 commit a6a6a74
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions Library/Homebrew/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?("../")

Expand Down
27 changes: 27 additions & 0 deletions Library/Homebrew/test/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a6a6a74

Please sign in to comment.