tests: possible fix for images getting nuked by tests.
This commit is contained in:
@@ -45,13 +45,14 @@ class ActiveSupport::TestCase
|
|||||||
Socket.stubs(:gethostname).returns("www.example.com")
|
Socket.stubs(:gethostname).returns("www.example.com")
|
||||||
WebMock.allow_net_connect!
|
WebMock.allow_net_connect!
|
||||||
|
|
||||||
storage_manager = StorageManager::Local.new(base_dir: Dir.mktmpdir("uploads-test-storage-"))
|
@temp_dir = Dir.mktmpdir("danbooru-temp-")
|
||||||
|
storage_manager = StorageManager::Local.new(base_dir: @temp_dir)
|
||||||
Danbooru.config.stubs(:storage_manager).returns(storage_manager)
|
Danbooru.config.stubs(:storage_manager).returns(storage_manager)
|
||||||
Danbooru.config.stubs(:backup_storage_manager).returns(StorageManager::Null.new)
|
Danbooru.config.stubs(:backup_storage_manager).returns(StorageManager::Null.new)
|
||||||
end
|
end
|
||||||
|
|
||||||
teardown do
|
teardown do
|
||||||
FileUtils.rm_rf(Danbooru.config.storage_manager.base_dir)
|
FileUtils.rm_rf(@temp_dir)
|
||||||
Cache.clear
|
Cache.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class StorageManagerTest < ActiveSupport::TestCase
|
class StorageManagerTest < ActiveSupport::TestCase
|
||||||
BASE_DIR = "#{Rails.root}/tmp/test-storage"
|
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
CurrentUser.ip_addr = "127.0.0.1"
|
CurrentUser.ip_addr = "127.0.0.1"
|
||||||
end
|
end
|
||||||
@@ -45,25 +43,21 @@ class StorageManagerTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
context "StorageManager::Local" do
|
context "StorageManager::Local" do
|
||||||
setup do
|
setup do
|
||||||
@storage_manager = StorageManager::Local.new(base_dir: BASE_DIR, base_url: "/data")
|
@storage_manager = StorageManager::Local.new(base_dir: @temp_dir, base_url: "/data")
|
||||||
end
|
|
||||||
|
|
||||||
teardown do
|
|
||||||
FileUtils.rm_rf(BASE_DIR)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context "#store method" do
|
context "#store method" do
|
||||||
should "store the file" do
|
should "store the file" do
|
||||||
@storage_manager.store(StringIO.new("data"), "#{BASE_DIR}/test.txt")
|
@storage_manager.store(StringIO.new("data"), "#{@temp_dir}/test.txt")
|
||||||
|
|
||||||
assert("data", File.read("#{BASE_DIR}/test.txt"))
|
assert("data", File.read("#{@temp_dir}/test.txt"))
|
||||||
end
|
end
|
||||||
|
|
||||||
should "overwrite the file if it already exists" do
|
should "overwrite the file if it already exists" do
|
||||||
@storage_manager.store(StringIO.new("foo"), "#{BASE_DIR}/test.txt")
|
@storage_manager.store(StringIO.new("foo"), "#{@temp_dir}/test.txt")
|
||||||
@storage_manager.store(StringIO.new("bar"), "#{BASE_DIR}/test.txt")
|
@storage_manager.store(StringIO.new("bar"), "#{@temp_dir}/test.txt")
|
||||||
|
|
||||||
assert("bar", File.read("#{BASE_DIR}/test.txt"))
|
assert("bar", File.read("#{@temp_dir}/test.txt"))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -72,7 +66,7 @@ class StorageManagerTest < ActiveSupport::TestCase
|
|||||||
@storage_manager.store(StringIO.new("data"), "test.txt")
|
@storage_manager.store(StringIO.new("data"), "test.txt")
|
||||||
@storage_manager.delete("test.txt")
|
@storage_manager.delete("test.txt")
|
||||||
|
|
||||||
assert_not(File.exist?("#{BASE_DIR}/test.txt"))
|
assert_not(File.exist?("#{@temp_dir}/test.txt"))
|
||||||
end
|
end
|
||||||
|
|
||||||
should "not fail if the file doesn't exist" do
|
should "not fail if the file doesn't exist" do
|
||||||
@@ -88,9 +82,9 @@ class StorageManagerTest < ActiveSupport::TestCase
|
|||||||
@storage_manager.store_file(StringIO.new("data"), @post, :large)
|
@storage_manager.store_file(StringIO.new("data"), @post, :large)
|
||||||
@storage_manager.store_file(StringIO.new("data"), @post, :original)
|
@storage_manager.store_file(StringIO.new("data"), @post, :original)
|
||||||
|
|
||||||
@file_path = "#{BASE_DIR}/preview/#{@post.md5}.jpg"
|
@file_path = "#{@temp_dir}/preview/#{@post.md5}.jpg"
|
||||||
@large_file_path = "#{BASE_DIR}/sample/sample-#{@post.md5}.jpg"
|
@large_file_path = "#{@temp_dir}/sample/sample-#{@post.md5}.jpg"
|
||||||
@preview_file_path = "#{BASE_DIR}/#{@post.md5}.#{@post.file_ext}"
|
@preview_file_path = "#{@temp_dir}/#{@post.md5}.#{@post.file_ext}"
|
||||||
end
|
end
|
||||||
|
|
||||||
should "store the files at the correct path" do
|
should "store the files at the correct path" do
|
||||||
@@ -134,12 +128,12 @@ class StorageManagerTest < ActiveSupport::TestCase
|
|||||||
context "when the original_subdir option is used" do
|
context "when the original_subdir option is used" do
|
||||||
should "store original files at the correct path" do
|
should "store original files at the correct path" do
|
||||||
@post = FactoryBot.create(:post, file_ext: "png")
|
@post = FactoryBot.create(:post, file_ext: "png")
|
||||||
@storage_manager = StorageManager::Local.new(base_dir: BASE_DIR, base_url: "/data", original_subdir: "original/")
|
@storage_manager = StorageManager::Local.new(base_dir: @temp_dir, base_url: "/data", original_subdir: "original/")
|
||||||
|
|
||||||
assert_equal("#{BASE_DIR}/original/#{@post.md5}.png", @storage_manager.file_path(@post, @post.file_ext, :original))
|
assert_equal("#{@temp_dir}/original/#{@post.md5}.png", @storage_manager.file_path(@post, @post.file_ext, :original))
|
||||||
|
|
||||||
@storage_manager.store_file(StringIO.new("data"), @post, :original)
|
@storage_manager.store_file(StringIO.new("data"), @post, :original)
|
||||||
assert_equal(true, File.exist?("#{BASE_DIR}/original/#{@post.md5}.png"))
|
assert_equal(true, File.exist?("#{@temp_dir}/original/#{@post.md5}.png"))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -151,24 +145,20 @@ class StorageManagerTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
@storage_manager = StorageManager::Hybrid.new do |id, md5, file_ext, type|
|
@storage_manager = StorageManager::Hybrid.new do |id, md5, file_ext, type|
|
||||||
if id.odd?
|
if id.odd?
|
||||||
StorageManager::Local.new(base_dir: "#{BASE_DIR}/i1", base_url: "/i1")
|
StorageManager::Local.new(base_dir: "#{@temp_dir}/i1", base_url: "/i1")
|
||||||
else
|
else
|
||||||
StorageManager::Local.new(base_dir: "#{BASE_DIR}/i2", base_url: "/i2")
|
StorageManager::Local.new(base_dir: "#{@temp_dir}/i2", base_url: "/i2")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
teardown do
|
|
||||||
FileUtils.rm_rf(BASE_DIR)
|
|
||||||
end
|
|
||||||
|
|
||||||
context "#store_file method" do
|
context "#store_file method" do
|
||||||
should "store odd-numbered posts under /i1 and even-numbered posts under /i2" do
|
should "store odd-numbered posts under /i1 and even-numbered posts under /i2" do
|
||||||
@storage_manager.store_file(StringIO.new("post1"), @post1, :original)
|
@storage_manager.store_file(StringIO.new("post1"), @post1, :original)
|
||||||
@storage_manager.store_file(StringIO.new("post2"), @post2, :original)
|
@storage_manager.store_file(StringIO.new("post2"), @post2, :original)
|
||||||
|
|
||||||
assert(File.exist?("#{BASE_DIR}/i1/#{@post1.md5}.png"))
|
assert(File.exist?("#{@temp_dir}/i1/#{@post1.md5}.png"))
|
||||||
assert(File.exist?("#{BASE_DIR}/i2/#{@post2.md5}.png"))
|
assert(File.exist?("#{@temp_dir}/i2/#{@post2.md5}.png"))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user