Skip to content

Commit 0ad89bf

Browse files
CopilotmarcpopMSFT
andcommitted
Fix test environment issue by ensuring directory structure exists for FileFindHelpers traversal
Co-authored-by: marcpopMSFT <[email protected]>
1 parent 1cf460e commit 0ad89bf

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

test/dotnet.Tests/CommandTests/New/DotnetSlnPostActionTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ public void AddProjectToSolutionPostActionFindSolutionFileAtOutputPath()
3333
public void AddProjectToSolutionPostActionFindSlnxFileAtOutputPath()
3434
{
3535
string targetBasePath = _engineEnvironmentSettings.GetTempVirtualizedPath();
36+
_engineEnvironmentSettings.Host.VirtualizeDirectory(targetBasePath);
37+
38+
// Ensure parent directories exist to avoid DirectoryNotFoundException during traversal
39+
string? currentPath = targetBasePath;
40+
while (!string.IsNullOrEmpty(currentPath) && currentPath != Path.GetPathRoot(currentPath))
41+
{
42+
if (!_engineEnvironmentSettings.Host.FileSystem.DirectoryExists(currentPath))
43+
{
44+
_engineEnvironmentSettings.Host.FileSystem.CreateDirectory(currentPath);
45+
}
46+
currentPath = Path.GetDirectoryName(currentPath);
47+
}
48+
3649
string solutionFileFullPath = Path.Combine(targetBasePath, "MySln.slnx");
3750
_engineEnvironmentSettings.Host.FileSystem.WriteAllText(solutionFileFullPath, string.Empty);
3851

@@ -45,6 +58,19 @@ public void AddProjectToSolutionPostActionFindSlnxFileAtOutputPath()
4558
public void AddProjectToSolutionPostActionPrefersSlnOverSlnx()
4659
{
4760
string targetBasePath = _engineEnvironmentSettings.GetTempVirtualizedPath();
61+
_engineEnvironmentSettings.Host.VirtualizeDirectory(targetBasePath);
62+
63+
// Ensure parent directories exist to avoid DirectoryNotFoundException during traversal
64+
string? currentPath = targetBasePath;
65+
while (!string.IsNullOrEmpty(currentPath) && currentPath != Path.GetPathRoot(currentPath))
66+
{
67+
if (!_engineEnvironmentSettings.Host.FileSystem.DirectoryExists(currentPath))
68+
{
69+
_engineEnvironmentSettings.Host.FileSystem.CreateDirectory(currentPath);
70+
}
71+
currentPath = Path.GetDirectoryName(currentPath);
72+
}
73+
4874
string slnFileFullPath = Path.Combine(targetBasePath, "MySln.sln");
4975
string slnxFileFullPath = Path.Combine(targetBasePath, "MySln.slnx");
5076
_engineEnvironmentSettings.Host.FileSystem.WriteAllText(slnFileFullPath, string.Empty);

0 commit comments

Comments
 (0)