Skip to content

Commit

Permalink
Added test for pspath schema.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkattan committed Feb 15, 2024
1 parent f32f422 commit 1faf9f2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 32 deletions.
28 changes: 24 additions & 4 deletions src/PowerShellEditorServices/Services/TextDocument/ScriptFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,32 @@ internal static List<string> GetLines(string text)
/// <returns>True if the path is an untitled file, false otherwise.</returns>
internal static bool IsUntitledPath(string path)
{
Validate.IsNotNull(nameof(path), path);
// This may not have been given a URI, so return false instead of throwing.
return Uri.IsWellFormedUriString(path, UriKind.RelativeOrAbsolute) &&
!string.Equals(DocumentUri.From(path).Scheme, Uri.UriSchemeFile, StringComparison.OrdinalIgnoreCase);
if (!Uri.IsWellFormedUriString(path, UriKind.RelativeOrAbsolute))
{
return false;
}
DocumentUri documentUri = DocumentUri.From(path);
if (!IsSupportedScheme(documentUri.Scheme))
{
return false;
}
return documentUri.Scheme switch
{
// List supported schemes here
"inmemory" or "untitled" or "vscode-notebook-cell" => true,
_ => false,
};
}

internal static bool IsSupportedScheme(string? scheme)
{
return scheme switch
{
// List supported schemes here
"file" or "inmemory" or "untitled" or "vscode-notebook-cell" or "pspath" => true,
_ => false,
};
}
/// <summary>
/// Gets a line from the file's contents.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Linq;
using System.Management.Automation;
using System.Security;
using System.Text;
using System.Threading;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Services.PowerShell;
Expand Down Expand Up @@ -186,20 +185,6 @@ public bool TryGetFile(Uri fileUri, out ScriptFile scriptFile) =>
/// <param name="scriptFile">The out parameter that will contain the ScriptFile object.</param>
public bool TryGetFile(DocumentUri documentUri, out ScriptFile scriptFile)
{
switch (documentUri.Scheme)
{
// List supported schemes here
case "file":
case "inmemory":
case "untitled":
case "vscode-notebook-cell":
break;

default:
scriptFile = null;
return false;
}

try
{
scriptFile = GetFile(documentUri);
Expand Down Expand Up @@ -375,31 +360,23 @@ public IEnumerable<string> EnumeratePSFiles(

#region Private Methods

internal static StreamReader OpenStreamReader(DocumentUri uri)
{
FileStream fileStream = new(uri.GetFileSystemPath(), FileMode.Open, FileAccess.Read);
// Default to UTF8 no BOM if a BOM is not present. Note that `Encoding.UTF8` is *with*
// BOM, so we call the ctor here to get the BOM-less version.
//
// TODO: Honor workspace encoding settings for the fallback.
return new StreamReader(fileStream, new UTF8Encoding(), detectEncodingFromByteOrderMarks: true);
}

internal string ReadFileContents(DocumentUri uri)
{
PSCommand psCommand = new();
string pspath;
if (uri.Scheme == Uri.UriSchemeFile)
{
// uri - "file:///c:/Users/me/test.ps1"

pspath = uri.ToUri().LocalPath;
}
else
{
string PSProvider = uri.Authority;
string path = uri.Path;
pspath = $"{PSProvider}::{path}";
string path = uri.Path.TrimStart('/');
pspath = $"{PSProvider.Replace("-", "\\")}::{path}";
}
/* uri - "file:///c:/Users/dkattan/source/repos/immybot-ref/submodules/PowerShellEditorServices/test/PowerShellEditorServices.Test.Shared/Completion/CompletionExamples.psm1"
/*
* Authority = ""
* Fragment = ""
* Path = "/C:/Users/dkattan/source/repos/immybot-ref/submodules/PowerShellEditorServices/test/PowerShellEditorServices.Test.Shared/Completion/CompletionExamples.psm1"
Expand Down
24 changes: 24 additions & 0 deletions test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Microsoft.PowerShell.EditorServices.Test;
using Microsoft.PowerShell.EditorServices.Test.Shared;
using Microsoft.PowerShell.EditorServices.Utility;
using OmniSharp.Extensions.LanguageServer.Protocol;
using Xunit;

namespace PowerShellEditorServices.Test.Debugging
Expand All @@ -42,6 +43,7 @@ public class DebugServiceTests : IDisposable
private readonly WorkspaceService workspace;
private readonly ScriptFile debugScriptFile;
private readonly ScriptFile oddPathScriptFile;
private readonly ScriptFile psProviderPathScriptFile;
private readonly ScriptFile variableScriptFile;
private readonly TestReadLine testReadLine = new();

Expand Down Expand Up @@ -74,6 +76,12 @@ public DebugServiceTests()
debugScriptFile = GetDebugScript("DebugTest.ps1");
oddPathScriptFile = GetDebugScript("Debug' W&ith $Params [Test].ps1");
variableScriptFile = GetDebugScript("VariableTest.ps1");
string variableScriptFilePath = TestUtilities.GetSharedPath(Path.Combine("Debugging", "VariableTest.ps1"));
dynamic psitem = psesHost.ExecutePSCommandAsync<dynamic>(new PSCommand().AddCommand("Get-Item").AddParameter("LiteralPath", variableScriptFilePath), CancellationToken.None).GetAwaiter().GetResult().FirstOrDefault();
Uri fileUri = new Uri(psitem.FullName);
string pspathUriString = new DocumentUri(scheme: "pspath", authority: $"{psitem.PSProvider.ToString().Replace("\\", "-")}", path: $"/{fileUri.AbsolutePath}", query: string.Empty, fragment: string.Empty).ToString();
// pspath://microsoft.powershell.core-filesystem/c:/Users/dkattan/source/repos/immybot-ref/submodules/PowerShellEditorServices/test/PowerShellEditorServices.Test.Shared/Debugging/VariableTest.ps1
psProviderPathScriptFile = workspace.GetFile(pspathUriString);
}

public void Dispose()
Expand Down Expand Up @@ -621,6 +629,22 @@ public async Task OddFilePathsLaunchCorrectly()
Assert.Equal(". " + PSCommandHelpers.EscapeScriptFilePath(oddPathScriptFile.FilePath), Assert.Single(historyResult));
}


[Fact]
public async Task PSProviderPathsLaunchCorrectly()
{
ConfigurationDoneHandler configurationDoneHandler = new(
NullLoggerFactory.Instance, null, debugService, null, null, psesHost, workspace, null, psesHost);
await configurationDoneHandler.LaunchScriptAsync(psProviderPathScriptFile.FilePath);

IReadOnlyList<string> historyResult = await psesHost.ExecutePSCommandAsync<string>(
new PSCommand().AddScript("(Get-History).CommandLine"),
CancellationToken.None);

// Check the PowerShell history
Assert.Equal(". " + PSCommandHelpers.EscapeScriptFilePath(oddPathScriptFile.FilePath), Assert.Single(historyResult));
}

[Fact]
public async Task DebuggerVariableStringDisplaysCorrectly()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ public void DocumentUriReturnsCorrectStringForAbsolutePath()
[InlineData("Untitled:Untitled-1", true)]
[InlineData(@"'a log statement' > 'c:\Users\me\Documents\test.txt'
", false)]
[InlineData(@"PSPath://FileSystem/C:/Users/me/Documents/test.ps1", false)]
public void IsUntitledFileIsCorrect(string path, bool expected) => Assert.Equal(expected, ScriptFile.IsUntitledPath(path));
}
}

0 comments on commit 1faf9f2

Please sign in to comment.