From 0a15ab7ce9fe14d7df55997c78d17c06656b75a2 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Wed, 28 Jun 2017 21:28:44 +0100 Subject: [PATCH] win_say: Fix issue, add integration tests (#26089) * win_say: Fix issue, add integration test This PR includes: - Make speed_speech an integer parameter - Test for empty parameters too - Add integration tests * Improve the $speech_speed parameter handling As requested --- lib/ansible/modules/windows/win_say.ps1 | 40 +++++------ lib/ansible/modules/windows/win_say.py | 67 +++++++++---------- test/integration/targets/win_say/aliases | 1 + .../targets/win_say/tasks/main.yml | 36 ++++++++++ 4 files changed, 85 insertions(+), 59 deletions(-) create mode 100644 test/integration/targets/win_say/aliases create mode 100644 test/integration/targets/win_say/tasks/main.yml diff --git a/lib/ansible/modules/windows/win_say.ps1 b/lib/ansible/modules/windows/win_say.ps1 index 2c33408524a..6cb8a13418c 100644 --- a/lib/ansible/modules/windows/win_say.ps1 +++ b/lib/ansible/modules/windows/win_say.ps1 @@ -27,44 +27,36 @@ $msg_file = Get-AnsibleParam -obj $params -name "msg_file" -type "path" $start_sound_path = Get-AnsibleParam -obj $params -name "start_sound_path" -type "path" $end_sound_path = Get-AnsibleParam -obj $params -name "end_sound_path" -type "path" $voice = Get-AnsibleParam -obj $params -name "voice" -type "str" -$speech_speed = Get-AnsibleParam -obj $params -name "speech_speed" -type "str" +$speech_speed = Get-AnsibleParam -obj $params -name "speech_speed" -type "int" -default 0 $result = @{ changed = $false } -$speed = 0 $words = $null -if ($speech_speed -ne $null) { - try { - $speed = [convert]::ToInt32($speech_speed, 10) - } catch { - Fail-Json $result "speech_speed needs to a integer in the range -10 to 10. The value $speech_speed could not be converted to an integer." - } - if ($speed -lt -10 -or $speed -gt 10) { - Fail-Json $result "speech_speed needs to a integer in the range -10 to 10. The value $speech_speed is outside this range." - } +f ($speech_speed -lt -10 -or $speech_speed -gt 10) { + Fail-Json $result "speech_speed needs to a integer in the range -10 to 10. The value $speech_speed is outside this range." } -if ($msg_file -ne $null -and $msg -ne $null) { +if ($msg_file -and $msg) { Fail-Json $result "Please specify either msg_file or msg parameters, not both" } -if ($msg_file -eq $null -and $msg -eq $null -and $start_sound_path -eq $null -and $end_sound_path -eq $null) { +if (-not $msg_file -and -not $msg -and -not $start_sound_path -and -not $end_sound_path) { Fail-Json $result "No msg_file, msg, start_sound_path, or end_sound_path parameters have been specified. Please specify at least one so the module has something to do" } -if ($msg_file -ne $null) { - if (Test-Path $msg_file) { +if ($msg_file) { + if (Test-Path -Path $msg_file) { $words = Get-Content $msg_file | Out-String } else { Fail-Json $result "Message file $msg_file could not be found or opened. Ensure you have specified the full path to the file, and the ansible windows user has permission to read the file." } } -if ($start_sound_path -ne $null) { - if (Test-Path $start_sound_path) { +if ($start_sound_path) { + if (Test-Path -Path $start_sound_path) { if (-not $check_mode) { (new-object Media.SoundPlayer $start_sound_path).playSync() } @@ -73,14 +65,14 @@ if ($start_sound_path -ne $null) { } } -if ($msg -ne $null) { +if ($msg) { $words = $msg } -if ($words -ne $null) { +if ($words) { Add-Type -AssemblyName System.speech $tts = New-Object System.Speech.Synthesis.SpeechSynthesizer - if ($voice -ne $null) { + if ($voice) { try { $tts.SelectVoice($voice) } catch [System.Management.Automation.MethodInvocationException] { @@ -89,8 +81,8 @@ if ($words -ne $null) { } $result.voice = $tts.Voice.Name - if ($speed -ne 0) { - $tts.Rate = $speed + if ($speech_speed -ne 0) { + $tts.Rate = $speech_speed } if (-not $check_mode) { $tts.Speak($words) @@ -98,8 +90,8 @@ if ($words -ne $null) { $tts.Dispose() } -if ($end_sound_path -ne $null) { - if (Test-Path $end_sound_path) { +if ($end_sound_path) { + if (Test-Path -Path $end_sound_path) { if (-not $check_mode) { (new-object Media.SoundPlayer $end_sound_path).playSync() } diff --git a/lib/ansible/modules/windows/win_say.py b/lib/ansible/modules/windows/win_say.py index c9c765e6342..191bd0e953a 100644 --- a/lib/ansible/modules/windows/win_say.py +++ b/lib/ansible/modules/windows/win_say.py @@ -25,7 +25,6 @@ ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['preview'], 'supported_by': 'community'} - DOCUMENTATION = r''' --- module: win_say @@ -37,75 +36,73 @@ description: options: msg: description: - - The text to be spoken. Use either msg or msg_file. Optional so that you can use this module just to play sounds. - required: false - default: none + - The text to be spoken. + - Use either C(msg) or C(msg_file). + - Optional so that you can use this module just to play sounds. msg_file: description: - - Full path to a windows format text file containing the text to be spokend. Use either msg or msg_file. Optional so that you can use this module - just to play sounds. - required: false - default: none + - Full path to a windows format text file containing the text to be spokend. + - Use either C(msg) or C(msg_file). + - Optional so that you can use this module just to play sounds. voice: description: - - Which voice to use. See notes for how to discover installed voices. If the requested voice is not available the default voice will be used. - Example voice names from Windows 10 are 'Microsoft Zira Desktop' and 'Microsoft Hazel Desktop'. - required: false + - Which voice to use. See notes for how to discover installed voices. + - If the requested voice is not available the default voice will be used. + Example voice names from Windows 10 are C(Microsoft Zira Desktop) and C(Microsoft Hazel Desktop). default: system default voice speech_speed: description: - - How fast or slow to speak the text. Must be an integer value in the range -10 to 10. -10 is slowest, 10 is fastest. - required: false + - How fast or slow to speak the text. + - Must be an integer value in the range -10 to 10. + - -10 is slowest, 10 is fastest. default: 0 start_sound_path: description: - - Full path to a C(.wav) file containing a sound to play before the text is spoken. Useful on conference calls to alert other speakers that ansible - has something to say. - required: false - default: null + - Full path to a C(.wav) file containing a sound to play before the text is spoken. + - Useful on conference calls to alert other speakers that ansible has something to say. end_sound_path: description: - - Full path to a C(.wav) file containing a sound to play after the text has been spoken. Useful on conference calls to alert other speakers that - ansible has finished speaking. - required: false - default: null -author: "Jon Hawkesworth (@jhawkesworth)" + - Full path to a C(.wav) file containing a sound to play after the text has been spoken. + - Useful on conference calls to alert other speakers that ansible has finished speaking. +author: +- Jon Hawkesworth (@jhawkesworth) notes: - Needs speakers or headphones to do anything useful. - | - To find which voices are installed, run the following powershell commands. + To find which voices are installed, run the following Powershell commands. Add-Type -AssemblyName System.Speech - $speech = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer - $speech.GetInstalledVoices() | ForEach-Object { $_.VoiceInfo } - $speech.Dispose() - - Speech can be surprisingly slow, so its best to keep message text short. + - Speech can be surprisingly slow, so it's best to keep message text short. ''' EXAMPLES = r''' - # Warn of impending deployment -- win_say: +- name: Warn of impending deployment + win_say: msg: Warning, deployment commencing in 5 minutes, please log out. - # Using a different voice and a start sound -- win_say: + +- name: Using a different voice and a start sound + win_say: start_sound_path: C:\Windows\Media\ding.wav msg: Warning, deployment commencing in 5 minutes, please log out. voice: Microsoft Hazel Desktop - # example with start and end sound -- win_say: + +- name: With start and end sound + win_say: start_sound_path: C:\Windows\Media\Windows Balloon.wav msg: New software installed end_sound_path: C:\Windows\Media\chimes.wav - # text from file example -- win_say: + +- name: Text from file example + win_say: start_sound_path: C:\Windows\Media\Windows Balloon.wav msg_file: AppData\Local\Temp\morning_report.txt end_sound_path: C:\Windows\Media\chimes.wav ''' + RETURN = r''' message_text: description: the text that the module attempted to speak diff --git a/test/integration/targets/win_say/aliases b/test/integration/targets/win_say/aliases new file mode 100644 index 00000000000..c6d61981670 --- /dev/null +++ b/test/integration/targets/win_say/aliases @@ -0,0 +1 @@ +windows/ci/group3 diff --git a/test/integration/targets/win_say/tasks/main.yml b/test/integration/targets/win_say/tasks/main.yml new file mode 100644 index 00000000000..60448723a8c --- /dev/null +++ b/test/integration/targets/win_say/tasks/main.yml @@ -0,0 +1,36 @@ +- name: Warn of impending deployment + win_say: + msg: Warning, deployment commencing in 5 minutes, please log out. + +- name: Using a different voice and a start sound + win_say: + msg: Warning, deployment commencing in 5 minutes, please log out. + start_sound_path: C:\Windows\Media\ding.wav + voice: Microsoft Hazel Desktop + +- name: Example with start and end sound + win_say: + msg: New software installed + start_sound_path: C:\Windows\Media\Windows Balloon.wav + end_sound_path: C:\Windows\Media\chimes.wav + +- name: Create message file + win_copy: + content: Stay calm and carry on + dest: C:\Windows\Temp\win_say_message.txt + +- name: Text from file example + win_say: + msg_file: C:\Windows\Temp\win_say_message.txt + start_sound_path: C:\Windows\Media\Windows Balloon.wav + end_sound_path: C:\Windows\Media\chimes.wav + +- name: Remove message file + win_file: + path: C:\Windows\Temp\win_say_message.txt + state: absent + +- name: Different spech peed + win_say: + speech_speed: 5 + msg: Stay calm and proceed to the closest fire exit.