From 1b0a7499f87c5fbd3c28c6378576fb733666fa63 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 17 Aug 2023 16:30:17 +0200 Subject: [PATCH] test(export): fix exporting participants CSV Signed-off-by: Thomas Citharel --- lib/service/export/participants/csv.ex | 14 ++++++++++++-- test/service/export/participants/csv_test.exs | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/service/export/participants/csv.ex b/lib/service/export/participants/csv.ex index 1a14ff0ea..5bbb8ead7 100644 --- a/lib/service/export/participants/csv.ex +++ b/lib/service/export/participants/csv.ex @@ -8,6 +8,7 @@ defmodule Mobilizon.Service.Export.Participants.CSV do alias Mobilizon.Storage.Repo alias Mobilizon.Web.Gettext import Mobilizon.Web.Gettext, only: [gettext: 2] + require Logger import Mobilizon.Service.Export.Participants.Common, only: [ @@ -30,9 +31,18 @@ defmodule Mobilizon.Service.Export.Participants.CSV do def export(%Event{} = event, options \\ []) do if ready?() do filename = "#{ShortUUID.encode!(Ecto.UUID.generate())}.csv" - full_path = Path.join([export_path(@extension), filename]) + folder = export_path(@extension) + folder_creation_result = File.mkdir_p(folder) - file = File.open!(full_path, [:write, :utf8]) + if folder_creation_result != :ok do + Logger.warning( + "Unable to create folder at #{folder}, error result #{inspect(folder_creation_result)}" + ) + end + + full_path = Path.join([folder, filename]) + + file = File.open!(full_path, [:write, :exclusive, :utf8]) case Repo.transaction( fn -> diff --git a/test/service/export/participants/csv_test.exs b/test/service/export/participants/csv_test.exs index 4ac91c502..f6430b3b4 100644 --- a/test/service/export/participants/csv_test.exs +++ b/test/service/export/participants/csv_test.exs @@ -15,7 +15,7 @@ defmodule Mobilizon.Service.Export.Participants.CSVTest do assert CSV.ready?() assert {:ok, path} = CSV.export(event) - assert content = File.read!("uploads/exports/csv/" <> path) + assert content = File.read!("test/uploads/exports/csv/" <> path) assert content =~ "Participant name,Participant status,Participant message" end end