Skip to content

Commit 8525c49

Browse files
committed
Raise KhiopsEnvironmentError on reading failing khiops_env script
closes #300
1 parent cb17bdb commit 8525c49

2 files changed

Lines changed: 78 additions & 1 deletion

File tree

khiops/core/internals/runner.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,12 @@ def _initialize_khiops_environment(self):
842842
stderr=subprocess.PIPE,
843843
universal_newlines=True,
844844
) as khiops_env_process:
845-
stdout, _ = khiops_env_process.communicate()
845+
stdout, stderr = khiops_env_process.communicate()
846+
if khiops_env_process.returncode != 0:
847+
raise KhiopsEnvironmentError(
848+
"Error initializing the environment for Khiops from the "
849+
f"{khiops_env_path} script. Contents of stderr:\n{stderr}"
850+
)
846851
for line in stdout.split("\n"):
847852
tokens = line.rstrip().split(maxsplit=1)
848853
if len(tokens) == 2:

tests/test_khiops_integrations.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import os
1010
import platform
1111
import shutil
12+
import stat
1213
import subprocess
14+
import tempfile
1315
import unittest
1416

1517
import khiops.core as kh
@@ -95,6 +97,76 @@ def test_runner_has_mpiexec_on_linux(self):
9597
else:
9698
self.skipTest("Skipping test: MPI support is not installed")
9799

100+
def test_environment_error_on_bogus_khiops_env_script(self):
101+
"""Test that error is raised on reading erroneous khiops_env script"""
102+
103+
with tempfile.TemporaryDirectory() as temp_dir:
104+
105+
# Create temporary khiops_env binary dir
106+
# Note: The "bin" subdir is needed for Windows.
107+
temp_khiops_env_dir = os.path.join(temp_dir, "bin")
108+
os.makedirs(temp_khiops_env_dir)
109+
110+
# Create temporary khiops_env path
111+
temp_khiops_env_file_path = os.path.join(temp_khiops_env_dir, "khiops_env")
112+
113+
# On Windows, set KHIOPS_HOME to the temp dir
114+
original_khiops_home_env_var = os.environ.get("KHIOPS_HOME")
115+
if platform.system() == "Windows":
116+
os.environ["KHIOPS_HOME"] = temp_dir
117+
temp_khiops_env_file_path += ".cmd"
118+
119+
# Replace the khiops_env with a script that fails showing an error message
120+
with open(
121+
temp_khiops_env_file_path, "w", encoding="utf-8"
122+
) as temp_khiops_env_file:
123+
test_error_message = "Test Khiops environment error"
124+
if platform.system() == "Windows":
125+
temp_khiops_env_file.write("@echo off\r\n")
126+
temp_khiops_env_file.write(f"echo {test_error_message}>&2\r\n")
127+
temp_khiops_env_file.write("exit /b 1")
128+
else:
129+
temp_khiops_env_file.write("#!/bin/bash\n")
130+
temp_khiops_env_file.write(f'>&2 echo "{test_error_message}"\n')
131+
temp_khiops_env_file.write("exit 1")
132+
133+
# Make the temporary khiops_env file executable
134+
os.chmod(
135+
temp_khiops_env_file_path,
136+
os.stat(temp_khiops_env_file_path).st_mode | stat.S_IEXEC,
137+
)
138+
139+
# Store initial PATH
140+
original_path_env_var = os.environ["PATH"]
141+
142+
# Prepend path to temporary khiops_env to PATH
143+
os.environ["PATH"] = (
144+
temp_khiops_env_dir + os.pathsep + original_path_env_var
145+
)
146+
147+
# Create new KhiopsLocalRunner and capture the exception
148+
with self.assertRaises(KhiopsEnvironmentError) as context:
149+
_ = KhiopsLocalRunner()
150+
151+
# Restore initial PATH
152+
os.environ["PATH"] = original_path_env_var
153+
154+
# On Windows, restore initial KHIOPS_HOME
155+
if (
156+
platform.system() == "Windows"
157+
and original_khiops_home_env_var is not None
158+
):
159+
os.environ["KHIOPS_HOME"] = original_khiops_home_env_var
160+
161+
# Check that the script error message matches the expected one
162+
expected_msg = (
163+
"Error initializing the environment for Khiops from the "
164+
f"{temp_khiops_env_file_path} script. "
165+
f"Contents of stderr:\n{test_error_message}\n"
166+
)
167+
output_msg = str(context.exception)
168+
self.assertEqual(output_msg, expected_msg)
169+
98170
def test_runner_with_conda_based_environment(self):
99171
"""Test that local runner works in non-Conda, Conda-based environments"""
100172

0 commit comments

Comments
 (0)