@@ -282,12 +282,85 @@ def test_environment_error_on_bogus_khiops_env_script(self):
282282 output_msg = str (context .exception )
283283 self .assertEqual (output_msg , expected_msg )
284284
285+ def test_runner_environment_for_openmpi5 (self ):
286+ """Test if HOME is actually corrected for OpenMPI 5+"""
287+
288+ with tempfile .TemporaryDirectory () as temp_dir :
289+
290+ # Create temporary khiops_env binary dir
291+ # Note: The "bin" subdir is needed for Windows.
292+ temp_khiops_env_dir = os .path .join (temp_dir , "bin" )
293+ os .makedirs (temp_khiops_env_dir )
294+
295+ # Create temporary khiops_env path
296+ temp_khiops_env_file_path = os .path .join (temp_khiops_env_dir , "khiops_env" )
297+
298+ # On Windows, set KHIOPS_HOME to the temp dir
299+ original_khiops_home_env_var = os .environ .get ("KHIOPS_HOME" )
300+ if platform .system () == "Windows" :
301+ os .environ ["KHIOPS_HOME" ] = temp_dir
302+ temp_khiops_env_file_path += ".cmd"
303+
304+ # Replace the khiops_env with a crafted one
305+ with open (
306+ temp_khiops_env_file_path , "w" , encoding = "utf-8"
307+ ) as temp_khiops_env_file :
308+ if platform .system () == "Windows" :
309+ temp_khiops_env_file .write (
310+ "echo KHIOPS_PATH /usr/bin/MODL_openmpi\r \n "
311+ )
312+ temp_khiops_env_file .write (
313+ "echo KHIOPS_COCLUSTERING_PATH "
314+ "/usr/bin/MODL_Coclustering_openmpi\r \n "
315+ )
316+ temp_khiops_env_file .write ("echo KHIOPS_MPI_HOME \t mp" + "\r \n " )
317+ else :
318+ temp_khiops_env_file .write ("#!/bin/bash\n " )
319+ temp_khiops_env_file .write (
320+ "echo KHIOPS_PATH /usr/bin/MODL_openmpi\n "
321+ )
322+ temp_khiops_env_file .write (
323+ "echo KHIOPS_COCLUSTERING_PATH "
324+ "/usr/bin/MODL_Coclustering_openmpi\n "
325+ )
326+ temp_khiops_env_file .write ("echo KHIOPS_MPI_HOME /tmp\n " )
327+
328+ # Make the temporary khiops_env file executable
329+ os .chmod (
330+ temp_khiops_env_file_path ,
331+ os .stat (temp_khiops_env_file_path ).st_mode | stat .S_IEXEC ,
332+ )
333+
334+ # Store initial PATH
335+ original_path_env_var = os .environ ["PATH" ]
336+
337+ # Prepend path to temporary khiops_env to PATH
338+ os .environ ["PATH" ] = (
339+ temp_khiops_env_dir + os .pathsep + original_path_env_var
340+ )
341+
342+ _ = KhiopsLocalRunner ()
343+
344+ # Check `KHIOPS_MPI_HOME` is correctly exported for OpenMPI 5+
345+ self .assertIsNotNone (os .environ .get ("KHIOPS_MPI_HOME" ))
346+
347+ # Restore initial PATH
348+ os .environ ["PATH" ] = original_path_env_var
349+
350+ # On Windows, restore initial KHIOPS_HOME
351+ if (
352+ platform .system () == "Windows"
353+ and original_khiops_home_env_var is not None
354+ ):
355+ os .environ ["KHIOPS_HOME" ] = original_khiops_home_env_var
356+
285357 def test_runner_environment_initialization (self ):
286358 """Test that local runner initializes/ed its environment properly
287359
288360 .. note::
289361 To test a real initialization this test should be executed alone.
290362 """
363+
291364 # Obtain the current runner
292365 runner = kh .get_runner ()
293366
0 commit comments