From 62a4734c9026500b77d30d092252b7e1ad59e6cc Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Tue, 27 Aug 2024 00:32:05 -0700 Subject: [PATCH] tap: add tests for formula_file? and cask_file? --- Library/Homebrew/tap.rb | 10 ++-- Library/Homebrew/test/tap_spec.rb | 98 +++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index c8696f8cf4..dec5485ee5 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -755,13 +755,15 @@ class Tap sig { returns(Regexp) } def formula_file_regex - @formula_file_regex ||= case formula_dir.basename.to_s - when "Formula" + @formula_file_regex ||= case formula_dir + when path/"Formula" %r{^Formula(/[^/]+)+\.rb$} - when "HomebrewFormula" + when path/"HomebrewFormula" %r{^HomebrewFormula(/[^/]+)+\.rb$} - else + when path %r{^[^/]+\.rb$} + else + raise ArgumentError, "Unexpected formula_dir: #{formula_dir}" end end private :formula_file_regex diff --git a/Library/Homebrew/test/tap_spec.rb b/Library/Homebrew/test/tap_spec.rb index adb60d9c38..a455b15cd7 100644 --- a/Library/Homebrew/test/tap_spec.rb +++ b/Library/Homebrew/test/tap_spec.rb @@ -3,6 +3,7 @@ RSpec.describe Tap do include FileUtils + alias_matcher :have_cask_file, :be_cask_file alias_matcher :have_formula_file, :be_formula_file alias_matcher :have_custom_remote, :be_custom_remote @@ -631,6 +632,103 @@ RSpec.describe Tap do expect(homebrew_foo_tap.pypi_formula_mappings).to eq expected_result end end + + describe "#formula_file?" do + it "matches files from Formula/" do + tap = described_class.fetch("hard/core") + FileUtils.mkdir_p(tap.path/"Formula") + + %w[ + kvazaar.rb + Casks/kvazaar.rb + Casks/k/kvazaar.rb + Formula/kvazaar.sh + HomebrewFormula/kvazaar.rb + HomebrewFormula/k/kvazaar.rb + ].each do |relative_path| + expect(tap).not_to have_formula_file(relative_path) + end + + %w[ + Formula/kvazaar.rb + Formula/k/kvazaar.rb + ].each do |relative_path| + expect(tap).to have_formula_file(relative_path) + end + ensure + FileUtils.rm_rf(tap.path.parent) if tap + end + + it "matches files from HomebrewFormula/" do + tap = described_class.fetch("hard/core") + FileUtils.mkdir_p(tap.path/"HomebrewFormula") + + %w[ + kvazaar.rb + Casks/kvazaar.rb + Casks/k/kvazaar.rb + Formula/kvazaar.rb + Formula/k/kvazaar.rb + HomebrewFormula/kvazaar.sh + ].each do |relative_path| + expect(tap).not_to have_formula_file(relative_path) + end + + %w[ + HomebrewFormula/kvazaar.rb + HomebrewFormula/k/kvazaar.rb + ].each do |relative_path| + expect(tap).to have_formula_file(relative_path) + end + ensure + FileUtils.rm_rf(tap.path.parent) if tap + end + + it "matches files from the top-level directory" do + tap = described_class.fetch("hard/core") + FileUtils.mkdir_p(tap.path) + + %w[ + kvazaar.sh + Casks/kvazaar.rb + Casks/k/kvazaar.rb + Formula/kvazaar.rb + Formula/k/kvazaar.rb + HomebrewFormula/kvazaar.rb + HomebrewFormula/k/kvazaar.rb + ].each do |relative_path| + expect(tap).not_to have_formula_file(relative_path) + end + + expect(tap).to have_formula_file("kvazaar.rb") + ensure + FileUtils.rm_rf(tap.path.parent) if tap + end + end + + describe "#cask_file?" do + it "matches files from Casks/" do + tap = described_class.fetch("hard/core") + + %w[ + kvazaar.rb + Casks/kvazaar.sh + Formula/kvazaar.rb + Formula/k/kvazaar.rb + HomebrewFormula/kvazaar.rb + HomebrewFormula/k/kvazaar.rb + ].each do |relative_path| + expect(tap).not_to have_cask_file(relative_path) + end + + %w[ + Casks/kvazaar.rb + Casks/k/kvazaar.rb + ].each do |relative_path| + expect(tap).to have_cask_file(relative_path) + end + end + end end describe CoreTap do