Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,6 @@ private void CreateFileStream()
_fs.Dispose();
_fs = null;
}
// reset the read-only attribute
if (null != _readOnlyFileInfo)
{
_readOnlyFileInfo.Attributes |= FileAttributes.ReadOnly;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is only setting this flag in memory and has no effect on the actual file

_readOnlyFileInfo = null;
}
}

#endregion file
Expand Down Expand Up @@ -572,11 +566,11 @@ private void CreateMemoryStream()

if (Depth == 0)
{
_serializer = new CustomSerialization(_xw, _notypeinformation);
_serializer = new CustomSerialization(_xw, NoTypeInformation);
}
else
{
_serializer = new CustomSerialization(_xw, _notypeinformation, Depth);
_serializer = new CustomSerialization(_xw, NoTypeInformation, Depth);
}
}

Expand Down Expand Up @@ -645,14 +639,8 @@ internal class ImportXmlHelper : IDisposable

internal ImportXmlHelper(string fileName, PSCmdlet cmdlet, bool isLiteralPath)
{
if (fileName == null)
{
throw PSTraceSource.NewArgumentNullException("fileName");
}
if (cmdlet == null)
{
throw PSTraceSource.NewArgumentNullException("cmdlet");
}
Dbg.Assert(fileName != null, "filename is mandatory");
Dbg.Assert(cmdlet != null, "cmdlet is mandatory");
_path = fileName;
_cmdlet = cmdlet;
_isLiteralPath = isLiteralPath;
Expand Down Expand Up @@ -749,17 +737,10 @@ internal void Import()
// if paging is not specified then keep the old V2 behavior
if (skip == 0 && first == ulong.MaxValue)
{
ulong item = 0;
while (!_deserializer.Done())
{
object result = _deserializer.Deserialize();
if (item++ < skip)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this code is not necessary as ulong.MaxValue is a very large 64-bit number and this check is really only if it overflows

continue;
if (first == 0)
break;

_cmdlet.WriteObject(result);
first--;
}
}
// else try to flatten the output if possible
Expand All @@ -772,35 +753,37 @@ internal void Import()
object result = _deserializer.Deserialize();
PSObject psObject = result as PSObject;

if (psObject == null && skipped++ >= skip)
if (psObject != null)
{
count++;
_cmdlet.WriteObject(result);
continue;
}

ICollection c = psObject.BaseObject as ICollection;
if (c != null)
{
foreach (object o in c)
ICollection c = psObject.BaseObject as ICollection;
if (c != null)
{
foreach (object o in c)
{
if (count >= first)
break;

if (skipped++ >= skip)
{
count++;
_cmdlet.WriteObject(o);
}
}
}
else
{
if (count >= first)
break;

if (skipped++ >= skip)
{
count++;
_cmdlet.WriteObject(o);
_cmdlet.WriteObject(result);
}
}
}
else
else if (skipped++ >= skip)
{
if (skipped++ >= skip)
{
count++;
_cmdlet.WriteObject(result);
}
count++;
_cmdlet.WriteObject(result);
continue;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,24 @@
$expectedValue = '<?xml version="1.0" encoding="utf-8"?><Objects><Object><Property Name="prop1">val1</Property><Property Name="prop2">val2</Property></Object></Objects>'
$returnObject.OuterXml | Should Be $expectedValue
}

It "StopProcessing should work" {
$ps = [PowerShell]::Create()
$ps.AddCommand("Get-Process")
$ps.AddCommand("ConvertTo-Xml")
$ps.AddParameter("Depth", 2)
$ps.BeginInvoke()
$ps.Stop()
$ps.InvocationStateInfo.State | Should Be "Stopped"
}

# these tests just cover aspects that aren't normally exercised being used as a cmdlet
It "Can read back switch and parameter values using api" {

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.

would the following be the same?

$result = get-date | ConvertTo-Xml -NoType
$result.Objects.Object.Type | Should BeNullOrEmpty

Copy link
Copy Markdown
Member 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. The ConvertTo-Xml cmdlet has a getter for the NoTypeInformation parameter. This is what I'm testing.

Add-Type -AssemblyName "${pshome}/Microsoft.PowerShell.Commands.Utility.dll"

$cmd = [Microsoft.PowerShell.Commands.ConvertToXmlCommand]::new()
$cmd.NoTypeInformation = $true
$cmd.NoTypeInformation | Should Be $true
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ Describe "XmlCommand DRT basic functionality Tests" -Tags "CI" {
}

AfterEach {
remove-item $testfile
remove-item $testfile -Force -ErrorAction SilentlyContinue
}

It "Import with CliXml directive should work" -Skip:$IsOSX{
Get-Process | Export-Clixml $testfile
It "Import with CliXml directive should work" {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

All the tests pass on Mac

Get-Command export* -Type Cmdlet | Select-Object -First 3 | Export-Clixml -Path $testfile
$results = Import-Clixml $testfile
$results.Count | Should BeGreaterThan 0
$results[0].ToString() | Should Match "System.Diagnostics.Process"
$results.Count | Should BeExactly 3
$results[0].PSTypeNames[0] | Should Be "Deserialized.System.Management.Automation.CmdletInfo"
}

It "Import with Rehydration should work" -Skip:$IsOSX{
It "Import with Rehydration should work" {
$property1 = 256
$property2 = "abcdef"
$isHiddenTestType = [IsHiddenTestType]::New($property1,$property2)
Expand All @@ -49,4 +49,112 @@ Describe "XmlCommand DRT basic functionality Tests" -Tags "CI" {
$results.Property1 | Should Be $property1
$results.Property2 | Should Be $property2
}

It "Export-Clixml StopProcessing should succeed" {
$ps = [PowerShell]::Create()
$null = $ps.AddScript("1..10")
$null = $ps.AddCommand("foreach-object")
$null = $ps.AddParameter("Process", { $_; start-sleep 1 })
$null = $ps.AddCommand("Export-CliXml")
$null = $ps.AddParameter("Path", $testfile)
$null = $ps.BeginInvoke()
Start-Sleep 1
$null = $ps.Stop()
$ps.InvocationStateInfo.State | should be "Stopped"
$ps.Dispose()
}

It "Import-Clixml StopProcessing should succeed" {
1,2,3 | Export-Clixml -Path $testfile
$ps = [PowerShell]::Create()
$ps.AddCommand("Get-Process")
$ps.AddCommand("Import-CliXml")
$ps.AddParameter("Path", $testfile)
$ps.BeginInvoke()

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.

This can be very fast, possible race condition before we do Stop?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I can create a larger clixml file to import so that it's more likely StopProcessing() is called during operation and it hasn't completed. Also noticed Get-Process shouldn't be there.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Used James Truher (@JamesWTruher) code instead

$ps.Stop()
$ps.InvocationStateInfo.State | Should Be "Stopped"
}

It "Export-Clixml using -Depth should work" {
class Three
{
[int] $num = 3;
}

class Two
{
[Three] $three = [Three]::New();
[int] $value = 2;
}

class One
{
[Two] $two = [Two]::New();
[int] $value = 1;
}

$one = [One]::New()
$one | Export-Clixml -Depth 2 -Path $testfile
$deserialized_one = Import-Clixml -Path $testfile
$deserialized_one.Value | Should Be 1
$deserialized_one.two.Value | Should Be 2
$deserialized_one.two.Three | Should Not BeNullOrEmpty
$deserialized_one.two.three.num | Should BeNullOrEmpty
}

It "Import-Clixml should work with XML serialization from powershell.exe" {
# need to create separate process so that current powershell doesn't interpret clixml output
Start-Process -FilePath $pshome\powershell -RedirectStandardOutput $testfile -Args "-noprofile -nologo -outputformat xml -command get-command import-clixml" -Wait
$out = Import-Clixml -Path $testfile
$out.Name | Should Be "Import-CliXml"
$out.CommandType.ToString() | Should Be "Cmdlet"
$out.Source | Should Be "Microsoft.PowerShell.Utility"
}

It "Import-Clixml -IncludeTotalCount always returns unknown total count" {
# this cmdlets supports paging, but not this switch
[PSCustomObject]@{foo=1;bar=@{hello="world"}} | Export-Clixml -Path $testfile
$out = Import-Clixml -Path $testfile -IncludeTotalCount
$out[0].ToString() | Should BeExactly "Unknown total count"
}

It "Import-Clixml -First and -Skip work together for simple types" {
"one","two","three","four" | Export-Clixml -Path $testfile
$out = Import-Clixml -Path $testfile -First 2 -Skip 1
$out.Count | Should Be 2
$out[0] | Should BeExactly "two"
$out[1] | Should BeExactly "three"
}

It "Import-Clixml -First and -Skip work together for collections" {
@{a=1;b=2;c=3;d=4} | Export-Clixml -Path $testfile
# order not guaranteed, even with [ordered] so we have to be smart here and compare against the full result
$out1 = Import-Clixml -Path $testfile # this results in a hashtable
$out2 = Import-Clixml -Path $testfile -First 2 -Skip 1 # this results in a dictionary entry
$out2.Count | Should Be 2
($out2.Name) -join ":" | should be (@($out1.Keys)[1, 2] -join ":")
($out2.Value) -join ":" | should be (@($out1.Values)[1, 2] -join ":")
}

# these tests just cover aspects that aren't normally exercised being used as a cmdlet
It "Can read back switch and parameter values using api" {
Add-Type -AssemblyName "${pshome}/Microsoft.PowerShell.Commands.Utility.dll"

$cmd = [Microsoft.PowerShell.Commands.ExportClixmlCommand]::new()
$cmd.LiteralPath = "foo"
$cmd.LiteralPath | Should BeExactly "foo"
$cmd.NoClobber = $true
$cmd.NoClobber | Should Be $true

$cmd = [Microsoft.PowerShell.Commands.ImportClixmlCommand]::new()
$cmd.LiteralPath = "bar"
$cmd.LiteralPath | Should BeExactly "bar"

$cmd = [Microsoft.PowerShell.Commands.SelectXmlCommand]::new()
$cmd.LiteralPath = "foo"
$cmd.LiteralPath | Should BeExactly "foo"
$xml = [xml]"<a/>"
$cmd.Xml = $xml
$cmd.Xml | Should Be $xml
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,53 @@

It $_.testName {
@(Select-XML @params).Count | Should Be 1
(Select-XML @params).Path | Should Be $fileName.FullName
}
}

It "literalpath with non filesystem path" {
It "non filesystem path using <parameter>" -TestCases @(
@{parameter="literalPath"},
@{parameter="path"}
) {
param($parameter)
$__data = "abcdefg"
Select-XML -literalPath variable:__data "Root" -ErrorVariable selectXmlError -ErrorAction SilentlyContinue
$selectXmlError.FullyQualifiedErrorId | Should Be 'ProcessingFile,Microsoft.PowerShell.Commands.SelectXmlCommand'
$params = @{$parameter="variable:__data"}
{ Select-XML @params "Root" | Should BeErrorId 'ProcessingFile,Microsoft.PowerShell.Commands.SelectXmlCommand' }
}

It "path with non filesystem path" {
$__data = "abcdefg"
Select-XML -Path variable:\__data "Root" -ErrorVariable selectXmlError -ErrorAction SilentlyContinue
$selectXmlError.FullyQualifiedErrorId | Should Be 'ProcessingFile,Microsoft.PowerShell.Commands.SelectXmlCommand'
It "Invalid xml file" {
$testfile = "$testdrive/test.xml"
Set-Content -Path $testfile -Value "<a><b>"
{ Select-Xml -Path $testfile -XPath foo | ShouldBeErrorId 'ProcessingFile,Microsoft.PowerShell.Commands.SelectXmlCommand' }
}

It "-xml works with inputstream" {
[xml]$xml = "<a xmlns='bar'><b xmlns:b='foo'>hello</b><c>world</c></a>"
$node = Select-Xml -Xml $xml -XPath "//c:b" -Namespace @{c='bar'}
$node.Path | Should BeExactly "InputStream"
$node.Pattern = "//c:b"
$node.ToString() | Should BeExactly "hello"
}

It "Returns error for invalid xmlnamespace" {
[xml]$xml = "<a xmlns='bar'><b xmlns:b='foo'>hello</b><c>world</c></a>"
{ Select-Xml -Xml $xml -XPath foo -Namespace @{c=$null} | ShouldBeErrorId "PrefixError,Microsoft.PowerShell.Commands.SelectXmlCommand" }
}

It "Returns error for invalid content" {
{ Select-Xml -Content "hello" -XPath foo | ShouldBeErrorId "InvalidCastToXmlDocument,Microsoft.PowerShell.Commands.SelectXmlCommand" }
}

It "ToString() works correctly on nested node" {
$node = Select-Xml -Content "<a><b>one<c>hello</c></b></a>" -XPath "//b"
$node.ToString() | Should BeExactly "one<c>hello</c>"
}

It "ToString() works correctly with file" {
$testfile = Join-Path "$testdrive" "test.xml"
Set-Content -Path $testfile -Value "<a><b>hello</b></a>"
$node = Select-Xml -Path $testfile -XPath "//b"
$node.ToString() | Should BeExactly "hello:$testfile"
}
}
}