Skip to content

Commit

Permalink
add disk cache test
Browse files Browse the repository at this point in the history
  • Loading branch information
benbalter committed Apr 24, 2015
1 parent abffa36 commit 3da5f24
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
11 changes: 1 addition & 10 deletions .gitignore
Expand Up @@ -4,13 +4,4 @@ vendor
Gemfile.lock

/.env
/env

# rack-cache
/cache*

#bower bloat
public/vendor/bootstrap/grunt
public/vendor/bootstrap/less
public/vendor/bootstrap/test-infra
public/vendor/jquery/src
/tmp
10 changes: 6 additions & 4 deletions lib/site-inspector/disk_cache.rb
Expand Up @@ -6,10 +6,6 @@ def initialize(dir = nil, replace = nil)
@memory = {}
end

def path(request)
File.join(@dir, request.cache_key)
end

def get(request)
return unless File.exist?(path(request))
return @memory[request] if @memory[request]
Expand All @@ -32,5 +28,11 @@ def set(request, response)
File.write(path(request), Marshal.dump(response))
@memory[request] = response
end

private

def path(request)
File.join(@dir, request)
end
end
end
30 changes: 30 additions & 0 deletions spec/site_inspector_disc_cache_spec.rb
@@ -0,0 +1,30 @@
require 'spec_helper'

describe 'SiteInspector::DiskCache' do
before do
@cache = SiteInspector::DiskCache.new(tmpdir)
FileUtils.rm_rf(tmpdir)
Dir.mkdir(tmpdir)
end

it "should write a value to disk" do
path = File.expand_path "foo", tmpdir
expect(File.exists?(path)).to eql(false)

@cache.set "foo", "bar"

expect(File.exists?(path)).to eql(true)
expect(File.open(path).read).to eql("I\"bar:ET")
end

it "should read a value from disk" do
path = File.expand_path "foo", tmpdir
File.write(path, "I\"bar:ET")
expect(@cache.get("foo")).to eql("bar")
end

it "should calculate a file's path" do
path = File.expand_path "foo", tmpdir
expect(@cache.send(:path, "foo")).to eql(path)
end
end
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
@@ -1,5 +1,6 @@
require "bundler/setup"
require 'webmock/rspec'
require 'fileutils'
require_relative "../lib/site-inspector"

WebMock.disable_net_connect!
Expand All @@ -10,3 +11,7 @@ def with_env(key, value)
yield
ENV[key] = old_env
end

def tmpdir
File.expand_path "../tmp", File.dirname(__FILE__)
end

0 comments on commit 3da5f24

Please sign in to comment.