MembersPDF test: async false and try/after to avoid flakiness

Tests that remove the template file run sequentially; restore
template in after block so it is restored even when assertion fails.
This commit is contained in:
Moritz 2026-03-04 17:08:32 +01:00
parent 1ce9915c7d
commit 6987733707
Signed by: moritz
GPG key ID: 1020A035E5DD0824

View file

@ -8,7 +8,9 @@ defmodule Mv.Membership.MembersPDFTest do
- Generates valid PDF binary (starts with "%PDF") - Generates valid PDF binary (starts with "%PDF")
- Handles errors gracefully - Handles errors gracefully
""" """
use ExUnit.Case, async: true # async: false so tests that manipulate the template file (e.g. "returns error when template file is missing")
# do not run in parallel and cannot leave the template removed on failure
use ExUnit.Case, async: false
alias Mv.Config alias Mv.Config
alias Mv.Membership.MembersPDF alias Mv.Membership.MembersPDF
@ -162,13 +164,13 @@ defmodule Mv.Membership.MembersPDFTest do
end end
test "returns error when template file is missing" do test "returns error when template file is missing" do
# Temporarily rename template to simulate missing file
template_path = template_path =
Path.join(Application.app_dir(:mv, "priv"), "pdf_templates/members_export.typ") Path.join(Application.app_dir(:mv, "priv"), "pdf_templates/members_export.typ")
original_content = File.read!(template_path) original_content = File.read!(template_path)
File.rm(template_path) File.rm(template_path)
try do
export_data = %{ export_data = %{
columns: [%{key: "first_name", kind: :member_field, label: "Vorname"}], columns: [%{key: "first_name", kind: :member_field, label: "Vorname"}],
rows: [["Max"]], rows: [["Max"]],
@ -178,10 +180,10 @@ defmodule Mv.Membership.MembersPDFTest do
result = MembersPDF.render(export_data) result = MembersPDF.render(export_data)
assert {:error, {:template_not_found, _reason}} = result assert {:error, {:template_not_found, _reason}} = result
after
# Restore template
File.write!(template_path, original_content) File.write!(template_path, original_content)
end end
end
test "handles empty rows gracefully" do test "handles empty rows gracefully" do
export_data = %{ export_data = %{