class RoxClient::RSpec::Cache

Public Class Methods

new(options = {}) click to toggle source
# File lib/rox-client-rspec/cache.rb, line 8
def initialize options = {}
  @tests = {}
  @workspace, @server_name, @project_api_id = options[:workspace], options[:server_name], options[:project_api_id]
end

Public Instance Methods

known?(test_result) click to toggle source
# File lib/rox-client-rspec/cache.rb, line 36
def known? test_result
  !!@tests[@project_api_id] && !!@tests[@project_api_id][test_result.key]
end
load() click to toggle source
# File lib/rox-client-rspec/cache.rb, line 25
def load
  validate!

  @tests = if File.exists? cache_file
    Oj.load(File.read(cache_file), mode: :strict) rescue {}
  else
    {}
  end
  self
end
save(test_run) click to toggle source
# File lib/rox-client-rspec/cache.rb, line 13
def save test_run
  validate!

  @tests = { @project_api_id => @tests[@project_api_id] || {} }
  test_run.results.each{ |r| @tests[@project_api_id][r.key] = test_result_hash(r) }

  FileUtils.mkdir_p File.dirname(cache_file)
  File.open(cache_file, 'w'){ |f| f.write Oj.dump(@tests, mode: :strict) }

  self
end
stale?(test_result) click to toggle source
# File lib/rox-client-rspec/cache.rb, line 40
def stale? test_result
  !!@tests[@project_api_id] && test_result_hash(test_result) != @tests[@project_api_id][test_result.key]
end

Private Instance Methods

cache_file() click to toggle source
# File lib/rox-client-rspec/cache.rb, line 56
def cache_file
  @cache_file ||= File.join(@workspace, 'rspec', 'servers', @server_name, 'cache.json')
end
test_result_hash(r) click to toggle source
# File lib/rox-client-rspec/cache.rb, line 52
def test_result_hash r
  Digest::SHA2.hexdigest "#{r.name} || #{r.category} || #{r.tags.collect(&:to_s).sort.join(' ')} || #{r.tickets.collect(&:to_s).sort.join(' ')}"
end
validate!() click to toggle source
# File lib/rox-client-rspec/cache.rb, line 46
def validate!
  required = { "workspace" => @workspace, "server name" => @server_name, "project API identifier" => @project_api_id }
  missing = required.keys.select{ |k| !required[k] }
  raise Error.new("Missing cache options: #{missing.join ', '}") if missing.any?
end