Skip to content

Commit

Permalink
Fix bug with truncating newline
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCoding committed Jan 14, 2022
1 parent 891a8ad commit cb956c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ansible_runner/utils/__init__.py
Expand Up @@ -374,7 +374,7 @@ def _emit_event(self, buffered_stdout, next_event_data=None):
event_data['uuid'] = str(uuid.uuid4())
self._counter += 1
event_data['counter'] = self._counter
event_data['stdout'] = stdout_chunk[:-2] if len(stdout_chunk) > 2 else ""
event_data['stdout'] = stdout_chunk.rstrip('\n\r')
n_lines = stdout_chunk.count('\n')
event_data['start_line'] = self._start_line
event_data['end_line'] = self._start_line + n_lines
Expand Down
15 changes: 13 additions & 2 deletions test/unit/test_runner.py
Expand Up @@ -151,7 +151,7 @@ def test_status_callback_interface(rc, mocker):
def test_stdout_file_write(rc, runner_mode):
if runner_mode == 'pexpect':
pytest.skip('Writing to stdout can be flaky, probably due to some pexpect bug')
rc.command = ['echo', 'hello_world_marker '] # workaround bug in stdout handl wrapper
rc.command = ['echo', 'hello_world_marker']
rc.runner_mode = runner_mode
runner = Runner(config=rc)
status, exitcode = runner.run()
Expand All @@ -168,7 +168,7 @@ def test_stdout_file_write(rc, runner_mode):

@pytest.mark.parametrize('runner_mode', ['pexpect', 'subprocess'])
def test_stdout_file_no_write(rc, runner_mode):
rc.command = ['echo', 'hello_world_marker '] # workaround bug in stdout handl wrapper
rc.command = ['echo', 'hello_world_marker']
rc.runner_mode = runner_mode
rc.suppress_output_file = True
runner = Runner(config=rc)
Expand All @@ -177,3 +177,14 @@ def test_stdout_file_no_write(rc, runner_mode):
for filename in ('stdout', 'stderr'):
stdout_path = Path(rc.artifact_dir) / filename
assert not stdout_path.exists()


@pytest.mark.parametrize('runner_mode', ['pexpect', 'subprocess'])
def test_multiline_blank_write(rc, runner_mode):
rc.command = ['echo', 'hello_world_marker\n\n\n']
rc.runner_mode = runner_mode
runner = Runner(config=rc)
status, exitcode = runner.run()
assert status == 'successful'
stdout_path = Path(rc.artifact_dir) / 'stdout'
assert stdout_path.read_text() == 'hello_world_marker\n\n\n\n' # one extra newline okay

0 comments on commit cb956c9

Please sign in to comment.