Skip to content

Commit 1e53a65

Browse files
committed
avoid test flakeness caused by static field
1 parent 4606dea commit 1e53a65

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/Common/Commands.Common.Test/Common/ProfileCmdltsTests.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,15 @@ public void ClearAzureProfileClearsTokenCache()
152152
[Fact]
153153
public void DeleteCorruptedTokenCache()
154154
{
155-
ProfileClient.DataStore.WriteFile(ProtectedFileTokenCache.CacheFileName, new byte[]{0,1});
156-
ProtectedFileTokenCache tokenCache = ProtectedFileTokenCache.Instance;
157-
Assert.False(ProfileClient.DataStore.FileExists(ProtectedFileTokenCache.CacheFileName));
155+
//setup
156+
string testFileName = @"c:\foobar\TokenCache.dat";
157+
ProfileClient.DataStore.WriteFile(testFileName, new byte[] { 0, 1 });
158+
159+
//Act
160+
ProtectedFileTokenCache tokenCache = new ProtectedFileTokenCache(testFileName);
161+
162+
//Assert
163+
Assert.False(ProfileClient.DataStore.FileExists(testFileName));
158164
}
159165

160166
[Fact]

src/Common/Commands.Common/Authentication/ProtectedFileTokenCache.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Microsoft.WindowsAzure.Commands.Utilities.Common.Authentication
2626
/// </summary>
2727
public class ProtectedFileTokenCache : TokenCache
2828
{
29-
public static readonly string CacheFileName = Path.Combine(AzurePowerShell.ProfileDirectory, "TokenCache.dat");
29+
private static readonly string CacheFileName = Path.Combine(AzurePowerShell.ProfileDirectory, "TokenCache.dat");
3030

3131
private static readonly object fileLock = new object();
3232

@@ -44,29 +44,39 @@ public static ProtectedFileTokenCache Instance
4444
// Initializes the cache against a local file.
4545
// If the file is already present, it loads its content in the ADAL cache
4646
private ProtectedFileTokenCache()
47+
{
48+
Initialize(CacheFileName);
49+
}
50+
51+
private void Initialize(string fileName)
4752
{
4853
AfterAccess = AfterAccessNotification;
4954
BeforeAccess = BeforeAccessNotification;
5055
lock (fileLock)
5156
{
52-
if (ProfileClient.DataStore.FileExists(CacheFileName))
57+
if (ProfileClient.DataStore.FileExists(fileName))
5358
{
54-
var existingData = ProfileClient.DataStore.ReadFileAsBytes(CacheFileName);
59+
var existingData = ProfileClient.DataStore.ReadFileAsBytes(fileName);
5560
if (existingData != null)
5661
{
5762
try
5863
{
5964
Deserialize(ProtectedData.Unprotect(existingData, null, DataProtectionScope.CurrentUser));
6065
}
61-
catch (CryptographicException)
66+
catch (CryptographicException)
6267
{
63-
ProfileClient.DataStore.DeleteFile(CacheFileName);
68+
ProfileClient.DataStore.DeleteFile(fileName);
6469
}
6570
}
6671
}
6772
}
6873
}
6974

75+
public ProtectedFileTokenCache(string cacheFile)
76+
{
77+
Initialize(cacheFile);
78+
}
79+
7080
// Empties the persistent store.
7181
public override void Clear()
7282
{

0 commit comments

Comments
 (0)