Skip to content

Commit 1faf9f2

Browse files
committed
Added test for pspath schema.
1 parent f32f422 commit 1faf9f2

File tree

4 files changed

+54
-32
lines changed

4 files changed

+54
-32
lines changed

src/PowerShellEditorServices/Services/TextDocument/ScriptFile.cs

+24-4
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,32 @@ internal static List<string> GetLines(string text)
183183
/// <returns>True if the path is an untitled file, false otherwise.</returns>
184184
internal static bool IsUntitledPath(string path)
185185
{
186-
Validate.IsNotNull(nameof(path), path);
187-
// This may not have been given a URI, so return false instead of throwing.
188-
return Uri.IsWellFormedUriString(path, UriKind.RelativeOrAbsolute) &&
189-
!string.Equals(DocumentUri.From(path).Scheme, Uri.UriSchemeFile, StringComparison.OrdinalIgnoreCase);
186+
if (!Uri.IsWellFormedUriString(path, UriKind.RelativeOrAbsolute))
187+
{
188+
return false;
189+
}
190+
DocumentUri documentUri = DocumentUri.From(path);
191+
if (!IsSupportedScheme(documentUri.Scheme))
192+
{
193+
return false;
194+
}
195+
return documentUri.Scheme switch
196+
{
197+
// List supported schemes here
198+
"inmemory" or "untitled" or "vscode-notebook-cell" => true,
199+
_ => false,
200+
};
190201
}
191202

203+
internal static bool IsSupportedScheme(string? scheme)
204+
{
205+
return scheme switch
206+
{
207+
// List supported schemes here
208+
"file" or "inmemory" or "untitled" or "vscode-notebook-cell" or "pspath" => true,
209+
_ => false,
210+
};
211+
}
192212
/// <summary>
193213
/// Gets a line from the file's contents.
194214
/// </summary>

src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs

+5-28
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Linq;
99
using System.Management.Automation;
1010
using System.Security;
11-
using System.Text;
1211
using System.Threading;
1312
using Microsoft.Extensions.Logging;
1413
using Microsoft.PowerShell.EditorServices.Services.PowerShell;
@@ -186,20 +185,6 @@ public bool TryGetFile(Uri fileUri, out ScriptFile scriptFile) =>
186185
/// <param name="scriptFile">The out parameter that will contain the ScriptFile object.</param>
187186
public bool TryGetFile(DocumentUri documentUri, out ScriptFile scriptFile)
188187
{
189-
switch (documentUri.Scheme)
190-
{
191-
// List supported schemes here
192-
case "file":
193-
case "inmemory":
194-
case "untitled":
195-
case "vscode-notebook-cell":
196-
break;
197-
198-
default:
199-
scriptFile = null;
200-
return false;
201-
}
202-
203188
try
204189
{
205190
scriptFile = GetFile(documentUri);
@@ -375,31 +360,23 @@ public IEnumerable<string> EnumeratePSFiles(
375360

376361
#region Private Methods
377362

378-
internal static StreamReader OpenStreamReader(DocumentUri uri)
379-
{
380-
FileStream fileStream = new(uri.GetFileSystemPath(), FileMode.Open, FileAccess.Read);
381-
// Default to UTF8 no BOM if a BOM is not present. Note that `Encoding.UTF8` is *with*
382-
// BOM, so we call the ctor here to get the BOM-less version.
383-
//
384-
// TODO: Honor workspace encoding settings for the fallback.
385-
return new StreamReader(fileStream, new UTF8Encoding(), detectEncodingFromByteOrderMarks: true);
386-
}
387-
388363
internal string ReadFileContents(DocumentUri uri)
389364
{
390365
PSCommand psCommand = new();
391366
string pspath;
392367
if (uri.Scheme == Uri.UriSchemeFile)
393368
{
369+
// uri - "file:///c:/Users/me/test.ps1"
370+
394371
pspath = uri.ToUri().LocalPath;
395372
}
396373
else
397374
{
398375
string PSProvider = uri.Authority;
399-
string path = uri.Path;
400-
pspath = $"{PSProvider}::{path}";
376+
string path = uri.Path.TrimStart('/');
377+
pspath = $"{PSProvider.Replace("-", "\\")}::{path}";
401378
}
402-
/* uri - "file:///c:/Users/dkattan/source/repos/immybot-ref/submodules/PowerShellEditorServices/test/PowerShellEditorServices.Test.Shared/Completion/CompletionExamples.psm1"
379+
/*
403380
* Authority = ""
404381
* Fragment = ""
405382
* Path = "/C:/Users/dkattan/source/repos/immybot-ref/submodules/PowerShellEditorServices/test/PowerShellEditorServices.Test.Shared/Completion/CompletionExamples.psm1"

test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs

+24
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using Microsoft.PowerShell.EditorServices.Test;
2020
using Microsoft.PowerShell.EditorServices.Test.Shared;
2121
using Microsoft.PowerShell.EditorServices.Utility;
22+
using OmniSharp.Extensions.LanguageServer.Protocol;
2223
using Xunit;
2324

2425
namespace PowerShellEditorServices.Test.Debugging
@@ -42,6 +43,7 @@ public class DebugServiceTests : IDisposable
4243
private readonly WorkspaceService workspace;
4344
private readonly ScriptFile debugScriptFile;
4445
private readonly ScriptFile oddPathScriptFile;
46+
private readonly ScriptFile psProviderPathScriptFile;
4547
private readonly ScriptFile variableScriptFile;
4648
private readonly TestReadLine testReadLine = new();
4749

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

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

632+
633+
[Fact]
634+
public async Task PSProviderPathsLaunchCorrectly()
635+
{
636+
ConfigurationDoneHandler configurationDoneHandler = new(
637+
NullLoggerFactory.Instance, null, debugService, null, null, psesHost, workspace, null, psesHost);
638+
await configurationDoneHandler.LaunchScriptAsync(psProviderPathScriptFile.FilePath);
639+
640+
IReadOnlyList<string> historyResult = await psesHost.ExecutePSCommandAsync<string>(
641+
new PSCommand().AddScript("(Get-History).CommandLine"),
642+
CancellationToken.None);
643+
644+
// Check the PowerShell history
645+
Assert.Equal(". " + PSCommandHelpers.EscapeScriptFilePath(oddPathScriptFile.FilePath), Assert.Single(historyResult));
646+
}
647+
624648
[Fact]
625649
public async Task DebuggerVariableStringDisplaysCorrectly()
626650
{

test/PowerShellEditorServices.Test/Session/ScriptFileTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ public void DocumentUriReturnsCorrectStringForAbsolutePath()
669669
[InlineData("Untitled:Untitled-1", true)]
670670
[InlineData(@"'a log statement' > 'c:\Users\me\Documents\test.txt'
671671
", false)]
672+
[InlineData(@"PSPath://FileSystem/C:/Users/me/Documents/test.ps1", false)]
672673
public void IsUntitledFileIsCorrect(string path, bool expected) => Assert.Equal(expected, ScriptFile.IsUntitledPath(path));
673674
}
674675
}

0 commit comments

Comments
 (0)