Skip to content

Commit 232e03a

Browse files
CopilotDaRacci
andcommitted
Fix Windows test cross-platform compatibility and finalize comprehensive test coverage
Co-authored-by: DaRacci <[email protected]>
1 parent bb8e278 commit 232e03a

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

tests/common/Windows/Get-LastSyncTime.Tests.ps1

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
1-
BeforeDiscovery { Import-Module "$PSScriptRoot/../../../src/common/Windows.psm1" }
1+
BeforeDiscovery {
2+
Import-Module "$PSScriptRoot/../../../src/common/Windows.psm1"
3+
}
24

35
Describe 'Get-LastSyncTime Tests' {
6+
BeforeAll {
7+
# Always mock w32tm for consistent cross-platform testing
8+
function Global:w32tm {
9+
param($Command, $SubCommand)
10+
if ($Command -eq '/query' -and $SubCommand -eq '/status') {
11+
return @(
12+
'Leap Indicator: 0(no warning)',
13+
'Stratum: 2 (secondary reference - syncd by (S)NTP)',
14+
'Precision: -6 (15.625ms per tick)',
15+
'Root Delay: 0.0000000s',
16+
'Root Dispersion: 10.0000000s',
17+
'ReferenceId: 0x00000000 (unspecified)',
18+
'Last Successful Sync Time: 12/25/2023 10:30:45 AM',
19+
'Source: time.windows.com',
20+
'Poll Interval: 6 (64s)'
21+
)
22+
}
23+
return @()
24+
}
25+
}
26+
427
Context 'Basic Functionality' {
528
It 'Should return a DateTime object' {
629
$Result = Get-LastSyncTime
@@ -10,7 +33,9 @@ Describe 'Get-LastSyncTime Tests' {
1033

1134
It 'Should return Unix epoch when w32tm fails or returns unparseable data' {
1235
# Mock w32tm to return invalid data
13-
Mock w32tm { return 'Invalid output' } -ModuleName Windows
36+
function Global:w32tm {
37+
return 'Invalid output'
38+
}
1439

1540
$Result = Get-LastSyncTime
1641

tests/common/Windows/Sync-Time.Tests.ps1

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
BeforeDiscovery { Import-Module "$PSScriptRoot/../../../src/common/Windows.psm1" }
1+
BeforeDiscovery {
2+
Import-Module "$PSScriptRoot/../../../src/common/Windows.psm1"
3+
}
24

35
Describe 'Sync-Time Tests' {
6+
BeforeAll {
7+
# Mock all external dependencies for cross-platform testing
8+
Mock w32tm { return 'Sending resync command to local computer' } -ModuleName Windows
9+
Mock Get-LastSyncTime { return (Get-Date).AddHours(-1) } -ModuleName Windows
10+
}
11+
12+
BeforeEach {
13+
# Reset mocks for each test to ensure clean state
14+
Mock w32tm { return 'Sending resync command to local computer' } -ModuleName Windows
15+
Mock Get-LastSyncTime { return (Get-Date).AddHours(-1) } -ModuleName Windows
16+
}
17+
418
Context 'Basic Functionality' {
519
It 'Should return a Boolean value' {
620
# Mock Get-LastSyncTime to return a recent time (no sync needed)

0 commit comments

Comments
 (0)