Closed jordanbreen28 closed 10 months ago
Merging #421 (a7bf869) into main (9566942) will increase coverage by
0%
. Report is 1 commits behind head on main. The diff coverage is100%
.
no additional test coverage is required, unsure why code coverage has failed.
It failed because it seems there a no current tests for the code path for which a line was modified in this change: https://app.codecov.io/gh/dsccommunity/ComputerManagementDsc/pull/421/blob/source/DSCResources/DSC_ScheduledTask/DSC_ScheduledTask.psm1#L672 So the check that failed, it reports that there were not at least 90% coverage of all the changed lines.
Suggest adding a test the verifies that the output is exactly AtLogon
(or the same as the schema) to make sure that there are no regressions.
hey @johlju thanks for reviewing.
I'll try to get something in, but I am very unfamiliar with DSC.
Looks to me like there is already a check to ensure that the returned value is equal to AtLogon
here https://github.com/dsccommunity/ComputerManagementDsc/blob/main/tests/Unit/DSC_ScheduledTask.Tests.ps1#L2206.
Yes, my bad. The code I referenced was in the Set-function. Get-function is fully covered.
But that line you referenced I think it should use BeExactly
(or use regex matching) to make sure it does a case-sensitive tests of the string? The test should fail if Get-function would return the wrong casing. π€
@johlju I've added a test now. it may very well be horrible so open to suggested improvements!
Ah I give up with the code coverage.. Can't see what I'm missing here
The new tests you added does not call any of the functions Get-TargetResource, Test-TargetResource or Set-TargetResource, so the test doesn't actually test anything in the resource so the code coverage cannot change. π
If you would like to make the status check codecov/patch
"green" then you need to add a test that calls Set-TargetResource
with the correct parameters so it enters into the correct code path that should be tested, in this case at least the ScheduleType
should be set to AtLogon
and User
to something like 'MockedUser
. Then add a mock for New-ScheduledTaskTrigger
and then assert that it was called with the expected parameters. Do something similar to this:
But you only need an It
-block that calls Set-TargetResource
(change to correct mock parameters), change the mock to New-ScheduledTaskTrigger
and assert that it was called with the correct parameters after you called Set-TargetResource
by using something similar to:
Assert-MockCalled -CommandName New-ScheduledTaskTrigger -ParameterFilters {
$AtLogon -eq $true -and $User -eq 'MockedUser'
} -Exactly -Times 1 -Scope It
Let me know if you get into any issues π
The new tests you added does not call any of the functions Get-TargetResource, Test-TargetResource or Set-TargetResource, so the test doesn't actually test anything in the resource so the code coverage cannot change. π
If you would like to make the status check
codecov/patch
"green" then you need to add a test that callsSet-TargetResource
with the correct parameters so it enters into the correct code path that should be tested, in this case at least theScheduleType
should be set toAtLogon
andUser
to something like'MockedUser
. Then add a mock forNew-ScheduledTaskTrigger
and then assert that it was called with the expected parameters. Do something similar to this:But you only need an
It
-block that callsSet-TargetResource
(change to correct mock parameters), change the mock toNew-ScheduledTaskTrigger
and assert that it was called with the correct parameters after you calledSet-TargetResource
by using something similar to:Assert-MockCalled -CommandName New-ScheduledTaskTrigger -ParameterFilters { $AtLogon -eq $true -and $User -eq 'MockedUser' } -Exactly -Times 1 -Scope It
Let me know if you get into any issues π
@johlju understood! That is much clearer now. I'll get the commit amended as soon as I can. Thanks for helping me push through with my lack of dsc/powershell expertise! π
hey @johlju - I've hit a bit of a wall. Tried every combination possible that I can think of, even broke out the help of AI π¬ Cannot get a working unit test case with suitable mocks.. Appreciate your help for getting to this point.
I will have a look tomorrow.
I will have a look tomorrow.
Thank you. I'll keep trying.
Sorry I haven't had time to look at this. I won't have time till later this week unfortunately.
@jordanbreen28 this one wasn't straight forward - it took me a little googling to get this one right, but this seems to work:
Context "When scheduling a task to trigger at user logon" {
BeforeAll {
Mock -CommandName New-ScheduledTaskTrigger -MockWith {
$cimInstance = New-CIMInstance -ClassName 'MSFT_TaskLogonTrigger' -Namespace 'root\Microsoft\Windows\TaskScheduler' -Property @{
# Fill the CIM instance with the properties we expect to be used by the resource.
UserId = $testParameters.User
Delay = ''
} -ClientOnly
<#
Must add the TypeName property to the CIM instance for the array .PSObject.PSTypeNames
to have the correct name for it to be recognized by the New-ScheduledTask command.
#>
$cimInstance | Add-Member -TypeName 'Microsoft.Management.Infrastructure.CimInstance#MSFT_TaskTrigger'
return $cimInstance
}
Mock -CommandName New-ScheduledTask -MockWith {
<#
Mock an object with properties that are used by the resource
for the newly created scheduled task.
#>
return [PSCustomObject] @{
Triggers = @(
@{
StartBoundary = '2018-09-27T18:45:08+02:00'
}
)
}
}
Mock -CommandName Register-ScheduledTask
}
It "Should correctly configure the task with 'AtLogon' ScheduleType and the specified user" {
$testParameters = $getTargetResourceParameters + @{
ScheduleType = 'AtLogon'
User = 'MockedUser'
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
LogonType = 'Password'
}
Set-TargetResource @testParameters
Assert-MockCalled -CommandName New-ScheduledTaskTrigger -ParameterFilter {
$AtLogon -eq $true -and $User -eq 'MockedUser'
} -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName New-ScheduledTask -Exactly -Times 1 -Scope It
}
}
@johlju Amazing! Lets try that.
@johlju and we're green... thanks for all your support on this
@jordanbreen28 you're welcome. Thank you for contributing and raising the code coverage.
Thank you @jordanbreen28 and @johlju !
Pull Request (PR) description
See https://github.com/dsccommunity/ComputerManagementDsc/issues/420
This Pull Request (PR) fixes the following issues
Fixes https://github.com/dsccommunity/ComputerManagementDsc/issues/420
Task list
This change isβ