Skip to content

Commit

Permalink
Merge pull request #2 from boegel/openfoam-workdir
Browse files Browse the repository at this point in the history
enhance test_run_cmd_with_hooks to check whether `run_cmd`/`complete_cmd` is robust against command that removes the directory it's running in
  • Loading branch information
lexming authored Dec 6, 2023
2 parents 1eed141 + dc24a69 commit 84bead8
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions test/framework/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# along with EasyBuild. If not, see <http://www.gnu.org/licenses/>.
# #
"""
Unit tests for filetools.py
Unit tests for run.py
@author: Toon Willems (Ghent University)
@author: Kenneth Hoste (Ghent University)
Expand All @@ -49,7 +49,7 @@
import easybuild.tools.utilities
from easybuild.tools.build_log import EasyBuildError, init_logging, stop_logging
from easybuild.tools.config import update_build_option
from easybuild.tools.filetools import adjust_permissions, read_file, write_file
from easybuild.tools.filetools import adjust_permissions, change_dir, mkdir, read_file, write_file
from easybuild.tools.run import check_async_cmd, check_log_for_errors, complete_cmd, get_output_from_process
from easybuild.tools.run import parse_log_for_error, run_cmd, run_cmd_qa
from easybuild.tools.config import ERROR, IGNORE, WARN
Expand Down Expand Up @@ -197,7 +197,7 @@ def test_run_cmd_log(self):

# Test that we can set the directory for the logfile
log_path = os.path.join(self.test_prefix, 'chicken')
os.mkdir(log_path)
mkdir(log_path)
logfile = None
init_logging(logfile, silent=True, tmp_logdir=log_path)
logfiles = os.listdir(log_path)
Expand Down Expand Up @@ -796,6 +796,22 @@ def post_run_shell_cmd_hook(cmd, *args, **kwargs):
])
self.assertEqual(stdout, expected_stdout)

# also check with a command that destroys the directory it's running in
workdir = os.path.join(self.test_prefix, 'workdir')
mkdir(workdir)

with self.mocked_stdout_stderr():
run_cmd("pwd; rm -rf %(workdir)s; echo '%(workdir)s removed'" % {'workdir': workdir}, path=workdir)
stdout = self.get_stdout()

patterns = [
r"pre-run hook 'pwd; rm .*/workdir removed'",
r"post-run hook 'pwd;\s*rm.*/workdir removed'' \(exit code: 0, output: '.*\n.*/workdir removed\n'\)",
]
for pattern in patterns:
regex = re.compile(pattern, re.M)
self.assertTrue(regex.search(stdout), "Pattern '%s' should be found in: %s" % (regex.pattern, stdout))


def suite():
""" returns all the testcases in this module """
Expand Down

0 comments on commit 84bead8

Please sign in to comment.