jicksta / adhearsion

Open-source framework for writing voice-enabled applications using Ruby.

This URL has Read+Write access

adhearsion / spec / test_initialization.rb
100644 163 lines (142 sloc) 6.133 kb
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
require File.dirname(__FILE__) + '/test_helper'
 
context "Adhearsion::Initializer" do
  
  include InitializerStubs
  # TODO: create a specification for aliases
  
  before :each do
    Adhearsion.send(:remove_const, 'AHN_CONFIG') if Adhearsion.const_defined? 'AHN_CONFIG'
    Adhearsion::AHN_CONFIG = Adhearsion::Configuration.new
  end
  
  after :each do
    Adhearsion::Events.reinitialize_theatre!
  end
  
  test "initialization will start with only a path given" do
    stub_behavior_for_initializer_with_no_path_changing_behavior do
      Adhearsion::Initializer.start path
    end
  end
 
  test "should create a pid file in the app's path when given 'true' as the pid_file hash key argument" do
    stub_behavior_for_initializer_with_no_path_changing_behavior do
       flexmock(File).should_receive(:open).with(File.join(path, 'adhearsion.pid'), 'w', Proc).at_least.once
       ahn = Adhearsion::Initializer.start path, :pid_file => true
       ahn.pid_file[0, path.length].should.equal(path)
    end
  end
  
  test "should NOT create a pid file in the app's path when given 'false' as the pid_file hash key argument" do
    stub_behavior_for_initializer_with_no_path_changing_behavior do
      ahn = Adhearsion::Initializer.start path, :pid_file => false
      assert_nil ahn.pid_file
    end
  end
  
  test "should create a pid file in the app's path by default when daemonizing" do
    stub_behavior_for_initializer_with_no_path_changing_behavior do
      flexmock(File).should_receive(:open).once.with(File.join(path, 'adhearsion.pid'), 'w', Proc)
      ahn = Adhearsion::Initializer.start path, :daemon => true
      ahn.pid_file[0, path.size].should.equal(path)
    end
  end
  
  test "should NOT create a pid file in the app's path when daemonizing and :pid_file is given as false" do
    stub_behavior_for_initializer_with_no_path_changing_behavior do
      ahn = Adhearsion::Initializer.start path, :daemon => true, :pid_file => false
      assert_nil ahn.pid_file
    end
  end
  
  test "should execute gem when .ahnrc contains gem names" do
    stub_behavior_for_initializer_with_no_path_changing_behavior do
      ahn_rc = {
        "gems" => {
          "activerecord" => { "version" => ">= 1.2.0" },
          "twitter" => nil
        },
        # Paths are unnecessary except to make the other part of bootstrap_rc happy.
        "paths"=>{"dialplan"=>"dialplan.rb", "init"=>"config/startup.rb", "events"=>"events.rb",
            "models"=> "models/*.rb"}
      }
      ahn = Adhearsion::Initializer.new path
      flexmock(Adhearsion::Initializer).should_receive(:get_rules_from).once.and_return ahn_rc
      flexmock(ahn).should_receive(:gem).once.with("activerecord", ">= 1.2.0")
      flexmock(ahn).should_receive(:gem).once.with("twitter")
      ahn.start
    end
  end
  
  test "should require() the lib when .ahnrc contains a require section with one name" do
    stub_behavior_for_initializer_with_no_path_changing_behavior do
      ahn_rc = {
        "gems" => {
          "twitter" => {
            "require" => "sometwitterstuffs"
          }
        },
        # Paths are unnecessary except to make the other part of bootstrap_rc happy.
        "paths"=>{"dialplan"=>"dialplan.rb", "init"=>"config/startup.rb", "events"=>"events.rb",
            "models"=>"models/*.rb"}
      }
      ahn = Adhearsion::Initializer.new path
      flexmock(Adhearsion::Initializer).should_receive(:get_rules_from).once.and_return ahn_rc
      flexstub(ahn).should_receive(:gem).once.with("twitter")
      flexmock(ahn).should_receive(:require).once.with("sometwitterstuffs")
      flexmock(ahn).should_receive(:require).at_least.once.with(String)
      ahn.start
    end
  end
  
  test "should create a designated pid file when supplied a String path as :pid_file" do
    random_file = "/tmp/AHN_TEST_#{rand 100000}.pid"
    stub_behavior_for_initializer_with_no_path_changing_behavior do
      ahn = Adhearsion::Initializer.start path, :pid_file => random_file
      ahn.pid_file.should.equal(random_file)
      assert File.exists?(random_file)
      File.delete random_file
    end
  end
  
  test "should initialze events properly" do
    require 'theatre'
    events_rb = Tempfile.new "events.rb"
    initializer = Adhearsion::Initializer.new("/does/not/matter")
    flexmock(Adhearsion::AHN_CONFIG).should_receive(:files_from_setting).at_least.once.with("paths", "events").
        and_return([events_rb.path])
    flexmock(Theatre::Theatre).new_instances.should_receive(:load_events_file).once.with events_rb.path
    flexmock(Adhearsion::Events.framework_theatre).should_receive(:start!).once
    
    initializer.send :init_events_subsystem
    initializer.send :init_events_file
  end
  
  private
    def path
      '/any/ole/path'
    end
end
 
context "AHN_ROOT" do
  include InitializerStubs
  setup do
    Object.send(:remove_const, :AHN_ROOT) if defined? AHN_ROOT
  end
  
  test "initializing will create the AHN_ROOT" do
    stub_behavior_for_initializer_with_no_path_changing_behavior do
      ahn = Adhearsion::Initializer.start path
      assert Object.constants.include?("AHN_ROOT")
    end
  end
  
  test "swapping out the base_path for the duration of the block" do
    original_base_path = '.'
    temporary_base = '/foo'
    
    path = Adhearsion::PathString.new(original_base_path)
    path.should.equal original_base_path
    
    path.using_base_path temporary_base do
      path.should.equal temporary_base
    end
    path.should.equal original_base_path
  end
 
  test "creating the AHN_ROOT will set defaults" do
    stub_behavior_for_initializer_with_no_path_changing_behavior do
      flexstub(Adhearsion::Initializer).new_instances.should_receive(:load).and_return
      ahn = Adhearsion::Initializer.start path
      full_path = File.expand_path(path)
      AHN_ROOT.to_s.should.equal(full_path)
      AHN_ROOT.component_path.should.equal(File.join(full_path, "components"))
      AHN_ROOT.log_path.should.equal(File.join(full_path, "logs"))
      AHN_ROOT.dialplan_path.should.equal(full_path)
    end
  end
  private
    def path
      '/any/ole/path'
    end
end