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

pull/75039/head
Sam Doran 3 years ago committed by GitHub
parent 4e608c02fb
commit 4ab791d501
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

@ -317,11 +317,7 @@ def main():
check_command(module, args) check_command(module, args)
if chdir: if chdir:
try:
chdir = to_bytes(chdir, errors='surrogate_or_strict') 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)
try: try:
os.chdir(chdir) os.chdir(chdir)

@ -132,12 +132,21 @@
chdir: "{{ output_dir_test }}" chdir: "{{ output_dir_test }}"
register: command_result2 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 - name: assert that the script executed correctly with chdir
assert: assert:
that: that:
- command_result2.rc == 0 - command_result2.rc == 0
- command_result2.stderr == '' - command_result2.stderr == ''
- command_result2.stdout == 'win' - command_result2.stdout == 'win'
- chdir_invalid is failed
- chdir_invalid.msg is search('Unable to change directory')
# creates # creates
@ -483,3 +492,31 @@
- parent_dir_chdir.stdout != parent_dir_cd.stdout - parent_dir_chdir.stdout != parent_dir_cd.stdout
- 'parent_dir_cd.stdout == "{{output_dir}}/www"' - 'parent_dir_cd.stdout == "{{output_dir}}/www"'
- 'parent_dir_chdir.stdout == "{{output_dir}}/www_root"' - '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'

Loading…
Cancel
Save