Ansible.Basic - Fix required_if check (#84562) (#84582)

Fixes the Ansible.Basic `required_if` check when the option to check is
either unset or explicitly set to null.

(cherry picked from commit 8c5e33cd3a)
pull/84583/head
Jordan Borean 11 months ago committed by GitHub
parent 5f5e5d321b
commit ee0084dbcf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,3 @@
bugfixes:
- >-
Ansible.Basic - Fix ``required_if`` check when the option value to check is unset or set to null.

@ -1210,7 +1210,7 @@ namespace Ansible.Basic
object val = requiredCheck[1];
IList requirements = (IList)requiredCheck[2];
if (ParseStr(param[key]) != ParseStr(val))
if (param[key] == null || ParseStr(param[key]) != ParseStr(val))
continue;
string term = "all";

@ -3054,6 +3054,34 @@ test_no_log - Invoked with:
$actual.invocation | Assert-DictionaryEqual -Expected @{module_args = $complex_args }
}
"Required if for unset option" = {
$spec = @{
options = @{
state = @{ choices = "absent", "present" }
name = @{}
path = @{}
}
required_if = @(, @("state", "absent", @("name", "path")))
}
Set-Variable -Name complex_args -Scope Global -Value @{}
$m = [Ansible.Basic.AnsibleModule]::Create(@(), $spec)
$failed = $false
try {
$m.ExitJson()
}
catch [System.Management.Automation.RuntimeException] {
$failed = $true
$_.Exception.Message | Assert-Equal -Expected "exit: 0"
$actual = [Ansible.Basic.AnsibleModule]::FromJson($_.Exception.InnerException.Output)
}
$failed | Assert-Equal -Expected $true
$actual.Keys.Count | Assert-Equal -Expected 2
$actual.changed | Assert-Equal -Expected $false
$actual.invocation | Assert-DictionaryEqual -Expected @{ module_args = $complex_args }
}
"PS Object in return result" = {
$m = [Ansible.Basic.AnsibleModule]::Create(@(), @{})

Loading…
Cancel
Save