From 37ecdb28f7c892d61d9625ef2f7c92f39f78c0dd Mon Sep 17 00:00:00 2001 From: Markus Reiter <me@reitermark.us> Date: Wed, 9 Sep 2020 22:10:33 +0200 Subject: [PATCH] Add spec for `NamedArgs#to_paths`. --- Library/Homebrew/cli/named_args.rb | 2 +- Library/Homebrew/test/cli/named_args_spec.rb | 41 ++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/cli/named_args.rb b/Library/Homebrew/cli/named_args.rb index 5ad8a27c43..fa63db0dfe 100644 --- a/Library/Homebrew/cli/named_args.rb +++ b/Library/Homebrew/cli/named_args.rb @@ -84,7 +84,7 @@ module Homebrew @to_paths ||= {} @to_paths[only] ||= downcased_unique_named.flat_map do |name| if File.exist?(name) - name + Pathname(name) elsif name.count("/") == 1 Tap.fetch(name).path else diff --git a/Library/Homebrew/test/cli/named_args_spec.rb b/Library/Homebrew/test/cli/named_args_spec.rb index 10856882a4..b70a67acc3 100644 --- a/Library/Homebrew/test/cli/named_args_spec.rb +++ b/Library/Homebrew/test/cli/named_args_spec.rb @@ -120,4 +120,45 @@ describe Homebrew::CLI::NamedArgs do expect(described_class.new("foo").homebrew_tap_cask_names).to be_empty end end + + describe "#to_paths" do + let(:existing_path) { mktmpdir } + let(:formula_path) { Pathname("/path/to/foo.rb") } + let(:cask_path) { Pathname("/path/to/baz.rb") } + + before do + allow(formula_path).to receive(:exist?).and_return(true) + allow(cask_path).to receive(:exist?).and_return(true) + + allow(Formulary).to receive(:path).and_call_original + allow(Cask::CaskLoader).to receive(:path).and_call_original + end + + it "returns taps, cask formula and existing paths" do + expect(Formulary).to receive(:path).with("foo").and_return(formula_path) + expect(Cask::CaskLoader).to receive(:path).with("baz").and_return(cask_path) + + expect(described_class.new("homebrew/core", "foo", "baz", existing_path.to_s).to_paths) + .to eq [Tap.fetch("homebrew/core").path, formula_path, cask_path, existing_path] + end + + it "returns both cask and formula paths if they exist" do + expect(Formulary).to receive(:path).with("foo").and_return(formula_path) + expect(Cask::CaskLoader).to receive(:path).with("baz").and_return(cask_path) + + expect(described_class.new("foo", "baz").to_paths).to eq [formula_path, cask_path] + end + + it "returns only formulae when `only: :formulae` is specified" do + expect(Formulary).to receive(:path).with("foo").and_return(formula_path) + + expect(described_class.new("foo", "baz").to_paths(only: :formulae)).to eq [formula_path, Formulary.path("baz")] + end + + it "returns only casks when `only: :casks` is specified" do + expect(Cask::CaskLoader).to receive(:path).with("foo").and_return(cask_path) + + expect(described_class.new("foo", "baz").to_paths(only: :casks)).to eq [cask_path, Cask::CaskLoader.path("baz")] + end + end end -- GitLab