Skip to content

Improve $PROFILE variable and profile loading test #1832

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

Merged
merged 1 commit into from
Jun 16, 2022
Merged
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
@@ -1,3 +1,7 @@
function Assert-ProfileLoaded {
if (-not $PROFILE) {
throw
}

function Assert-ProfileLoaded {
return $true
}
}
53 changes: 25 additions & 28 deletions test/PowerShellEditorServices.Test/Session/PsesInternalHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,37 +110,34 @@ public async Task CanCancelExecutionWithMethod()
[Fact]
public async Task CanResolveAndLoadProfilesForHostId()
{
string[] expectedProfilePaths =
new string[]
{
PsesHostFactory.TestProfilePaths.AllUsersAllHosts,
PsesHostFactory.TestProfilePaths.AllUsersCurrentHost,
PsesHostFactory.TestProfilePaths.CurrentUserAllHosts,
PsesHostFactory.TestProfilePaths.CurrentUserCurrentHost
};

// Load the profiles for the test host name
await psesHost.LoadHostProfilesAsync(CancellationToken.None).ConfigureAwait(true);

// Ensure that all the paths are set in the correct variables
// and that the current user's host profile got loaded
PSCommand psCommand = new PSCommand().AddScript(
"\"$($profile.AllUsersAllHosts) " +
"$($profile.AllUsersCurrentHost) " +
"$($profile.CurrentUserAllHosts) " +
"$($profile.CurrentUserCurrentHost) " +
"$(Assert-ProfileLoaded)\"");

IReadOnlyList<string> result = await psesHost.ExecutePSCommandAsync<string>(psCommand, CancellationToken.None).ConfigureAwait(true);

string expectedString =
string.Format(
"{0} True",
string.Join(
" ",
expectedProfilePaths));

Assert.Equal(expectedString, result[0], ignoreCase: true);
// Ensure that the $PROFILE variable is a string with the value of CurrentUserCurrentHost.
IReadOnlyList<string> profileVariable = await psesHost.ExecutePSCommandAsync<string>(
new PSCommand().AddScript("$PROFILE"),
CancellationToken.None).ConfigureAwait(true);

Assert.Collection(profileVariable,
(p) => Assert.Equal(PsesHostFactory.TestProfilePaths.CurrentUserCurrentHost, p));

// Ensure that all the profile paths are set in the correct note properties.
IReadOnlyList<string> profileProperties = await psesHost.ExecutePSCommandAsync<string>(
new PSCommand().AddScript("$PROFILE | Get-Member -Type NoteProperty"),
CancellationToken.None).ConfigureAwait(true);

Assert.Collection(profileProperties,
(p) => Assert.Equal($"string AllUsersAllHosts={PsesHostFactory.TestProfilePaths.AllUsersAllHosts}", p, ignoreCase: true),
(p) => Assert.Equal($"string AllUsersCurrentHost={PsesHostFactory.TestProfilePaths.AllUsersCurrentHost}", p, ignoreCase: true),
(p) => Assert.Equal($"string CurrentUserAllHosts={PsesHostFactory.TestProfilePaths.CurrentUserAllHosts}", p, ignoreCase: true),
(p) => Assert.Equal($"string CurrentUserCurrentHost={PsesHostFactory.TestProfilePaths.CurrentUserCurrentHost}", p, ignoreCase: true));

// Ensure that the profile was loaded. The profile also checks that $PROFILE was defined.
IReadOnlyList<bool> profileLoaded = await psesHost.ExecutePSCommandAsync<bool>(
new PSCommand().AddScript("Assert-ProfileLoaded"),
CancellationToken.None).ConfigureAwait(true);

Assert.Collection(profileLoaded, (p) => Assert.True(p));
}

[Fact]
Expand Down