Skip to content

A platform for community discussion. Free, open, simple.

DEV: Attempt to fix csv export spec flake (#39723)

Jarek Radosz6690a3166871
diff --git a/spec/system/helpers/downloads.rb b/spec/system/helpers/downloads.rb
index 41806d6a8c9..c124e5ac0c8 100644
--- a/spec/system/helpers/downloads.rb
+++ b/spec/system/helpers/downloads.rb
@@ -4,27 +4,24 @@ class Downloads
FOLDER = Rails.root.join("tmp/downloads")
TIMEOUT = 10
- def self.wait_for_download
- Timeout.timeout(TIMEOUT) { sleep 0.1 until downloaded? }
+ # Waits for `path` to appear and finish writing.
+ def self.wait_for(path)
+ previous_size = -1
+ Timeout.timeout(TIMEOUT) do
+ loop do
+ size = File.size?(path)
+ break if size == previous_size
+
+ previous_size = size if size
+ sleep 0.1
+ end
+ end
+ rescue Timeout::Error
+ last = previous_size == -1 ? "never appeared" : "last size #{previous_size}"
+ raise Timeout::Error, "Timed out after #{TIMEOUT}s waiting for #{path} (#{last})"
end
def self.clear
FileUtils.rm_rf(FOLDER)
end
-
- private
-
- def self.downloaded?
- !downloading? && downloads.any?
- end
-
- def self.downloading?
- downloads.grep(/\.crdownload$/).any?
- end
-
- def self.downloads
- Dir[FOLDER.join("*")]
- end
-
- private_class_method :downloaded?, :downloading?, :downloads
end
diff --git a/spec/system/page_objects/pages/csv_export_pm.rb b/spec/system/page_objects/pages/csv_export_pm.rb
index aa0fe4ab8ba..af407ee4406 100644
--- a/spec/system/page_objects/pages/csv_export_pm.rb
+++ b/spec/system/page_objects/pages/csv_export_pm.rb
@@ -12,13 +12,13 @@ module PageObjects
original_save_path = Capybara.save_path
Capybara.save_path = Downloads::FOLDER
- click_link ".zip"
- Downloads.wait_for_download
-
zip_name = find("a.attachment").text
zip_path = File.join(Downloads::FOLDER, zip_name)
@downloaded_files << zip_path
+ click_link ".zip"
+ Downloads.wait_for(zip_path)
+
csv_path = unzip(zip_path).first
@downloaded_files << csv_path
CSV.read(csv_path)
@@ -27,7 +27,7 @@ module PageObjects
end
def clear_downloads
- @downloaded_files.each { |file| FileUtils.rm(file) }
+ @downloaded_files.each { |file| FileUtils.rm_f(file) }
@downloaded_files = []
end