Skip to content

Commit

Permalink
Making app exe copying safer
Browse files Browse the repository at this point in the history
  • Loading branch information
Piedone committed Nov 3, 2021
1 parent 9cbc681 commit 9edfdef
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions Lombiq.Tests.UI/Services/OrchardCoreInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public sealed class OrchardCoreInstance : IWebApplicationInstance

private static readonly PortLeaseManager _portLeaseManager;
private static readonly ConcurrentDictionary<string, bool> _exeCopyMarkers = new();
private static readonly object _exeCopyLock = new();

private readonly OrchardCoreConfiguration _configuration;
private readonly ITestOutputHelper _testOutputHelper;
Expand Down Expand Up @@ -99,20 +100,26 @@ public async Task<Uri> StartUpAsync()
exePath,
key =>
{
var copyExePath = Path.Combine(
Path.GetDirectoryName(exePath),
"Lombiq.UITestingToolbox.AppUnderTest." + Path.GetFileName(exePath));

if (File.Exists(copyExePath) && File.GetLastWriteTimeUtc(copyExePath) < File.GetLastWriteTimeUtc(exePath))
// Using a lock because ConcurrentDictionary doesn't guarantee that two value factories won't
// run for the same key.
lock (_exeCopyLock)
{
File.Delete(copyExePath);
}
var copyExePath = Path.Combine(
Path.GetDirectoryName(exePath),
"Lombiq.UITestingToolbox.AppUnderTest." + Path.GetFileName(exePath));

if (!File.Exists(copyExePath)) File.Copy(exePath, copyExePath);
if (File.Exists(copyExePath) &&
File.GetLastWriteTimeUtc(copyExePath) < File.GetLastWriteTimeUtc(exePath))
{
File.Delete(copyExePath);
}

exePath = copyExePath;
if (!File.Exists(copyExePath)) File.Copy(exePath, copyExePath);

return true;
exePath = copyExePath;

return true;
}
});
}

Expand Down

0 comments on commit 9edfdef

Please sign in to comment.