From 1b116260a23dd0b60d93b47899a7dba83fb527ca Mon Sep 17 00:00:00 2001
From: Marco Clemencic <marco.clemencic@cern.ch>
Date: Fri, 16 Jun 2023 10:09:36 +0200
Subject: [PATCH] Make sure OutputStream instances are configured with and
 explicit OutputFile

---
 .../src/PersistencySvc/OutputStream.cpp       |  5 ++
 .../pytest/test_outputfile_not_defined.py     | 47 +++++++++++++++++++
 2 files changed, 52 insertions(+)
 create mode 100644 GaudiExamples/tests/pytest/test_outputfile_not_defined.py

diff --git a/GaudiCommonSvc/src/PersistencySvc/OutputStream.cpp b/GaudiCommonSvc/src/PersistencySvc/OutputStream.cpp
index f6f3b3aa7c..f194b0a604 100644
--- a/GaudiCommonSvc/src/PersistencySvc/OutputStream.cpp
+++ b/GaudiCommonSvc/src/PersistencySvc/OutputStream.cpp
@@ -54,6 +54,11 @@ StatusCode OutputStream::initialize() {
   auto status = Algorithm::initialize();
   if ( status.isFailure() ) return status;
 
+  if ( m_output.empty() ) {
+    fatal() << "property Output not set. OutputStream instances require an output" << endmsg;
+    return StatusCode::FAILURE;
+  }
+
   // Reset the number of events written
   m_events = 0;
   // Get access to the DataManagerSvc
diff --git a/GaudiExamples/tests/pytest/test_outputfile_not_defined.py b/GaudiExamples/tests/pytest/test_outputfile_not_defined.py
new file mode 100644
index 0000000000..615ee7522e
--- /dev/null
+++ b/GaudiExamples/tests/pytest/test_outputfile_not_defined.py
@@ -0,0 +1,47 @@
+#####################################################################################
+# (c) Copyright 2023 CERN for the benefit of the LHCb and ATLAS collaborations      #
+#                                                                                   #
+# This software is distributed under the terms of the Apache version 2 licence,     #
+# copied verbatim in the file "LICENSE".                                            #
+#                                                                                   #
+# In applying this licence, CERN does not waive the privileges and immunities       #
+# granted to it by virtue of its status as an Intergovernmental Organization        #
+# or submit itself to any jurisdiction.                                             #
+#####################################################################################
+import os
+import sys
+
+from GaudiTests import run_gaudi
+
+
+def config():
+    """
+    Configure a job writing to an empty file.
+    """
+    from Configurables import GaudiPersistency
+    from GaudiConfig2 import Configurables as C
+
+    GaudiPersistency()
+
+    out = C.OutputStream(
+        ItemList=["/Event#999"],
+    )
+    app = C.ApplicationMgr(EvtMax=1, OutStream=[out.__opt_value__()])
+
+    return [app, out]
+
+
+def test(tmp_path):
+    """
+    Check that an output file is created even if no event is written to it.
+    """
+    os.chdir(tmp_path)
+
+    result = run_gaudi(
+        f"{__file__}:config", capture_output=True, errors="surrogateescape"
+    )
+    print(result.stdout, end="")
+    print(result.stderr, end="", file=sys.stderr)
+
+    assert result.returncode == 1
+    assert "OutputStream instances require an output" in result.stdout
-- 
GitLab