Skip to content

Commit

Permalink
allow triggering deprecated behaviour in tests for run_cmd and run_cm…
Browse files Browse the repository at this point in the history
…d_qa
  • Loading branch information
boegel committed Apr 14, 2024
1 parent 04413a5 commit 72704a7
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 30 deletions.
8 changes: 4 additions & 4 deletions test/framework/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ def test_run_cmd(self):

error_pattern = r"Undefined build option: .*"
error_pattern += r" Make sure you have set up the EasyBuild configuration using set_up_configuration\(\)"
self.assertErrorRegex(EasyBuildError, error_pattern, run_cmd, "echo hello")
with self.mocked_stdout_stderr():
self.assertErrorRegex(EasyBuildError, error_pattern, run_cmd, "echo hello")

self.configure()

# run_cmd works fine if set_up_configuration was called first
self.mock_stdout(True)
(out, ec) = run_cmd("echo hello")
self.mock_stdout(False)
with self.mocked_stdout_stderr():
(out, ec) = run_cmd("echo hello")
self.assertEqual(ec, 0)
self.assertEqual(out, 'hello\n')

Expand Down
108 changes: 82 additions & 26 deletions test/framework/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ def get_proc(cmd, asynchronous=False):

def test_run_cmd(self):
"""Basic test for run_cmd function."""

# use of run_cmd is deprecated, so we need to allow it here
self.allow_deprecated_behaviour()

with self.mocked_stdout_stderr():
(out, ec) = run_cmd("echo hello")
self.assertEqual(out, "hello\n")
Expand Down Expand Up @@ -217,6 +221,10 @@ def test_fileprefix_from_cmd(self):

def test_run_cmd_log(self):
"""Test logging of executed commands."""

# use of run_cmd is deprecated, so we need to allow it here
self.allow_deprecated_behaviour()

fd, logfile = tempfile.mkstemp(suffix='.log', prefix='eb-test-')
os.close(fd)

Expand Down Expand Up @@ -300,6 +308,10 @@ def test_run_shell_cmd_log(self):

def test_run_cmd_negative_exit_code(self):
"""Test run_cmd function with command that has negative exit code."""

# use of run_cmd is deprecated, so we need to allow it here
self.allow_deprecated_behaviour()

# define signal handler to call in case run_cmd takes too long
def handler(signum, _):
raise RuntimeError("Signal handler called with signal %s" % signum)
Expand Down Expand Up @@ -433,6 +445,10 @@ def handler(signum, _):

def test_run_cmd_bis(self):
"""More 'complex' test for run_cmd function."""

# use of run_cmd is deprecated, so we need to allow it here
self.allow_deprecated_behaviour()

# a more 'complex' command to run, make sure all required output is there
with self.mocked_stdout_stderr():
(out, ec) = run_cmd("for j in `seq 1 3`; do for i in `seq 1 100`; do echo hello; done; sleep 1.4; done")
Expand All @@ -453,6 +469,10 @@ def test_run_cmd_work_dir(self):
"""
Test running command in specific directory with run_cmd function.
"""

# use of run_cmd is deprecated, so we need to allow it here
self.allow_deprecated_behaviour()

orig_wd = os.getcwd()
self.assertFalse(os.path.samefile(orig_wd, self.test_prefix))

Expand Down Expand Up @@ -493,6 +513,10 @@ def test_run_shell_cmd_work_dir(self):

def test_run_cmd_log_output(self):
"""Test run_cmd with log_output enabled"""

# use of run_cmd is deprecated, so we need to allow it here
self.allow_deprecated_behaviour()

with self.mocked_stdout_stderr():
(out, ec) = run_cmd("seq 1 100", log_output=True)
self.assertEqual(ec, 0)
Expand Down Expand Up @@ -548,6 +572,9 @@ def test_run_shell_cmd_split_stderr(self):
def test_run_cmd_trace(self):
"""Test run_cmd in trace mode, and with tracing disabled."""

# use of run_cmd is deprecated, so we need to allow it here
self.allow_deprecated_behaviour()

pattern = [
r"^ >> running command:",
r"\t\[started at: .*\]",
Expand All @@ -567,7 +594,7 @@ def test_run_cmd_trace(self):
self.mock_stderr(False)
self.assertEqual(out, 'hello\n')
self.assertEqual(ec, 0)
self.assertEqual(stderr, '')
self.assertTrue(stderr.strip().startswith("WARNING: Deprecated functionality"))
regex = re.compile('\n'.join(pattern))
self.assertTrue(regex.search(stdout), "Pattern '%s' found in: %s" % (regex.pattern, stdout))

Expand All @@ -582,7 +609,7 @@ def test_run_cmd_trace(self):
self.mock_stderr(False)
self.assertEqual(out, 'hello\n')
self.assertEqual(ec, 0)
self.assertEqual(stderr, '')
self.assertTrue(stderr.strip().startswith("WARNING: Deprecated functionality"))
self.assertEqual(stdout, '')

init_config(build_options={'trace': True})
Expand All @@ -597,7 +624,7 @@ def test_run_cmd_trace(self):
self.mock_stderr(False)
self.assertEqual(out, 'hello')
self.assertEqual(ec, 0)
self.assertEqual(stderr, '')
self.assertTrue(stderr.strip().startswith("WARNING: Deprecated functionality"))
pattern.insert(3, r"\t\[input: hello\]")
pattern[-2] = "\tcat"
regex = re.compile('\n'.join(pattern))
Expand All @@ -614,7 +641,7 @@ def test_run_cmd_trace(self):
self.mock_stderr(False)
self.assertEqual(out, 'hello')
self.assertEqual(ec, 0)
self.assertEqual(stderr, '')
self.assertTrue(stderr.strip().startswith("WARNING: Deprecated functionality"))
self.assertEqual(stdout, '')

# trace output can be disabled on a per-command basis
Expand All @@ -631,7 +658,7 @@ def test_run_cmd_trace(self):
self.assertEqual(out, 'hello\n')
self.assertEqual(ec, 0)
self.assertEqual(stdout, '')
self.assertEqual(stderr, '')
self.assertTrue(stderr.strip().startswith("WARNING: Deprecated functionality"))

def test_run_shell_cmd_trace(self):
"""Test run_shell_cmd function in trace mode, and with tracing disabled."""
Expand Down Expand Up @@ -963,6 +990,9 @@ def test_run_shell_cmd_qa_log(self):
def test_run_cmd_qa_trace(self):
"""Test run_cmd under --trace"""

# use of run_cmd is deprecated, so we need to allow it here
self.allow_deprecated_behaviour()

# --trace is enabled by default
self.mock_stdout(True)
self.mock_stderr(True)
Expand All @@ -989,7 +1019,7 @@ def test_run_cmd_qa_trace(self):
self.mock_stdout(False)
self.mock_stderr(False)
self.assertEqual(stdout, '')
self.assertEqual(stderr, '')
self.assertTrue(stderr.strip().startswith("WARNING: Deprecated functionality"))

def test_run_shell_cmd_qa_trace(self):
"""Test run_shell_cmd with qa_patterns under --trace"""
Expand Down Expand Up @@ -1071,12 +1101,20 @@ def test_run_shell_cmd_qa_answers(self):

def test_run_cmd_simple(self):
"""Test return value for run_cmd in 'simple' mode."""

# use of run_cmd is deprecated, so we need to allow it here
self.allow_deprecated_behaviour()

with self.mocked_stdout_stderr():
self.assertEqual(True, run_cmd("echo hello", simple=True))
self.assertEqual(False, run_cmd("exit 1", simple=True, log_all=False, log_ok=False))

def test_run_cmd_cache(self):
"""Test caching for run_cmd"""

# use of run_cmd is deprecated, so we need to allow it here
self.allow_deprecated_behaviour()

with self.mocked_stdout_stderr():
(first_out, ec) = run_cmd("ulimit -u")
self.assertEqual(ec, 0)
Expand Down Expand Up @@ -1164,6 +1202,10 @@ def test_parse_log_error(self):

def test_run_cmd_dry_run(self):
"""Test use of run_cmd function under (extended) dry run."""

# use of run_cmd is deprecated, so we need to allow it here
self.allow_deprecated_behaviour()

build_options = {
'extended_dry_run': True,
'silent': False,
Expand All @@ -1172,43 +1214,38 @@ def test_run_cmd_dry_run(self):

cmd = "somecommand foo 123 bar"

self.mock_stdout(True)
run_cmd(cmd)
stdout = self.get_stdout()
self.mock_stdout(False)
with self.mocked_stdout_stderr():
run_cmd(cmd)
stdout = self.get_stdout()

expected = """ running command "somecommand foo 123 bar"\n"""
self.assertIn(expected, stdout)

# check disabling 'verbose'
self.mock_stdout(True)
run_cmd("somecommand foo 123 bar", verbose=False)
stdout = self.get_stdout()
self.mock_stdout(False)
with self.mocked_stdout_stderr():
run_cmd("somecommand foo 123 bar", verbose=False)
stdout = self.get_stdout()
self.assertNotIn(expected, stdout)

# check forced run_cmd
outfile = os.path.join(self.test_prefix, 'cmd.out')
self.assertNotExists(outfile)
self.mock_stdout(True)
run_cmd("echo 'This is always echoed' > %s" % outfile, force_in_dry_run=True)
self.mock_stdout(False)
with self.mocked_stdout_stderr():
run_cmd("echo 'This is always echoed' > %s" % outfile, force_in_dry_run=True)
self.assertExists(outfile)
self.assertEqual(read_file(outfile), "This is always echoed\n")

# Q&A commands
self.mock_stdout(True)
run_shell_cmd("some_qa_cmd", qa_patterns=[('question1', 'answer1')])
stdout = self.get_stdout()
self.mock_stdout(False)
with self.mocked_stdout_stderr():
run_shell_cmd("some_qa_cmd", qa_patterns=[('question1', 'answer1')])
stdout = self.get_stdout()

expected = """ running interactive shell command "some_qa_cmd"\n"""
self.assertIn(expected, stdout)

self.mock_stdout(True)
run_cmd_qa("some_qa_cmd", {'question1': 'answer1'})
stdout = self.get_stdout()
self.mock_stdout(False)
with self.mocked_stdout_stderr():
run_cmd_qa("some_qa_cmd", {'question1': 'answer1'})
stdout = self.get_stdout()

expected = """ running interactive command "some_qa_cmd"\n"""
self.assertIn(expected, stdout)
Expand Down Expand Up @@ -1264,6 +1301,10 @@ def test_run_shell_cmd_dry_run(self):

def test_run_cmd_list(self):
"""Test run_cmd with command specified as a list rather than a string"""

# use of run_cmd is deprecated, so we need to allow it here
self.allow_deprecated_behaviour()

cmd = ['/bin/sh', '-c', "echo hello"]
with self.mocked_stdout_stderr():
self.assertErrorRegex(EasyBuildError, "When passing cmd as a list then `shell` must be set explictely!",
Expand All @@ -1275,6 +1316,10 @@ def test_run_cmd_list(self):

def test_run_cmd_script(self):
"""Testing use of run_cmd with shell=False to call external scripts"""

# use of run_cmd is deprecated, so we need to allow it here
self.allow_deprecated_behaviour()

py_test_script = os.path.join(self.test_prefix, 'test.py')
write_file(py_test_script, '\n'.join([
'#!%s' % sys.executable,
Expand Down Expand Up @@ -1313,6 +1358,10 @@ def test_run_shell_cmd_no_bash(self):

def test_run_cmd_stream(self):
"""Test use of run_cmd with streaming output."""

# use of run_cmd is deprecated, so we need to allow it here
self.allow_deprecated_behaviour()

self.mock_stdout(True)
self.mock_stderr(True)
(out, ec) = run_cmd("echo hello", stream_output=True)
Expand All @@ -1324,7 +1373,7 @@ def test_run_cmd_stream(self):
self.assertEqual(ec, 0)
self.assertEqual(out, "hello\n")

self.assertEqual(stderr, '')
self.assertTrue(stderr.strip().startswith("WARNING: Deprecated functionality"))
expected = [
"== (streaming) output for command 'echo hello':",
"hello",
Expand Down Expand Up @@ -1370,6 +1419,9 @@ def test_run_shell_cmd_stream(self):
def test_run_cmd_async(self):
"""Test asynchronously running of a shell command via run_cmd + complete_cmd."""

# use of run_cmd is deprecated, so we need to allow it here
self.allow_deprecated_behaviour()

os.environ['TEST'] = 'test123'

test_cmd = "echo 'sleeping...'; sleep 2; echo $TEST"
Expand Down Expand Up @@ -1604,6 +1656,10 @@ def test_run_cmd_with_hooks(self):
"""
Test running command with run_cmd with pre/post run_shell_cmd hooks in place.
"""

# use of run_cmd is deprecated, so we need to allow it here
self.allow_deprecated_behaviour()

cwd = os.getcwd()

hooks_file = os.path.join(self.test_prefix, 'my_hooks.py')
Expand Down

0 comments on commit 72704a7

Please sign in to comment.