Fixing the path to the user's JSON configuration file

This commit is contained in:
Mike Richmond 2016-08-22 16:20:15 -07:00
parent 1d1bf1942c
commit 18f3589d68
4 changed files with 108 additions and 20 deletions

View file

@ -146,7 +146,7 @@ namespace System.Management.Automation
//
// Sets the per-user configuration directory
//
appDataConfigDirectory = Utils.GetUserSettingsDirectory();
appDataConfigDirectory = Utils.GetUserConfigurationDirectory();
if (!Directory.Exists(appDataConfigDirectory))
{
try
@ -166,11 +166,11 @@ namespace System.Management.Automation
/// not interfere with PowerShell initialization
/// </summary>
/// <returns>Returns the directory if present or creatable. Throws otherwise.</returns>
private string GetAppDataConfigDirectory()
private string GetCurrentUserConfigDirectory()
{
if (null == appDataConfigDirectory)
{
string tempAppDataConfigDir = Utils.GetUserSettingsDirectory();
string tempAppDataConfigDir = Utils.GetUserConfigurationDirectory();
if (!Directory.Exists(tempAppDataConfigDir))
{
Directory.CreateDirectory(tempAppDataConfigDir);
@ -194,7 +194,7 @@ namespace System.Management.Automation
// Defaults to system wide.
if (PropertyScope.CurrentUser == scope)
{
scopeDirectory = GetAppDataConfigDirectory();
scopeDirectory = GetCurrentUserConfigDirectory();
}
string fileName = Path.Combine(scopeDirectory, configFileName);
@ -229,7 +229,7 @@ namespace System.Management.Automation
// Defaults to system wide.
if(PropertyScope.CurrentUser == scope)
{
scopeDirectory = GetAppDataConfigDirectory();
scopeDirectory = GetCurrentUserConfigDirectory();
}
string fileName = Path.Combine(scopeDirectory, configFileName);
@ -250,7 +250,7 @@ namespace System.Management.Automation
// Defaults to system wide.
if (PropertyScope.CurrentUser == scope)
{
scopeDirectory = GetAppDataConfigDirectory();
scopeDirectory = GetCurrentUserConfigDirectory();
}
string fileName = Path.Combine(scopeDirectory, configFileName);
@ -265,7 +265,7 @@ namespace System.Management.Automation
// Defaults to system wide.
if (PropertyScope.CurrentUser == scope)
{
scopeDirectory = GetAppDataConfigDirectory();
scopeDirectory = GetCurrentUserConfigDirectory();
}
string fileName = Path.Combine(scopeDirectory, configFileName);

View file

@ -282,16 +282,16 @@ namespace System.Management.Automation
/// <summary>
/// Specifies the per-user configuration settings directory in a platform agnostic manner.
/// Windows Ex:
/// %LOCALAPPDATA%\PowerShell
/// Non-Windows Ex:
/// ~/.config/PowerShell
/// </summary>
/// <returns>The current user's configuration settings directory</returns>
internal static string GetUserSettingsDirectory()
internal static string GetUserConfigurationDirectory()
{
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
return Path.Combine(appDataPath, "PowerShell");
#if UNIX
return Platform.SelectProductNameForDirectory(Platform.XDG_Type.CONFIG);
#else
string basePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
return IO.Path.Combine(basePath, Utils.ProductNameForDirectory);
#endif
}
private static string[] GetProductFolderDirectories()

View file

@ -182,12 +182,7 @@ namespace System.Management.Automation
if (forCurrentUser)
{
#if UNIX
basePath = Platform.SelectProductNameForDirectory(Platform.XDG_Type.CONFIG);
#else
basePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
basePath = IO.Path.Combine(basePath, Utils.ProductNameForDirectory);
#endif
basePath = Utils.GetUserConfigurationDirectory();
}
else
{

View file

@ -851,6 +851,99 @@ ZoneId=$FileType
}
}
Describe "Validate Set-ExecutionPolicy -Scope" -Tags "CI" {
BeforeAll {
$originalPolicies = Get-ExecutionPolicy -list
# Calls Set-ExecutionPolicy with a known-bad Scope and expects failure.
# It is defined here so that it will be available at It scope.
function VerfiyBlockedSetExecutionPolicy
{
param(
[string]
$policyScope
)
$fqeid = ""
try {
Set-ExecutionPolicy -Scope $policyScope -ExecutionPolicy Restricted
}
catch {
$fqeid = $_.FullyQualifiedErrorId
}
$fqeid | Should Be "CantSetGroupPolicy,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand"
}
}
AfterAll {
foreach ($scopedPolicy in $originalPolicies)
{
if (($scopedPolicy.Scope -eq "Process") -or
($scopedPolicy.Scope -eq "CurrentUser"))
{
try {
Set-ExecutionPolicy -Scope $scopedPolicy.Scope -ExecutionPolicy $scopedPolicy.ExecutionPolicy -Force
}
catch {
if ($_.FullyQualifiedErrorId -ne "ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand")
{
# Re-throw unrecognized exceptions. Otherwise, swallow
# the exception that warns about overridden policies
throw $_
}
}
}
elseif($scopedPolicy.Scope -eq "LocalMachine")
{
try {
Set-ExecutionPolicy -Scope $scopedPolicy.Scope -ExecutionPolicy $scopedPolicy.ExecutionPolicy -Force
}
catch {
if ($_.FullyQualifiedErrorId -eq "System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand")
{
# Do nothing. Depending on the ownership of the file,
# regular users may or may not be able to set its
# value.
#
# When targetting the Registry, regular users cannot
# modify this value.
}
elseif ($_.FullyQualifiedErrorId -ne "ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand")
{
# Re-throw unrecognized exceptions. Otherwise, swallow
# the exception that warns about overridden policies
throw $_
}
}
}
}
}
It "-Scope MachinePolicy is not Modifiable" {
VerfiyBlockedSetExecutionPolicy "MachinePolicy"
}
It "-Scope UserPolicy is not Modifiable" {
VerfiyBlockedSetExecutionPolicy "UserPolicy"
}
It "-Scope Process is Settable" {
Set-ExecutionPolicy -Scope Process -ExecutionPolicy ByPass
Get-ExecutionPolicy -Scope Process | Should Be "ByPass"
}
It "-Scope CurrentUser is Settable" {
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy ByPass
Get-ExecutionPolicy -Scope CurrentUser | Should Be "ByPass"
}
# This test requires Administrator privileges on Windows.
It "-Scope LocalMachine is Settable" {
Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy ByPass
Get-ExecutionPolicy -Scope LocalMachine | Should Be "ByPass"
}
}
Describe "Validate that 'ConvertTo-SecureString -Key' and 'ConvertFrom-SecureString -Key' work on NanoServer and IoT" -Tags "CI" {