---
- name: raise failure when state=present and name=all
  win_chocolatey:
    name: all
    state: present
  register: fail_all_present
  failed_when: fail_all_present.msg != "Cannot specify the package name as 'all' when state=present"

- name: raise failure when state=reinstalled and name=all
  win_chocolatey:
    name: all
    state: reinstalled
  register: fail_all_reinstalled
  failed_when: fail_all_reinstalled.msg != "Cannot specify the package name as 'all' when state=reinstalled"

- name: install package (check mode)
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: present
  check_mode: yes
  register: install_check

- name: get result of install package (check mode)
  win_command: choco.exe list --local-only --exact --limit-output {{ test_choco_package1|quote }}
  register: install_actual_check
  failed_when: not install_actual_check.rc in [0, 2]  # v0.10.12+ returns 2 for no package

- name: assert install package (check mode)
  assert:
    that:
    - install_check is changed
    - install_actual_check.stdout_lines == []

- name: install package
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: present
  register: install

- name: get result of install package
  win_command: choco.exe list --local-only --exact --limit-output {{ test_choco_package1|quote }}
  register: install_actual

- name: get package info of install package
  win_shell: Get-Content -Path '{{ test_choco_path }}\{{ test_choco_package1 }}-0.1.0.txt' -Raw
  register: install_actual_info

- name: assert install package
  assert:
    that:
    - install is changed
    - install_actual.stdout_lines == [test_choco_package1 + "|0.1.0"]
    - (install_actual_info.stdout|from_json).allow_empty_checksums == False
    - (install_actual_info.stdout|from_json).force == False
    - (install_actual_info.stdout|from_json).force_x86 == False
    - (install_actual_info.stdout|from_json).ignore_checksums == False
    - (install_actual_info.stdout|from_json).install_args == None
    - (install_actual_info.stdout|from_json).package_params == {}
    - (install_actual_info.stdout|from_json).proxy_url == None
    - (install_actual_info.stdout|from_json).source == "normal"
    - (install_actual_info.stdout|from_json).timeout == "2700000"

- name: install package (idempotent)
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: present
  register: install_again

- name: assert install package (idempotent)
  assert:
    that:
    - not install_again is changed

- name: remove package (check mode)
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: absent
  check_mode: yes
  register: remove_check

- name: get result of remove package (check mode)
  win_command: choco.exe list --local-only --exact --limit-output {{ test_choco_package1|quote }}
  register: remove_actual_check

- name: assert remove package (check mode)
  assert:
    that:
    - remove_check is changed
    - remove_actual_check.stdout_lines == [test_choco_package1 + "|0.1.0"]

- name: remove package
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: absent
  register: remove

- name: get result of remove package
  win_command: choco.exe list --local-only --exact --limit-output {{ test_choco_package1|quote }}
  register: remove_actual
  failed_when: not remove_actual.rc in [0, 2]

- name: check if removed package file still exists
  win_stat:
    path: '{{ test_choco_path }}\{{ test_choco_package1 }}-0.1.0.txt'
  register: remove_actual_info

- name: assert remove package
  assert:
    that:
    - remove is changed
    - remove_actual.stdout_lines == []
    - remove_actual_info.stat.exists == False

- name: remove package (idempotent)
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: absent
  register: remove_again

- name: assert remove_package (idempotent)
  assert:
    that:
    - not remove_again is changed

- name: install multiple packages with timeout
  win_chocolatey:
    name: '{{ test_choco_packages }}'
    state: present
    timeout: 1000
  register: install_multiple

- name: get list of installed packages with timeout
  win_command: choco.exe list --local-only --limit-output ansible
  register: install_multiple_actual

- name: get info on package 1
  win_shell: Get-Content -Path '{{ test_choco_path }}\{{ test_choco_package1 }}-0.1.0.txt' -Raw
  register: install_multiple_package1

- name: get info on package 2
  win_shell: Get-Content -Path '{{ test_choco_path }}\{{ test_choco_package2 }}-1.0.0.txt' -Raw
  register: install_multiple_package2

- name: assert install multiple packages with timeout
  assert:
    that:
    - install_multiple is changed
    - install_multiple_actual.stdout_lines == [test_choco_package1 + "|0.1.0", test_choco_package2 + "|1.0.0"]
    - (install_multiple_package1.stdout|from_json).allow_empty_checksums == False
    - (install_multiple_package1.stdout|from_json).force == False
    - (install_multiple_package1.stdout|from_json).force_x86 == False
    - (install_multiple_package1.stdout|from_json).ignore_checksums == False
    - (install_multiple_package1.stdout|from_json).install_args == None
    - (install_multiple_package1.stdout|from_json).package_params == {}
    - (install_multiple_package1.stdout|from_json).proxy_url == None
    - (install_multiple_package1.stdout|from_json).source == "normal"
    - (install_multiple_package1.stdout|from_json).timeout == "1000000"
    - (install_multiple_package2.stdout|from_json).allow_empty_checksums == False
    - (install_multiple_package2.stdout|from_json).force == False
    - (install_multiple_package2.stdout|from_json).force_x86 == False
    - (install_multiple_package2.stdout|from_json).ignore_checksums == False
    - (install_multiple_package2.stdout|from_json).install_args == None
    - (install_multiple_package2.stdout|from_json).package_params == {}
    - (install_multiple_package2.stdout|from_json).proxy_url == None
    - (install_multiple_package2.stdout|from_json).source == "normal"
    - (install_multiple_package2.stdout|from_json).timeout == "1000000"

- name: install multiple packages (idempotent)
  win_chocolatey:
    name: '{{ test_choco_packages }}'
    state: present
  register: install_multiple_again

- name: assert install multiple packages (idempotent)
  assert:
    that:
    - not install_multiple_again is changed

- name: remove multiple packages
  win_chocolatey:
    name: '{{ test_choco_packages }}'
    state: absent
  register: remove_multiple

- name: get list of installed packages after removal
  win_command: choco.exe list --local-only --limit-output ansible
  register: remove_multiple_actual
  failed_when: not remove_multiple_actual.rc in [0, 2]

- name: get info on package 1
  win_stat:
    path: '{{ test_choco_path }}\{{ test_choco_package1 }}-0.1.0.txt'
  register: remove_multiple_package1

- name: get info on package 2
  win_stat:
    path: '{{ test_choco_path }}\{{ test_choco_package2 }}-1.0.0.txt'
  register: remove_multiple_package2

- name: assert remove multiple packages
  assert:
    that:
    - remove_multiple is changed
    - remove_multiple_actual.stdout_lines == []
    - remove_multiple_package1.stat.exists == False
    - remove_multiple_package2.stat.exists == False

- name: remove multiple packages (idempotent)
  win_chocolatey:
    name: '{{ test_choco_packages }}'
    state: absent
  register: remove_multiple_again

- name: assert remove multiple packages (idempotent)
  assert:
    that:
    - not remove_multiple_again is changed

- name: install package with params
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: present
    install_args: /install_arg 1 /install_arg 2
    package_params: /param1 /param2:value
    allow_empty_checksums: yes
    architecture: x86
    force: yes
    ignore_checksums: yes
    proxy_url: http://proxyhost
    version: 0.0.1
  register: install_params

- name: get result of install package with params
  win_command: choco.exe list --local-only --limit-output --exact {{ test_choco_package1|quote }}
  register: install_params_actual

- name: get info of install package with params
  win_shell: Get-Content -Path '{{ test_choco_path }}\{{ test_choco_package1 }}-0.0.1.txt'
  register: install_params_info

- name: assert install package with params
  assert:
    that:
    - install_params is changed
    - install_params_actual.stdout_lines == [test_choco_package1 + "|0.0.1"]
    - (install_params_info.stdout|from_json).allow_empty_checksums == True
    - (install_params_info.stdout|from_json).force == True
    - (install_params_info.stdout|from_json).force_x86 == True
    - (install_params_info.stdout|from_json).ignore_checksums == True
    - (install_params_info.stdout|from_json).install_args == "/install_arg 1 /install_arg 2"
    - (install_params_info.stdout|from_json).package_params.keys()|count == 2
    - (install_params_info.stdout|from_json).package_params.param1 == True
    - (install_params_info.stdout|from_json).package_params.param2 == "value"
    - (install_params_info.stdout|from_json).proxy_url == "http://proxyhost"
    - (install_params_info.stdout|from_json).source == "normal"
    - (install_params_info.stdout|from_json).timeout == "2700000"

- name: install package with version (idempotent)
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: present
    version: 0.0.1
  register: install_with_version

- name: assert install package with version (idempotent)
  assert:
    that:
    - not install_with_version is changed

- name: fail to install side by side package
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: present
    version: 0.1.0
  register: fail_multiple_versions
  failed_when: fail_multiple_versions.msg != "Chocolatey package '" + test_choco_package1 + "' is already installed with version(s) '0.0.1' but was expecting '0.1.0'. Either change the expected version, set state=latest, set allow_multiple=yes, or set force=yes to continue"

- name: force the upgrade of an existing version
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: present
    version: 0.1.0
    force: yes
  register: force_different_version

- name: get result of force the upgrade of an existing version
  win_command: choco.exe list --local-only --limit-output --exact {{ test_choco_package1|quote }}
  register: force_different_version_actual

- name: get result of forced package install file
  win_stat:
    path: '{{ test_choco_path }}\{{ test_choco_package1 }}-0.1.0.txt'
  register: force_different_version_info

- name: assert force the upgrade of an existing version
  assert:
    that:
    - force_different_version is changed
    - force_different_version_actual.stdout_lines == [test_choco_package1 + "|0.1.0"]
    - force_different_version_info.stat.exists

- name: remove package after force clobbered everything
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: absent
  ignore_errors: yes  # the mock package created doesn't really handle force well

- name: install package with reference to source name
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: present
    source: ansible-test-override
  register: install_source_name

- name: get result of install package with reference to source name
  win_command: choco.exe list --local-only --limit-output --exact {{ test_choco_package1|quote }}
  register: install_source_name_actual

- name: get result fo installed package with reference to source name info
  win_shell: Get-Content -Path '{{ test_choco_path }}\{{ test_choco_package1 }}-0.1.0.txt' -Raw
  register: install_source_name_info

- name: assert install package with reference to source name
  assert:
    that:
    - install_source_name is changed
    - install_source_name_actual.stdout_lines == [test_choco_package1 + "|0.1.0"]
    - (install_source_name_info.stdout|from_json).source == "override"

- name: reinstall package without source override
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: reinstalled
  register: reinstalled_package

- name: get result of reinstalled package without source override
  win_shell: Get-Content -Path '{{ test_choco_path }}\{{ test_choco_package1 }}-0.1.0.txt' -Raw
  register: reinstalled_package_info

- name: assert reinstall package without source override
  assert:
    that:
    - reinstalled_package is changed
    - (reinstalled_package_info.stdout|from_json).source == "normal"

- name: downgrade package
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: downgrade
    version: 0.0.1
  register: downgraded_package

- name: get result of downgrade package
  win_command: choco.exe list --local-only --limit-output --exact {{ test_choco_package1|quote }}
  register: downgraded_package_actual

- name: assert downgrade package
  assert:
    that:
    - downgraded_package is changed
    - downgraded_package_actual.stdout_lines == [test_choco_package1 + "|0.0.1"]

- name: downgrade package (idempotent)
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: downgrade
    version: 0.0.1
  register: downgraded_package_again

- name: assert downgrade package (idempotent)
  assert:
    that:
    - not downgraded_package_again is changed

- name: downgrade package without version specified
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: downgrade
  register: downgrade_without_version

- name: get result of downgrade without version
  win_command: choco.exe list --local-only --limit-output --exact {{ test_choco_package1|quote }}
  register: downgrade_without_version_actual

- name: assert downgrade package without version specified
  assert:
    that:
    - not downgrade_without_version is changed
    - downgrade_without_version_actual.stdout_lines == [test_choco_package1 + "|0.0.1"]

- name: upgrade package
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: latest
  register: upgrade_package

- name: get result of upgrade package
  win_command: choco.exe list --local-only --limit-output --exact {{ test_choco_package1|quote }}
  register: upgrade_package_actual

- name: assert upgrade package
  assert:
    that:
    - upgrade_package is changed
    - upgrade_package_actual.stdout_lines == [test_choco_package1 + "|0.1.0"]

- name: upgrade package (idempotent)
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: latest
  register: upgrade_package_again

- name: assert upgrade package (idempotent)
  assert:
    that:
    - not upgrade_package_again is changed

- name: install prerelease package
  win_chocolatey:
    name: '{{ test_choco_package2 }}'
    state: present
    allow_prerelease: yes
  register: install_prerelease

- name: get result of install prerelease package
  win_command: choco.exe list --local-only --limit-output --exact {{ test_choco_package2|quote }}
  register: install_prerelease_actual

- name: assert install prerelease package
  assert:
    that:
    - install_prerelease is changed
    - install_prerelease_actual.stdout_lines == [test_choco_package2 + "|1.0.1-beta1"]

- name: downgrade package
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: downgrade
    version: 0.0.1

- name: upgrade all packages
  win_chocolatey:
    name: all
    state: latest
  register: all_latest

- name: get result of upgrade all packages
  win_command: choco.exe list --local-only --limit-output --exact {{ test_choco_package1|quote }}
  register: all_latest_actual

- name: assert upgrade all packages
  assert:
    that:
    - all_latest is changed
    - all_latest_actual.stdout_lines == [test_choco_package1 + "|0.1.0"]

- name: install newer version of package
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: present

- name: install older package with allow_multiple
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: present
    allow_multiple: True
    version: '0.0.1'
  register: allow_multiple

- name: get result of install older package with allow_multiple
  win_command: choco.exe list --local-only --limit-output --all-versions
  register: allow_multiple_actual

- name: assert install older package with allow_multiple
  assert:
    that:
    - allow_multiple is changed
    - '"ansible|0.1.0" in allow_multiple_actual.stdout_lines'
    - '"ansible|0.0.1" in allow_multiple_actual.stdout_lines'

- name: pin 2 packages (check mode)
  win_chocolatey:
    name:
    - '{{ test_choco_package1 }}'
    - '{{ test_choco_package2 }}'
    state: present
    pinned: yes
  register: pin_multiple_check
  check_mode: True

- name: get result of pin 2 packages (check mode)
  win_command: choco.exe pin list --limit-output
  register: pin_multiple_actual_check

- name: assert pin 2 packages (check mode)
  assert:
    that:
    - pin_multiple_check is changed
    - pin_multiple_actual_check.stdout == ""

- name: pin 2 packages
  win_chocolatey:
    name:
    - '{{ test_choco_package1 }}'
    - '{{ test_choco_package2 }}'
    state: present
    pinned: yes
  register: pin_multiple

- name: get result of pin 2 packages
  win_command: choco.exe pin list --limit-output
  register: pin_multiple_actual

- name: assert pin 2 packages
  assert:
    that:
    - pin_multiple is changed
    - pin_multiple_actual.stdout_lines == ["ansible|0.1.0", "ansible-test|1.0.1-beta1"]

- name: pin 2 packages (idempotent)
  win_chocolatey:
    name:
    - '{{ test_choco_package1 }}'
    - '{{ test_choco_package2 }}'
    state: present
    pinned: yes
  register: pin_multiple_again

- name: assert pin 2 packages (idempoent)
  assert:
    that:
    - not pin_multiple_again is changed

- name: pin specific older version
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: present
    pinned: yes
    version: '0.0.1'
  register: pin_older

- name: get result of pin specific older version
  win_command: choco.exe pin list --limit-output
  register: pin_older_actual

- name: assert pin specific older version
  assert:
    that:
    - pin_older is changed
    - pin_older_actual.stdout_lines == ["ansible|0.1.0", "ansible|0.0.1", "ansible-test|1.0.1-beta1"]

- name: unpin package at version
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: present
    pinned: no
    version: '0.1.0'
  register: unpin_version

- name: get result of unpin package at version
  win_command: choco.exe pin list --limit-output
  register: unpin_version_actual

- name: assert unpin package at version
  assert:
    that:
    - unpin_version is changed
    - unpin_version_actual.stdout_lines == ["ansible|0.0.1", "ansible-test|1.0.1-beta1"]

- name: unpin multiple packages without a version
  win_chocolatey:
    name:
    - '{{ test_choco_package1 }}'
    - '{{ test_choco_package2 }}'
    state: present
    pinned: no
  register: unpin_multiple

- name: get result of unpin multiple packages without a version
  win_command: choco.exe pin list --limit-output
  register: unpin_multiple_actual

- name: assert unpin multiple packages without a version
  assert:
    that:
    - unpin_multiple is changed
    - unpin_multiple_actual.stdout == ""

- name: uninstall specific version installed with allow_multiple
  win_chocolatey:
    name: '{{ test_choco_package1 }}'
    state: absent
    version: '0.0.1'
  register: remove_multiple

- name: get result of uninstall specific version installed with allow_multiple
  win_command: choco.exe list --local-only --limit-output --all-versions
  register: remove_multiple_actual

- name: assert uninstall specific version installed with allow_multiple
  assert:
    that:
    - remove_multiple is changed
    - '"ansible|0.0.1" not in remove_multiple_actual.stdout_lines'
    - '"ansible|0.1.0" in remove_multiple_actual.stdout_lines'
