Class: Sprout::FlashPlayerTask

Inherits:
Rake::Task
  • Object
show all
Defined in:
lib/sprout/tasks/flashplayer_task.rb

Overview

The FlashPlayerTask will download, unpack, configure and launch the debug Flash Player for OS X, Windows and Linux.

Simply send the rake task the swf you’d like to launch either indirectly as a rake dependency or directly as the swf property of this task.

flashplayer :run => 'bin/SomeProject.swf'

Or you could:

flashplayer :run do |t|
  t.swf = 'bin/SomeProject.swf'
end

Constant Summary collapse

@@test_result_pre_delimiter =

This is the opening prelude to a collection of test results. When the task encounters this string in the trace output log file, it will begin collecting trace statements with the expectation that the following strings will be well-formatted XML data matching what JUnit emits for Cruise Control.

See the lib/asunit3/asunit.framework.XMLResultPrinter for more information.

'<XMLResultPrinter>'
@@test_result_post_delimiter =

This is the closing string that will indicate the end of test result XML data

'</XMLResultPrinter>'
@@home =
nil
@@trust =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task_name, app) ⇒ FlashPlayerTask

Returns a new instance of FlashPlayerTask.



67
68
69
70
71
72
73
# File 'lib/sprout/tasks/flashplayer_task.rb', line 67

def initialize(task_name, app)
  super(task_name, app)
  @default_gem_name = 'sprout-flashplayer-tool'
  @default_gem_version = '10.22.0'
  @default_result_file = 'AsUnitResults.xml'
  @inside_test_result = false
end

Class Method Details

.define_task(args) {|t| ... } ⇒ Object

Yields:

  • (t)


75
76
77
78
79
# File 'lib/sprout/tasks/flashplayer_task.rb', line 75

def self.define_task(args, &block)
  t = super
  yield t if block_given?
  t.define
end

.homeObject

Local system path to where the Flash Player stores trace output logs and trust files



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/sprout/tasks/flashplayer_task.rb', line 91

def FlashPlayerTask.home
  if(@@home)
    return @@home
  end

  FlashPlayerTask.home_paths.each do |path|
    if(File.exists?(path))
      return @@home = path
    end
  end

  if(@@home.nil?)
    raise FlashPlayerError.new('FlashPlayer unable to find home folder for your platform')
  end
  return @@home
end

.home_pathsObject

Collection of the potential locations of the Flash Player Home For each supported Platform, the first existing location will be used.



111
112
113
114
115
116
# File 'lib/sprout/tasks/flashplayer_task.rb', line 111

def FlashPlayerTask.home_paths
  return [File.join(User.library, 'Preferences', 'Macromedia', 'Flash Player'),
          File.join(User.library, 'Application Support', 'Macromedia'),
          File.join(User.home, 'Application Data', 'Macromedia', 'Flash Player'),
          File.join(User.home, '.macromedia', 'Flash_Player')]
end

.trustObject

Local system path to the Flash Player Trust file



82
83
84
85
86
87
88
# File 'lib/sprout/tasks/flashplayer_task.rb', line 82

def FlashPlayerTask.trust
  if(@@trust)
    return @@trust
  end
  @@trust = File.join(FlashPlayerTask.home, '#Security', 'FlashPlayerTrust', 'sprout.cfg')
  return @@trust
end

Instance Method Details

#closeObject



269
270
271
272
273
274
275
276
277
278
# File 'lib/sprout/tasks/flashplayer_task.rb', line 269

def close
  usr = User.new
  if(usr.is_a?(WinUser))
    Thread.kill(@thread)
  elsif(usr.is_a?(OSXUser))
    @thread.kill
  else
    Process.kill("SIGALRM", @player_pid)
  end
end

#defineObject

:nodoc:



210
211
212
# File 'lib/sprout/tasks/flashplayer_task.rb', line 210

def define # :nodoc:
  CLEAN.add(test_result_file)
end

#do_not_focusObject



158
159
160
# File 'lib/sprout/tasks/flashplayer_task.rb', line 158

def do_not_focus
  @do_not_focus ||= nil
end

#do_not_focus=(focus) ⇒ Object

By default, the Flash Player should be given focus after being launched. Unfortunately, this doesn’t work properly on OS X, so we needed to do some hackery in order to make it happen. This in turn can lead to multiple instances of the Player being instantiated. In the case of running a test harness, this is absolutely not desirable, so we had expose a parameter that allows us to prevent auto-focus of the player.

This feature is deprecated in current versions of the FlashPlayerTask



153
154
155
156
# File 'lib/sprout/tasks/flashplayer_task.rb', line 153

def do_not_focus=(focus)
  @do_not_focus = focus
  puts "[WARNING] Thanks to fixes in the FlashPlayer task, do_not_focus is deprecated and no longer needs to be used"
end

#examine_test_result(result) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/sprout/tasks/flashplayer_task.rb', line 334

def examine_test_result(result)
  require 'rexml/document'
  doc = nil
  begin
    doc = REXML::Document.new(result)
  rescue REXML::ParseException => e
    puts "[WARNING] Invalid test results encountered"
    return
  end
  
  # Handle JUnit Failures
  failures = []

  doc.elements.each('/testsuites/testsuite/testsuite/testcase/error') do |element|
    failures << element.text
  end

  doc.elements.each("/testsuites/testsuite/testsuite/testcase/failure") do |element|
    failures << element.text
  end

  if(failures.size > 0)
    raise AssertionFailure.new("[ERROR] Test Failures Encountered \n#{failures.join("\n")}")
  end
end

#execute(*args) ⇒ Object

Raises:



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
# File 'lib/sprout/tasks/flashplayer_task.rb', line 214

def execute(*args)
  super
  raise FlashPlayerError.new("FlashPlayer task #{name} required field swf is nil") unless swf
  
  log_file = nil

  # Don't let trust or log file failures break other features...
  begin
    log_file = FlashPlayerConfig.new().log
    FlashPlayerTrust.new(File.expand_path(File.dirname(swf)))

    if(File.exists?(log_file))
      File.open(log_file, 'w') do |f|
        f.write('')
      end
    else
      FileUtils.makedirs(File.dirname(log_file))
      FileUtils.touch(log_file)
    end
  rescue StandardError => e
    puts '[WARNING] FlashPlayer encountered an error working with the mm.cfg log and/or editing the Trust file'
  end
  
  @running_process = nil
  @thread = run(gem_name, gem_version, swf)
  read_log(@thread, log_file)
  @thread.join
end

#gem_nameObject



191
192
193
# File 'lib/sprout/tasks/flashplayer_task.rb', line 191

def gem_name
  return @gem_name ||= @default_gem_name
end

#gem_name=(name) ⇒ Object

Full name of the sprout tool gem that this tool task will use. This defaults to sprout-flashplayer-tool



187
188
189
# File 'lib/sprout/tasks/flashplayer_task.rb', line 187

def gem_name=(name)
  @gem_name = name
end

#gem_versionObject



181
182
183
# File 'lib/sprout/tasks/flashplayer_task.rb', line 181

def gem_version
  return @gem_version ||= nil
end

#gem_version=(version) ⇒ Object

The gem version of the sprout-flashplayer-tool RubyGem to download.

It’s important to note that this version number will differ slightly from the actual player version in that the final revision (the last of three numbers), is the gem version, while the first two describe the player version being downloaded. The exact gem version that you would like the ToolTask to execute. By default this value should be nil and will download the latest version of the gem that is available unless there is a version already installed on your system.

This attribute could be an easy way to update your local gem to the latest version without leaving your build file, but it’s primary purpose is to allow you to specify very specific versions of the tools that your project depends on. This way your team can rest assured that they are all working with the same tools.



177
178
179
# File 'lib/sprout/tasks/flashplayer_task.rb', line 177

def gem_version=(version)
  @gem_version = version
end

#parse_test_result(line, thread) ⇒ Object

Returns true if inside of a test result



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/sprout/tasks/flashplayer_task.rb', line 307

def parse_test_result(line, thread)
  if(@inside_test_result)
    if(line.index(@@test_result_post_delimiter))
      @inside_test_result = false
      write_test_result(test_result)
      close
      examine_test_result test_result
      return true
    else
      test_result << line
    end
  end

  if(line.index(@@test_result_pre_delimiter))
    @inside_test_result = true
  end
  
  return @inside_test_result
end

#read_log(thread, log_file) ⇒ Object



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
# File 'lib/sprout/tasks/flashplayer_task.rb', line 280

def read_log(thread, log_file)
  lines_put = 0

  if(!File.exists?(log_file))
    raise FlashPlayerError.new('[ERROR] Unable to find the trace output log file in the expected location: ' + log_file)
  end

  while(thread.alive?)
    sleep(0.2)
    lines_read = 0

    File.open(log_file, 'r') do |file|
      file.readlines.each do |line|
        lines_read = lines_read + 1
        if(lines_read > lines_put)
          if(!parse_test_result(line, thread))
            puts "[trace] #{line}"
          end
          $stdout.flush
          lines_put = lines_put + 1
        end
      end
    end
  end
end

#run(tool, gem_version, swf) ⇒ Object



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
# File 'lib/sprout/tasks/flashplayer_task.rb', line 243

def run(tool, gem_version, swf)
  path_to_exe = Sprout.get_executable(tool, nil, gem_version)
  target = User.clean_path(path_to_exe)
  @player_pid = nil
  
  thread_out = $stdout
  command = "#{target} #{User.clean_path(swf)}"

  usr = User.new()
  if(usr.is_a?(WinUser) && !usr.is_a?(CygwinUser))
    return Thread.new {
        system command
    }
  elsif usr.is_a?(OSXUser)
    @clix_player = CLIXFlashPlayer.new
    @clix_player.execute(target, swf)
    return @clix_player
  else
    return Thread.new {
      require 'open4'
      @player_pid, stdin, stdout, stderr = Open4.popen4(command)
      stdout.read
    }
  end
end

#swfObject



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/sprout/tasks/flashplayer_task.rb', line 133

def swf
  @swf ||= nil
  if(@swf.nil?)
    prerequisites.each do |req|
      if(req.index('.swf'))
        @swf = req.to_s
        break
      end
    end
  end
  return @swf
end

#swf=(swf) ⇒ Object

The swf parameter can be set explicitly in the block sent to this task as in:

flashplayer :run do |t|
  t.swf = 'bin/SomeProject.swf'
end

Or it can be set implicitly as a rake prerequisite as follows:

flashplayer :run => 'bin/SomeProject' do |t|
end


129
130
131
# File 'lib/sprout/tasks/flashplayer_task.rb', line 129

def swf=(swf)
  @swf = swf
end

#test_resultObject



206
207
208
# File 'lib/sprout/tasks/flashplayer_task.rb', line 206

def test_result
  @test_result ||= ''
end

#test_result_fileObject



202
203
204
# File 'lib/sprout/tasks/flashplayer_task.rb', line 202

def test_result_file
  @test_result_file ||= @default_result_file
end

#test_result_file=(file) ⇒ Object

The File where JUnit test results should be written. This value defaults to ‘AsUnitResults.xml’



198
199
200
# File 'lib/sprout/tasks/flashplayer_task.rb', line 198

def test_result_file=(file)
  @test_result_file = file
end

#write_test_result(result) ⇒ Object



327
328
329
330
331
332
# File 'lib/sprout/tasks/flashplayer_task.rb', line 327

def write_test_result(result)
  FileUtils.makedirs(File.dirname(test_result_file))
  File.open(test_result_file, File::CREAT|File::TRUNC|File::RDWR) do |f|
    f.puts(result)
  end
end