Fix ConvertTo-Json DateTime timezone test#27524
Open
KirtiRamchandani wants to merge 1 commit into
Open
Conversation
Contributor
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 |
|
|
||
| # 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"' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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