From 4ab791d501771d34fef6edb8f2f7932ed3885687 Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Wed, 16 Jun 2021 18:06:18 -0400 Subject: [PATCH] command - remove unreachable code and achieve full test coverage (#75036) --- .../command-remove-unreachable-code.yml | 2 + lib/ansible/modules/command.py | 6 +-- .../targets/command_shell/tasks/main.yml | 37 +++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/command-remove-unreachable-code.yml diff --git a/changelogs/fragments/command-remove-unreachable-code.yml b/changelogs/fragments/command-remove-unreachable-code.yml new file mode 100644 index 00000000000..a5b237a9ab9 --- /dev/null +++ b/changelogs/fragments/command-remove-unreachable-code.yml @@ -0,0 +1,2 @@ +bugfixes: + - command - remove unreachable code path when trying to convert the value for ``chdir`` to bytes (https://github.com/ansible/ansible/pull/75036) diff --git a/lib/ansible/modules/command.py b/lib/ansible/modules/command.py index 1ccd3388b26..f3b5dfd9a23 100644 --- a/lib/ansible/modules/command.py +++ b/lib/ansible/modules/command.py @@ -317,11 +317,7 @@ def main(): check_command(module, args) if chdir: - try: - chdir = to_bytes(chdir, errors='surrogate_or_strict') - except ValueError as e: - r['msg'] = 'Unable to use supplied chdir from %s: %s ' % (os.getcwd(), to_text(e)) - module.fail_json(**r) + chdir = to_bytes(chdir, errors='surrogate_or_strict') try: os.chdir(chdir) diff --git a/test/integration/targets/command_shell/tasks/main.yml b/test/integration/targets/command_shell/tasks/main.yml index 45746f5da37..653b00594be 100644 --- a/test/integration/targets/command_shell/tasks/main.yml +++ b/test/integration/targets/command_shell/tasks/main.yml @@ -132,12 +132,21 @@ chdir: "{{ output_dir_test }}" register: command_result2 +- name: Check invalid chdir + command: echo + args: + chdir: "{{ output_dir }}/nope" + ignore_errors: yes + register: chdir_invalid + - name: assert that the script executed correctly with chdir assert: that: - command_result2.rc == 0 - command_result2.stderr == '' - command_result2.stdout == 'win' + - chdir_invalid is failed + - chdir_invalid.msg is search('Unable to change directory') # creates @@ -483,3 +492,31 @@ - parent_dir_chdir.stdout != parent_dir_cd.stdout - 'parent_dir_cd.stdout == "{{output_dir}}/www"' - 'parent_dir_chdir.stdout == "{{output_dir}}/www_root"' + +- name: Set print error command for Python 2 + set_fact: + print_error_command: print >> sys.stderr, msg + when: ansible_facts.python_version is version('3', '<') + +- name: Set print error command for Python 3 + set_fact: + print_error_command: print(msg, file=sys.stderr) + when: ansible_facts.python_version is version('3', '>=') + +- name: run command with strip + command: '{{ ansible_playbook_python}} -c "import sys; msg=''hello \n \r''; print(msg); {{ print_error_command }}"' + register: command_strip + +- name: run command without strip + command: '{{ ansible_playbook_python}} -c "import sys; msg=''hello \n \r''; print(msg); {{ print_error_command }}"' + args: + strip_empty_ends: no + register: command_no_strip + +- name: Verify strip behavior worked as expected + assert: + that: + - command_strip.stdout == 'hello \n ' + - command_strip.stderr == 'hello \n ' + - command_no_strip.stdout== 'hello \n \r\n' + - command_no_strip.stderr == 'hello \n \r\n'