Class: PRSpecThread

Inherits:
Object
  • Object
show all
Defined in:
lib/prspec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, tests, environment, args) ⇒ PRSpecThread

Returns a new instance of PRSpecThread.



326
327
328
329
330
331
332
333
334
335
336
# File 'lib/prspec.rb', line 326

def initialize(id, tests, environment, args)
  @id = id
  @tests = tests
  @env = environment
  @args = args
  @output = ''
  @out = Tempfile.new("prspec-t-#{@id}.out").path
  $log.debug("Thread#{@id} @out file: #{@out}")
  @err = Tempfile.new("prspec-t-#{@id}.err").path
  $log.debug("Thread#{@id} @err file: #{@err}")
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



325
326
327
# File 'lib/prspec.rb', line 325

def args
  @args
end

#envObject

Returns the value of attribute env.



325
326
327
# File 'lib/prspec.rb', line 325

def env
  @env
end

#idObject

Returns the value of attribute id.



325
326
327
# File 'lib/prspec.rb', line 325

def id
  @id
end

#outputObject

Returns the value of attribute output.



325
326
327
# File 'lib/prspec.rb', line 325

def output
  @output
end

#testsObject

Returns the value of attribute tests.



325
326
327
# File 'lib/prspec.rb', line 325

def tests
  @tests
end

#threadObject

Returns the value of attribute thread.



325
326
327
# File 'lib/prspec.rb', line 325

def thread
  @thread
end

Instance Method Details

#closeObject



374
375
376
377
378
379
380
# File 'lib/prspec.rb', line 374

def close
  @thread.sleep
  @thread.kill
  @thread = nil
  FileUtils.remove_file(@out, :force => true) unless !File.exist?(@out)
  FileUtils.remove_file(@err, :force => true) unless !File.exist?(@err)
end

#done?Boolean

Returns:

  • (Boolean)


368
369
370
371
372
# File 'lib/prspec.rb', line 368

def done?
  return !@thread.alive?
rescue
  return true # if there's a problem with @thread we're done
end

#get_exportsObject



382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/prspec.rb', line 382

def get_exports
  separator = PRSpec.is_windows? ? ' & ' : ';'
  exports = @env.map do |k,v|
    if PRSpec.is_windows?
      "(SET \"#{k}=#{v}\")"
    else
      "export #{k}=#{v}"
    end
  end.join(separator)

  return exports+separator
end

#startObject



338
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
364
365
366
# File 'lib/prspec.rb', line 338

def start
  filter_str = @tests.join(" -e ")
  filter_str = "-e " + filter_str
  exports = get_exports
  rspec_args = @args[:rspec_args].join(' ')
  cmd = "#{exports}rspec #{@args[:path]} #{filter_str} #{rspec_args}"
  $log.debug "Starting child process for thread#{@id}: #{cmd}"
  @thread = Thread.new do 
    Thread.current[:id] = @id
    Dir::chdir @args[:dir] do # change directory for process execution
      begin
        pid = nil
        if (@args[:quiet_mode] || @args[:serialize])
          pid = Process.spawn(cmd, :out=>@out, :err=>@err) # capture both sdtout and stderr
        else
          pid = Process.spawn(cmd)
        end
        Process.wait(pid)
        @output = File.readlines(@out).join("\n") if (@args[:quiet_mode] || @args[:serialize])
      rescue
        if (@args[:quiet_mode] || @args[:serialize])
          error = "ErrorCode: #{$?.errorcode}; ErrorOutput: "+File.readlines(@err).join("\n")
          $log.error "Something bad happened while executing thread#{@id}: #{error}"
        end
      end
      close
    end
  end
end