Fix ConvertTo-Json DateTime timezone test#27524
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Updates a ConvertTo-Json test to make the expected JSON output deterministic by normalizing a DateTime value to UTC and asserting an exact ISO-8601 string.
Changes:
- Parse the input timestamp as
DateTimeOffsetand convert toUtcDateTime. - Replace a wildcard string assertion with an exact
-BeExactlyassertion for the JSON output.
| It 'Should not serialize ETS properties added to DateTime' { | ||
| $date = "2021-06-24T15:54:06.796999-07:00" | ||
| $d = [DateTime]::Parse($date) | ||
| $d = [DateTimeOffset]::Parse($date).UtcDateTime |
There was a problem hiding this comment.
Addressed in ce1e23f47: the test now parses the input with System.Globalization.CultureInfo.InvariantCulture and DateTimeStyles.RoundtripKind, so it is not dependent on the machine locale.
|
|
||
| # need to use wildcard here due to some systems may be configured with different culture setting showing time in different format | ||
| $d | ConvertTo-Json -Compress | Should -BeLike '"2021-06-24T*' | ||
| $d | ConvertTo-Json -Compress | Should -BeExactly '"2021-06-24T22:54:06.796999Z"' |
There was a problem hiding this comment.
Addressed in ce1e23f47: the test no longer requires one exact fractional-second JSON string. It now verifies the serialized value is a UTC ISO-8601 JSON string and parses back to the expected UTC DateTime. Checks run: Start-PSBuild -Configuration Debug -Runtime win7-x64; Start-PSPester -Path test\powershell\Modules\Microsoft.PowerShell.Utility\ConvertTo-Json.Tests.ps1 (222 passed, 0 failed, 1 pending).
PR Summary
Make the
ConvertTo-JsonDateTime ETS test independent of the machine's local timezone.PR Context
The test parsed
2021-06-24T15:54:06.796999-07:00into a localDateTime, then expected the compressed JSON to start with2021-06-24. On machines at UTC+1 or later, that same instant serializes as the next local date, so the assertion fails even thoughConvertTo-Jsonis behaving correctly.This normalizes the fixture through
DateTimeOffset.Parse(...).UtcDateTimeand checks the exact UTC JSON string. The roundtrip assertion remains in place, so the test still verifies that DateTime is serialized as a scalar value rather than with ETS properties.Fixes #27500
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerTests
Start-PSPester -Path ./test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 -UseNuGetOrg -SkipTestToolBuild -ThrowOnFailureStart-PSPester -Path ./test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 -UseNuGetOrg -SkipTestToolBuild -ThrowOnFailure(222 passed, 1 pending)git diff --check