Bläddra i källkod

Add Store#size

Thibaut 11 år sedan
förälder
incheckning
456c4cb811

+ 9 - 0
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

+ 4 - 0
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

+ 1 - 0
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

+ 21 - 0
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'

+ 7 - 0
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 = []