File: session_functional.rb

package info (click to toggle)
rake 0.8.7-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 808 kB
  • ctags: 877
  • sloc: ruby: 6,912; makefile: 36; ansic: 19
file content (339 lines) | stat: -rw-r--r-- 8,863 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env ruby

begin
  require 'rubygems'
rescue LoadError => ex
end
require 'test/unit'
require 'fileutils'
require 'session'
require 'test/in_environment'
require 'test/rake_test_setup'

# Version 2.1.9 of session has a bug where the @debug instance
# variable is not initialized, causing warning messages.  This snippet
# of code fixes that problem.
module Session
  class AbstractSession
    alias old_initialize initialize
    def initialize(*args)
      @debug = nil
      old_initialize(*args)
    end
  end
end

class FunctionalTest < Test::Unit::TestCase
  include InEnvironment
  include TestMethods

  RUBY_COMMAND = 'ruby'

  def setup
    @rake_path = File.expand_path("bin/rake")
    lib_path = File.expand_path("lib")
    @ruby_options = "-I#{lib_path} -I."
    @verbose = ! ENV['VERBOSE'].nil?
    if @verbose
      puts
      puts
      puts "--------------------------------------------------------------------"
      puts name
      puts "--------------------------------------------------------------------"
    end
  end

  def test_rake_default
    Dir.chdir("test/data/default") do rake end
    assert_match(/^DEFAULT$/, @out)
    assert_status
  end

  def test_rake_error_on_bad_task
    Dir.chdir("test/data/default") do rake "xyz" end
    assert_match(/rake aborted/, @err)
    assert_status(1)
  end

  def test_env_availabe_at_top_scope
    Dir.chdir("test/data/default") do rake "TESTTOPSCOPE=1" end
    assert_match(/^TOPSCOPE$/, @out)
    assert_status
  end

  def test_env_availabe_at_task_scope
    Dir.chdir("test/data/default") do rake "TESTTASKSCOPE=1 task_scope" end
    assert_match(/^TASKSCOPE$/, @out)
    assert_status
  end

  def test_multi_desc
    in_environment(
      'RAKE_COLUMNS' => "80",
      "PWD" => "test/data/multidesc"
      ) do
      rake "-T"
    end
    assert_match %r{^rake a *# A / A2 *$}, @out
    assert_match %r{^rake b *# B *$}, @out
    assert_no_match %r{^rake c}, @out
    assert_match %r{^rake d *# x{65}\.\.\.$}, @out
  end
  
  def test_long_description
    in_environment("PWD" => "test/data/multidesc") do
      rake "--describe"
    end
    assert_match %r{^rake a\n *A / A2 *$}m, @out
    assert_match %r{^rake b\n *B *$}m, @out
    assert_match %r{^rake d\n *x{80}}m, @out
    assert_no_match %r{^rake c\n}m, @out
  end

  def test_rbext
    in_environment("PWD" => "test/data/rbext") do
      rake "-N"
    end
    assert_match %r{^OK$}, @out
  end

  def test_system
    in_environment('RAKE_SYSTEM' => 'test/data/sys') do
      rake '-g', "sys1"
    end
    assert_match %r{^SYS1}, @out
  end

  def test_system_excludes_rakelib_files_too
    in_environment('RAKE_SYSTEM' => 'test/data/sys') do
      rake '-g', "sys1", '-T', 'extra'
    end
    assert_no_match %r{extra:extra}, @out
  end

  def test_by_default_rakelib_files_are_include
    in_environment('RAKE_SYSTEM' => 'test/data/sys') do
      rake '-T', 'extra'
    end
    assert_match %r{extra:extra}, @out
  end

  def test_implicit_system
    in_environment('RAKE_SYSTEM' => File.expand_path('test/data/sys'), "PWD" => "/") do
      rake "sys1", "--trace"
    end
    assert_match %r{^SYS1}, @out
  end

  def test_no_system
    in_environment('RAKE_SYSTEM' => 'test/data/sys') do
      rake '-G', "sys1"
    end
    assert_match %r{^Don't know how to build task}, @err # emacs wart: '
  end

  def test_nosearch_with_rakefile_uses_local_rakefile
    in_environment("PWD" => "test/data/default") do
      rake "--nosearch"
    end
    assert_match %r{^DEFAULT}, @out
  end

  def test_nosearch_without_rakefile_finds_system
    in_environment(
      "PWD" => "test/data/nosearch",
      "RAKE_SYSTEM" => File.expand_path("test/data/sys")
      ) do
      rake "--nosearch", "sys1"
    end
    assert_match %r{^SYS1}, @out
  end

  def test_nosearch_without_rakefile_and_no_system_fails
    in_environment("PWD" => "test/data/nosearch", "RAKE_SYSTEM" => "not_exist") do
      rake "--nosearch"
    end
    assert_match %r{^No Rakefile found}, @err
  end

  def test_dry_run
    in_environment("PWD" => "test/data/default") do rake "-n", "other" end
    assert_match %r{Execute \(dry run\) default}, @out
    assert_match %r{Execute \(dry run\) other}, @out
    assert_no_match %r{DEFAULT}, @out
    assert_no_match %r{OTHER}, @out
  end

  # Test for the trace/dry_run bug found by Brian Chandler
  def test_dry_run_bug
    in_environment("PWD" => "test/data/dryrun") do
      rake
    end
    FileUtils.rm_f "test/data/dryrun/temp_one"
    in_environment("PWD" => "test/data/dryrun") do
      rake "--dry-run"
    end
    assert_no_match(/No such file/, @out)
    assert_status
  end

  # Test for the trace/dry_run bug found by Brian Chandler
  def test_trace_bug
    in_environment("PWD" => "test/data/dryrun") do
      rake
    end
    FileUtils.rm_f "test/data/dryrun/temp_one"
    in_environment("PWD" => "test/data/dryrun") do
      rake "--trace"
    end
    assert_no_match(/No such file/, @out)
    assert_status
  end

  def test_imports
    open("test/data/imports/static_deps", "w") do |f|
      f.puts 'puts "STATIC"'
    end
    FileUtils.rm_f "test/data/imports/dynamic_deps"
    in_environment("PWD" => "test/data/imports") do
      rake
    end
    assert File.exist?("test/data/imports/dynamic_deps"),
      "'dynamic_deps' file should exist"
    assert_match(/^FIRST$\s+^DYNAMIC$\s+^STATIC$\s+^OTHER$/, @out)
    assert_status
    FileUtils.rm_f "test/data/imports/dynamic_deps"
    FileUtils.rm_f "test/data/imports/static_deps"
  end

  def test_rules_chaining_to_file_task
    remove_chaining_files
    in_environment("PWD" => "test/data/chains") do
      rake
    end
    assert File.exist?("test/data/chains/play.app"),
      "'play.app' file should exist"
    assert_status
    remove_chaining_files
  end

  def test_file_creation_task
    in_environment("PWD" => "test/data/file_creation_task") do
      rake "prep"
      rake "run"
      rake "run"
    end
    assert(@err !~ /^cp src/, "Should not recopy data")
  end

  def test_dash_f_with_no_arg_foils_rakefile_lookup
    rake "-I test/data/rakelib -rtest1 -f"
    assert_match(/^TEST1$/, @out)
  end

  def test_dot_rake_files_can_be_loaded_with_dash_r
    rake "-I test/data/rakelib -rtest2 -f"
    assert_match(/^TEST2$/, @out)
  end

  def test_can_invoke_task_in_toplevel_namespace
    in_environment("PWD" => "test/data/namespace") do
      rake "copy"
    end
    assert_match(/^COPY$/, @out)
  end

  def test_can_invoke_task_in_nested_namespace
    in_environment("PWD" => "test/data/namespace") do
      rake "nest:copy"
      assert_match(/^NEST COPY$/, @out)
    end
  end

  def test_tasks_can_reference_task_in_same_namespace
    in_environment("PWD" => "test/data/namespace") do
      rake "nest:xx"
      assert_match(/^NEST COPY$/m, @out)
    end
  end

  def test_tasks_can_reference_task_in_other_namespaces
    in_environment("PWD" => "test/data/namespace") do
      rake "b:run"
      assert_match(/^IN A\nIN B$/m, @out)
    end
  end

  def test_anonymous_tasks_can_be_invoked_indirectly
    in_environment("PWD" => "test/data/namespace") do
      rake "anon"
      assert_match(/^ANON COPY$/m, @out)
    end
  end

  def test_rake_namespace_refers_to_toplevel
    in_environment("PWD" => "test/data/namespace") do
      rake "very:nested:run"
      assert_match(/^COPY$/m, @out)
    end
  end

  def test_file_task_are_not_scoped_by_namespaces
    in_environment("PWD" => "test/data/namespace") do
      rake "xyz.rb"
      assert_match(/^XYZ1\nXYZ2$/m, @out)
    end
  end
  
  def test_rake_returns_status_error_values
    in_environment("PWD" => "test/data/statusreturn") do
      rake "exit5"
      assert_status(5)
    end
  end

  def test_rake_returns_no_status_error_on_normal_exit
    in_environment("PWD" => "test/data/statusreturn") do
      rake "normal"
      assert_status(0)
    end
  end

  private

  def remove_chaining_files
    %w(play.scpt play.app base).each do |fn|
      FileUtils.rm_f File.join("test/data/chains", fn)
    end
  end

  class << self
    def format_command
      @format_command ||= lambda { |ruby_options, rake_path, options|
        "ruby #{ruby_options} #{rake_path} #{options}"
      }
    end
    
    def format_command=(fmt_command)
      @format_command = fmt_command
    end
  end
  
  def rake(*option_list)
    options = option_list.join(' ')
    shell = Session::Shell.new
    command = self.class.format_command[@ruby_options, @rake_path, options]
    puts "COMMAND: [#{command}]" if @verbose
    @out, @err = shell.execute command
    @status = shell.exit_status
    puts "STATUS:  [#{@status}]" if @verbose
    puts "OUTPUT:  [#{@out}]" if @verbose
    puts "ERROR:   [#{@err}]" if @verbose
    puts "PWD:     [#{Dir.pwd}]" if @verbose
    shell.close
  end

  def assert_status(expected_status=0)
    assert_equal expected_status, @status
  end
end