Skip to content

Add Test coverage for XmlCommand.cs - #4201

Merged
Travis Plunk (TravisEz13) merged 4 commits into
PowerShell:masterfrom
SteveL-MSFT:test-xmlcommand
Jul 21, 2017
Merged

Travis Plunk (TravisEz13) merged 4 commits into
PowerShell:masterfrom
SteveL-MSFT:test-xmlcommand

Conversation

@SteveL-MSFT

Copy link
Copy Markdown
Member

fixed issues in XmlCommand not working correctly for -First and -Skip
removed dead code form XmlCommand
added tests

I'll see if I can get some code coverage data

Fix #4152

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

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

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

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

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.

Should also verify that $deserialized_one.two.three is not null

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.

Exception will be thrown if .three is null since I'm accessing a member

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.

No exception is not thrown.

image

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.

ok, will fix

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.

member access does not produce exception, unless strict mode is set. Method invocation will throw null reference exception.

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 Join-Path

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.

will fix

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor note: The loop has unnecessary nesting and duplication. Consider handling psObject != null and c != null) first to reduce nesting. Something like the following:

object result = _deserializer.Deserialize();
PSObject psObject = result as PSObject;

if (psObject != null)
{
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);
        }
    }
    continue;
}

}

if (skipped++ >= skip)
{
count++;
_cmdlet.WriteObject(result);
}

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.

will fix

@TravisEz13

Copy link
Copy Markdown
Member

Steve Lee (@SteveL-MSFT) Your Microsoft org membership is not public.

@SteveL-MSFT

Copy link
Copy Markdown
Member Author

Travis Plunk (@TravisEz13) the only thing I noticed was that my email wasn't public which I changed

@SteveL-MSFT
Steve Lee (SteveL-MSFT) force-pushed the test-xmlcommand branch 5 times, most recently from 1202d4d to b6bf9eb Compare July 13, 2017 20:19

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.

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.

running this in a container could fail (as there will likely not be 5 processes running)

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.

Good point. I'll change this to Get-Command instead.

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.

there's some timing issues here, would the following suffice?

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

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.

Thanks, I think that'll work better

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.

member access does not produce exception, unless strict mode is set. Method invocation will throw null reference exception.

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.

an alternative approach to lines 126-135

        ($out2.Name) -join ":" | should be (@($out1.Keys)[1,2] -join ":")
        ($out2.Value) -join ":" | should be (@($out1.Values)[1,2] -join ":")

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.

Got it, much simpler

@SteveL-MSFT

Copy link
Copy Markdown
Member Author

Aditya Patwardhan (@adityapatwardhan) feedback addressed

@TravisEz13

Copy link
Copy Markdown
Member

Aditya Patwardhan (@adityapatwardhan) Can you update your review?

removed dead code form XmlCommand
added tests
removed necessary get-process and increased content of xml to be imported to avoid race condition

fixed issues in XmlCommand not working correctly for -First and -Skip
removed dead code form XmlCommand
@SteveL-MSFT

Copy link
Copy Markdown
Member Author

Finally successfully got updated code coverage numbers:

ConvertToXmlCommand 100%
ExportCliXmlCommand 100%
SelectXmlCommand 60.2% (the remainder are exception handling)

@TravisEz13 Travis Plunk (TravisEz13) changed the title Test coverage for XmlCommand.cs Add Test coverage for XmlCommand.cs Jul 21, 2017
@TravisEz13
Travis Plunk (TravisEz13) merged commit e23d2e5 into PowerShell:master Jul 21, 2017
@SteveL-MSFT
Steve Lee (SteveL-MSFT) deleted the test-xmlcommand branch August 24, 2017 03:02
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
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.

Add tests for ConvertTo-XML for serializing PSObjects

6 participants