Skip to content

Commit 541822d

Browse files
committed
Add unit tests and PTLE catch - just in case.
1 parent ae975a1 commit 541822d

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/PowerShellEditorServices/Workspace/Workspace.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,10 @@ internal static bool IsPathInMemory(string filePath)
407407
{
408408
isInMemory = true;
409409
}
410+
catch (PathTooLongException)
411+
{
412+
// If we ever get here, it should be an actual file so, not in memory
413+
}
410414
}
411415

412416
return isInMemory;

test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,35 @@ public void CanResolveWorkspaceRelativePath()
3535
Assert.Equal(@"..\PeerPath\FilePath.ps1", workspace.GetRelativePath(testPathOutside));
3636
Assert.Equal(testPathAnotherDrive, workspace.GetRelativePath(testPathAnotherDrive));
3737
}
38+
39+
[Fact]
40+
public void CanDetermineIsPathInMemory()
41+
{
42+
var tempDir = Environment.GetEnvironmentVariable("TEMP");
43+
44+
var shortDirPath = Path.Combine(tempDir, "GitHub", "PowerShellEditorServices");
45+
var shortFilePath = Path.Combine(shortDirPath, "foo.ps1");
46+
var shortDirUriForm = new Uri(shortDirPath).ToString();
47+
var shortFileUriForm = new Uri(shortFilePath).ToString();
48+
49+
// Test short file absolute paths
50+
Assert.False(Workspace.IsPathInMemory(shortDirPath));
51+
Assert.False(Workspace.IsPathInMemory(shortFilePath));
52+
Assert.False(Workspace.IsPathInMemory(shortDirUriForm));
53+
Assert.False(Workspace.IsPathInMemory(shortFileUriForm));
54+
55+
// Test short file relative paths - not sure we'll ever get these but just in case
56+
Assert.False(Workspace.IsPathInMemory("foo.ps1"));
57+
Assert.False(Workspace.IsPathInMemory(".." + Path.DirectorySeparatorChar + "foo.ps1"));
58+
59+
// Test short non-file paths
60+
Assert.True(Workspace.IsPathInMemory("untitled:untitled-1"));
61+
var shortUriForm = "git:/c%3A/Users/Keith/GitHub/dahlbyk/posh-git/src/PoshGitTypes.ps1?%7B%22path%22%3A%22c%3A%5C%5CUsers%5C%5CKeith%5C%5CGitHub%5C%5Cdahlbyk%5C%5Cposh-git%5C%5Csrc%5C%5CPoshGitTypes.ps1%22%2C%22ref%22%3A%22~%22%7D";
62+
Assert.True(Workspace.IsPathInMemory(shortUriForm));
63+
64+
// Test long non-file path - known to have crashed PSES
65+
var longUriForm = "gitlens-git:c%3A%5CUsers%5CKeith%5CGitHub%5Cdahlbyk%5Cposh-git%5Csrc%5CPoshGitTypes%3Ae0022701.ps1?%7B%22fileName%22%3A%22src%2FPoshGitTypes.ps1%22%2C%22repoPath%22%3A%22c%3A%2FUsers%2FKeith%2FGitHub%2Fdahlbyk%2Fposh-git%22%2C%22sha%22%3A%22e0022701fa12e0bc22d0458673d6443c942b974a%22%7D";
66+
Assert.True(Workspace.IsPathInMemory(longUriForm));
67+
}
3868
}
3969
}

0 commit comments

Comments
 (0)