From ba616ffb503686ac811fee67580de1a2d4e47287 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 28 Apr 2020 14:03:04 +0100 Subject: [PATCH] DEV: Use a tmp directory for storing uploads in tests (#9554) This avoids development-mode upload files from polluting the test environment --- config/routes.rb | 4 ++-- lib/file_store/base_store.rb | 6 ++---- lib/s3_helper.rb | 4 ++-- spec/lib/backup_restore/uploads_restorer_spec.rb | 4 +--- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index e280af6dd1c..10199d1dff8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -519,8 +519,8 @@ Discourse::Application.routes.draw do get "uploads/short-url/:base62(.:extension)" => "uploads#show_short", constraints: { site: /\w+/, base62: /[a-zA-Z0-9]+/, extension: /[a-z0-9\._]+/i }, as: :upload_short # used to download attachments get "uploads/:site/original/:tree:sha(.:extension)" => "uploads#show", constraints: { site: /\w+/, tree: /([a-z0-9]+\/)+/i, sha: /\h{40}/, extension: /[a-z0-9\._]+/i } - if Discourse.is_parallel_test? - get "uploads/:site/:index/original/:tree:sha(.:extension)" => "uploads#show", constraints: { site: /\w+/, index: /\d+/, tree: /([a-z0-9]+\/)+/i, sha: /\h{40}/, extension: /[a-z0-9\._]+/i } + if Rails.env.test? + get "uploads/:site/test_:index/original/:tree:sha(.:extension)" => "uploads#show", constraints: { site: /\w+/, index: /\d+/, tree: /([a-z0-9]+\/)+/i, sha: /\h{40}/, extension: /[a-z0-9\._]+/i } end # used to download attachments (old route) get "uploads/:site/:id/:sha" => "uploads#show", constraints: { site: /\w+/, id: /\d+/, sha: /\h{16}/, format: /.*/ } diff --git a/lib/file_store/base_store.rb b/lib/file_store/base_store.rb index 563970d0880..a3623f1fea4 100644 --- a/lib/file_store/base_store.rb +++ b/lib/file_store/base_store.rb @@ -32,10 +32,8 @@ module FileStore def upload_path path = File.join("uploads", RailsMultisite::ConnectionManagement.current_db) - return path unless Discourse.is_parallel_test? - - n = ENV['TEST_ENV_NUMBER'].presence || '1' - File.join(path, n) + return path if !Rails.env.test? + File.join(path, "test_#{ENV['TEST_ENV_NUMBER'].presence || '0'}") end def has_been_uploaded?(url) diff --git a/lib/s3_helper.rb b/lib/s3_helper.rb index 5763921caa4..a1f921e9afa 100644 --- a/lib/s3_helper.rb +++ b/lib/s3_helper.rb @@ -248,8 +248,8 @@ class S3Helper def multisite_upload_path path = File.join("uploads", RailsMultisite::ConnectionManagement.current_db, "/") - return path unless Discourse.is_parallel_test? - File.join(path, ENV['TEST_ENV_NUMBER'].presence || '1', "/") + return path if !Rails.env.test? + File.join(path, "test_#{ENV['TEST_ENV_NUMBER'].presence || '0'}", "/") end def s3_resource diff --git a/spec/lib/backup_restore/uploads_restorer_spec.rb b/spec/lib/backup_restore/uploads_restorer_spec.rb index 15fbcd24ce3..fba41193fee 100644 --- a/spec/lib/backup_restore/uploads_restorer_spec.rb +++ b/spec/lib/backup_restore/uploads_restorer_spec.rb @@ -73,9 +73,7 @@ describe BackupRestore::UploadsRestorer do def uploads_path(database) path = File.join("uploads", database) - if Discourse.is_parallel_test? - path = File.join(path, ENV['TEST_ENV_NUMBER'].presence || '1') - end + path = File.join(path, "test_#{ENV['TEST_ENV_NUMBER'].presence || '0'}") "/#{path}/" end