---
- name: fail when source does not exist remote
  win_copy:
    src: fakesource
    dest: fakedest
    remote_src: yes
  register: fail_remote_invalid_source
  failed_when: "'it does not exist' not in fail_remote_invalid_source.msg"

- name: setup source folder for remote tests
  win_copy:
    src: files/
    dest: '{{test_win_copy_path}}\source\'

- name: setup remote failure tests
  win_file:
    path: '{{item.path}}'
    state: '{{item.state}}'
  with_items:
  - { 'path': '{{test_win_copy_path}}\target\folder', 'state': 'directory' }
  - { 'path': '{{test_win_copy_path}}\target\file', 'state': 'touch' }
  - { 'path': '{{test_win_copy_path}}\target\subdir', 'state': 'touch' }

- name: fail source is a file but dest is a folder
  win_copy:
    src: '{{test_win_copy_path}}\source\foo.txt'
    dest: '{{test_win_copy_path}}\target\folder'
    remote_src: yes
  register: fail_remote_file_to_folder
  failed_when: "'dest is already a folder' not in fail_remote_file_to_folder.msg"

- name: fail source is a file but dest is a folder
  win_copy:
    src: '{{test_win_copy_path}}\source\'
    dest: '{{test_win_copy_path}}\target\'
    remote_src: yes
  register: fail_remote_folder_to_file
  failed_when: "'dest is already a file' not in fail_remote_folder_to_file.msg"

- name: fail source is a file dest parent dir is also a file
  win_copy:
    src: '{{test_win_copy_path}}\source\foo.txt'
    dest: '{{test_win_copy_path}}\target\file\foo.txt'
    remote_src: yes
  register: fail_remote_file_parent_dir_file
  failed_when: "'is currently a file' not in fail_remote_file_parent_dir_file.msg"

- name: fail source is a folder dest parent dir is also a file
  win_copy:
    src: '{{test_win_copy_path}}\source\subdir'
    dest: '{{test_win_copy_path}}\target\file'
    remote_src: yes
  register: fail_remote_folder_parent_dir_file
  failed_when: "'object at dest parent dir is not a folder' not in fail_remote_folder_parent_dir_file.msg"

- name: fail to copy a remote file with parent dir that doesn't exist and filename is set
  win_copy:
    src: '{{test_win_copy_path}}\source\foo.txt'
    dest: '{{test_win_copy_path}}\missing-dir\foo.txt'
    remote_src: yes
  register: fail_remote_missing_parent_dir
  failed_when: "'does not exist' not in fail_remote_missing_parent_dir.msg"

- name: remove target after remote failure tests
  win_file:
    path: '{{test_win_copy_path}}\target'
    state: absent

- name: create remote target after cleaning
  win_file:
    path: '{{test_win_copy_path}}\target'
    state: directory

- name: copy single file remote (check mode)
  win_copy:
    src: '{{test_win_copy_path}}\source\foo.txt'
    dest: '{{test_win_copy_path}}\target\foo-target.txt'
    remote_src: yes
  register: remote_copy_file_check
  check_mode: yes

- name: get result of copy single file remote (check mode)
  win_stat:
    path: '{{test_win_copy_path}}\target\foo-target.txt'
  register: remote_copy_file_actual_check

- name: assert copy single file remote (check mode)
  assert:
    that:
    - remote_copy_file_check is changed
    - remote_copy_file_actual_check.stat.exists == False

- name: copy single file remote
  win_copy:
    src: '{{test_win_copy_path}}\source\foo.txt'
    dest: '{{test_win_copy_path}}\target\foo-target.txt'
    remote_src: yes
  register: remote_copy_file

- name: get result of copy single file remote
  win_stat:
    path: '{{test_win_copy_path}}\target\foo-target.txt'
  register: remote_copy_file_actual

- name: assert copy single file remote
  assert:
    that:
    - remote_copy_file is changed
    - remote_copy_file.operation == 'file_copy'
    - remote_copy_file.checksum == 'c79a6506c1c948be0d456ab5104d5e753ab2f3e6'
    - remote_copy_file.size == 8
    - remote_copy_file.original_basename == 'foo.txt'
    - remote_copy_file_actual.stat.exists == True
    - remote_copy_file_actual.stat.checksum == 'c79a6506c1c948be0d456ab5104d5e753ab2f3e6'

- name: copy single file remote (idempotent)
  win_copy:
    src: '{{test_win_copy_path}}\source\foo.txt'
    dest: '{{test_win_copy_path}}\target\foo-target.txt'
    remote_src: yes
  register: remote_copy_file_again

- name: assert copy single file remote (idempotent)
  assert:
    that:
    - remote_copy_file_again is not changed

- name: copy single file into folder remote (check mode)
  win_copy:
    src: '{{test_win_copy_path}}\source\foo.txt'
    dest: '{{test_win_copy_path}}\target\'
    remote_src: yes
  register: remote_copy_file_to_folder_check
  check_mode: yes

- name: get result of copy single file into folder remote (check mode)
  win_stat:
    path: '{{test_win_copy_path}}\target\foo.txt'
  register: remote_copy_file_to_folder_actual_check

- name: assert copy single file into folder remote (check mode)
  assert:
    that:
    - remote_copy_file_to_folder_check is changed
    - remote_copy_file_to_folder_actual_check.stat.exists == False

- name: copy single file into folder remote
  win_copy:
    src: '{{test_win_copy_path}}\source\foo.txt'
    dest: '{{test_win_copy_path}}\target\'
    remote_src: yes
  register: remote_copy_file_to_folder

- name: get result of copy single file into folder remote
  win_stat:
    path: '{{test_win_copy_path}}\target\foo.txt'
  register: remote_copy_file_to_folder_actual

- name: assert copy single file into folder remote
  assert:
    that:
    - remote_copy_file_to_folder is changed
    - remote_copy_file_to_folder.operation == 'file_copy'
    - remote_copy_file_to_folder.checksum == 'c79a6506c1c948be0d456ab5104d5e753ab2f3e6'
    - remote_copy_file_to_folder.size == 8
    - remote_copy_file_to_folder.original_basename == 'foo.txt'
    - remote_copy_file_to_folder_actual.stat.exists == True
    - remote_copy_file_to_folder_actual.stat.checksum == 'c79a6506c1c948be0d456ab5104d5e753ab2f3e6'

- name: copy single file into folder remote (idempotent)
  win_copy:
    src: '{{test_win_copy_path}}\source\foo.txt'
    dest: '{{test_win_copy_path}}\target\'
    remote_src: yes
  register: remote_copy_file_to_folder_again

- name: assert copy single file into folder remote
  assert:
    that:
    - remote_copy_file_to_folder_again is not changed

- name: copy single file to missing folder (check mode)
  win_copy:
    src: '{{test_win_copy_path}}\source\foo.txt'
    dest: '{{test_win_copy_path}}\target\missing\'
    remote_src: yes
  register: remote_copy_file_to_missing_folder_check
  check_mode: yes

- name: get result of copy single file to missing folder remote (check mode)
  win_stat:
    path: '{{test_win_copy_path}}\target\missing\foo.txt'
  register: remote_copy_file_to_missing_folder_actual_check

- name: assert copy single file to missing folder remote (check mode)
  assert:
    that:
    - remote_copy_file_to_missing_folder_check is changed
    - remote_copy_file_to_missing_folder_check.operation == 'file_copy'
    - remote_copy_file_to_missing_folder_actual_check.stat.exists == False

- name: copy single file to missing folder remote
  win_copy:
    src: '{{test_win_copy_path}}\source\foo.txt'
    dest: '{{test_win_copy_path}}\target\missing\'
    remote_src: yes
  register: remote_copy_file_to_missing_folder

- name: get result of copy single file to missing folder remote
  win_stat:
    path: '{{test_win_copy_path}}\target\missing\foo.txt'
  register: remote_copy_file_to_missing_folder_actual

- name: assert copy single file to missing folder remote
  assert:
    that:
    - remote_copy_file_to_missing_folder is changed
    - remote_copy_file_to_missing_folder.checksum == 'c79a6506c1c948be0d456ab5104d5e753ab2f3e6'
    - remote_copy_file_to_missing_folder.operation == 'file_copy'
    - remote_copy_file_to_missing_folder.size == 8
    - remote_copy_file_to_missing_folder_actual.stat.exists == True
    - remote_copy_file_to_missing_folder_actual.stat.checksum == 'c79a6506c1c948be0d456ab5104d5e753ab2f3e6'

- name: clear target for folder to folder test
  win_file:
    path: '{{test_win_copy_path}}\target'
    state: absent

- name: copy folder to folder remote (check mode)
  win_copy:
    src: '{{test_win_copy_path}}\source'
    dest: '{{test_win_copy_path}}\target'
    remote_src: yes
  register: remote_copy_folder_to_folder_check
  check_mode: yes

- name: get result of copy folder to folder remote (check mode)
  win_stat:
    path: '{{test_win_copy_path}}\target'
  register: remote_copy_folder_to_folder_actual_check

- name: assert copy folder to folder remote (check mode)
  assert:
    that:
    - remote_copy_folder_to_folder_check is changed
    - remote_copy_folder_to_folder_check.operation == 'folder_copy'
    - remote_copy_folder_to_folder_actual_check.stat.exists == False

- name: copy folder to folder remote
  win_copy:
    src: '{{test_win_copy_path}}\source'
    dest: '{{test_win_copy_path}}\target'
    remote_src: yes
  register: remote_copy_folder_to_folder

- name: get result of copy folder to folder remote
  win_find:
    paths: '{{test_win_copy_path}}\target'
    recurse: yes
    file_type: directory
  register: remote_copy_folder_to_folder_actual

- name: assert copy folder to folder remote
  assert:
    that:
    - remote_copy_folder_to_folder is changed
    - remote_copy_folder_to_folder.operation == 'folder_copy'
    - remote_copy_folder_to_folder_actual.examined == 11
    - remote_copy_folder_to_folder_actual.matched == 6
    - remote_copy_folder_to_folder_actual.files[0].filename == 'source'
    - remote_copy_folder_to_folder_actual.files[1].filename == 'subdir'
    - remote_copy_folder_to_folder_actual.files[2].filename == 'empty'
    - remote_copy_folder_to_folder_actual.files[3].filename == 'subdir2'
    - remote_copy_folder_to_folder_actual.files[4].filename == 'subdir3'
    - remote_copy_folder_to_folder_actual.files[5].filename == 'subdir4'

- name: copy folder to folder remote (idempotent)
  win_copy:
    src: '{{test_win_copy_path}}\source'
    dest: '{{test_win_copy_path}}\target'
    remote_src: yes
  register: remote_copy_folder_to_folder_again

- name: assert copy folder to folder remote (idempotent)
  assert:
    that:
    - remote_copy_folder_to_folder_again is not changed

- name: change remote file after folder to folder test
  win_copy:
    content: bar.txt
    dest: '{{test_win_copy_path}}\target\source\foo.txt'

- name: remote remote folder after folder to folder test
  win_file:
    path: '{{test_win_copy_path}}\target\source\subdir\subdir2\subdir3\subdir4'
    state: absent

- name: copy folder to folder remote after change
  win_copy:
    src: '{{test_win_copy_path}}\source'
    dest: '{{test_win_copy_path}}\target'
    remote_src: yes
  register: remote_copy_folder_to_folder_after_change

- name: get result of copy folder to folder remote after change
  win_find:
    paths: '{{test_win_copy_path}}\target\source'
    recurse: yes
    patterns: ['foo.txt', 'qux.txt']
  register: remote_copy_folder_to_folder_after_change_actual

- name: assert copy folder after changes
  assert:
    that:
    - remote_copy_folder_to_folder_after_change is changed
    - remote_copy_folder_to_folder_after_change_actual.matched == 2
    - remote_copy_folder_to_folder_after_change_actual.files[0].checksum == 'c79a6506c1c948be0d456ab5104d5e753ab2f3e6'
    - remote_copy_folder_to_folder_after_change_actual.files[1].checksum == 'b54ba7f5621240d403f06815f7246006ef8c7d43'

- name: clear target folder before folder contents to remote test
  win_file:
    path: '{{test_win_copy_path}}\target'
    state: absent

- name: copy folder contents to folder remote with backslash (check mode)
  win_copy:
    src: '{{test_win_copy_path}}\source\'
    dest: '{{test_win_copy_path}}\target'
    remote_src: yes
  register: remote_copy_folder_content_backslash_check
  check_mode: yes

- name: get result of copy folder contents to folder remote with backslash (check mode)
  win_stat:
    path: '{{test_win_copy_path}}\target'
  register: remote_copy_folder_content_backslash_actual_check

- name: assert copy folder content to folder remote with backslash (check mode)
  assert:
    that:
    - remote_copy_folder_content_backslash_check is changed
    - remote_copy_folder_content_backslash_actual_check.stat.exists == False

- name: copy folder contents to folder remote with backslash
  win_copy:
    src: '{{test_win_copy_path}}\source\'
    dest: '{{test_win_copy_path}}\target'
    remote_src: yes
  register: remote_copy_folder_content_backslash

- name: get result of copy folder contents to folder remote with backslash
  win_find:
    paths: '{{test_win_copy_path}}\target'
    recurse: yes
    file_type: directory
  register: remote_copy_folder_content_backslash_actual

- name: assert copy folder content to folder remote with backslash
  assert:
    that:
    - remote_copy_folder_content_backslash is changed
    - remote_copy_folder_content_backslash.operation == 'folder_copy'
    - remote_copy_folder_content_backslash_actual.examined == 10
    - remote_copy_folder_content_backslash_actual.matched == 5
    - remote_copy_folder_content_backslash_actual.files[0].filename == 'subdir'
    - remote_copy_folder_content_backslash_actual.files[1].filename == 'empty'
    - remote_copy_folder_content_backslash_actual.files[2].filename == 'subdir2'
    - remote_copy_folder_content_backslash_actual.files[3].filename == 'subdir3'
    - remote_copy_folder_content_backslash_actual.files[4].filename == 'subdir4'

- name: copy folder contents to folder remote with backslash (idempotent)
  win_copy:
    src: '{{test_win_copy_path}}\source\'
    dest: '{{test_win_copy_path}}\target'
    remote_src: yes
  register: remote_copy_folder_content_backslash_again

- name: assert copy folder content to folder remote with backslash (idempotent)
  assert:
    that:
    - remote_copy_folder_content_backslash_again is not changed

- name: change remote file after folder content to folder test
  win_copy:
    content: bar.txt
    dest: '{{test_win_copy_path}}\target\foo.txt'

- name: remote remote folder after folder content to folder test
  win_file:
    path: '{{test_win_copy_path}}\target\subdir\subdir2\subdir3\subdir4'
    state: absent

- name: copy folder content to folder remote after change
  win_copy:
    src: '{{test_win_copy_path}}/source/'
    dest: '{{test_win_copy_path}}/target/'
    remote_src: yes
  register: remote_copy_folder_content_to_folder_after_change

- name: get result of copy folder content to folder remote after change
  win_find:
    paths: '{{test_win_copy_path}}\target'
    recurse: yes
    patterns: ['foo.txt', 'qux.txt']
  register: remote_copy_folder_content_to_folder_after_change_actual

- name: assert copy folder content to folder after changes
  assert:
    that:
    - remote_copy_folder_content_to_folder_after_change is changed
    - remote_copy_folder_content_to_folder_after_change_actual.matched == 2
    - remote_copy_folder_content_to_folder_after_change_actual.files[0].checksum == 'c79a6506c1c948be0d456ab5104d5e753ab2f3e6'
    - remote_copy_folder_content_to_folder_after_change_actual.files[1].checksum == 'b54ba7f5621240d403f06815f7246006ef8c7d43'

# https://github.com/ansible/ansible/issues/50077
- name: create empty nested directory
  win_file:
    path: '{{ test_win_copy_path }}\source\empty-nested\nested-dir'
    state: directory

- name: copy empty nested directory (check mode)
  win_copy:
    src: '{{ test_win_copy_path }}\source\empty-nested'
    dest: '{{ test_win_copy_path }}\target'
    remote_src: True
  check_mode: True
  register: copy_empty_dir_check

- name: get result of copy empty nested directory (check mode)
  win_stat:
    path: '{{ test_win_copy_path }}\target\empty-nested'
  register: copy_empty_dir_actual_check

- name: assert copy empty nested directory (check mode)
  assert:
    that:
    - copy_empty_dir_check is changed
    - copy_empty_dir_check.operation == "folder_copy"
    - not copy_empty_dir_actual_check.stat.exists

- name: copy empty nested directory
  win_copy:
    src: '{{ test_win_copy_path }}\source\empty-nested'
    dest: '{{ test_win_copy_path }}\target'
    remote_src: True
  register: copy_empty_dir

- name: get result of copy empty nested directory
  win_stat:
    path: '{{ test_win_copy_path }}\target\empty-nested\nested-dir'
  register: copy_empty_dir_actual

- name: assert copy empty nested directory
  assert:
    that:
    - copy_empty_dir is changed
    - copy_empty_dir.operation == "folder_copy"
    - copy_empty_dir_actual.stat.exists

- name: copy empty nested directory (idempotent)
  win_copy:
    src: '{{ test_win_copy_path }}\source\empty-nested'
    dest: '{{ test_win_copy_path }}\target'
    remote_src: True
  register: copy_empty_dir_again

- name: assert copy empty nested directory (idempotent)
  assert:
    that:
    - not copy_empty_dir_again is changed
