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.



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

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)


73
74
75
76
77
# File 'lib/sprout/tasks/flashplayer_task.rb', line 73

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



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

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.



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

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, 'AppData', 'Roaming', 'Macromedia', 'Flash Player'),
          File.join(User.home, '.macromedia', 'Flash_Player')]
end

.trustObject

Local system path to the Flash Player Trust file



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

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

Instance Method Details

#closeObject



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

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

#defineObject

:nodoc:



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

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

#do_not_focusObject



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

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



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

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



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/sprout/tasks/flashplayer_task.rb', line 339

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:



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

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
    config = FlashPlayerConfig.new
    log_file = config.log_file
    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) unless log_file.nil?
  @thread.join
end

#gem_nameObject



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

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



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

def gem_name=(name)
  @gem_name = name
end

#gem_versionObject



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

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.



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

def gem_version=(version)
  @gem_version = version
end

#parse_test_result(line, thread) ⇒ Object

Returns true if inside of a test result



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/sprout/tasks/flashplayer_task.rb', line 312

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



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

def read_log(thread, log_file)
  lines_put = 0

  if(log_file.nil?)
    raise FlashPlayerError.new('[ERROR] Unable to find the trace output log file because the expected location was nil')
  end

  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
268
# 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)
    require 'clix_flash_player'
    @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



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

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


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

def swf=(swf)
  @swf = swf
end

#test_resultObject



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

def test_result
  @test_result ||= ''
end

#test_result_fileObject



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

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’



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

def test_result_file=(file)
  @test_result_file = file
end

#write_test_result(result) ⇒ Object



332
333
334
335
336
337
# File 'lib/sprout/tasks/flashplayer_task.rb', line 332

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