From 3dbce15ccb79e068d8449cc327bea32e73aefe7b Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Thu, 2 Feb 2017 10:29:56 +0100 Subject: [PATCH] win_shortcut: Add missing $check_mode definition + bugfix + tests (#20911) * win_shortcut: Add missing $check_mode definition For some reason this entry was missing, possible a merge-conflict gone wrong :-( * Added integration tests and bugfix Add missing changes. --- lib/ansible/modules/windows/win_shortcut.ps1 | 8 + test/integration/targets/win_shortcut/aliases | 1 + .../targets/win_shortcut/tasks/main.yml | 157 ++++++++++++++++++ test/integration/test_win_group2.yml | 1 + 4 files changed, 167 insertions(+) create mode 100644 test/integration/targets/win_shortcut/aliases create mode 100644 test/integration/targets/win_shortcut/tasks/main.yml diff --git a/lib/ansible/modules/windows/win_shortcut.ps1 b/lib/ansible/modules/windows/win_shortcut.ps1 index 0180f3a3478..a46b31e1116 100644 --- a/lib/ansible/modules/windows/win_shortcut.ps1 +++ b/lib/ansible/modules/windows/win_shortcut.ps1 @@ -24,6 +24,7 @@ $ErrorActionPreference = "Stop" $params = Parse-Args $args -supports_check_mode $true +$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false $src = Get-AnsibleParam -obj $params -name "src" -type "path" -default $null $dest = Get-AnsibleParam -obj $params -name "dest" -type "path" -failifempty $true @@ -78,6 +79,13 @@ If ($state -eq "absent") { # Compare existing values with new values, report as changed if required + If ($src -ne $null) { + # Windows translates executables to absolute path, so do we + If (Get-Command -Name $src -Type Application -ErrorAction SilentlyContinue) { + $src = (Get-Command -Name $src -Type Application).Definition + } + } + If ($src -ne $null -and $ShortCut.TargetPath -ne $src) { $result.changed = $true $ShortCut.TargetPath = $src diff --git a/test/integration/targets/win_shortcut/aliases b/test/integration/targets/win_shortcut/aliases new file mode 100644 index 00000000000..1ab64312711 --- /dev/null +++ b/test/integration/targets/win_shortcut/aliases @@ -0,0 +1 @@ +windows/ci/group2 \ No newline at end of file diff --git a/test/integration/targets/win_shortcut/tasks/main.yml b/test/integration/targets/win_shortcut/tasks/main.yml new file mode 100644 index 00000000000..26f6cd8f717 --- /dev/null +++ b/test/integration/targets/win_shortcut/tasks/main.yml @@ -0,0 +1,157 @@ +- name: Clean up Ansible website link + win_file: + path: '%UserProfile%\Desktop\Ansible website.url' + state: absent + +- name: Add Ansible website link on the desktop + win_shortcut: + src: 'https://ansible.com/' + dest: '%UserProfile%\Desktop\Ansible website.url' + state: present + register: ansible_website_link_add + +- name: Check there was a change + assert: + that: + - ansible_website_link_add.changed == true + +- name: Add Ansible website link on the desktop again + win_shortcut: + src: 'https://ansible.com/' + dest: '%UserProfile%\Desktop\Ansible website.url' + state: present + register: ansible_website_link_add_again + +- name: Check there was no change + assert: + that: + - ansible_website_link_add_again.changed == false + +- name: Remove link + win_shortcut: + dest: '%UserProfile%\Desktop\Ansible website.url' + state: absent + register: ansible_website_link_remove + +- name: Check there was a change + assert: + that: + - ansible_website_link_remove.changed == true + +- name: Remove link again + win_shortcut: + dest: '%UserProfile%\Desktop\Ansible website.url' + state: absent + register: ansible_website_link_remove_again + +- name: Check there was no change + assert: + that: + - ansible_website_link_remove_again.changed == false + +- name: Clean up Registry Editor shortcut + win_file: + path: '%Public%\Desktop\Registry Editor.lnk' + state: absent + +- name: Add a regedit shortcut on the desktop + win_shortcut: + description: "Registry Editor" + src: regedit.exe + dest: '%Public%\Desktop\Registry Editor.lnk' + state: present + register: regedit_shortcut_add + +- name: Check there was a change + assert: + that: + - regedit_shortcut_add.changed == true + +- name: Add a regedit shortcut on the desktop again + win_shortcut: + description: "Registry Editor" + src: regedit.exe + dest: '%Public%\Desktop\Registry Editor.lnk' + state: present + register: regedit_shortcut_add_again + +- name: Check there was no change + assert: + that: + - regedit_shortcut_add_again.changed == false + +- name: Update a regedit shortcut on the desktop + win_shortcut: + description: "Registry Editor" + src: C:\BogusPath\regedit.exe + dest: '%Public%\Desktop\Registry Editor.lnk' + state: present + register: regedit_shortcut_update + +- name: Check there was a change + assert: + that: + - regedit_shortcut_update.changed == true + +- name: Update a regedit shortcut on the desktop again + win_shortcut: + description: "Registry Editor" + src: C:\BogusPath\regedit.exe + dest: '%Public%\Desktop\Registry Editor.lnk' + state: present + register: regedit_shortcut_update_again + +- name: Check there was no change + assert: + that: + - regedit_shortcut_update_again.changed == false + +- name: Add an (explicit) icon + win_shortcut: + description: "Registry Editor" + src: C:\Windows\regedit.exe + dest: '%Public%\Desktop\Registry Editor.lnk' + icon: 'C:\Windows\regedit.exe,0' + state: present + register: regedit_shortcut_add_icon + +- name: Check there was a change + assert: + that: + - regedit_shortcut_add_icon.changed == true + +- name: Add an (explicit) icon again + win_shortcut: + description: "Registry Editor" + src: C:\Windows\regedit.exe + dest: '%Public%\Desktop\Registry Editor.lnk' + icon: 'C:\Windows\regedit.exe,0' + state: present + register: regedit_shortcut_add_icon_again + +- name: Check there was no change + assert: + that: + - regedit_shortcut_add_icon_again.changed == false + +- name: Remove shortcut + win_shortcut: + dest: '%Public%\Desktop\Registry Editor.lnk' + state: absent + register: regedit_shortcut_remove + +- name: Check there was a change + assert: + that: + - regedit_shortcut_remove.changed == true + +- name: Remove shortcut again + win_shortcut: + dest: '%Public%\Desktop\Registry Editor.lnk' + state: absent + register: regedit_shortcut_remove_again + +- name: Check there was no change + assert: + that: + - regedit_shortcut_remove_again.changed == false \ No newline at end of file diff --git a/test/integration/test_win_group2.yml b/test/integration/test_win_group2.yml index 94724ad0730..27765ea5995 100644 --- a/test/integration/test_win_group2.yml +++ b/test/integration/test_win_group2.yml @@ -11,3 +11,4 @@ - { role: win_msi, tags: test_win_msi } - { role: win_package, tags: test_win_package } - { role: win_path, tags: test_win_path } + - { role: win_shortcut, tags: test_win_shortcut }