Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit b5f7a94

Browse files
Fixing Keychain to use the FileSystem instance from the Environment
1 parent 3f7bd6b commit b5f7a94

File tree

4 files changed

+43
-35
lines changed

4 files changed

+43
-35
lines changed

src/GitHub.Api/Authentication/Keychain.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class Keychain : IKeychain
3333
private readonly ILogging logger = Logging.GetLogger<Keychain>();
3434

3535
private readonly IEnvironment environment;
36-
private readonly IFileSystem fileSystem;
3736
private readonly ICredentialManager credentialManager;
3837
private readonly string cachePath;
3938

@@ -44,17 +43,17 @@ class Keychain : IKeychain
4443
private readonly Dictionary<UriString, KeychainAdapter> keychainAdapters
4544
= new Dictionary<UriString, KeychainAdapter>();
4645

47-
public Keychain(IEnvironment environment, IFileSystem fileSystem, ICredentialManager credentialManager)
46+
private IFileSystem FileSystem => environment.FileSystem;
47+
48+
public Keychain(IEnvironment environment, ICredentialManager credentialManager)
4849
{
4950
Guard.ArgumentNotNull(environment, nameof(environment));
50-
Guard.ArgumentNotNull(fileSystem, nameof(fileSystem));
5151
Guard.ArgumentNotNull(credentialManager, nameof(credentialManager));
5252

5353
Guard.NotNull(environment, environment.UserCachePath, nameof(environment.UserCachePath));
5454

5555
cachePath = environment.UserCachePath.Combine(ConnectionFile).ToString();
5656
this.environment = environment;
57-
this.fileSystem = fileSystem;
5857
this.credentialManager = credentialManager;
5958
}
6059

@@ -109,17 +108,17 @@ private void ReadCacheFromDisk()
109108

110109
ConnectionCacheItem[] connections = null;
111110

112-
if (fileSystem.FileExists(cachePath))
111+
if (FileSystem.FileExists(cachePath))
113112
{
114-
var json = fileSystem.ReadAllText(cachePath);
113+
var json = FileSystem.ReadAllText(cachePath);
115114
try
116115
{
117116
connections = SimpleJson.DeserializeObject<ConnectionCacheItem[]>(json);
118117
}
119118
catch (Exception ex)
120119
{
121120
logger.Error(ex, "Error deserializing connection cache: {0}", cachePath);
122-
fileSystem.FileDelete(cachePath);
121+
FileSystem.FileDelete(cachePath);
123122
}
124123
}
125124

@@ -148,7 +147,7 @@ private void WriteCacheToDisk()
148147
}).ToArray();
149148

150149
var json = SimpleJson.SerializeObject(connectionCacheItems);
151-
fileSystem.WriteAllText(cachePath, json);
150+
FileSystem.WriteAllText(cachePath, json);
152151
}
153152

154153
public async Task Clear(UriString host, bool deleteFromCredentialManager)

src/GitHub.Api/Platform/Platform.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public IPlatform Initialize(IProcessManager processManager, ITaskManager taskMan
2727
if (CredentialManager == null)
2828
{
2929
CredentialManager = new GitCredentialManager(Environment, processManager, taskManager);
30-
Keychain = new Keychain(Environment, Environment.FileSystem, CredentialManager);
30+
Keychain = new Keychain(Environment, CredentialManager);
3131
Keychain.Initialize();
3232
}
3333

src/tests/IntegrationTests/SetupFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public class SetupFixture
1010
[SetUp]
1111
public void Setup()
1212
{
13-
//Logging.TracingEnabled = true;
13+
Logging.TracingEnabled = true;
1414

15-
Logging.LogAdapter = new MultipleLogAdapter(new FileLogAdapter($"{DateTime.UtcNow.ToString("yyyyMMddHHmmss")}-integration-tests.log"));
15+
Logging.LogAdapter = new MultipleLogAdapter(new FileLogAdapter($"{DateTime.UtcNow.ToString("yyyyMMddHHmmss")}-integration-tests.log"), new ConsoleLogAdapter());
1616
}
1717
}
1818
}

src/tests/UnitTests/Authentication/KeychainTests.cs

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ public void Should_Initialize_When_Cache_Does_Not_Exist()
2222
{
2323
const string connectionsCachePath = @"c:\UserCachePath\";
2424

25-
var environment = SubstituteFactory.CreateEnvironment();
26-
environment.UserCachePath.Returns(info => connectionsCachePath.ToNPath());
27-
2825
var fileSystem = SubstituteFactory.CreateFileSystem();
2926
var credentialManager = Substitute.For<ICredentialManager>();
3027

31-
var keychain = new Keychain(environment, fileSystem, credentialManager);
28+
var environment = SubstituteFactory.CreateEnvironment();
29+
environment.UserCachePath.Returns(info => connectionsCachePath.ToNPath());
30+
environment.FileSystem.Returns(fileSystem);
31+
32+
var keychain = new Keychain(environment, credentialManager);
3233
keychain.Initialize();
3334

3435
fileSystem.Received(1).FileExists(@"c:\UserCachePath\connections.json");
@@ -53,19 +54,20 @@ public void Should_Initialize_When_Cache_Invalid()
5354
const string connectionsCachePath = @"c:\UserCachePath\";
5455
const string connectionsCacheFile = @"c:\UserCachePath\connections.json";
5556

56-
var environment = SubstituteFactory.CreateEnvironment();
57-
environment.UserCachePath.Returns(info => connectionsCachePath.ToNPath());
58-
5957
var fileSystem = SubstituteFactory.CreateFileSystem(new CreateFileSystemOptions {
6058
FilesThatExist = new List<string> { connectionsCacheFile },
6159
FileContents = new Dictionary<string, IList<string>> {
6260
{connectionsCacheFile, new List<string> { @"invalid json" }}
6361
}
6462
});
6563

64+
var environment = SubstituteFactory.CreateEnvironment();
65+
environment.UserCachePath.Returns(info => connectionsCachePath.ToNPath());
66+
environment.FileSystem.Returns(fileSystem);
67+
6668
var credentialManager = Substitute.For<ICredentialManager>();
6769

68-
var keychain = new Keychain(environment, fileSystem, credentialManager);
70+
var keychain = new Keychain(environment, credentialManager);
6971
keychain.Initialize();
7072

7173
fileSystem.Received(1).FileExists(connectionsCacheFile);
@@ -92,19 +94,20 @@ public void Should_Initialize_When_Cache_Exists()
9294

9395
var hostUri = new UriString("https://github.com/");
9496

95-
var environment = SubstituteFactory.CreateEnvironment();
96-
environment.UserCachePath.Returns(info => connectionsCachePath.ToNPath());
97-
9897
var fileSystem = SubstituteFactory.CreateFileSystem(new CreateFileSystemOptions {
9998
FilesThatExist = new List<string> { connectionsCacheFile },
10099
FileContents = new Dictionary<string, IList<string>> {
101100
{connectionsCacheFile, new List<string> { @"[{""Host"":""https://github.com/"",""Username"":""SomeUser""}]" }}
102101
}
103102
});
104103

104+
var environment = SubstituteFactory.CreateEnvironment();
105+
environment.UserCachePath.Returns(info => connectionsCachePath.ToNPath());
106+
environment.FileSystem.Returns(fileSystem);
107+
105108
var credentialManager = Substitute.For<ICredentialManager>();
106109

107-
var keychain = new Keychain(environment, fileSystem, credentialManager);
110+
var keychain = new Keychain(environment, credentialManager);
108111
keychain.Initialize();
109112

110113
fileSystem.Received(1).FileExists(connectionsCacheFile);
@@ -131,16 +134,17 @@ public void Should_Load_From_ConnectionManager()
131134

132135
var hostUri = new UriString("https://github.com/");
133136

134-
var environment = SubstituteFactory.CreateEnvironment();
135-
environment.UserCachePath.Returns(info => connectionsCachePath.ToNPath());
136-
137137
var fileSystem = SubstituteFactory.CreateFileSystem(new CreateFileSystemOptions {
138138
FilesThatExist = new List<string> { connectionsCacheFile },
139139
FileContents = new Dictionary<string, IList<string>> {
140140
{connectionsCacheFile, new List<string> { @"[{""Host"":""https://github.com/"",""Username"":""SomeUser""}]" }}
141141
}
142142
});
143143

144+
var environment = SubstituteFactory.CreateEnvironment();
145+
environment.UserCachePath.Returns(info => connectionsCachePath.ToNPath());
146+
environment.FileSystem.Returns(fileSystem);
147+
144148
const string username = "SomeUser";
145149
const string token = "SomeToken";
146150

@@ -154,7 +158,7 @@ public void Should_Load_From_ConnectionManager()
154158
return TaskEx.FromResult(credential);
155159
});
156160

157-
var keychain = new Keychain(environment, fileSystem, credentialManager);
161+
var keychain = new Keychain(environment, credentialManager);
158162
keychain.Initialize();
159163

160164
fileSystem.Received(1).FileExists(connectionsCacheFile);
@@ -188,20 +192,21 @@ public void Should_Delete_From_Cache_When_Load_Returns_Null_From_ConnectionManag
188192

189193
var hostUri = new UriString("https://github.com/");
190194

191-
var environment = SubstituteFactory.CreateEnvironment();
192-
environment.UserCachePath.Returns(info => connectionsCachePath.ToNPath());
193-
194195
var fileSystem = SubstituteFactory.CreateFileSystem(new CreateFileSystemOptions {
195196
FilesThatExist = new List<string> { connectionsCacheFile },
196197
FileContents = new Dictionary<string, IList<string>> {
197198
{connectionsCacheFile, new List<string> { @"[{""Host"":""https://github.com/"",""Username"":""SomeUser""}]" }}
198199
}
199200
});
200201

202+
var environment = SubstituteFactory.CreateEnvironment();
203+
environment.UserCachePath.Returns(info => connectionsCachePath.ToNPath());
204+
environment.FileSystem.Returns(fileSystem);
205+
201206
var credentialManager = Substitute.For<ICredentialManager>();
202207
credentialManager.Load(hostUri).Returns(info => TaskEx.FromResult<ICredential>(null));
203208

204-
var keychain = new Keychain(environment, fileSystem, credentialManager);
209+
var keychain = new Keychain(environment, credentialManager);
205210
keychain.Initialize();
206211

207212
fileSystem.Received(1).FileExists(connectionsCacheFile);
@@ -243,17 +248,19 @@ public void Should_Connect_Set_Credentials_Token_And_Save()
243248

244249
var hostUri = new UriString("https://github.com/");
245250

251+
var fileSystem = SubstituteFactory.CreateFileSystem();
252+
246253
var environment = SubstituteFactory.CreateEnvironment();
247254
environment.UserCachePath.Returns(info => connectionsCachePath.ToNPath());
255+
environment.FileSystem.Returns(fileSystem);
248256

249-
var fileSystem = SubstituteFactory.CreateFileSystem();
250257
var credentialManager = Substitute.For<ICredentialManager>();
251258

252259
credentialManager.Delete(Args.UriString).Returns(info => TaskEx.FromResult(0));
253260

254261
credentialManager.Save(Arg.Any<ICredential>()).Returns(info => TaskEx.FromResult(0));
255262

256-
var keychain = new Keychain(environment, fileSystem, credentialManager);
263+
var keychain = new Keychain(environment, credentialManager);
257264
keychain.Initialize();
258265

259266
fileSystem.Received(1).FileExists(connectionsCacheFile);
@@ -325,17 +332,19 @@ public void Should_Connect_Set_Credentials_And_Clear()
325332

326333
var hostUri = new UriString("https://github.com/");
327334

335+
var fileSystem = SubstituteFactory.CreateFileSystem();
336+
328337
var environment = SubstituteFactory.CreateEnvironment();
329338
environment.UserCachePath.Returns(info => connectionsCachePath.ToNPath());
339+
environment.FileSystem.Returns(fileSystem);
330340

331-
var fileSystem = SubstituteFactory.CreateFileSystem();
332341
var credentialManager = Substitute.For<ICredentialManager>();
333342

334343
credentialManager.Delete(Args.UriString).Returns(info => TaskEx.FromResult(0));
335344

336345
credentialManager.Save(Arg.Any<ICredential>()).Returns(info => TaskEx.FromResult(0));
337346

338-
var keychain = new Keychain(environment, fileSystem, credentialManager);
347+
var keychain = new Keychain(environment, credentialManager);
339348
keychain.Initialize();
340349

341350
fileSystem.Received(1).FileExists(connectionsCacheFile);

0 commit comments

Comments
 (0)