From 456c4cb811be229de6f61cc3e7672c0d6fe6d919 Mon Sep 17 00:00:00 2001 From: Thibaut Date: Fri, 2 Jan 2015 15:22:14 -0500 Subject: [PATCH] Add Store#size --- lib/docs/storage/abstract_store.rb | 9 +++++++++ lib/docs/storage/file_store.rb | 4 ++++ lib/docs/storage/null_store.rb | 1 + test/lib/docs/storage/abstract_store_test.rb | 21 ++++++++++++++++++++ test/lib/docs/storage/file_store_test.rb | 7 +++++++ 5 files changed, 42 insertions(+) diff --git a/lib/docs/storage/abstract_store.rb b/lib/docs/storage/abstract_store.rb index ce628105..da90a3d8 100644 --- a/lib/docs/storage/abstract_store.rb +++ b/lib/docs/storage/abstract_store.rb @@ -71,6 +71,11 @@ module Docs file_mtime(path) if file_exist?(path) end + def size(path) + path = expand_path(path) + file_size(path) if file_exist?(path) + end + def each(&block) list_files(working_path, &block) end @@ -109,6 +114,10 @@ module Docs raise NotImplementedError end + def file_size(path) + raise NotImplementedError + end + def list_files(path, &block) raise NotImplementedError end diff --git a/lib/docs/storage/file_store.rb b/lib/docs/storage/file_store.rb index 1fd532a9..47c0e6b4 100644 --- a/lib/docs/storage/file_store.rb +++ b/lib/docs/storage/file_store.rb @@ -37,6 +37,10 @@ module Docs File.mtime(path) end + def file_size(path) + File.size(path) + end + def list_files(path) Find.find path do |file| next if file == path diff --git a/lib/docs/storage/null_store.rb b/lib/docs/storage/null_store.rb index 688b8f61..fa36e294 100644 --- a/lib/docs/storage/null_store.rb +++ b/lib/docs/storage/null_store.rb @@ -16,6 +16,7 @@ module Docs alias_method :delete_file, :nil alias_method :file_exist?, :nil alias_method :file_mtime, :nil + alias_method :file_size, :nil alias_method :list_files, :nil end end diff --git a/test/lib/docs/storage/abstract_store_test.rb b/test/lib/docs/storage/abstract_store_test.rb index 2cb91d69..f552cd4a 100644 --- a/test/lib/docs/storage/abstract_store_test.rb +++ b/test/lib/docs/storage/abstract_store_test.rb @@ -330,6 +330,27 @@ class DocsAbstractStoreTest < MiniTest::Spec end end + describe "#size" do + it "raises an error with a path outside of #working_path" do + @path = '/path' + assert_raises InvalidPathError do + store.size '../file' + end + end + + it "returns nil when the file doesn't exist" do + stub(store).file_exist?('/file') { false } + dont_allow(store).file_size + assert_nil store.size('file') + end + + it "returns #file_size when the file exists" do + stub(store).file_exist?('/file') { true } + stub(store).file_size('/file') { 1 } + assert_equal 1, store.size('file') + end + end + describe "#each" do it "calls #list_files with #working_path" do store.open 'dir' diff --git a/test/lib/docs/storage/file_store_test.rb b/test/lib/docs/storage/file_store_test.rb index e2775abf..586d8a41 100644 --- a/test/lib/docs/storage/file_store_test.rb +++ b/test/lib/docs/storage/file_store_test.rb @@ -121,6 +121,13 @@ class DocsFileStoreTest < MiniTest::Spec end end + describe "#size" do + it "returns the file's size" do + write 'file', 'content' + assert_equal File.size(expand_path('file')), store.size('file') + end + end + describe "#each" do let :paths do paths = []