file_store_test.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. require 'test_helper'
  2. require 'docs'
  3. class DocsFileStoreTest < MiniTest::Spec
  4. let :store do
  5. Docs::FileStore.new(tmp_path)
  6. end
  7. after do
  8. FileUtils.rm_rf "#{tmp_path}/."
  9. end
  10. def expand_path(path)
  11. File.join(tmp_path, path)
  12. end
  13. def read(path)
  14. File.read expand_path(path)
  15. end
  16. def write(path, content)
  17. File.write expand_path(path), content
  18. end
  19. def exists?(path)
  20. File.exist? expand_path(path)
  21. end
  22. def touch(path)
  23. FileUtils.touch expand_path(path)
  24. end
  25. def mkpath(path)
  26. FileUtils.mkpath expand_path(path)
  27. end
  28. describe "#read" do
  29. it "reads a file" do
  30. write 'file', 'content'
  31. assert_equal 'content', store.read('file')
  32. end
  33. end
  34. describe "#write" do
  35. context "with a string" do
  36. it "creates the file when it doesn't exist" do
  37. store.write 'file', 'content'
  38. assert exists?('file')
  39. assert_equal 'content', read('file')
  40. end
  41. it "updates the file when it exists" do
  42. touch 'file'
  43. store.write 'file', 'content'
  44. assert_equal 'content', read('file')
  45. end
  46. end
  47. context "with a Tempfile" do
  48. let :file do
  49. Tempfile.new('tmp').tap do |file|
  50. file.write 'content'
  51. file.close
  52. end
  53. end
  54. it "creates the file when it doesn't exist" do
  55. store.write 'file', file
  56. assert exists?('file')
  57. assert_equal 'content', read('file')
  58. end
  59. it "updates the file when it exists" do
  60. touch 'file'
  61. store.write 'file', file
  62. assert_equal 'content', read('file')
  63. end
  64. end
  65. it "recursively creates directories" do
  66. store.write '1/2/file', ''
  67. assert exists?('1/2/file')
  68. end
  69. end
  70. describe "#delete" do
  71. it "deletes a file" do
  72. touch 'file'
  73. store.delete 'file'
  74. refute exists?('file')
  75. end
  76. it "deletes a directory" do
  77. mkpath '1/2'
  78. touch '1/2/file'
  79. store.delete '1'
  80. refute exists?('1/2/exist')
  81. refute exists?('1/2')
  82. refute exists?('1')
  83. end
  84. end
  85. describe "#exist?" do
  86. it "returns true when the file exists" do
  87. touch 'file'
  88. assert store.exist?('file')
  89. end
  90. it "returns false when the file doesn't exist" do
  91. refute store.exist?('file')
  92. end
  93. end
  94. describe "#mtime" do
  95. it "returns the file modification time" do
  96. touch 'file'
  97. created_at = Time.now.round - 86400
  98. modified_at = created_at + 1
  99. File.utime created_at, modified_at, expand_path('file')
  100. assert_equal modified_at, store.mtime('file')
  101. end
  102. end
  103. describe "#size" do
  104. it "returns the file's size" do
  105. write 'file', 'content'
  106. assert_equal File.size(expand_path('file')), store.size('file')
  107. end
  108. end
  109. describe "#each" do
  110. let :paths do
  111. paths = []
  112. store.each { |path| paths << path.remove(tmp_path) }
  113. paths
  114. end
  115. it "yields file paths" do
  116. touch 'file'
  117. assert_equal ['/file'], paths
  118. end
  119. it "yields directory paths" do
  120. mkpath 'dir'
  121. assert_equal ['/dir'], paths
  122. end
  123. it "yields file paths recursively" do
  124. mkpath 'dir'
  125. touch 'dir/file'
  126. assert_includes paths, '/dir/file'
  127. end
  128. it "yields directory paths recursively" do
  129. mkpath 'dir/dir'
  130. assert_includes paths, '/dir/dir'
  131. end
  132. it "doesn't yield file paths that start with '.'" do
  133. touch '.file'
  134. assert_empty paths
  135. end
  136. it "doesn't yield directory paths that start with '.'" do
  137. mkpath '.dir'
  138. assert_empty paths
  139. end
  140. it "yields directories before what's inside them" do
  141. mkpath 'dir'
  142. touch 'dir/file'
  143. assert paths.index('/dir') < paths.index('/dir/file')
  144. end
  145. context "when the block deletes the directory" do
  146. it "stops yielding what was inside it" do
  147. mkpath 'dir'
  148. touch 'dir/file'
  149. store.each do |path|
  150. (@paths ||= []) << path
  151. FileUtils.rm_rf(path) if path == expand_path('dir')
  152. end
  153. refute_includes @paths, expand_path('dir/file')
  154. end
  155. end
  156. end
  157. end