Skip to content

Trace-Command tests for code coverage - #4288

Merged
Dongbo Wang (daxian-dbw) merged 4 commits into
PowerShell:masterfrom
anmenaga:TraceCommandTests
Sep 1, 2017
Merged

Dongbo Wang (daxian-dbw) merged 4 commits into
PowerShell:masterfrom
anmenaga:TraceCommandTests

Conversation

@anmenaga

Copy link
Copy Markdown

Added tests for Trace-Command that increase code coverage to 80%~85% per class.

Fix #4151

}

AfterEach {
#copy $filePath 'C:\Temp' -Force -ErrorAction SilentlyContinue

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.

You should remove this comment

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.

removed

}

It "Get non-existing trace source" {
{ 'abc' | get-tracesource -ErrorAction Stop} | ShouldBeErrorID 'TraceSourceNotFound,Microsoft.PowerShell.Commands.GetTraceSourceCommand'

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.

Not a big deal, but a guid might be better to ensure it doesn't exist

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.

updated.


It "Set-TraceSource to file" {
Set-TraceSource -Name "ParameterBinding" -Option ExecutionFlow -FilePath $filePath -Force -ListenerOption "ProcessId,TimeStamp" -PassThru
Set-TraceSource -Name "ParameterBinding" -RemoveFileListener *

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.

Is this covering a specific code path for *? If so, please update test description. If not, specify the specific path rather than using the wildcard. Perhaps make this a -TestCases to handle both?

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.

there is specific code path for *
test description updated.


It "Basic Trace-Command -Expression" {
$a = Trace-Command -Name metadata,parameterbinding,cmdlet -Expression {Get-Process} -PSHost
$a.count -gt 0 | 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.

Seems like you should be verifying the count of the debug traces rather than the output of Get-Process

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.

Like the other one - it is not worth it.


It "Trace-Command contains wildcard characters" {
$a = Trace-Command -Name ParameterB* -Command 'get-alias' -Debugger
$a.count -gt 0 | 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.

Seems like you should be verifying the count of the debug traces rather than the output of Get-Alias

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.

Don't know an easy way to verify this; an actual debugger is needed.
Considering that this was added to cover just 1 getter - it is not worth it.

@SteveL-MSFT

Steve Lee (SteveL-MSFT) commented Jul 18, 2017 •

Copy link
Copy Markdown
Member

Andrew (@anmenaga) take a look at #4201 on how to get coverage for Getters/Dispose/StopProcessing using api

}

AfterEach {
#copy $filePath 'C:\Temp' -Force -ErrorAction SilentlyContinue

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.

Remove commented line.

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.

updated.

}

It "Set-TraceSource to file" {
Set-TraceSource -Name "ParameterBinding" -Option ExecutionFlow -FilePath $filePath -Force -ListenerOption "ProcessId,TimeStamp" -PassThru

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 write to output pipeline, so either store the results into a variable of $null.

$null = Set-TraceSource -Name "ParameterBinding" -Option ExecutionFlow -FilePath $filePath -Force -ListenerOption "ProcessId,TimeStamp" -PassThru 

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.

updated.

}

It "Trace-Command to readonly file" {
New-Item $filePath -Force

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 writes to output pipeline. Please set to $null or | to out-null

$null = New-Item $filePath -Force 

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.

updated.

@mirichmo

Copy link
Copy Markdown
Member

Steve Lee (@SteveL-MSFT) Aditya Patwardhan (@adityapatwardhan) This PR is ready for further review

$results = $log | ForEach-Object {$_.Split("=")[1]}

$results | ForEach-Object { $_ | Should Be ([threading.thread]::CurrentThread.ManagedThreadId) }
$results | % { $_ | Should Be ([threading.thread]::CurrentThread.ManagedThreadId) }

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.

Usage of ForEach-Object was correct. Using % throws ScriptAnalyzer errors.

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 am not sure how it got here; Updated.

Context "Trace-Command tests for code coverage" {

BeforeEach {
$filePath = join-path $TestDrive 'testtracefile.txt'

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 done in BeforeAll. It is a path so setting it once should be ok.

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.

Updated.


It "Trace-Command contains wildcard characters" {
$a = Trace-Command -Name ParameterB* -Command 'get-alias'
$a.count -gt 0 | 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.

This should be

$a.count | Should BeGreaterThan 0

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.

Updated.

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.

LGTM

@mirichmo

Copy link
Copy Markdown
Member

Steve Lee (@SteveL-MSFT) Do you have any additional concerns? If not, please approve

@daxian-dbw
Dongbo Wang (daxian-dbw) merged commit deaa7c9 into PowerShell:master Sep 1, 2017
@anmenaga
Andrew (anmenaga) deleted the TraceCommandTests branch October 31, 2018 21:19
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
Added tests for Trace-Command that increase code coverage to 80%~85% per class.
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.

6 participants