Sha256: 64959d43f7aaf4d34f420323e2f3015be5ae6c17a3ddef8db2def4d1c5b85a53

Contents?: true

Size: 1.86 KB

Versions: 2

Compression:

Stored size: 1.86 KB

Contents

require 'thread'

module Rgot
  class Common
    attr_accessor :output

    def initialize
      @output = ""
      @failed = false
      @skipped = false
      @finished = false
      @start = Rgot.now
      @mutex = Mutex.new
    end

    def failed?
      @mutex.synchronize { @failed }
    end

    def skipped?
      @mutex.synchronize { @skipped }
    end

    def finished?
      @mutex.synchronize { @finished }
    end

    def fail!
      @mutex.synchronize { @failed = true }
    end

    def skip!
      @mutex.synchronize { @skipped = true }
    end

    def finish!
      @mutex.synchronize { @finished = true }
    end

    def log(*args)
      internal_log(args.map(&:to_s).join(' '))
      nil
    end

    def logf(*args)
      internal_log(sprintf(*args))
      nil
    end

    def error(*args)
      internal_log(args.map(&:to_s).join(' '))
      fail!
      nil
    end

    def errorf(*args)
      internal_log(sprintf(*args))
      fail!
      nil
    end

    def fatal(*args)
      internal_log(args.map(&:to_s).join(' '))
      fail_now
    end

    def fatalf(*args)
      internal_log(sprintf(*args))
      fail_now
    end

    def skip(*args)
      internal_log(args.map(&:to_s).join(' '))
      skip_now
    end

    def skipf(*args)
      internal_log(sprintf(*args))
      skip_now
    end

    def skip_now
      skip!
      finish!
      throw :skip
    end

    def fail_now
      fail!
      finish!
      throw :skip
    end

    private

    def decorate(str)
      # internal_log -> synchronize -> internal_log -> other log -> running method
      c = caller[4]
      path = c.sub(/:.*/, '')
      line = c.match(/:(\d+?):/)[1]
      relative_path = Pathname.new(path).relative_path_from(Pathname.new(Dir.pwd)).to_s
      "\t#{relative_path}:#{line}: #{str}\n"
    end

    def internal_log(msg)
      @mutex.synchronize { @output << decorate(msg) }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rgot-0.1.4 lib/rgot/common.rb
rgot-0.1.3 lib/rgot/common.rb