File: daemons_spec.rb

package info (click to toggle)
ruby-daemons 1.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 388 kB
  • sloc: ruby: 2,133; makefile: 7
file content (107 lines) | stat: -rwxr-xr-x 2,261 bytes parent folder | download | duplicates (2)
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
require 'spec_helper'

describe Daemons do
  let(:script) {
    'spec/support/test_script.rb'
  }
  let(:shell_args) {
    ['run']
  }
  let(:options) {
    { ARGV: shell_args }
  }
  let(:group) {
    instance_double 'ApplicationGroup'
  }
  let(:controller) {
    instance_double 'Controller',
      group: group,
      catch_exceptions: ->{},
      run: ->{}
  }

  def stub_controller
    allow(Daemons::Controller)
      .to receive(:new)
      .and_return controller

    allow(controller)
      .to receive(:catch_exceptions)
      .and_yield
  end

  describe '.run' do
    let(:ctrl_instance) {
      described_class.instance_variable_get :@controller
    }
    let(:group_instance) {
      described_class.instance_variable_get :@group
    }

    before do
      stub_controller
      Daemons.run script, options
    end

    it { expect(ctrl_instance).to eq controller }
    it { expect(group_instance).to eq group }
    it { expect(controller).to have_received :catch_exceptions }
    it { expect(controller).to have_received :run }

    context 'with custom options hash' do
      let(:options) {
        { foo: "bar", ARGV: shell_args }
      }

      it 'passes a custom options had to the controller' do
        expect(Daemons::Controller)
          .to have_received(:new)
          .with(options, shell_args)
      end
    end
  end

  describe '.run_proc' do
    let(:app_name) { 'my_app' }
    let(:block) { ->{} }
    let(:test_object) { double }
    let(:dir_mode) { :normal }
    let(:dir) {
      File.expand_path '.'
    }
    let(:routine_block) {
      ->{ test_object.run }
    }
    let(:options) {
      { ARGV: shell_args,
        app_name: app_name,
        mode: :proc,
        proc: routine_block,
        dir_mode: dir_mode,
        dir: dir }
    }

    context 'when options[:dir_mode] is nil' do
      let(:dir_mode) { nil }

      before do
        allow(test_object)
          .to receive(:run)

        stub_controller
        Daemons.run_proc(app_name, options, &routine_block)
      end

      it do
        expect(Daemons::Controller)
          .to have_received(:new)
          .with options, shell_args
      end

      it do
        expect(controller)
          .to have_received(:run)
      end
    end
  end
end