Skip to content

Commit 6ef94c1

Browse files
authored
Make the experience better when start-pspester doesn't find pester (#5673)
refactor code to restore pester into a separate function called Restore-PSPester update message on what to do when pester is missing Add ability for get-psoptions to default to new-psoptions fix an issue with publish-pstesttools when a build has not been run since build.psm1 has been imported (try to use the default options) make start-pspester use the last build, not just use the default options fix an issue in restore caused some files not to be removed
1 parent d966f59 commit 6ef94c1

5 files changed

Lines changed: 54 additions & 42 deletions

File tree

build.psm1

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,21 @@ function Restore-PSModuleToBuild
660660

661661
if($CI.IsPresent)
662662
{
663-
Restore-GitModule -Destination $modulesDir -Uri 'https://github.com/PowerShell/psl-pester' -Name Pester -CommitSha 'aa243108e7da50a8cf82513b6dd649b653c70b0e'
663+
Restore-PSPester -Destination $modulesDir
664664
}
665665
}
666+
667+
function Restore-PSPester
668+
{
669+
param
670+
(
671+
[ValidateNotNullOrEmpty()]
672+
[string]
673+
$Destination = ([IO.Path]::Combine((Split-Path (Get-PSOptions -DefaultToNew).Output), "Modules"))
674+
)
675+
676+
Restore-GitModule -Destination $Destination -Uri 'https://github.com/PowerShell/psl-pester' -Name Pester -CommitSha 'aa243108e7da50a8cf82513b6dd649b653c70b0e'
677+
}
666678
function Compress-TestContent {
667679
[CmdletBinding()]
668680
param(
@@ -824,6 +836,17 @@ function New-PSOptions {
824836

825837
# Get the Options of the last build
826838
function Get-PSOptions {
839+
param(
840+
[Parameter(HelpMessage='Defaults to New-PSOption if a build has not ocurred.')]
841+
[switch]
842+
$DefaultToNew
843+
)
844+
845+
if(!$script:Options -and $DefaultToNew.IsPresent)
846+
{
847+
return New-PSOptions
848+
}
849+
827850
return $script:Options
828851
}
829852

@@ -916,10 +939,8 @@ function Publish-PSTestTools {
916939
@{Path="${PSScriptRoot}/test/tools/TestExe";Output="testexe"}
917940
@{Path="${PSScriptRoot}/test/tools/WebListener";Output="WebListener"}
918941
)
919-
if ($null -eq $Options)
920-
{
921-
$Options = New-PSOptions
922-
}
942+
943+
$Options = Get-PSOptions -DefaultToNew
923944

924945
# Publish tools so it can be run by tests
925946
foreach ($tool in $tools)
@@ -947,7 +968,7 @@ function Start-PSPester {
947968
[string[]]$Tag = @("CI","Feature"),
948969
[string[]]$Path = @("$PSScriptRoot/test/common","$PSScriptRoot/test/powershell"),
949970
[switch]$ThrowOnFailure,
950-
[string]$binDir = (Split-Path (New-PSOptions).Output),
971+
[string]$binDir = (Split-Path (Get-PSOptions -DefaultToNew).Output),
951972
[string]$powershell = (Join-Path $binDir 'pwsh'),
952973
[string]$Pester = ([IO.Path]::Combine($binDir, "Modules", "Pester")),
953974
[Parameter(ParameterSetName='Unelevate',Mandatory=$true)]
@@ -963,8 +984,8 @@ function Start-PSPester {
963984
{
964985
Write-Warning @"
965986
Pester module not found.
966-
Make sure that the proper git submodules are installed by running:
967-
git submodule update --init
987+
Restore the module to '$Pester' by running:
988+
Restore-PSPester
968989
"@
969990
return;
970991
}
@@ -2414,7 +2435,7 @@ function Restore-GitModule
24142435

24152436
$gitItems = Join-Path -Path $clonePath -ChildPath '.git*'
24162437
$ymlItems = Join-Path -Path $clonePath -ChildPath '*.yml'
2417-
Get-ChildItem -Path $gitItems, $ymlItems | Remove-Item -Recurse -Force
2438+
Get-ChildItem -attributes hidden,normal -Path $gitItems, $ymlItems | Remove-Item -Recurse -Force
24182439
}
24192440
finally
24202441
{

docs/git/submodules.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
Submodules
2-
==========
1+
# Submodules
32

43
While most developers will not have to deal with submodules on a regular basis, those who do should read this information.
54
The submodules currently in this project are:
65

7-
- `src/Modules/Pester`: The Pester testing framework for PowerShell
8-
96
- `src/libpsl-native/test/googletest`: The GoogleTest framework for
107
Linux native code
118

129
[submodules]: https://www.git-scm.com/book/en/v2/Git-Tools-Submodules
1310

14-
Rebase and Fast-Forward Merge Pull Requests in Submodules
15-
=========================================================
11+
## Rebase and Fast-Forward Merge Pull Requests in Submodules
1612

17-
*This is not necessary in the superproject, only submodules!*
13+
Note: *This is not necessary in the superproject, only submodules!*
1814

1915
**DO NOT** commit updates unless absolutely necessary.
2016
When submodules must be updated, a separate Pull Request must be submitted, reviewed, and merged before updating the superproject.

docs/testing-guidelines/testing-guidelines.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
21
# Testing Guidelines
32

43
Testing is a critical and required part of the PowerShell project.
54

65
The Microsoft PowerShell team created nearly 100,000 tests over the last 12 years which we run as part of the release process for Windows PowerShell.
7-
Having all of those tests available for the initial release of PowerShell was not feasible, and we have targeted those tests which we believe will provide us the ability to catch regressions in the areas which have had the largest changes for PowerShell.
6+
Having all of those tests available for the initial release of PowerShell was not feasible,
7+
and we have targeted those tests which we believe will provide us the ability to catch regressions in the areas which have had the largest changes for PowerShell.
88
It is our intent to continue to release more and more of our tests until we have the coverage we need.
99

1010
For creating new tests, please review the [documents](https://github.com/PowerShell/PowerShell/tree/master/docs/testing-guidelines) on how to create tests for PowerShell.
@@ -69,7 +69,8 @@ The Pester framework allows `Describe` blocks to be tagged, and our CI system re
6969
One of the following tags must be used:
7070

7171
* `CI` - this tag indicates that the tests in the `Describe` block will be executed as part of the CI/PR process
72-
* `Feature` - tests with this tag will not be executed as part of the CI/PR process, but they will be executed on a daily basis as part of a `cron` driven build. They indicate that the test will be validating more behavior, or will be using remote network resources (ex: package management tests)
72+
* `Feature` - tests with this tag will not be executed as part of the CI/PR process, but they will be executed on a daily basis as part of a `cron` driven build.
73+
They indicate that the test will be validating more behavior, or will be using remote network resources (ex: package management tests)
7374
* `Scenario` - this tag indicates a larger scale test interacting with multiple areas of functionality and/or remote resources, these tests are also run daily.
7475

7576
Additionally, the tag:
@@ -105,39 +106,35 @@ Currently, we have a minuscule number of tests which are run by using xUnit.
105106
## Running tests outside of CI
106107

107108
When working on new features or fixes, it is natural to want to run those tests locally before making a PR.
108-
Two helper functions are part of the build.psm1 module to help with that:
109+
Three helper functions are part of the build.psm1 module to help with that:
109110

111+
* `Restore-PSPester` will restore Pester, which is needed to run `Start-PSPester`
110112
* `Start-PSPester` will execute all Pester tests which are run by the CI system
111113
* `Start-PSxUnit` will execute the available xUnit tests run by the CI system
112114

113115
Our CI system runs these as well; there should be no difference between running these on your dev system, versus in CI.
114116

115-
Make sure that the git submodules have been loaded into your project before running `Start-PSPester`, or it will fail to run.
116-
If you did not clone the project with the `--recursive` flag, you can load the submodules by running:
117-
118-
```
119-
git submodule update --init
120-
```
117+
Make sure that you run `Restore-PSPester` before running `Start-PSPester`, or it will fail to run.
121118

122119
When running tests in this way, be sure that you have started PowerShell with `-noprofile` as some tests will fail if the
123120
environment is not the default or has any customization.
124121

125122
For example, to run all the Pester tests for CI (assuming you are at the root of the PowerShell repo):
126123

127-
```
124+
```PowerShell
128125
Import-Module ./build.psm1
129126
Start-PSPester
130127
```
131128

132129
If you wish to run specific tests, that is possible as well:
133130

134-
```
131+
```PowerShell
135132
Start-PSPester -Path test/powershell/engine/Api
136133
```
137134

138135
Or a specific Pester test file:
139136

140-
```
137+
```PowerShell
141138
Start-PSPester -Path test/powershell/engine/Api/XmlAdapter.Tests.ps1
142139
```
143140

@@ -148,8 +145,6 @@ in Microsoft's internal test frameworks.
148145
The tests that you created for your change and the library of historical tests will be run to determine if any regressions are present.
149146
If these tests find regressions, you'll be notified that your PR is not ready, and provided with enough information to investigate why the failure happened.
150147

151-
152-
153148
## Test Layout
154149

155150
We have taken a functional approach to the layout of our Pester tests and you should place new tests in their appropriate location.

test/common/markdown/markdown.tests.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,13 @@ Describe 'Common Tests - Validate Markdown Files' -Tag 'CI' {
7676
'./docs/*.md'
7777
'./docs/building/*.md'
7878
'./docs/cmdlet-example/*.md'
79+
'./docs/git/submodules.md'
7980
'./docs/installation/*.md'
8081
'./docs/maintainers/README.md'
82+
'./docs/testing-guidelines/testing-guidelines.md'
8183
'./demos/SSHRemoting/*.md'
8284
'./docker/*.md'
85+
'./test/powershell/README.md'
8386
'./tools/*.md'
8487
'./.github/CONTRIBUTING.md'
8588
)

test/powershell/README.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
Pester Testing Test Guide
2-
=========================
1+
# Pester Testing Test Guide
32

43
Also see the [Writing Pester Tests](../../docs/testing-guidelines/WritingPesterTests.md)
54
document.
65

7-
Running Pester Tests
8-
--------------------
6+
## Running Pester Tests
97

10-
Go to the top level of the PowerShell repository and run: `Start-PSPester`
8+
First, restore the correct version of Pester using `Restore-PSPester`.
9+
10+
Then, go to the top level of the PowerShell repository and run: `Start-PSPester`
1111
inside a self-hosted copy of PowerShell.
1212

1313
You can use `Start-PSPester -Tests SomeTestSuite*` to limit the tests run.
1414

15-
Testing new `powershell` processes
16-
----------------------------------
15+
## Testing new `powershell` processes
1716

1817
Any launch of a new `powershell` process must include `-noprofile` so that
1918
modified user and system profiles do not causes tests to fail. You also must
@@ -27,8 +26,7 @@ Example:
2726
& $powershell -noprofile -command "ExampleCommand" | Should Be "ExampleOutput"
2827
```
2928

30-
Portability
31-
-----------
29+
## Portability
3230

3331
Some tests simply must be tied to certain platforms. Use Pester's
3432
`-Skip` directive on an `It` statement to do this. For instance to run
@@ -44,8 +42,7 @@ Or only on Linux and OS X:
4442
It "Should do something on Linux" -Skip:$IsWindows { ... }
4543
```
4644

47-
Pending
48-
-------
45+
## Pending
4946

5047
When writing a test that should pass, but does not, please do not skip or delete
5148
the test, but use `It "Should Pass" -Pending` to mark the test as pending, and

0 commit comments

Comments
 (0)