Skip to content

Commit 45a6f3a

Browse files
authored
Merge pull request #21669 from Homebrew/migrator_cask_loads
migrator: fix cask loads.
2 parents 1582674 + 199396a commit 45a6f3a

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

Library/Homebrew/cask/migrator.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# typed: strict
22
# frozen_string_literal: true
33

4-
require "cask/cask_loader"
54
require "utils/inreplace"
65
require "utils/output"
76

@@ -27,9 +26,10 @@ def self.migrate_if_needed(new_cask, dry_run: false)
2726

2827
return unless (installed_caskfile = new_cask.installed_caskfile)
2928

30-
old_cask = CaskLoader.load(installed_caskfile)
31-
return if new_cask.token == old_cask.token
29+
installed_token = installed_caskfile.basename(installed_caskfile.extname).to_s
30+
return if new_cask.token == installed_token
3231

32+
old_cask = Cask.new(installed_token)
3333
migrator = new(old_cask, new_cask)
3434
migrator.migrate(dry_run:)
3535
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
require "cask/migrator"
4+
5+
RSpec.describe Cask::Migrator do
6+
describe ".migrate_if_needed" do
7+
let(:new_cask) do
8+
instance_double(
9+
Cask::Cask,
10+
old_tokens: ["old-token"],
11+
installed_caskfile:,
12+
token: new_token,
13+
)
14+
end
15+
16+
context "when the installed token matches the current token" do
17+
let(:installed_caskfile) { Pathname("/tmp/Casks/docker-desktop.rb") }
18+
let(:new_token) { "docker-desktop" }
19+
20+
it "returns without loading or migrating" do
21+
expect(Cask::Cask).not_to receive(:new)
22+
expect(described_class).not_to receive(:new)
23+
24+
described_class.migrate_if_needed(new_cask)
25+
end
26+
end
27+
28+
context "when the installed token differs from the current token" do
29+
let(:installed_caskfile) { Pathname("/tmp/Casks/old-token.rb") }
30+
let(:new_token) { "new-token" }
31+
32+
it "migrates using the installed token from the caskfile path" do
33+
old_cask = instance_double(Cask::Cask)
34+
migrator = instance_double(described_class)
35+
36+
expect(Cask::Cask).to receive(:new).with("old-token").and_return(old_cask)
37+
expect(described_class).to receive(:new).with(old_cask, new_cask).and_return(migrator)
38+
expect(migrator).to receive(:migrate).with(dry_run: false)
39+
40+
described_class.migrate_if_needed(new_cask)
41+
end
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)