Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with truncating newline #950

Merged
merged 1 commit into from Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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