Sha256: c808578237981132b48878189744e014fd3ce6c48f0f102e43e209c398382486

Contents?: true

Size: 1.93 KB

Versions: 12

Compression:

Stored size: 1.93 KB

Contents

module ParallelReportPortal
  # Common file handling methods
  module FileUtils
  
    # Open a file with an exclusive lock and yield the open
    # file to the block. If the system is unable to access the 
    # file it will block until the lock is removed. This method
    # guarantees that the lock will be removed.
    # 
    # @param [String] filename the name of the file to open
    # @param [String] mode the mode to open the file with (e.g. +r+, +w++)
    # @yieldparam [File] file the opened file
    def file_open_exlock_and_block(filename, mode, &block)
      file = File.new(filename, mode)
      begin
        file.flock(File::LOCK_EX)
        yield(file) if block_given?
      ensure
        file.flock(File::LOCK_UN)
        file.close
      end
    end
    
    # Attempts to determin if the current environment is running under a
    # parallel testing environment
    # 
    # @return [Boolean] true if parallel
    def parallel?
      !ENV['PARALLEL_PID_FILE'].nil?
    end
  
    # Returns a pathname for the pid for this launch, initialising if necesssary.
    # 
    # @return [Pathname] the pid pathname
    def launch_id_file
      @lock_file ||= Pathname(Dir.tmpdir) + ("report_portal_tracking_file_#{pid}.lck")
    end
    
    # Returns a pathname for the hierarchy of this launch, initialising if necesssary.
    # 
    # @return [Pathname] the hierarchy pathname
    def hierarchy_file
      @hierarchy_file ||= Pathname(Dir.tmpdir) + ("report_portal_hierarchy_file_#{pid}.lck")
    end
    
    # Gets the pid of this process or the parent pid if running in parallel mode.
    # 
    # @return [Integer] the pid
    def pid
      pid = parallel? ? Process.ppid : Process.pid
    end
    
    # Helper for deleting a file. It will not throw an exception
    # if the file does not exist.
    # 
    # @param [String] the filename to delete
    def delete_file(filename)
      File.delete(filename) if File.exist?(filename)
    end
  
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
parallel_report_portal-3.0.2.beta.1 lib/parallel_report_portal/file_utils.rb
parallel_report_portal-3.0.1 lib/parallel_report_portal/file_utils.rb
parallel_report_portal-3.0.0 lib/parallel_report_portal/file_utils.rb
parallel_report_portal-2.5.1 lib/parallel_report_portal/file_utils.rb
parallel_report_portal-2.5.0 lib/parallel_report_portal/file_utils.rb
parallel_report_portal-2.4.0 lib/parallel_report_portal/file_utils.rb
parallel_report_portal-2.3.0 lib/parallel_report_portal/file_utils.rb
parallel_report_portal-2.1.1 lib/parallel_report_portal/file_utils.rb
parallel_report_portal-2.0.3 lib/parallel_report_portal/file_utils.rb
parallel_report_portal-2.0.2 lib/parallel_report_portal/file_utils.rb
parallel_report_portal-2.0.1 lib/parallel_report_portal/file_utils.rb
parallel_report_portal-2.0.0 lib/parallel_report_portal/file_utils.rb