File 1179-eunit-ensure-the-output-directory-exist-for-surefire.patch of Package erlang

From 90c5ea371e467176373208a350c62f612e51eb23 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Henrique=20Ferreira=20de=20Freitas?=
 <joaohf@gmail.com>
Date: Tue, 30 Mar 2021 23:18:06 -0300
Subject: [PATCH] eunit: ensure the output directory exist for surefire xml
 files

---
 lib/eunit/src/eunit_surefire.erl | 14 ++++++++++++++
 lib/eunit/test/eunit_SUITE.erl   |  9 +++++++--
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/lib/eunit/src/eunit_surefire.erl b/lib/eunit/src/eunit_surefire.erl
index 002a069a92..71f3765f21 100644
--- a/lib/eunit/src/eunit_surefire.erl
+++ b/lib/eunit/src/eunit_surefire.erl
@@ -95,6 +95,7 @@ start(Options) ->
 
 init(Options) ->
     XMLDir = proplists:get_value(dir, Options, ?XMLDIR),
+    ensure_xmldir(XMLDir),
     St = #state{verbose = proplists:get_bool(verbose, Options),
 		xmldir = XMLDir,
 		testsuites = []},
@@ -255,6 +256,19 @@ add_testcase_to_testsuite({error, Exception}, TestCaseTmp, TestSuite) ->
 	      testcases = [TestCase|TestSuite#testsuite.testcases] }
     end.
 
+ensure_xmldir(XMLDir) ->
+    Steps = [
+        fun filelib:ensure_dir/1,
+        fun file:make_dir/1],
+    lists:foldl(fun ensure_xmldir/2, XMLDir, Steps).
+
+ensure_xmldir(Fun, XMLDir) ->
+    case Fun(XMLDir) of
+        ok -> XMLDir;
+        {error, eexist} -> XMLDir;
+        {error, _Reason} = Error -> throw(Error)
+    end.
+
 %% ----------------------------------------------------------------------------
 %% Write a report to the XML directory.
 %% This function opens the report file, calls write_report_to/2 and closes the file.
diff --git a/lib/eunit/test/eunit_SUITE.erl b/lib/eunit/test/eunit_SUITE.erl
index e55091e8c1..df754e46c5 100644
--- a/lib/eunit/test/eunit_SUITE.erl
+++ b/lib/eunit/test/eunit_SUITE.erl
@@ -22,7 +22,7 @@
 -export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1,
 	 init_per_group/2,end_per_group/2,
 	 app_test/1,appup_test/1,eunit_test/1,surefire_utf8_test/1,surefire_latin_test/1,
-	 surefire_c0_test/1]).
+	 surefire_c0_test/1, surefire_ensure_dir_test/1]).
 
 -include_lib("common_test/include/ct.hrl").
 
@@ -30,7 +30,7 @@ suite() -> [{ct_hooks,[ts_install_cth]}].
 
 all() ->
     [app_test, appup_test, eunit_test, surefire_utf8_test, surefire_latin_test,
-     surefire_c0_test].
+     surefire_c0_test, surefire_ensure_dir_test].
 
 groups() ->
     [].
@@ -76,6 +76,11 @@ surefire_c0_test(Config) when is_list(Config) ->
     true = lists:member($\t, Chars),
     ok.
 
+surefire_ensure_dir_test(Config) when is_list(Config) ->
+    XMLDir = filename:join(proplists:get_value(priv_dir, Config), "c1"),
+    ok = eunit:test(tc0, [{report,{eunit_surefire,[{dir,XMLDir}]}}]),
+    ok = file:del_dir_r(XMLDir).
+
 check_surefire(Module) ->
 	File = "TEST-"++atom_to_list(Module)++".xml",
 	file:delete(File),
-- 
2.26.2

openSUSE Build Service is sponsored by