aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/cask/cli/cleanup_spec.rb
blob: 64e3ef49fe742827915744c38a0b7a6bdcc1a031 (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
describe Hbc::CLI::Cleanup, :cask do
  let(:cache_location) { Pathname.new(Dir.mktmpdir).realpath }
  let(:outdated_only) { false }

  subject { described_class.new(*cask_tokens, cache_location: cache_location) }

  before(:each) do
    allow_any_instance_of(described_class).to receive(:outdated_only?).and_return(outdated_only)
  end

  after do
    cache_location.rmtree
  end

  describe "cleanup" do
    let(:cask_token) { "caffeine" }
    let(:cask_tokens) { [cask_token] }

    it "removes cached downloads of given casks" do
      cached_downloads = [
        cache_location.join("#{cask_token}--latest.zip"),
        cache_location.join("transmission--2.61.dmg"),
      ]

      cached_downloads.each(&FileUtils.method(:touch))

      cleanup_size = cached_downloads[0].disk_usage

      expect {
        subject.run
      }.to output(<<-EOS.undent).to_stdout
        ==> Removing cached downloads for #{cask_token}
        #{cached_downloads[0]}
        ==> This operation has freed approximately #{disk_usage_readable(cleanup_size)} of disk space.
      EOS

      expect(cached_downloads[0].exist?).to eq(false)
      expect(cached_downloads[1].exist?).to eq(true)
    end

    context "when no argument is given" do
      let(:cask_tokens) { [] }

      it "removes all cached downloads" do
        cached_download = cache_location.join("SomeDownload.dmg")
        FileUtils.touch(cached_download)
        cleanup_size = subject.disk_cleanup_size

        expect {
          subject.run
        }.to output(<<-EOS.undent).to_stdout
          ==> Removing cached downloads
          #{cached_download}
          ==> This operation has freed approximately #{disk_usage_readable(cleanup_size)} of disk space.
        EOS

        expect(cached_download.exist?).to eq(false)
      end

      context "and :outdated_only is specified" do
        let(:outdated_only) { true }

        it "does not remove cache files newer than 10 days old" do
          cached_download = cache_location.join("SomeNewDownload.dmg")
          FileUtils.touch(cached_download)

          expect {
            subject.run
          }.to output(<<-EOS.undent).to_stdout
            ==> Removing cached downloads older than 10 days old
            Nothing to do
          EOS

          expect(cached_download.exist?).to eq(true)
        end
      end
    end
  end
end