jicksta / adhearsion

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

This URL has Read+Write access

adhearsion / spec / test_ahn_command.rb
100644 292 lines (221 sloc) 10.351 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
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
require File.dirname(__FILE__) + "/test_helper"
require 'adhearsion/cli'
 
context 'The Ahn Command helper' do
  
  include AhnCommandSpecHelper
  
  test "args are simulated properly" do
    before = ARGV.clone
    simulate_args "create", "/tmp/blah"
    ARGV.should.not.equal before
  end
  
  test "STDOUT should be captured" do
    capture_stdout do
      puts "wee"
    end.should.equal "wee\n"
  end
  
end
 
context "A simulated use of the 'ahn' command" do
  
  include AhnCommandSpecHelper
  
  test "USAGE is defined" do
    assert Adhearsion::CLI::AhnCommand.const_defined?('USAGE')
  end
  
  test "arguments to 'create' are executed properly" do
    some_path = "/path/somewhere"
    simulate_args "create", some_path
    flexmock(Adhearsion::CLI::AhnCommand::CommandHandler).should_receive(:create).once.with(some_path)
    capture_stdout { Adhearsion::CLI::AhnCommand.execute! }
  end
  
  test "arguments to 'start' are executed properly properly" do
    some_path = "/tmp/blargh"
    simulate_args "start", some_path
    flexmock(Adhearsion::CLI::AhnCommand::CommandHandler).should_receive(:start).once.with(some_path, false, nil)
    Adhearsion::CLI::AhnCommand.execute!
  end
  
  test "should execute arguments to 'start' for daemonizing properly" do
    somewhere = "/tmp/blarghh"
    simulate_args "start", 'daemon', somewhere
    flexmock(Adhearsion::CLI::AhnCommand::CommandHandler).should_receive(:start).once.with(somewhere, true, nil)
    Adhearsion::CLI::AhnCommand.execute!
  end
  
  test 'parse_arguments should recognize start with daemon properly' do
    path = '/path/to/somesuch'
    arguments = ["start", 'daemon', path]
    Adhearsion::CLI::AhnCommand.parse_arguments(arguments).should == [:start, path, true, nil]
  end
  
  test 'should recognize start with daemon and pid file properly' do
    project_path = '/second/star/on/the/right'
    pid_file_path = '/straight/on/til/morning'
    arguments = ["start", "daemon", project_path, "--pid-file=#{pid_file_path}"]
    Adhearsion::CLI::AhnCommand.parse_arguments(arguments).should == [:start, project_path, true, pid_file_path]
  end
  
  test 'parse_arguments should recognize start without daemon properly' do
    path = '/path/to/somewhere'
    arguments = ['start', path]
    Adhearsion::CLI::AhnCommand.parse_arguments(arguments).should == [:start, path, false, nil]
  end
  
  test "if no path is provided, running Ahn command blows up" do
    flexmock(Adhearsion::CLI::AhnCommand).should_receive(:fail_and_print_usage).once.and_return
    Adhearsion::CLI::AhnCommand.parse_arguments(['start'])
  end
  
  test "printing the version" do
    capture_stdout do
      simulate_args 'version'
      Adhearsion::CLI::AhnCommand.execute!
    end.should =~ Regexp.new(Regexp.escape(Adhearsion::VERSION::STRING))
  end
  
  test "printing the help" do
    capture_stdout do
      simulate_args 'help'
      Adhearsion::CLI::AhnCommand.execute!
    end.should =~ Regexp.new(Regexp.escape(Adhearsion::CLI::AhnCommand::USAGE))
  end
  
  test "reacting to unrecognized commands" do
    simulate_args "alpha", "beta"
    flexmock(Adhearsion::CLI::AhnCommand).should_receive(:fail_and_print_usage).once.and_return
    Adhearsion::CLI::AhnCommand.execute!
  end
  
  test "giving a path that doesn't contain a project raises an exception" do
    simulate_args "start", "/asjdfas/sndjfabsdfbqwb/qnjwejqbwh"
    executing_ahn_command_should_fail_with Adhearsion::CLI::AhnCommand::CommandHandler::PathInvalid
  end
  
end
 
context "Component-related commands" do
  
  include AhnCommandSpecHelper
  
  
  test "should move a folder from the components/disabled/ folder of an app to the components/ directory if it exists" do
    sandbox = create_component_sandbox
    disabled_dir = "#{sandbox}/components/disabled/foobar"
    enabled_dir = "#{sandbox}/components/foobar"
    
    FileUtils.mkdir_p disabled_dir
    FileUtils.touch disabled_dir + "/foobar.rb"
    
    flexmock(Dir).should_receive(:pwd).once.and_return disabled_dir
    
    simulate_args "enable", "component", "foobar"
    capture_stdout { execute_ahn_command }
    
    File.directory?(disabled_dir).should.not.equal true
    File.exists?(enabled_dir + "/foobar.rb").should.equal true
  end
  
  test "should raise a ComponentError exception if there is no disabled folder" do
    sandbox = create_component_sandbox
    
    flexmock(Dir).should_receive(:pwd).once.and_return sandbox
    
    simulate_args "enable", "component", "foo"
    
    executing_ahn_command_should_fail_with Adhearsion::CLI::AhnCommand::CommandHandler::ComponentError
  end
  
  test "should raise an exception if the disabled component exists and there's an enabled component of the same name" do
    sandbox = create_component_sandbox
    
    flexmock(Dir).should_receive(:pwd).once.and_return sandbox
    
    FileUtils.mkdir_p sandbox + "/disabled/lolcats"
    FileUtils.mkdir_p sandbox + "/lolcats"
    
    simulate_args "enable", "component", "foo"
    
    executing_ahn_command_should_fail_with Adhearsion::CLI::AhnCommand::CommandHandler::ComponentError
  end
  
  test "should raise a PathInvalid error if the current directory does not belong to an Adhearsion app" do
    flexmock(Dir).should_receive("pwd").and_return "/"
    simulate_args "enable", "component", "foo"
    executing_ahn_command_should_fail_with Adhearsion::CLI::AhnCommand::CommandHandler::PathInvalid
  end
  
  test "should properly create the disabled folder if it doesn't exist when disabling a component" do
    sandbox = create_component_sandbox
    flexmock(Dir).should_receive(:pwd).once.and_return sandbox
    
    FileUtils.mkdir_p sandbox + "/components/rickroller"
    
    simulate_args 'disable', 'component', 'rickroller'
    capture_stdout { execute_ahn_command }
    File.directory?(sandbox + "/components/disabled/rickroller").should.equal true
    File.directory?(sandbox + "/components/rickroller").should.equal false
  end
  
  test "should raise an UnknownCommand error when trying to enable a kind of feature which doesn't exist" do
    simulate_args "enable", "bonobo"
    executing_ahn_command_should_fail_with Adhearsion::CLI::AhnCommand::CommandHandler::UnknownCommand
  end
  
  test "should raise an UnknownCommand error when trying to disable a kind of feature which doesn't exist" do
    simulate_args "disable", "thanksgiving_dinner"
    executing_ahn_command_should_fail_with Adhearsion::CLI::AhnCommand::CommandHandler::UnknownCommand
  end
  
  test "should raise a ComponentError when the component to disable doesn't exist" do
    sandbox = create_component_sandbox
    flexmock(Dir).should_receive(:pwd).once.and_return sandbox
    
    simulate_args "disable", "component", "monkeybars"
    executing_ahn_command_should_fail_with Adhearsion::CLI::AhnCommand::CommandHandler::ComponentError
  end
  
  test "should raise an exception when disabling a component and the component's disabled directory already exists" do
    sandbox = create_component_sandbox
    
    flexmock(Dir).should_receive(:pwd).once.and_return sandbox
    
    FileUtils.mkdir_p sandbox + "/disabled/lolcats"
    FileUtils.mkdir_p sandbox + "/lolcats"
    
    simulate_args "disable", "component", "foo"
    executing_ahn_command_should_fail_with Adhearsion::CLI::AhnCommand::CommandHandler::ComponentError
  end
  
end
 
context 'The "create" command' do
  
  include AhnCommandSpecHelper
  
  test "creating a project" do
    the_following_code {
      tmp_path = new_tmp_dir
      simulate_args "create", tmp_path
      RubiGen::Base.default_options.merge! :quiet => true
      # capture_stdout { Adhearsion::CLI::AhnCommand.execute! }
      execute_ahn_command
      File.exists?(File.join(tmp_path, ".ahnrc")).should.equal true
    }.should.not.raise
  end
  
  test "should raise a PathInvalid error if the given directory does not belong to an Adhearsion app" do
    simulate_args "create", "component", "foo"
    executing_ahn_command_should_fail_with Adhearsion::CLI::AhnCommand::CommandHandler::PathInvalid
  end
  
  test "should raise an UnknownCommand if running create with no arguments" do
    simulate_args "create"
    executing_ahn_command_should_fail_with Adhearsion::CLI::AhnCommand::CommandHandler::UnknownCommand
  end
  
  test "should raise a ComponentError if the name of the component is not a valid Ruby symbol name" do
    bad_names = ["!))", "37signals", "foo bar", "*"]
    bad_names.each do |bad_name|
      simulate_args "create", "component", bad_name
      executing_ahn_command_should_fail_with Adhearsion::CLI::AhnCommand::CommandHandler::ComponentError
    end
  end
  
  test "should raise a ComponentError if the component name already exists in the folder" do
    sandbox = create_component_sandbox
    Dir.mkdir sandbox + "/components/blehhh"
    flexmock(Dir).should_receive(:pwd).once.and_return sandbox
    simulate_args "create", "component", "blehhh"
    
    executing_ahn_command_should_fail_with Adhearsion::CLI::AhnCommand::CommandHandler::ComponentError
  end
  
  test "should create a folder with matching .rb file and .yml file when all guards pass" do
    sandbox = create_component_sandbox
    flexmock(Dir).should_receive(:pwd).once.and_return sandbox
    
    simulate_args "create", "component", "ohai"
    capture_stdout { execute_ahn_command }
    
    File.exists?(sandbox + "/components/ohai/ohai.rb").should.equal true
    File.exists?(sandbox + "/components/ohai/ohai.yml").should.equal true
  end
  
end
 
BEGIN {
  module AhnCommandSpecHelper
    
    def simulate_args(*args)
      ARGV.clear
      ARGV.concat args
    end
    
    def capture_stdout(&block)
      old = $stdout
      $stdout = io = StringIO.new
      yield
    ensure
      $stdout = old
      return io.string
    end
 
    def new_tmp_dir(filename=new_guid)
      File.join Dir.tmpdir, filename
    end
 
    def create_component_sandbox
      returning new_tmp_dir do |dir|
        Dir.mkdir dir
        FileUtils.touch dir + "/.ahnrc"
        Dir.mkdir dir + "/components"
      end
    end
    
    def execute_ahn_command
      Adhearsion::CLI::AhnCommand.execute!
    end
    
    def executing_ahn_command_should_fail_with(exception)
      flexmock(Adhearsion::CLI::AhnCommand).should_receive(:fail_and_print_usage).with(exception).once
      execute_ahn_command
    end
    
  end
}