tests: possible fix for images getting nuked by tests.

This commit is contained in:
evazion
2020-06-17 12:30:37 -05:00
parent d5a7eef53d
commit fd6ba473a5
2 changed files with 20 additions and 29 deletions

View File

@@ -45,13 +45,14 @@ class ActiveSupport::TestCase
Socket.stubs(:gethostname).returns("www.example.com")
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(:backup_storage_manager).returns(StorageManager::Null.new)
end
teardown do
FileUtils.rm_rf(Danbooru.config.storage_manager.base_dir)
FileUtils.rm_rf(@temp_dir)
Cache.clear
end

View File

@@ -1,8 +1,6 @@
require 'test_helper'
class StorageManagerTest < ActiveSupport::TestCase
BASE_DIR = "#{Rails.root}/tmp/test-storage"
setup do
CurrentUser.ip_addr = "127.0.0.1"
end
@@ -45,25 +43,21 @@ class StorageManagerTest < ActiveSupport::TestCase
context "StorageManager::Local" do
setup do
@storage_manager = StorageManager::Local.new(base_dir: BASE_DIR, base_url: "/data")
end
teardown do
FileUtils.rm_rf(BASE_DIR)
@storage_manager = StorageManager::Local.new(base_dir: @temp_dir, base_url: "/data")
end
context "#store method" 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
should "overwrite the file if it already exists" do
@storage_manager.store(StringIO.new("foo"), "#{BASE_DIR}/test.txt")
@storage_manager.store(StringIO.new("bar"), "#{BASE_DIR}/test.txt")
@storage_manager.store(StringIO.new("foo"), "#{@temp_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
@@ -72,7 +66,7 @@ class StorageManagerTest < ActiveSupport::TestCase
@storage_manager.store(StringIO.new("data"), "test.txt")
@storage_manager.delete("test.txt")
assert_not(File.exist?("#{BASE_DIR}/test.txt"))
assert_not(File.exist?("#{@temp_dir}/test.txt"))
end
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, :original)
@file_path = "#{BASE_DIR}/preview/#{@post.md5}.jpg"
@large_file_path = "#{BASE_DIR}/sample/sample-#{@post.md5}.jpg"
@preview_file_path = "#{BASE_DIR}/#{@post.md5}.#{@post.file_ext}"
@file_path = "#{@temp_dir}/preview/#{@post.md5}.jpg"
@large_file_path = "#{@temp_dir}/sample/sample-#{@post.md5}.jpg"
@preview_file_path = "#{@temp_dir}/#{@post.md5}.#{@post.file_ext}"
end
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
should "store original files at the correct path" do
@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)
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
@@ -151,24 +145,20 @@ class StorageManagerTest < ActiveSupport::TestCase
@storage_manager = StorageManager::Hybrid.new do |id, md5, file_ext, type|
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
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
teardown do
FileUtils.rm_rf(BASE_DIR)
end
context "#store_file method" 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("post2"), @post2, :original)
assert(File.exist?("#{BASE_DIR}/i1/#{@post1.md5}.png"))
assert(File.exist?("#{BASE_DIR}/i2/#{@post2.md5}.png"))
assert(File.exist?("#{@temp_dir}/i1/#{@post1.md5}.png"))
assert(File.exist?("#{@temp_dir}/i2/#{@post2.md5}.png"))
end
end