WIP: Add ConvertTo-Clixml and ConvertFrom-Clixml cmdlets#5815
WIP: Add ConvertTo-Clixml and ConvertFrom-Clixml cmdlets#5815charlieschmidt wants to merge 3 commits intoPowerShell:masterfrom
Conversation
…port-Clixml; like Import/Export/ConvertFrom/ConvertTo-Csv) Refactored Export-Clixml into base functionality shared with ConvertTo-Clixml. Import/ConvertFrom-Clixml both use existing ImportXmlHelper class - extended that to create MemoryStream for ConvertFrom- Corresponding pester tests. Closes PowerShell#3898
| { | ||
| /// <summary> | ||
| /// implementation for the Export-Clixml command | ||
| /// base impl for ConvertTo-Clixml and Export-Clixml |
There was a problem hiding this comment.
Please spell out implementation. impl -> implementation
| #region Overrides | ||
| #region to-clixml | ||
| /// <summary> | ||
| /// impl specific TextWriter for the xml serializer to use |
There was a problem hiding this comment.
Please spell out implementation. impl -> implementation
|
|
||
| /// <summary> | ||
| /// BeginProcessing override | ||
| /// IDisposable style cleanup for any imple specific stuff, be sure to call base.CleanUp() |
There was a problem hiding this comment.
Please spell out implementation. imple -> implementation
| } | ||
| else | ||
| { | ||
| _serializer = new Serializer(_xw, Depth, true); |
There was a problem hiding this comment.
Please use named variables.
| #region PSCmdlet Members | ||
|
|
||
| /// <summary> | ||
| /// BeginProcessing override, create the xml scaffolding stuff |
There was a problem hiding this comment.
Consider rewording, BegingProcessing override to create the XML scaffolding.
| } | ||
| } | ||
|
|
||
| $isExisted | Should Be $true |
There was a problem hiding this comment.
Typically the error message on console when a failure happens with assertions like this are not helpful.
Was false when true was expected.
| $isExisted | Should Be $true | ||
| } | ||
|
|
||
|
|
| foreach($gpsItem in $gpsList) | ||
| { | ||
| $checkId = $gpsItem.Id | ||
| if (($null -ne $(Select-String -InputObject $item -SimpleMatch $checkId)) -and ($null -ne $(Select-String -InputObject $item -SimpleMatch "Id"))) |
There was a problem hiding this comment.
Same comment as above with looping and select-string
| $filePath | Should Exist | ||
|
|
||
| $fileContent = Get-Content $filePath -raw | ||
| $fileContent | Should Not Be $null |
There was a problem hiding this comment.
Use Should Not BeNullOrEmpty instead
| $filePath | Should Exist | ||
|
|
||
| $fileContent = Get-Content $filePath | ||
| $fileContent | Should Not Be $null |
|
Should address the review issues, and a few other spelling/correction stuff. I've also added documentation and a PR for PowerShell-Docs at MicrosoftDocs/PowerShell-Docs#2022 |
Change to auto properties where appropriate Don't allow empty string as input to ConvertFrom-Clixml Fix IDisposable implementation in ConvertFrom-Clixml SImplify logic of ConvertFrom-Clixml Simplier/corrected tests
3ed2b92 to
9e26e79
Compare
| Remove-Item $filePath -Force -ErrorAction SilentlyContinue | ||
| } | ||
|
|
||
| It "deserializes an object as expected" { |
There was a problem hiding this comment.
These two tests seem very similar. Can we use -TestCases? Similar to
| Remove-Item $filePath -Force -ErrorAction SilentlyContinue | ||
| } | ||
|
|
||
| It "converts an object to string as expected" { |
There was a problem hiding this comment.
These two tests seem very similar. Can we use -TestCases? Similar to
There was a problem hiding this comment.
I don't think so, not without making it uglier. Because the difference in the two tests isn't a variable or parameter, it's function param vs pipeline. That said, can move some of the duplicate code to the BeforeAll, should make it more clear what is really being tested
| } | ||
| else | ||
| { | ||
| _serializer = new Serializer(_xmlWriter, Depth, true); |
| { | ||
| if (_disposed == false) | ||
| _stringWriter = new StringWriter(); | ||
|
|
| } | ||
|
|
||
| /// <summary> | ||
| /// |
| _inputObjectBuffer.Add(InputObject); | ||
| } | ||
|
|
||
|
|
|
@charlieschmidt Thanks for adding the documentation, much appreciated. |
|
@adityapatwardhan yup. Anything else needed? |
…From-Clixml test cases
iSazonov
left a comment
There was a problem hiding this comment.
@charlieschmidt Thanks for your contribution!
|
|
||
| $fileContent = Get-Content $filePath | ||
| $fileContent | Should Not Be $null | ||
| $fileContent | Should Not BeNullOrEmpty |
There was a problem hiding this comment.
It seems it is unrelated the PR - please revert.
There was a problem hiding this comment.
can, but was cleaning up other instances of this, want me to pull it to a separate PR or?
There was a problem hiding this comment.
Such changes increase the reviewing time because a reviewer must understand that this change is not related to code changes.
Thanks for clarify. I don't see the request. I am fine to leave the change.
Closed.
|
|
||
| $fileContent = Get-Content $filePath | ||
| $fileContent | Should Not Be $null | ||
| $fileContent | Should Not BeNullOrEmpty |
There was a problem hiding this comment.
It seems it is unrelated the PR - please revert.
| Remove-Item $filePath -Force -ErrorAction SilentlyContinue | ||
| } | ||
|
|
||
| It "converts an object to string as expected" { |
There was a problem hiding this comment.
Below we use "deserializes" - so I believe here we should use "serializes".
| $objClixml | Should Match "<I32 N=`"Id`">$($gps.Id)</I32>" | ||
| } | ||
|
|
||
| It "converts an object from the pipeline to string as expected" { |
There was a problem hiding this comment.
The same - "serializes".
|
|
||
| Import-Clixml -Path (Join-Path $PSScriptRoot "assets\CorruptedCim.clixml") | ||
|
|
||
| # Wait up to 10 seconds for calc.exe to run |
There was a problem hiding this comment.
Thats a new function to me, will definitely add that
There was a problem hiding this comment.
On second reading, may not be appropriate. WIth other uses of Wait-UntilTrue, we're waiting until something is true, with a 10second time out, checking every second. Here - we want to wait at least 10 seconds, checking to make sure that calc hasn't started
Maybe just
start-sleep -Seconds 10
Get-Process ... | Should BeNullOrEmpty
? I'm not as familiar with how a corrupted Cim class could kick off the exe (and have a mac anyway [and the whole thing may not apply in .netcore anyway who knows?]) - I just used the existing Import-Clixml test
There was a problem hiding this comment.
You can put Get-Process Calc to a script block and send it to Wait-UntilTrue.
There was a problem hiding this comment.
It seems Microsoft.Management.Infrastructure is still not ported #4562
| $cliXmlContent = Get-Content -Path (Join-Path $PSScriptRoot "assets\CorruptedCim.clixml") -Raw | ||
| ConvertFrom-CliXml -InputObject $cliXmlContent | ||
|
|
||
| # Wait up to 10 seconds for calc.exe to run |
There was a problem hiding this comment.
It is not your code but please use our Wait-UntilTrue function too.
| { | ||
| /// <summary> | ||
| /// implementation for the Export-Clixml command | ||
| /// Base implementation for ConvertTo-Clixml and Export-Clixml |
There was a problem hiding this comment.
The comment is public - please add final dot.
Below please review formatting public comments too.
| #region FileStream implementation for ToClixmlCommand | ||
|
|
||
| /// <summary> | ||
| /// handle to file stream |
There was a problem hiding this comment.
Please remove the obvious comment . And why the comment is public for private field? Below too.
| if (!successfullyConverted && _inputObjectBuffer.Count > 0) | ||
| { | ||
| _helper = new ImportXmlHelper(this, ImportXmlHelper.CreateMemoryStream(string.Join(System.Environment.NewLine, _inputObjectBuffer))); | ||
| _helper.Import(); |
There was a problem hiding this comment.
- We already create
_helper- can we reuse it? - Can
XmlExceptionbe raised here? If so do we catch it and throw use freadly exception?
|
@charlieschmidt Can you respond to some comments from @iSazonov |
|
Ponging to say am still following this and will respond with comments or commits tomorrow evening |
|
@charlieschmidt Please have a look at failing tests. Shall I go ahead and mark this PR as |
|
Yes please |
|
@charlieschmidt Please resolve merge conflict as well. Thanks! |
|
This PR has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs within 10 days. Thank you for your contributions. |
PR Summary
ConvertTo-Clixml and ConvertFrom-Clixml cmdlets (to go with Export/Import-Clixml; like Import/Export/ConvertFrom/ConvertTo-Csv)
Refactored Export-Clixml into base functionality shared with ConvertTo-Clixml.
Import/ConvertFrom-Clixml both use existing ImportXmlHelper class - extended that to create MemoryStream for ConvertFrom-
Corresponding pester tests.
Closes #3898
PR Checklist
Note: Please mark anything not applicable to this PR
NA.[feature]if the change is significant or affectes feature testsWIP:to the beginning of the title and remove the prefix when the PR is ready.