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
5 changes: 5 additions & 0 deletions src/Pester.Runtime.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,11 @@ function Invoke-ContainerDiscovery {
# set correctly as if we provided -Data to New-Block
$root.Data = $Container.Data

# Record which Pester.BeforeContainer.ps1 files applied, so the result says where a
# container's setup came from. The folder tree alone does not answer it, #pester:no-inherit
# can cut the chain short.
$root.BeforeContainerFile = [System.Collections.Generic.List[string]]@($BeforeContainerFile)

Reset-PerContainerState -RootBlock $root

$steps = $state.Plugin.ContainerDiscoveryStart
Expand Down
4 changes: 4 additions & 0 deletions src/csharp/Pester/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public Block()
public string ItemType { get; } = "Block";

public ContainerInfo BlockContainer { get; set; }
// Every Pester.BeforeContainer.ps1 that applied to this container, outermost first.
// Reading the folder tree is not enough to work this out, because #pester:no-inherit can
// cut the chain short.
public List<string> BeforeContainerFile { get; set; } = new List<string>();
public object Root { get; set; }
public bool IsRoot { get; set; }
public object Parent { get; set; }
Expand Down
6 changes: 5 additions & 1 deletion src/csharp/Pester/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public static Container CreateFromBlock (Block block) {
Item = block.BlockContainer.Item,
Blocks = block.Blocks,
Data = block.Data,
StandardOutput = block.StandardOutput
StandardOutput = block.StandardOutput,
BeforeContainerFile = block.BeforeContainerFile
};
}

Expand All @@ -56,6 +57,9 @@ public string Type
}
public object Item { get; set; }
public object Data { get; set; }
// Every Pester.BeforeContainer.ps1 that applied to this container, outermost first, so
// "where did this function come from" is answerable from the result object.
public List<string> BeforeContainerFile { get; set; } = new List<string>();
public List<Block> Blocks { get; set; } = new List<Block>();
public string Result { get; set; } = "NotRun";
public TimeSpan Duration { get => DiscoveryDuration + UserDuration + FrameworkDuration; }
Expand Down
48 changes: 48 additions & 0 deletions tst/Pester.RSpec.Parallel.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,54 @@ Describe 'Second' {
finally { Remove-Item -Path $folder -Recurse -Force }
}

t "reports which Pester.BeforeContainer.ps1 files applied to each container" {
# The folder tree alone does not answer this, #pester:no-inherit can cut the chain
# short, so the applied files are on the container. (#3007)
$folder = New-CascadingBeforeContainerFolder
try {
$c = [PesterConfiguration]::Default
$c.Run.Path = $folder
$c.Run.RepoRoot = $folder
$c.Run.PassThru = $true
$c.Output.Verbosity = 'None'
$r = Invoke-Pester -Configuration $c

$r.FailedCount | Verify-Equal 0

$unit = $r.Containers | Where-Object { $_.Item.FullName -like '*unit*' }
$integration = $r.Containers | Where-Object { $_.Item.FullName -like '*integration*' }

# unit sees root + tests + unit, integration sees root + tests, outermost first.
$unit.BeforeContainerFile.Count | Verify-Equal 3
$integration.BeforeContainerFile.Count | Verify-Equal 2
(Split-Path $unit.BeforeContainerFile[0] -Parent) | Verify-Equal $folder
}
finally { Remove-Item -Path $folder -Recurse -Force }
}

t "reports only the files that survived #pester:no-inherit" {
$folder = New-NoInheritBeforeContainerFolder
try {
$c = [PesterConfiguration]::Default
$c.Run.Path = $folder
$c.Run.RepoRoot = $folder
$c.Run.PassThru = $true
$c.Output.Verbosity = 'None'
$r = Invoke-Pester -Configuration $c

$r.FailedCount | Verify-Equal 0

$docs = $r.Containers | Where-Object { $_.Item.FullName -like '*docs*' }
$normal = $r.Containers | Where-Object { $_.Item.FullName -notlike '*docs*' }

# docs cut the chain, so it reports only its own file, not the root one.
$docs.BeforeContainerFile.Count | Verify-Equal 1
$normal.BeforeContainerFile.Count | Verify-Equal 1
(Split-Path $docs.BeforeContainerFile[0] -Parent) | Verify-Equal (Join-Path $folder 'docs')
}
finally { Remove-Item -Path $folder -Recurse -Force }
}

t "does not overwrite a Run.RepoRoot the user set" {
# A real directory. Resolving the chain runs the value through GetFullPath, and a made
# up path is not portable: on Windows something like 'TestDrive:whatever' reads as a
Expand Down
Loading