Sha256: c41858580b6e2f369724e0500eb1e699dabcbbdeaf136ea8ce46e2ba9c3225ff

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 KB

Contents

module ProbeDockRSpec

  class Cache

    class Error < ProbeDockRSpec::Error; end

    def initialize options = {}
      @tests = {}
      @workspace, @server_name, @project_api_id = options[:workspace], options[:server_name], options[:project_api_id]
    end

    def save test_run
      validate!

      @tests = { @project_api_id => @tests[@project_api_id] || {} }
      test_run.results.select(&:key).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

    def load
      validate!

      @tests = if File.exists? cache_file
        Oj.load(File.read(cache_file), mode: :strict) rescue {}
      else
        {}
      end

      self
    end

    def known? test_result
      test_result.key && !!@tests[@project_api_id] && !!@tests[@project_api_id][test_result.key]
    end

    def stale? test_result
      !!@tests[@project_api_id] && test_result_hash(test_result) != @tests[@project_api_id][test_result.key]
    end

    private

    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

    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

    def cache_file
      @cache_file ||= File.join(@workspace, 'rspec', 'servers', @server_name, 'cache.json')
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
probedock-rspec-0.5.3 lib/probe_dock_rspec/cache.rb
probe-dock-rspec-0.5.2 lib/probe_dock_rspec/cache.rb
probe-dock-rspec-0.5.1 lib/probe_dock_rspec/cache.rb
probe-dock-rspec-0.5.0 lib/probe_dock_rspec/cache.rb