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
226
227
228
229
230
231
232
233
234
235
  | 
require "English"
require "open3"
require "rubygems"
describe Hbc::CLI::Style, :cask do
  let(:args) { [] }
  let(:cli) { described_class.new(args) }
  around do |example|
    shutup { example.run }
  end
  describe ".run" do
    subject { described_class.run(args) }
    before do
      allow(described_class).to receive(:new).and_return(cli)
      allow(cli).to receive(:run).and_return(retval)
    end
    context "when rubocop succeeds" do
      let(:retval) { true }
      it "exits successfully" do
        subject
      end
    end
    context "when rubocop fails" do
      let(:retval) { false }
      it "raises an exception" do
        expect { subject }.to raise_error(Hbc::CaskError)
      end
    end
  end
  describe "#run" do
    subject { cli.run }
    before do
      allow(cli).to receive_messages(install_rubocop: nil,
                                     system:          nil,
                                     rubocop_args:    nil,
                                     cask_paths:      nil)
      allow($CHILD_STATUS).to receive(:success?).and_return(success)
    end
    context "when rubocop succeeds" do
      let(:success) { true }
      it { is_expected.to be_truthy }
    end
    context "when rubocop fails" do
      let(:success) { false }
      it { is_expected.to be_falsey }
    end
  end
  describe "#install_rubocop" do
    subject { cli.install_rubocop }
    context "when installation succeeds" do
      before do
        allow(Homebrew).to receive(:install_gem_setup_path!)
      end
      it "exits successfully" do
        expect { subject }.not_to raise_error
      end
    end
    context "when installation fails" do
      before do
        allow(Homebrew).to receive(:install_gem_setup_path!).and_raise(SystemExit)
      end
      it "raises an error" do
        expect { subject }.to raise_error(Hbc::CaskError)
      end
    end
    context "version" do
      it "matches `HOMEBREW_RUBOCOP_VERSION`", :needs_network do
        stdout, status = Open3.capture2("gem", "dependency", "rubocop-cask", "--version", HOMEBREW_RUBOCOP_CASK_VERSION, "--pipe", "--remote")
        expect(status).to be_a_success
        requirement = Gem::Requirement.new(stdout.scan(/rubocop --version '(.*)'/).flatten.first)
        version = Gem::Version.new(HOMEBREW_RUBOCOP_VERSION)
        expect(requirement).not_to be_none
        expect(requirement).to be_satisfied_by(version)
      end
    end
  end
  describe "#cask_paths" do
    subject { cli.cask_paths }
    before do
      allow(cli).to receive(:cask_tokens).and_return(tokens)
    end
    context "when no cask tokens are given" do
      let(:tokens) { [] }
      before do
        allow(Hbc).to receive(:all_tapped_cask_dirs).and_return(%w[Casks MoreCasks])
      end
      it { is_expected.to eq(%w[Casks MoreCasks]) }
    end
    context "when at least one cask token is a path that exists" do
      let(:tokens) { ["adium", "Casks/dropbox.rb"] }
      before do
        allow(File).to receive(:exist?).and_return(false, true)
      end
      it "treats all tokens as paths" do
        expect(subject).to eq(tokens)
      end
    end
    context "when no cask tokens are paths that exist" do
      let(:tokens) { %w[adium dropbox] }
      before do
        allow(File).to receive(:exist?).and_return(false)
      end
      it "tries to find paths for all tokens" do
        expect(Hbc::CaskLoader).to receive(:path).twice
        subject
      end
    end
  end
  describe "#cask_tokens" do
    subject { cli.cask_tokens }
    context "when no args are given" do
      let(:args) { [] }
      it { is_expected.to be_empty }
    end
    context "when only flags are given" do
      let(:args) { ["--fix"] }
      it { is_expected.to be_empty }
    end
    context "when only empty args are given" do
      let(:args) { ["", ""] }
      it { is_expected.to be_empty }
    end
    context "when a cask token is given" do
      let(:args) { ["adium"] }
      it { is_expected.to eq(["adium"]) }
    end
    context "when multiple cask tokens are given" do
      let(:args) { %w[adium dropbox] }
      it { is_expected.to eq(%w[adium dropbox]) }
    end
    context "when cask tokens are given with flags" do
      let(:args) { ["adium", "dropbox", "--fix"] }
      it { is_expected.to eq(%w[adium dropbox]) }
    end
  end
  describe "#rubocop_args" do
    subject { cli.rubocop_args }
    before do
      allow(cli).to receive(:fix?).and_return(fix)
    end
    context "when fix? is true" do
      let(:fix) { true }
      it { is_expected.to include("--auto-correct") }
    end
    context "when fix? is false" do
      let(:fix) { false }
      it { is_expected.not_to include("--auto-correct") }
    end
  end
  describe "#default_args" do
    subject { cli.default_args }
    it { is_expected.to include("--require", "rubocop-cask", "--format", "simple", "--force-exclusion") }
  end
  describe "#autocorrect_args" do
    subject { cli.autocorrect_args }
    let(:default_args) { ["--format", "simple"] }
    it "should add --auto-correct to default args" do
      allow(cli).to receive(:default_args).and_return(default_args)
      expect(subject).to include("--auto-correct", *default_args)
    end
  end
  describe "#fix?" do
    subject { cli.fix? }
    context "when --fix is passed as an argument" do
      let(:args) { ["adium", "--fix"] }
      it { is_expected.to be_truthy }
    end
    context "when --correct is passed as an argument" do
      let(:args) { ["adium", "--correct"] }
      it { is_expected.to be_truthy }
    end
    context "when --auto-correct is passed as an argument" do
      let(:args) { ["adium", "--auto-correct"] }
      it { is_expected.to be_truthy }
    end
    context "when --auto-correct is misspelled as --autocorrect" do
      let(:args) { ["adium", "--autocorrect"] }
      it { is_expected.to be_truthy }
    end
    context "when no flag equivalent to --fix is passed as an argument" do
      let(:args) { ["adium"] }
      it { is_expected.to be_falsey }
    end
  end
end
  |