Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
Fix for 187
Browse files Browse the repository at this point in the history
  • Loading branch information
bgavrilMS committed Aug 3, 2022
1 parent fca7a55 commit 96dd5bc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Shared/CrossPlatLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public CrossPlatLock(string lockfilePath, int lockFileRetryDelay = LockfileRetry
FileStream fileStream = null;

// Create lock file dir if it doesn't already exist

Directory.CreateDirectory(Path.GetDirectoryName(lockfilePath));
string lockerProcessInfo = $"{SharedUtilities.GetCurrentProcessId()} {SharedUtilities.GetCurrentProcessName()}";

for (int tryCount = 0; tryCount < lockFileRetryCount; tryCount++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,40 @@ public class CrossPlatLockTests
const int NumTasks = 100;

public TestContext TestContext { get; set; }

[RunOnWindows]
[TestCategory(TestCategories.Regression)]
[WorkItem(187)] // https://github.com/AzureAD/microsoft-authentication-extensions-for-dotnet/issues/187
public void DirNotExists()
{
// Arrange

string cacheFileDir;

// ensure the cache directory does not exist
do
{
string tempDirName = System.IO.Path.GetRandomFileName();
cacheFileDir = Path.Combine(Path.GetTempPath(), tempDirName);

} while (Directory.Exists(cacheFileDir));

using (var crossPlatLock = new CrossPlatLock(
Path.Combine(cacheFileDir, "file.lockfile"), // the directory is guaranteed to not exist
100,
1))
{
// no-op
}

// before fixing the bug, an exception would occur here:
// System.InvalidOperationException: Could not get access to the shared lock file.
// ---> System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\....
}




#if NETCOREAPP
[TestMethod]
[Ignore] // Could not get this to run on CI build due to small differences in where the App file gets dropped
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
#pragma warning disable CA2000 // Dispose objects before losing scope

namespace Microsoft.Identity.Client.Extensions.Msal.UnitTests
{
Expand Down Expand Up @@ -361,8 +360,7 @@ public async Task RegressionTest_CorruptedCacheIsDeleted_NoEvent_Async()
Assert.IsFalse(data.Any(), "Cache is corrupt, so it should have been deleted");
}




[DoNotRunOnLinux] // The FileSystemWatcher on Linux doesn't always fire
public async Task ClearCacheUsesTheLockAsync()
{
Expand Down Expand Up @@ -547,4 +545,3 @@ public async Task EventNeedsConfigurationAsync()
}
}
}
#pragma warning restore CA2000 // Dispose objects before losing scope

0 comments on commit 96dd5bc

Please sign in to comment.