|
9 | 9 | import os |
10 | 10 | import platform |
11 | 11 | import shutil |
| 12 | +import stat |
12 | 13 | import subprocess |
| 14 | +import tempfile |
13 | 15 | import unittest |
14 | 16 |
|
15 | 17 | import khiops.core as kh |
@@ -95,6 +97,76 @@ def test_runner_has_mpiexec_on_linux(self): |
95 | 97 | else: |
96 | 98 | self.skipTest("Skipping test: MPI support is not installed") |
97 | 99 |
|
| 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 | + |
98 | 170 | def test_runner_with_conda_based_environment(self): |
99 | 171 | """Test that local runner works in non-Conda, Conda-based environments""" |
100 | 172 |
|
|
0 commit comments