diff --git a/src/Microsoft.TemplateEngine.Utils/InMemoryFileSystem.cs b/src/Microsoft.TemplateEngine.Utils/InMemoryFileSystem.cs index 7bdfa45d325..e0d5d9927d9 100644 --- a/src/Microsoft.TemplateEngine.Utils/InMemoryFileSystem.cs +++ b/src/Microsoft.TemplateEngine.Utils/InMemoryFileSystem.cs @@ -696,58 +696,12 @@ public IDisposable WatchFileChanges(string filePath, FileSystemEventHandler file private bool IsPathInCone(string path, out string processedPath) { - if (!Path.IsPathRooted(path)) - { - path = Path.Combine(GetCurrentDirectory(), path); - } - - path = path.Replace('\\', '/'); - - bool leadSlash = path[0] == '/'; - - if (leadSlash) - { - path = path.Substring(1); - } - - string[] parts = path.Split('/'); - - List realParts = new(); - - for (int i = 0; i < parts.Length; ++i) - { - if (string.IsNullOrEmpty(parts[i])) - { - continue; - } - - switch (parts[i]) - { - case ".": - continue; - case "..": - realParts.RemoveAt(realParts.Count - 1); - break; - default: - realParts.Add(parts[i]); - break; - } - } - - if (leadSlash) - { - realParts.Insert(0, string.Empty); - } - - processedPath = string.Join(Path.DirectorySeparatorChar + string.Empty, realParts); - if (processedPath.Equals(_root.FullPath) || processedPath.StartsWith(_root.FullPath.TrimEnd('/', '\\') + Path.DirectorySeparatorChar)) - { - return true; - } - else - { - return false; - } + processedPath = Path.IsPathRooted(path) ? path : Path.Combine(GetCurrentDirectory(), path); + processedPath = processedPath + .Replace('/', Path.DirectorySeparatorChar) + .Replace('\\', Path.DirectorySeparatorChar) + .Replace($"{Path.DirectorySeparatorChar}.{Path.DirectorySeparatorChar}", $"{Path.DirectorySeparatorChar}"); + return processedPath.StartsWith(_root.FullPath); } private class FileSystemDirectory diff --git a/test/Microsoft.TemplateEngine.Utils.UnitTests/InMemoryFileSystemTests.cs b/test/Microsoft.TemplateEngine.Utils.UnitTests/InMemoryFileSystemTests.cs index 7da80beb1b1..0828fc5f65d 100644 --- a/test/Microsoft.TemplateEngine.Utils.UnitTests/InMemoryFileSystemTests.cs +++ b/test/Microsoft.TemplateEngine.Utils.UnitTests/InMemoryFileSystemTests.cs @@ -22,5 +22,20 @@ public void VerifyMultipleVirtualizationsAreHandled() Assert.True(virtualized1.FileExists(testFilePath)); Assert.True(virtualized2.FileExists(testFilePath)); } + + [Fact] + public void VerifyRootCanBeVirtualized() + { + IPhysicalFileSystem mockFileSystem = new MockFileSystem(); + IPhysicalFileSystem virtualized = new InMemoryFileSystem(Path.GetPathRoot(Directory.GetCurrentDirectory())!, mockFileSystem); + + string testFilePath = Path.Combine(Directory.GetCurrentDirectory(), "test.txt"); + virtualized.WriteAllText(testFilePath, "test"); + + var entries = virtualized.EnumerateFileSystemEntries(Directory.GetCurrentDirectory(), "*", SearchOption.TopDirectoryOnly).ToList(); + Assert.Single(entries); + + Assert.Equal(testFilePath, entries[0]); + } } }