command - remove unreachable code and achieve full test coverage (#75036)

This commit is contained in:
Sam Doran 2021-06-16 18:06:18 -04:00 committed by GitHub
parent 4e608c02fb
commit 4ab791d501
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 5 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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'