aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/cask/cli/upgrade_spec.rb
blob: af026157eb9c1002ddf55140aaf66e995c06edc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
require_relative "shared_examples/invalid_option"

describe Hbc::CLI::Upgrade, :cask do
  it_behaves_like "a command that handles invalid options"

  context "successful upgrade" do
    let(:installed) {
      [
        "outdated/local-caffeine",
        "outdated/local-transmission",
        "outdated/auto-updates",
        "outdated/version-latest",
      ]
    }

    before(:example) do
      installed.each { |cask| Hbc::CLI::Install.run(cask) }

      allow_any_instance_of(described_class).to receive(:verbose?).and_return(true)
    end

    describe 'without --greedy it ignores the Casks with "version latest" or "auto_updates true"' do
      it "updates all the installed Casks when no token is provided" do
        local_caffeine = Hbc::CaskLoader.load("local-caffeine")
        local_caffeine_path = Hbc::Config.global.appdir.join("Caffeine.app")
        local_transmission = Hbc::CaskLoader.load("local-transmission")
        local_transmission_path = Hbc::Config.global.appdir.join("Transmission.app")

        expect(local_caffeine).to be_installed
        expect(local_caffeine_path).to be_a_directory
        expect(local_caffeine.versions).to include("1.2.2")

        expect(local_transmission).to be_installed
        expect(local_transmission_path).to be_a_directory
        expect(local_transmission.versions).to include("2.60")

        described_class.run

        expect(local_caffeine).to be_installed
        expect(local_caffeine_path).to be_a_directory
        expect(local_caffeine.versions).to include("1.2.3")

        expect(local_transmission).to be_installed
        expect(local_transmission_path).to be_a_directory
        expect(local_transmission.versions).to include("2.61")
      end

      it "updates only the Casks specified in the command line" do
        local_caffeine = Hbc::CaskLoader.load("local-caffeine")
        local_caffeine_path = Hbc::Config.global.appdir.join("Caffeine.app")
        local_transmission = Hbc::CaskLoader.load("local-transmission")
        local_transmission_path = Hbc::Config.global.appdir.join("Transmission.app")

        expect(local_caffeine).to be_installed
        expect(local_caffeine_path).to be_a_directory
        expect(local_caffeine.versions).to include("1.2.2")

        expect(local_transmission).to be_installed
        expect(local_transmission_path).to be_a_directory
        expect(local_transmission.versions).to include("2.60")

        described_class.run("local-caffeine")

        expect(local_caffeine).to be_installed
        expect(local_caffeine_path).to be_a_directory
        expect(local_caffeine.versions).to include("1.2.3")

        expect(local_transmission).to be_installed
        expect(local_transmission_path).to be_a_directory
        expect(local_transmission.versions).to include("2.60")
      end

      it 'updates "auto_updates" and "latest" Casks when their tokens are provided in the command line' do
        local_caffeine = Hbc::CaskLoader.load("local-caffeine")
        local_caffeine_path = Hbc::Config.global.appdir.join("Caffeine.app")
        auto_updates = Hbc::CaskLoader.load("auto-updates")
        auto_updates_path = Hbc::Config.global.appdir.join("MyFancyApp.app")

        expect(local_caffeine).to be_installed
        expect(local_caffeine_path).to be_a_directory
        expect(local_caffeine.versions).to include("1.2.2")

        expect(auto_updates).to be_installed
        expect(auto_updates_path).to be_a_directory
        expect(auto_updates.versions).to include("2.57")

        described_class.run("local-caffeine", "auto-updates")

        expect(local_caffeine).to be_installed
        expect(local_caffeine_path).to be_a_directory
        expect(local_caffeine.versions).to include("1.2.3")

        expect(auto_updates).to be_installed
        expect(auto_updates_path).to be_a_directory
        expect(auto_updates.versions).to include("2.61")
      end
    end

    describe "with --greedy it checks additional Casks" do
      it 'includes the Casks with "auto_updates true" or "version latest"' do
        local_caffeine = Hbc::CaskLoader.load("local-caffeine")
        local_caffeine_path = Hbc::Config.global.appdir.join("Caffeine.app")
        auto_updates = Hbc::CaskLoader.load("auto-updates")
        auto_updates_path = Hbc::Config.global.appdir.join("MyFancyApp.app")
        local_transmission = Hbc::CaskLoader.load("local-transmission")
        local_transmission_path = Hbc::Config.global.appdir.join("Transmission.app")
        version_latest = Hbc::CaskLoader.load("version-latest")
        version_latest_path_1 = Hbc::Config.global.appdir.join("Caffeine Mini.app")
        version_latest_path_2 = Hbc::Config.global.appdir.join("Caffeine Pro.app")

        expect(local_caffeine).to be_installed
        expect(local_caffeine_path).to be_a_directory
        expect(local_caffeine.versions).to include("1.2.2")

        expect(auto_updates).to be_installed
        expect(auto_updates_path).to be_a_directory
        expect(auto_updates.versions).to include("2.57")

        expect(local_transmission).to be_installed
        expect(local_transmission_path).to be_a_directory
        expect(local_transmission.versions).to include("2.60")

        expect(version_latest).to be_installed
        expect(version_latest_path_1).to be_a_directory
        expect(version_latest_path_2).to be_a_directory
        expect(version_latest.versions).to include("latest")

        described_class.run("--greedy")

        expect(local_caffeine).to be_installed
        expect(local_caffeine_path).to be_a_directory
        expect(local_caffeine.versions).to include("1.2.3")

        expect(auto_updates).to be_installed
        expect(auto_updates_path).to be_a_directory
        expect(auto_updates.versions).to include("2.61")

        expect(local_transmission).to be_installed
        expect(local_transmission_path).to be_a_directory
        expect(local_transmission.versions).to include("2.61")

        expect(version_latest).to be_installed
        expect(version_latest_path_1).to be_a_directory
        expect(version_latest_path_2).to be_a_directory
        expect(version_latest.versions).to include("latest")
      end

      it 'does not include the Casks with "auto_updates true" when the version did not change' do
        cask = Hbc::CaskLoader.load("auto-updates")
        cask_path = Hbc::Config.global.appdir.join("MyFancyApp.app")

        expect(cask).to be_installed
        expect(cask_path).to be_a_directory
        expect(cask.versions).to include("2.57")

        described_class.run("auto-updates", "--greedy")

        expect(cask).to be_installed
        expect(cask_path).to be_a_directory
        expect(cask.versions).to include("2.61")

        described_class.run("auto-updates", "--greedy")

        expect(cask).to be_installed
        expect(cask_path).to be_a_directory
        expect(cask.versions).to include("2.61")
      end
    end
  end

  context "failed upgrade" do
    let(:installed) {
      [
        "outdated/bad-checksum",
        "outdated/will-fail-if-upgraded",
      ]
    }

    before(:example) do
      installed.each { |cask| Hbc::CLI::Install.run(cask) }

      allow_any_instance_of(described_class).to receive(:verbose?).and_return(true)
    end

    output_reverted = Regexp.new <<~EOS
      Warning: Reverting upgrade for Cask .*
    EOS

    it "restores the old Cask if the upgrade failed" do
      will_fail_if_upgraded = Hbc::CaskLoader.load("will-fail-if-upgraded")
      will_fail_if_upgraded_path = Hbc::Config.global.appdir.join("container")

      expect(will_fail_if_upgraded).to be_installed
      expect(will_fail_if_upgraded_path).to be_a_file
      expect(will_fail_if_upgraded.versions).to include("1.2.2")

      expect {
        described_class.run("will-fail-if-upgraded")
      }.to raise_error(Hbc::CaskError).and output(output_reverted).to_stderr

      expect(will_fail_if_upgraded).to be_installed
      expect(will_fail_if_upgraded_path).to be_a_file
      expect(will_fail_if_upgraded.versions).to include("1.2.2")
      expect(will_fail_if_upgraded.staged_path).to_not exist
    end

    it "does not restore the old Cask if the upgrade failed pre-install" do
      bad_checksum = Hbc::CaskLoader.load("bad-checksum")
      bad_checksum_path = Hbc::Config.global.appdir.join("Caffeine.app")

      expect(bad_checksum).to be_installed
      expect(bad_checksum_path).to be_a_directory
      expect(bad_checksum.versions).to include("1.2.2")

      expect {
        described_class.run("bad-checksum")
      }.to raise_error(Hbc::CaskSha256MismatchError).and(not_to_output(output_reverted).to_stderr)

      expect(bad_checksum).to be_installed
      expect(bad_checksum_path).to be_a_directory
      expect(bad_checksum.versions).to include("1.2.2")
      expect(bad_checksum.staged_path).to_not exist
    end
  end
end