File: commander_spec.rb

package info (click to toggle)
ruby-guard 2.18.1-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,380 kB
  • sloc: ruby: 9,256; makefile: 6
file content (304 lines) | stat: -rw-r--r-- 8,688 bytes parent folder | download | duplicates (4)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# frozen_string_literal: true

require "guard/commander"

RSpec.describe Guard::Commander do
  subject { Guard }

  let(:interactor) { instance_double("Guard::Interactor") }
  let(:runner) { instance_double("Guard::Runner", run: true) }

  let(:scope) { instance_double("Guard::Internals::Scope") }
  let(:state) { instance_double("Guard::Internals::State") }
  let(:session) { instance_double("Guard::Internals::Session") }

  before do
    allow(state).to receive(:scope).and_return(scope)
    allow(state).to receive(:session).and_return(session)
    allow(Guard).to receive(:state).and_return(state)

    allow(Guard::Interactor).to receive(:new) { interactor }
    allow(Guard::Runner).to receive(:new).and_return(runner)
  end

  describe ".start" do
    let(:listener) do
      instance_double("Listen::Listener", start: true, stop: true)
    end

    let(:watched_dir) { Dir.pwd }

    before do
      stub_guardfile(" ")
      stub_user_guard_rb

      # from stop()
      allow(Guard).to receive(:setup)
      allow(Guard).to receive(:listener).and_return(listener)
      allow(session).to receive(:watchdirs).and_return(%w[dir1 dir2])
      allow(Guard).to receive(:interactor).and_return(interactor)

      # Simulate Ctrl-D in Pry, or Ctrl-C in non-interactive mode
      allow(interactor).to receive(:foreground).and_return(:exit)

      allow(interactor).to receive(:background)
      allow(Guard::Notifier).to receive(:disconnect)
    end

    it "calls Guard setup" do
      expect(Guard).to receive(:setup).with({ foo: "bar" })
      Guard.start(foo: "bar")
    end

    it "displays an info message" do
      expect(Guard::UI).to receive(:info)
        .with("Guard is now watching at 'dir1', 'dir2'")

      Guard.start
    end

    it "tell the runner to run the :start task" do
      expect(runner).to receive(:run).with(:start)
      allow(listener).to receive(:stop)
      Guard.start
    end

    it "start the listener" do
      expect(listener).to receive(:start)

      Guard.start
    end

    context "when finished" do
      it "stops everything" do
        expect(interactor).to receive(:foreground).and_return(:exit)

        # From stop()
        expect(interactor).to receive(:background)
        expect(listener).to receive(:stop)
        expect(runner).to receive(:run).with(:stop)
        expect(Guard::UI).to receive(:info).with("Bye bye...", { reset: true })

        Guard.start
      end
    end

    context "when listener.start raises an error" do
      it "calls Commander#stop" do
        allow(listener).to receive(:start).and_raise(RuntimeError)

        # From stop()
        expect(interactor).to receive(:background)
        expect(listener).to receive(:stop)
        expect(runner).to receive(:run).with(:stop)
        expect(Guard::UI).to receive(:info).with("Bye bye...", { reset: true })

        expect { Guard.start }.to raise_error(RuntimeError)
      end
    end

    context "when setup raises an error" do
      it "calls Commander#stop" do
        # Reproduce a case where an error is raised during Guardfile evaluation
        # before the listener and interactor are instantiated.
        expect(Guard).to receive(:listener).and_return(nil)
        expect(Guard).to receive(:interactor).and_return(nil)
        expect(Guard).to receive(:setup).and_raise(RuntimeError)

        # From stop()
        expect(runner).to receive(:run).with(:stop)
        expect(Guard::UI).to receive(:info).with("Bye bye...", { reset: true })

        expect { Guard.start }.to raise_error(RuntimeError)
      end
    end
  end

  describe ".stop" do
    let(:runner) { instance_double("Guard::Runner", run: true) }
    let(:listener) { instance_double("Listen::Listener", stop: true) }

    before do
      allow(Guard::Notifier).to receive(:disconnect)
      allow(Guard).to receive(:listener).and_return(listener)
      allow(listener).to receive(:stop)
      allow(Guard).to receive(:interactor).and_return(interactor)
      allow(interactor).to receive(:background)

      Guard.stop
    end

    it "turns off the interactor" do
      expect(interactor).to have_received(:background)
    end

    it "turns the notifier off" do
      expect(Guard::Notifier).to have_received(:disconnect)
    end

    it "tell the runner to run the :stop task" do
      expect(runner).to have_received(:run).with(:stop)
    end

    it "stops the listener" do
      expect(listener).to have_received(:stop)
    end
  end

  describe ".reload" do
    let(:runner) { instance_double("Guard::Runner", run: true) }
    let(:group) { instance_double("Guard::Group", name: "frontend") }

    before do
      allow(Guard::Notifier).to receive(:connect)
      allow(Guard::UI).to receive(:info)
      allow(Guard::UI).to receive(:clear)

      allow(scope).to receive(:titles).and_return(["all"])

      stub_guardfile(" ")
      stub_user_guard_rb
    end

    it "clears the screen" do
      expect(Guard::UI).to receive(:clear)

      Guard.reload
    end

    it "reloads Guard" do
      expect(runner).to receive(:run).with(:reload, { groups: [group] })
      Guard.reload(groups: [group])
    end
  end

  describe ".run_all" do
    let(:group) { instance_double("Guard::Group", name: "frontend") }

    before do
      allow(::Guard::Notifier).to receive(:connect)
      allow(::Guard::UI).to receive(:action_with_scopes)
      allow(::Guard::UI).to receive(:clear)
    end

    context "with a given scope" do
      it "runs all with the scope" do
        expect(runner).to receive(:run).with(:run_all, { groups: [group] })

        subject.run_all(groups: [group])
      end
    end

    context "with an empty scope" do
      it "runs all" do
        expect(runner).to receive(:run).with(:run_all, {})

        subject.run_all
      end
    end
  end

  describe ".pause" do
    context "when unpaused" do
      let(:listener) { instance_double("Listen::Listener") }

      before do
        allow(::Guard::Notifier).to receive(:connect)
        allow(Guard).to receive(:listener).and_return(listener)
        allow(listener).to receive(:paused?) { false }
      end

      [:toggle, nil, :paused].each do |mode|
        context "with #{mode.inspect}" do
          before do
            allow(listener).to receive(:pause)
          end

          it "pauses" do
            expect(listener).to receive(:pause)
            Guard.pause(mode)
          end

          it "shows a message" do
            expected = /File event handling has been paused/
            expect(Guard::UI).to receive(:info).with(expected)
            Guard.pause(mode)
          end
        end
      end

      context "with :unpaused" do
        it "does nothing" do
          expect(listener).to_not receive(:start)
          expect(listener).to_not receive(:pause)
          Guard.pause(:unpaused)
        end
      end

      context "with invalid parameter" do
        it "raises an ArgumentError" do
          expect { Guard.pause(:invalid) }
            .to raise_error(ArgumentError, "invalid mode: :invalid")
        end
      end
    end

    context "when already paused" do
      let(:listener) { instance_double("Listen::Listener") }

      before do
        allow(::Guard::Notifier).to receive(:connect)
        allow(Guard).to receive(:listener).and_return(listener)
        allow(listener).to receive(:paused?) { true }
      end

      [:toggle, nil, :unpaused].each do |mode|
        context "with #{mode.inspect}" do
          before do
            allow(listener).to receive(:start)
          end

          it "unpauses" do
            expect(listener).to receive(:start)
            Guard.pause(mode)
          end

          it "shows a message" do
            expected = /File event handling has been resumed/
            expect(Guard::UI).to receive(:info).with(expected)
            Guard.pause(mode)
          end
        end
      end

      context "with :paused" do
        it "does nothing" do
          expect(listener).to_not receive(:start)
          expect(listener).to_not receive(:pause)
          Guard.pause(:paused)
        end
      end

      context "with invalid parameter" do
        it "raises an ArgumentError" do
          expect { Guard.pause(:invalid) }
            .to raise_error(ArgumentError, "invalid mode: :invalid")
        end
      end
    end
  end

  describe ".show" do
    let(:dsl_describer) { instance_double("Guard::DslDescriber") }

    before do
      allow(Guard::DslDescriber).to receive(:new).with(no_args)
                                                 .and_return(dsl_describer)
    end

    it "shows list of plugins" do
      expect(dsl_describer).to receive(:show)
      Guard.show
    end
  end
end