Skip to content

Commit

Permalink
Merge pull request #1309 from jneubrand/no-cached-greedy
Browse files Browse the repository at this point in the history
Don’t prefetch greedy outdated casks
  • Loading branch information
MikeMcQuaid authored Feb 22, 2024
2 parents 1fa8f10 + c0fa3dd commit 5da3147
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
13 changes: 11 additions & 2 deletions lib/bundle/cask_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,22 @@ def cask_names
@cask_names ||= casks.map(&:to_s)
end

def outdated_cask_names(greedy: false)
def outdated_cask_names
return [] unless Bundle.cask_installed?

casks.select { |c| c.outdated?(greedy: greedy) }
casks.select { |c| c.outdated?(greedy: false) }
.map(&:to_s)
end

def cask_is_outdated_using_greedy?(cask_name)
return false unless Bundle.cask_installed?

cask = casks.find { |c| c.to_s == cask_name }
return false if cask.nil?

cask.outdated?(greedy: true)
end

def cask_versions
return {} unless Bundle.cask_installed?

Expand Down
7 changes: 1 addition & 6 deletions lib/bundle/cask_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ module CaskInstaller
def reset!
@installed_casks = nil
@outdated_casks = nil
@greedy_outdated_casks = nil
end

def upgrading?(no_upgrade, name, options)
return false if no_upgrade
return true if outdated_casks.include?(name)
return false unless options[:greedy]

greedy_outdated_casks.include?(name)
Bundle::CaskDumper.cask_is_outdated_using_greedy?(name)
end

def preinstall(name, no_upgrade: false, verbose: false, **options)
Expand Down Expand Up @@ -83,9 +82,5 @@ def installed_casks
def outdated_casks
@outdated_casks ||= Bundle::CaskDumper.outdated_cask_names
end

def greedy_outdated_casks
@greedy_outdated_casks ||= Bundle::CaskDumper.outdated_cask_names(greedy: true)
end
end
end
32 changes: 26 additions & 6 deletions spec/bundle/cask_dumper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@
it "dumps as empty string" do
expect(dumper.dump).to eql("")
end

it "doesn’t want to greedily update a non-installed cask" do
expect(dumper.cask_is_outdated_using_greedy?("foo")).to be(false)
end
end

context "when casks `foo`, `bar` and `baz` are installed, with `baz` being a formula requirement" do
before do
described_class.reset!

foo = instance_double(Cask::Cask, to_s: "foo", desc: nil, config: nil)
baz = instance_double(Cask::Cask, to_s: "baz", desc: "Software", config: nil)
bar = instance_double(
let(:foo) { instance_double(Cask::Cask, to_s: "foo", desc: nil, config: nil) }
let(:baz) { instance_double(Cask::Cask, to_s: "baz", desc: "Software", config: nil) }
let(:bar) do
instance_double(
Cask::Cask, to_s: "bar",
desc: nil,
config: instance_double(Cask::Config,
Expand All @@ -53,6 +55,10 @@
},
explicit_s: 'fontdir: "/Library/Fonts", language: "zh-TW"')
)
end

before do
described_class.reset!

allow(Bundle).to receive(:cask_installed?).and_return(true)
allow(Cask::Caskroom).to receive(:casks).and_return([foo, bar, baz])
Expand All @@ -71,6 +77,20 @@
EOS
expect(dumper.dump(describe: true)).to eql(expected.chomp)
end

it "doesn’t want to greedily update a non-installed cask" do
expect(dumper.cask_is_outdated_using_greedy?("qux")).to be(false)
end

it "wants to greedily update foo if there is an update available" do
expect(foo).to receive(:outdated?).with(greedy: true).and_return(true)
expect(dumper.cask_is_outdated_using_greedy?("foo")).to be(true)
end

it "does not want to greedily update bar if there is no update available" do
expect(bar).to receive(:outdated?).with(greedy: true).and_return(false)
expect(dumper.cask_is_outdated_using_greedy?("bar")).to be(false)
end
end

describe "#formula_dependencies" do
Expand Down
2 changes: 1 addition & 1 deletion spec/bundle/cask_installer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
context "when cask is outdated and uses auto-update" do
before do
allow(Bundle::CaskDumper).to receive_messages(cask_names: ["opera"], outdated_cask_names: [])
allow(Bundle::CaskDumper).to receive(:outdated_cask_names).with(greedy: true).and_return(["opera"])
allow(Bundle::CaskDumper).to receive(:cask_is_outdated_using_greedy?).with("opera").and_return(true)
end

it "upgrades" do
Expand Down
4 changes: 4 additions & 0 deletions spec/stub/cask/cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ def depends_on
def config
nil
end

def outdated?(greedy: false)
false
end
end
end

0 comments on commit 5da3147

Please sign in to comment.