file_store_test.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "#each" do
  104. let :paths do
  105. paths = []
  106. store.each { |path| paths << path.remove(tmp_path) }
  107. paths
  108. end
  109. it "yields file paths" do
  110. touch 'file'
  111. assert_equal ['/file'], paths
  112. end
  113. it "yields directory paths" do
  114. mkpath 'dir'
  115. assert_equal ['/dir'], paths
  116. end
  117. it "yields file paths recursively" do
  118. mkpath 'dir'
  119. touch 'dir/file'
  120. assert_includes paths, '/dir/file'
  121. end
  122. it "yields directory paths recursively" do
  123. mkpath 'dir/dir'
  124. assert_includes paths, '/dir/dir'
  125. end
  126. it "doesn't yield file paths that start with '.'" do
  127. touch '.file'
  128. assert_empty paths
  129. end
  130. it "doesn't yield directory paths that start with '.'" do
  131. mkpath '.dir'
  132. assert_empty paths
  133. end
  134. it "yields directories before what's inside them" do
  135. mkpath 'dir'
  136. touch 'dir/file'
  137. assert paths.index('/dir') < paths.index('/dir/file')
  138. end
  139. context "when the block deletes the directory" do
  140. it "stops yielding what was inside it" do
  141. mkpath 'dir'
  142. touch 'dir/file'
  143. store.each do |path|
  144. (@paths ||= []) << path
  145. FileUtils.rm_rf(path) if path == expand_path('dir')
  146. end
  147. refute_includes @paths, expand_path('dir/file')
  148. end
  149. end
  150. end
  151. end