Skip to content
Merged
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
57 changes: 36 additions & 21 deletions test/powershell/Modules/PowerShellGet/PowerShellGet.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $Initialized = $false

function IsInbox { $PSHOME.EndsWith('\WindowsPowerShell\v1.0', [System.StringComparison]::OrdinalIgnoreCase) }
function IsWindows { $PSVariable = Get-Variable -Name IsWindows -ErrorAction Ignore; return (-not $PSVariable -or $PSVariable.Value) }
function IsCoreCLR { $PSVariable = Get-Variable -Name IsCoreCLR -ErrorAction Ignore; return ($PSVariable -and $PSVariable.Value) }
function IsCoreCLR { $PSVersionTable.ContainsKey('PSEdition') -and $PSVersionTable.PSEdition -eq 'Core' }

#endregion

Expand All @@ -23,22 +23,26 @@ if(IsInbox)
{
$script:ProgramFilesPSPath = Microsoft.PowerShell.Management\Join-Path -Path $env:ProgramFiles -ChildPath "WindowsPowerShell"
}
else
elseif(IsCoreCLR){
if(IsWindows) {
$script:ProgramFilesPSPath = Microsoft.PowerShell.Management\Join-Path -Path $env:ProgramFiles -ChildPath 'PowerShell'
}
else {
$script:ProgramFilesPSPath = Split-Path -Path ([System.Management.Automation.Platform]::SelectProductNameForDirectory('SHARED_MODULES')) -Parent
}
}

try
{
$script:MyDocumentsFolderPath = [Environment]::GetFolderPath("MyDocuments")
}
catch
{
$script:ProgramFilesPSPath = $PSHome
$script:MyDocumentsFolderPath = $null
}

if(IsInbox)
{
try
{
$script:MyDocumentsFolderPath = [Environment]::GetFolderPath("MyDocuments")
}
catch
{
$script:MyDocumentsFolderPath = $null
}

$script:MyDocumentsPSPath = if($script:MyDocumentsFolderPath)
{
Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsFolderPath -ChildPath "WindowsPowerShell"
Expand All @@ -48,13 +52,22 @@ if(IsInbox)
Microsoft.PowerShell.Management\Join-Path -Path $env:USERPROFILE -ChildPath "Documents\WindowsPowerShell"
}
}
elseif(IsWindows)
{
$script:MyDocumentsPSPath = Microsoft.PowerShell.Management\Join-Path -Path $HOME -ChildPath 'Documents\PowerShell'
}
else
{
$script:MyDocumentsPSPath = Microsoft.PowerShell.Management\Join-Path -Path $HOME -ChildPath '.local/share/powershell'
elseif(IsCoreCLR) {
if(IsWindows)
{
$script:MyDocumentsPSPath = if($script:MyDocumentsFolderPath)
{
Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsFolderPath -ChildPath 'PowerShell'
}
else
{
Microsoft.PowerShell.Management\Join-Path -Path $HOME -ChildPath "Documents\PowerShell"
}
}
else
{
$script:MyDocumentsPSPath = Split-Path -Path ([System.Management.Automation.Platform]::SelectProductNameForDirectory('USER_MODULES')) -Parent
Copy link
Collaborator

Choose a reason for hiding this comment

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

we don't really have a way to handle this if you don't have the privilege to write in the directory on non-windows platforms, we can look at extending the test framework for next release.

I recommend a test hook approach, which allows you to provide an alternative location. From a test perspective, the only thing you want to be sure of is when you select a location the files get placed there. It doesn't really matter what that location is, it could be anywhere - A testhook which makes [system.management.automation.platform]::SelectProductNameForDirectory('SHARED_MODULES') point to $TESTDRIVE should be sufficient as long as you have a separate test which returns what the default location of SHARED_MODULES should be.
This provides far more flexibility in the long run
If you can't do that, I would mark this specific test as pending for non-windows

}
}

$script:ProgramFilesModulesPath = Microsoft.PowerShell.Management\Join-Path -Path $script:ProgramFilesPSPath -ChildPath 'Modules'
Expand Down Expand Up @@ -143,7 +156,8 @@ Describe "PowerShellGet - Module tests (Admin)" -tags @('Feature', 'RequireAdmin
Remove-InstalledModules
}

It "Should install a module correctly to the required location with default AllUsers scope" {
## Marked as 'Pending' on Linux for now because the test requires root privilege but we cannot do it now in our Travis CI Linux build
It "Should install a module correctly to the required location with default AllUsers scope" -Pending:$IsLinux {
Install-Module -Name $ContosoServer -Repository $RepositoryName
$installedModuleInfo = Get-InstalledModule -Name $ContosoServer

Expand Down Expand Up @@ -212,7 +226,8 @@ Describe "PowerShellGet - Script tests (Admin)" -tags @('Feature', 'RequireAdmin
Remove-InstalledScripts
}

It "Should install a script correctly to the required location with default AllUsers scope" {
## Marked as 'Pending' on Linux for now because the test requires root privilege but we cannot do it now in our Travis CI Linux build
It "Should install a script correctly to the required location with default AllUsers scope" -Pending:$IsLinux {
Install-Script -Name $FabrikamServerScript -Repository $RepositoryName -NoPathUpdate
$installedScriptInfo = Get-InstalledScript -Name $FabrikamServerScript

Expand Down