Skip to content

Use a dedicated configuration directory path for all-users profiles on non-windows systems. #25748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -233,16 +233,27 @@ internal static string GetFullProfileFileName(string shellId, bool forCurrentUse
/// <returns>The base path for all users profiles.</returns>
private static string GetAllUsersFolderPath(string shellId)
{
string folderPath = string.Empty;
try
string folderPath = Environment.GetEnvironmentVariable("POWERSHELL_COMMON_APPLICATION_DATA");
if (!string.IsNullOrEmpty(folderPath)) return folderPath;

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
folderPath = Utils.GetApplicationBase(shellId);
try
{
return Utils.GetApplicationBase(shellId);
}
catch (System.Security.SecurityException)
{
return string.Empty;
}
}
catch (System.Security.SecurityException)

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return "/Library/Application Support/PowerShell";
}

return folderPath;
return "/etc/opt/powershell";
}
#endregion GetProfileCommands

Expand Down
37 changes: 27 additions & 10 deletions test/powershell/Host/Base-Directory.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@ Describe "Configuration file locations" -tags "CI","Slow" {
{
$ProductName = "PowerShell"
}
$expectedCache = [IO.Path]::Combine($env:LOCALAPPDATA, "Microsoft", "Windows", "PowerShell", "StartupProfileData-NonInteractive")
$expectedModule = [IO.Path]::Combine($env:USERPROFILE, "Documents", $ProductName, "Modules")
$expectedProfile = [io.path]::Combine($env:USERPROFILE, "Documents", $ProductName, $profileName)
$expectedReadline = [IO.Path]::Combine($env:AppData, "Microsoft", "Windows", "PowerShell", "PSReadline", "ConsoleHost_history.txt")
$expectedCache = [IO.Path]::Combine($env:LOCALAPPDATA, "Microsoft", "Windows", "PowerShell", "StartupProfileData-NonInteractive")
$expectedModule = [IO.Path]::Combine($env:USERPROFILE, "Documents", $ProductName, "Modules")
$expectedAllUsersProfile = [IO.Path]::Combine($PSHOME, $profileName)
$expectedCurrentUserProfile = [IO.Path]::Combine($env:USERPROFILE, "Documents", $ProductName, $profileName)
$expectedReadline = [IO.Path]::Combine($env:AppData, "Microsoft", "Windows", "PowerShell", "PSReadline", "ConsoleHost_history.txt")
} else {
$expectedCache = [IO.Path]::Combine($env:HOME, ".cache", "powershell", "StartupProfileData-NonInteractive")
$expectedModule = [IO.Path]::Combine($env:HOME, ".local", "share", "powershell", "Modules")
$expectedProfile = [io.path]::Combine($env:HOME,".config","powershell",$profileName)
$expectedReadline = [IO.Path]::Combine($env:HOME, ".local", "share", "powershell", "PSReadLine", "ConsoleHost_history.txt")
if ($IsMacOS) {
$PowershellConfigRoot = "/Library/Application Support/PowerShell"
} else {
$PowershellConfigRoot = "/etc/opt/powershell"
}

$expectedCache = [IO.Path]::Combine($env:HOME, ".cache", "powershell", "StartupProfileData-NonInteractive")
$expectedModule = [IO.Path]::Combine($env:HOME, ".local", "share", "powershell", "Modules")
$expectedAllUsersProfile = [IO.Path]::Combine($PowershellConfigRoot, $profileName)
$expectedCurrentUserProfile = [IO.Path]::Combine($env:HOME,".config","powershell", $profileName)
$expectedReadline = [IO.Path]::Combine($env:HOME, ".local", "share", "powershell", "PSReadLine", "ConsoleHost_history.txt")
}

$ItArgs = @{}
Expand All @@ -39,8 +47,17 @@ Describe "Configuration file locations" -tags "CI","Slow" {
$env:PSModulePath = $original_PSModulePath
}

It @ItArgs "Profile location should be correct" {
& $powershell -noprofile -c `$PROFILE | Should -Be $expectedProfile
It @ItArgs "Current User Profile location should be correct" {
& $powershell -noprofile -c `$PROFILE | Should -Be $expectedCurrentUserProfile
}

It @ItArgs "All Users Profile location should be correct" {
& $powershell -noprofile -c `$PROFILE.AllUsersAllHosts | Should -Be $expectedAllUsersProfile
}

It @ItArgs "All Users Profile location should be correct" {
$env:POWERSHELL_COMMON_APPLICATION_DATA = Join-Path -Path $PSHOME -ChildPath $profileName
& $powershell -noprofile -c `$PROFILE.AllUsersAllHosts | Should -Be $env:POWERSHELL_COMMON_APPLICATION_DATA
}

It @ItArgs "PSModulePath should contain the correct path" {
Expand Down