Skip to content
Open
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
58 changes: 6 additions & 52 deletions src/Microsoft.TemplateEngine.Utils/InMemoryFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
}