Skip to content

Commit

Permalink
Instead of trying to delete the Zap directory use Zap1, Zap2, etc to …
Browse files Browse the repository at this point in the history
…avoid reporting conflicts.
  • Loading branch information
sarahelsaig committed Jan 10, 2024
1 parent b225f61 commit 3127ba7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
19 changes: 19 additions & 0 deletions Lombiq.Tests.UI/Helpers/DirectoryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,23 @@ public static void SafelyDeleteDirectoryIfExists(string path, int maxTryCount =
}
}
}

/// <summary>
/// Creates a directory with the given path and a numeric suffix from 1 to <see cref="int.MaxValue"/>. This means if
/// the <paramref name="path"/> is <c>c:\MyDirectory</c> then it will first attempt to create <c>c:\MyDirectory1</c>
/// but if that already exists it will try to create <c>c:\MyDirectory2</c> instead, and so on.
/// </summary>
/// <param name="path">The base path to use when constructing the final path.</param>
/// <returns>The final path created.</returns>
public static string CreateEnumeratedDirectory(string path)

Check failure on line 52 in Lombiq.Tests.UI/Helpers/DirectoryHelper.cs

View workflow job for this annotation

GitHub Actions / publish-nuget / publish-nuget

CS0161: 'DirectoryHelper.CreateEnumeratedDirectory(string)': not all code paths return a value [/home/runner/work/UI-Testing-Toolbox/UI-Testing-Toolbox/Lombiq.Tests.UI/Lombiq.Tests.UI.csproj]
{
for (var i = 1; i < int.MaxValue; i++)
{
var newPath = path + i.ToTechnicalString();
if (Directory.Exists(newPath)) continue;

Directory.CreateDirectory(newPath);
return newPath;
}
}
}
27 changes: 3 additions & 24 deletions Lombiq.Tests.UI/SecurityScanning/ZapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,11 @@ public async Task<SecurityScanResult> RunSecurityScanAsync(
automationFrameworkYamlPath = AutomationFrameworkPlanPaths.BaselinePlanPath;
}

var mountedDirectoryPath = DirectoryPaths.GetTempSubDirectoryPath(context.Id, "Zap");
// Each attempt will have it's own "ZapN" directory inside the temp, starting with "Zap1".
var mountedDirectoryPath = DirectoryHelper.CreateEnumeratedDirectory(
DirectoryPaths.GetTempSubDirectoryPath(context.Id, "Zap"));
var reportsDirectoryPath = Path.Combine(mountedDirectoryPath, _zapReportsDirectoryName);

// If there is already a Zap directory, delete it before recreating the necessary directory structure. This is
// only needed if a scan has already been executed once for the current context. Deleting the existing "Zap"
// directory ensures a clean slate.
if (Directory.Exists(mountedDirectoryPath))
{
var temporaryPath = $"{mountedDirectoryPath}-{Guid.NewGuid():N}";

// The directory is first renamed with a random suffix. This way even if the deletion is failed, the test
// should continue without problems. If this rename fails, that's fatal and should throw.
Directory.Move(mountedDirectoryPath, temporaryPath);

try
{
DirectoryHelper.SafelyDeleteDirectoryIfExists(temporaryPath);
}
catch (IOException exception)
{
_testOutputHelper.WriteLineTimestampedAndDebug(
$"Failed to delete existing Zap directory ({exception.Message}). This is not a fatal error " +
$"because the directory has already been renamed.");
}
}

Directory.CreateDirectory(reportsDirectoryPath);

// Giving write permission to all users to the reports folder. This is to avoid issues under GitHub-hosted
Expand Down

0 comments on commit 3127ba7

Please sign in to comment.