Skip to content
Snippets Groups Projects
Commit 54f4a18d authored by Frank Sauerburger's avatar Frank Sauerburger
Browse files

Implement OpConsoleOutput

Implement the operation console_output, which runs the listed commands and check
that the output matches.
parent f8ebda48
No related tags found
No related merge requests found
Pipeline #
......@@ -6,7 +6,7 @@ stages:
doxec_unittest:
stage: test
script:
- python3 -m unittest doxec.tests
- python3 -m unittest -v doxec.tests
doxec_doctest:
......
......@@ -94,6 +94,7 @@ class OpConsole(Operation):
if job.returncode != 0:
raise TestException("Script failed with %d:" % job.returncode,
stdoutdata.decode('utf8'), stderrdata.decode('utf8'))
return stdoutdata
......@@ -105,7 +106,30 @@ class OpConsoleOutput(Operation):
command = "console_output"
def execute(self):
pass
commands = [] # items are (command, [output lines])
for line in self.content:
if line.startswith("$"):
commands.append((line[1:], []))
elif len(commands) == 0:
# no command yet
continue
else:
commands[-1][1].append(line)
for command, lines in commands:
job = subprocess.Popen("/bin/bash", stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdoutdata, stderrdata) = job.communicate(command.encode('utf8'))
if job.returncode != 0:
raise TestException("Script failed with %d:" % job.returncode,
stdoutdata.decode('utf8'), stderrdata.decode('utf8'))
output = stdoutdata.decode('utf8')
output = re.split(r'\r?\n', output)
if len(output) > 0 and output[-1] == '':
del output[-1]
if lines != output:
raise TestException("Output differs", lines, output)
# add operations to op_store
Operation.op_store.append(OpConsole)
......
......@@ -169,4 +169,29 @@ class OpConsoleOutputTestCase(unittest.TestCase):
Test the functionality of the console-output-operation. This test case focuses on
the execute method.
"""
pass
def test_execute_pass(self):
"""
Creates an OpConsoleOutput operation and checks that no exception is raised,
when the expected output matches the actual output.
"""
console = OpConsoleOutput(None,
["$ echo 'Hi'", "Hi", "$ echo 'Hello\n World'", "Hello", " World"])
console.execute()
def test_execute_fail(self):
"""
Creates an OpConsoleOutput operation and checks that an exception is raised,
when the expected output does not match the actual output.
"""
console = OpConsoleOutput(None,
["$ echo 'Hi'", "Hi", "$ echo 'Hello\n World'", "Hello", "Moon"])
self.assertRaises(TestException, console.execute)
def test_execute_leading_comments(self):
"""
Creates an OpConsoleOutput operation and checks whether leading
non-code lines are ignored.
"""
console = OpConsoleOutput(None, ["heyho", "$ echo 'Hi'", "Hi"])
console.execute()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment