Skip to content

Commit dc3826d

Browse files
authored
Merge pull request #4662 from Micket/declare
also defined functions in interactive shell session created by `cmd.sh` script that is produced by `run_shell_cmd`
2 parents d3c0127 + 1222890 commit dc3826d

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

.github/workflows/unit_tests.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ jobs:
152152
cd $HOME
153153
# initialize environment for modules tool
154154
if [ -f $HOME/moduleshome ]; then export MODULESHOME=$(cat $HOME/moduleshome); fi
155-
source $(cat $HOME/mod_init); type module
155+
source $(cat $HOME/mod_init)
156+
type module
157+
module --version
156158
# make sure 'eb' is available via $PATH, and that $PYTHONPATH is set (some tests expect that);
157159
# also pick up changes to $PATH set by sourcing $MOD_INIT
158160
export PREFIX=/tmp/$USER/$GITHUB_SHA

easybuild/tools/run.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ def create_cmd_scripts(cmd_str, work_dir, env, tmpdir, out_file, err_file):
208208
if env is None:
209209
env = os.environ.copy()
210210

211+
# Decode any declared bash functions
212+
proc = subprocess.Popen('declare -f', stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
213+
env=env, shell=True, executable='bash')
214+
(bash_functions, _) = proc.communicate()
215+
211216
env_fp = os.path.join(tmpdir, 'env.sh')
212217
with open(env_fp, 'w') as fid:
213218
# unset all environment variables in current environment first to start from a clean slate;
@@ -219,6 +224,8 @@ def create_cmd_scripts(cmd_str, work_dir, env, tmpdir, out_file, err_file):
219224
fid.write('\n'.join(f'export {key}={shlex.quote(value)}' for key, value in sorted(env.items())
220225
if not key.endswith('%')) + '\n')
221226

227+
fid.write(bash_functions.decode(errors='ignore') + '\n')
228+
222229
fid.write('\n\nPS1="eb-shell> "')
223230

224231
# define $EB_CMD_OUT_FILE to contain path to file with command output

test/framework/run.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
from easybuild.tools.build_log import EasyBuildError, init_logging, stop_logging
5353
from easybuild.tools.config import update_build_option
5454
from easybuild.tools.filetools import adjust_permissions, change_dir, mkdir, read_file, remove_dir, write_file
55+
from easybuild.tools.modules import EnvironmentModules, Lmod
5556
from easybuild.tools.run import RunShellCmdResult, RunShellCmdError, check_async_cmd, check_log_for_errors
5657
from easybuild.tools.run import complete_cmd, fileprefix_from_cmd, get_output_from_process, parse_log_for_error
5758
from easybuild.tools.run import run_cmd, run_cmd_qa, run_shell_cmd, subprocess_terminate
@@ -233,6 +234,20 @@ def test_run_shell_cmd_basic(self):
233234
pwd = res[0].strip()[5:]
234235
self.assertTrue(os.path.samefile(pwd, self.test_prefix))
235236

237+
cmd = f"{cmd_script} -c 'module --version'"
238+
with self.mocked_stdout_stderr():
239+
res = run_shell_cmd(cmd, fail_on_error=False)
240+
self.assertEqual(res.exit_code, 0)
241+
242+
if isinstance(self.modtool, Lmod):
243+
regex = re.compile("^Modules based on Lua: Version [0-9]", re.M)
244+
elif isinstance(self.modtool, EnvironmentModules):
245+
regex = re.compile("^Modules Release [0-9]", re.M)
246+
else:
247+
self.fail("Unknown modules tool used!")
248+
249+
self.assertTrue(regex.search(res.output), f"Pattern '{regex.pattern}' should be found in {res.output}")
250+
236251
# test running command that emits non-UTF-8 characters
237252
# this is constructed to reproduce errors like:
238253
# UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2

0 commit comments

Comments
 (0)