Skip to content

WIP: Add ConvertTo-Clixml and ConvertFrom-Clixml cmdlets - #5815

Closed
Charlie Schmidt (charlieschmidt) wants to merge 3 commits into
PowerShell:masterfrom
charlieschmidt:powershell-3898-convertfromto-clixml
Closed

Charlie Schmidt (charlieschmidt) wants to merge 3 commits into
PowerShell:masterfrom
charlieschmidt:powershell-3898-convertfromto-clixml

Conversation

@charlieschmidt

@charlieschmidt Charlie Schmidt (charlieschmidt) commented Jan 8, 2018

Copy link
Copy Markdown

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.

…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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please spell out implementation. impl -> implementation

#region Overrides
#region to-clixml
/// <summary>
/// impl specific TextWriter for the xml serializer to use

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please spell out implementation. impl -> implementation


/// <summary>
/// BeginProcessing override
/// IDisposable style cleanup for any imple specific stuff, be sure to call base.CleanUp()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please spell out implementation. imple -> implementation

}
else
{
_serializer = new Serializer(_xw, Depth, true);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use named variables.

#region PSCmdlet Members

/// <summary>
/// BeginProcessing override, create the xml scaffolding stuff

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider rewording, BegingProcessing override to create the XML scaffolding.

}
}

$isExisted | Should Be $true

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
}


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra lines

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")))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above with looping and select-string

$filePath | Should Exist

$fileContent = Get-Content $filePath -raw
$fileContent | Should Not Be $null

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Should Not BeNullOrEmpty instead

$filePath | Should Exist

$fileContent = Get-Content $filePath
$fileContent | Should Not Be $null

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

@charlieschmidt

Copy link
Copy Markdown
Author

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
@charlieschmidt
Charlie Schmidt (charlieschmidt) force-pushed the powershell-3898-convertfromto-clixml branch from 3ed2b92 to 9e26e79 Compare January 9, 2018 05:51
Remove-Item $filePath -Force -ErrorAction SilentlyContinue
}

It "deserializes an object as expected" {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two tests seem very similar. Can we use -TestCases? Similar to

It "throws ParserException - <testname>" -TestCases $testcases {

Remove-Item $filePath -Force -ErrorAction SilentlyContinue
}

It "converts an object to string as expected" {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two tests seem very similar. Can we use -TestCases? Similar to

It "throws ParserException - <testname>" -TestCases $testcases {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use named parameter.

{
if (_disposed == false)
_stringWriter = new StringWriter();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra line.

}

/// <summary>
///

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment here.

_inputObjectBuffer.Add(InputObject);
}


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra line.

@adityapatwardhan

Copy link
Copy Markdown
Member

Charlie Schmidt (@charlieschmidt) Thanks for adding the documentation, much appreciated.

@charlieschmidt

Copy link
Copy Markdown
Author

Aditya Patwardhan (@adityapatwardhan) yup. Anything else needed?

@iSazonov Ilya (iSazonov) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Charlie Schmidt (@charlieschmidt) Thanks for your contribution!


$fileContent = Get-Content $filePath
$fileContent | Should Not Be $null
$fileContent | Should Not BeNullOrEmpty

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems it is unrelated the PR - please revert.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can, but was cleaning up other instances of this, want me to pull it to a separate PR or?

#5815 (comment)

@iSazonov Ilya (iSazonov) Jan 12, 2018

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems it is unrelated the PR - please revert.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closed.

Remove-Item $filePath -Force -ErrorAction SilentlyContinue
}

It "converts an object to string as expected" {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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" {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same - "serializes".


Import-Clixml -Path (Join-Path $PSScriptRoot "assets\CorruptedCim.clixml")

# Wait up to 10 seconds for calc.exe to run

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use our Wait-UntilTrue function.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats a new function to me, will definitely add that

@charlieschmidt Charlie Schmidt (charlieschmidt) Jan 12, 2018

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@iSazonov Ilya (iSazonov) Jan 12, 2018

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can put Get-Process Calc to a script block and send it to Wait-UntilTrue.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. We already create _helper - can we reuse it?
  2. Can XmlException be raised here? If so do we catch it and throw use freadly exception?

@adityapatwardhan

Copy link
Copy Markdown
Member

Charlie Schmidt (@charlieschmidt) Can you respond to some comments from Ilya (@iSazonov)

@charlieschmidt

Copy link
Copy Markdown
Author

Ponging to say am still following this and will respond with comments or commits tomorrow evening

@adityapatwardhan

Copy link
Copy Markdown
Member

Charlie Schmidt (@charlieschmidt) Please have a look at failing tests. Shall I go ahead and mark this PR as WIP: since there is more work expected here.

@charlieschmidt

Copy link
Copy Markdown
Author

Yes please

@adityapatwardhan Aditya Patwardhan (adityapatwardhan) changed the title Add ConvertTo-Clixml and ConvertFrom-Clixml cmdlets WIP: Add ConvertTo-Clixml and ConvertFrom-Clixml cmdlets Jan 18, 2018
@adityapatwardhan

Copy link
Copy Markdown
Member

Charlie Schmidt (@charlieschmidt) Please resolve merge conflict as well. Thanks!

@stale

stale Bot commented Mar 22, 2018

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Export-CliXml shouldn't require writing to a file

4 participants