Skip to content
Closed
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
56 changes: 50 additions & 6 deletions test/powershell/engine/Help/HelpSystem.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ function UpdateHelpFromLocalContentPath
{
param ([string]$ModuleName, [string]$Tag = 'CI')

# Update-Help fails if module path is Not writable, so skip tests in this situation
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why the path can be read only if we run the tests with 'RequireAdminOnWindows' tag? Do you means Unix where we haven't still such tag?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we do not run the tests under sudo on Linux.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps we should not complicate these tests if we want to implement #5645?

Copy link
Member

Choose a reason for hiding this comment

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

I believe we need to go through a bunch to tests when #5645 is done. We can revert these changes then.

if ($moduleName -eq "Microsoft.PowerShell.Core")
{
if ($MicrosoftPowerShellCorePathIsReadOnly) { return }
}
else
{
$modulePath = (Get-Module -Name $ModuleName -ListAvailable).ModuleBase
$modulePathIsReadOnly = PathIsReadOnly $modulePath
if ($modulePathIsReadOnly) { return }
}

if ($Tag -eq 'CI')
{
$helpContentPath = Join-Path $PSScriptRoot "assets"
Expand Down Expand Up @@ -47,7 +59,7 @@ function RunTestCase
{
if ($cmdletsToSkip -notcontains $cmdletName)
{
It "Validate -Description and -Examples sections in help content. Run 'Get-help -name $cmdletName'" {
It "Validate -Description and -Examples sections in help content. Run 'Get-help -name $cmdletName'" -Skip:$MicrosoftPowerShellCorePathIsReadOnly {

$help = get-help -name $cmdletName
$help.Description | Out-String | Should Match $cmdletName
Expand All @@ -64,6 +76,28 @@ function RunTestCase
}
}

function PathIsReadOnly
{
param ([string]$Path)
# attempt to write to location and catch
$readonly = $false
try
{
$filepath = Join-Path $Path "testfile-deleteme.txt"
"test" | Out-File $filepath
}
catch
{
$readonly = $_.Exception.GetType().Name -eq 'UnauthorizedAccessException'
}
Remove-Item -Path $filepath -ErrorAction SilentlyContinue
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add -Force.


$readonly
}

$PsHomePathIsReadOnly = PathIsReadOnly $PSHOME
$MicrosoftPowerShellCorePathIsReadOnly = $PsHomePathIsReadOnly

Describe "Validate that <pshome>/<culture>/default.help.txt is present" -Tags @('CI') {

It "Get-Help returns information about the help system." {
Expand Down Expand Up @@ -184,9 +218,19 @@ Describe "Validate that Get-Help returns provider-specific help" -Tags @('CI', '
}

Describe "Validate about_help.txt under culture specific folder works" -Tags @('CI', 'RequireAdminOnWindows') {

BeforeAll {
$modulePath = "$pshome\Modules\Test"
$null = New-Item -Path $modulePath\en-US -ItemType Directory -Force
try
{
$null = New-Item -Path $modulePath\en-US -ItemType Directory -Force -ErrorAction Stop
}
catch
{
# $pshome is readonly-path for non-sudo on Linux, skip tests in this case
$skip = $_.Exception.GetType().Name -eq 'UnauthorizedAccessException'
if ($skip) {return}
}
New-ModuleManifest -Path $modulePath\test.psd1 -RootModule test.psm1
Set-Content -Path $modulePath\test.psm1 -Value "function foo{}"
Set-Content -Path $modulePath\en-US\about_testhelp.help.txt -Value "Hello" -NoNewline
Expand All @@ -201,26 +245,26 @@ Describe "Validate about_help.txt under culture specific folder works" -Tags @('
}

AfterAll {
Remove-Item $modulePath -Recurse -Force
Remove-Item $modulePath -Recurse -Force -ErrorAction SilentlyContinue
# Remove all the help content.
Get-ChildItem -Path $PSHOME -Include @('about_*.txt', "*help.xml") -Recurse | Remove-Item -Force -ErrorAction SilentlyContinue
}

It "Get-Help should return help text and not multiple HelpInfo objects when help is under `$pshome path" {
It "Get-Help should return help text and not multiple HelpInfo objects when help is under `$pshome path" -Skip:$skip {

$help = Get-Help about_testhelp
$help.count | Should Be 1
$help | Should BeExactly "Hello"
}

It "Get-Help for about_Variable should return only one help object" {
It "Get-Help for about_Variable should return only one help object" -Skip:$skip {
$help = Get-Help about_Variables
$help.count | Should Be 1
}
}

Describe "Get-Help should find help info within help files" -Tags @('CI', 'RequireAdminOnWindows') {
It "Get-Help should find help files under pshome" {
It "Get-Help should find help files under pshome" -Skip:$PsHomePathIsReadOnly {
$helpFile = "about_testCase.help.txt"
$culture = (Get-Culture).Name
$helpFolderPath = Join-Path $PSHOME $culture
Expand Down
35 changes: 34 additions & 1 deletion test/powershell/engine/Help/UpdatableHelpSystem.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,27 @@ $testCases = @{
$modulesInBox = @("Microsoft.PowerShell.Core"
Get-Module -ListAvailable | ForEach-Object{$_.Name}
)
function PathIsReadOnly
Copy link
Member

Choose a reason for hiding this comment

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

Can we move this function to a common file and then dot source it in both the test files.

{
param ([string]$Path)
# attempt to write to location and catch
$readonly = $false
try
{
$filepath = Join-Path $Path "testfile-deleteme.txt"
"test" | Out-File $filepath
}
catch
{
$readonly = $_.Exception.GetType().Name -eq 'UnauthorizedAccessException'
}
Remove-Item -Path $filepath -ErrorAction SilentlyContinue

$readonly
}

$PsHomePathIsReadOnly = PathIsReadOnly $PSHOME
$MicrosoftPowerShellCorePathIsReadOnly = $PsHomePathIsReadOnly

function GetFiles
{
Expand Down Expand Up @@ -174,8 +195,20 @@ function RunUpdateHelpTests
{
if ($powershellCoreModules -contains $moduleName)
{
if ($moduleName -eq "Microsoft.PowerShell.Core")
{
$skip = $MicrosoftPowerShellCorePathIsReadOnly
}
else
{
$modulePath = (Get-Module -Name $moduleName -ListAvailable).ModuleBase
$skip = PathIsReadOnly $modulePath
}

# -Skip and -$Pending are mutually exclusive on It block, so skip Pending tests
Copy link
Member

Choose a reason for hiding this comment

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

Skip and Pending have specific meanings. you can use splatting to choose one of them for the It block.

If ($Pending) {$skip = $true}

It "Validate Update-Help for module '$moduleName'" -Pending:$Pending {
It "Validate Update-Help for module '$moduleName'" -Skip:$skip {

# If the help file is already installed, delete it.
Get-ChildItem $testCases[$moduleName].HelpInstallationPath -Include @("*help.xml") -Recurse -ea SilentlyContinue |
Expand Down