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

Commit f44f30b

Browse files
Initializing settings correctly; Adding some debug code
1 parent a038764 commit f44f30b

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

src/GitHub.Api/ApplicationManagerBase.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ protected void Initialize(IUIDispatcher uiDispatcher)
2828
UserSettings = new UserSettings(Environment, ApplicationInfo.ApplicationName);
2929
SystemSettings = new SystemSettings(Environment, ApplicationInfo.ApplicationName);
3030

31+
LocalSettings.Initialize();
32+
UserSettings.Initialize();
33+
SystemSettings.Initialize();
34+
3135
Platform = new Platform(Environment, FileSystem, uiDispatcher);
3236
ProcessManager = new ProcessManager(Environment, Platform.GitEnvironment, CancellationToken);
3337
Platform.Initialize(ProcessManager);

src/GitHub.Api/LocalSettings.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ abstract class BaseSettings : ISettings
2121

2222
class JsonBackedSettings : BaseSettings
2323
{
24-
private static readonly ILogging logger = Logging.GetLogger<JsonBackedSettings>();
25-
26-
private const string SettingsParseError = "Failed to parse settings file at '{0}'";
27-
2824
private string cachePath;
2925
private CacheData cacheData = new CacheData();
3026
private Action<string> dirCreate;
@@ -33,9 +29,11 @@ class JsonBackedSettings : BaseSettings
3329
private Func<string, bool> fileExists;
3430
private Func<string, Encoding, string> readAllText;
3531
private Action<string, string> writeAllText;
32+
private readonly ILogging logger;
3633

3734
public JsonBackedSettings()
3835
{
36+
logger = Logging.GetLogger(GetType());
3937
fileExists = (path) => File.Exists(path);
4038
readAllText = (path, encoding) => File.ReadAllText(path, encoding);
4139
writeAllText = (path, content) => File.WriteAllText(path, content);
@@ -100,6 +98,8 @@ public override void Rename(string oldKey, string newKey)
10098

10199
private void LoadFromCache(string cachePath)
102100
{
101+
logger.Trace("LoadFromCache: {0}", cachePath);
102+
103103
EnsureCachePath(cachePath);
104104

105105
if (!fileExists(cachePath))
@@ -111,8 +111,9 @@ private void LoadFromCache(string cachePath)
111111
{
112112
cacheData = SimpleJson.DeserializeObject<CacheData>(data);
113113
}
114-
catch
114+
catch(Exception ex)
115115
{
116+
logger.Error(ex, "LoadFromCache Error");
116117
cacheData = null;
117118
}
118119

@@ -126,6 +127,8 @@ private void LoadFromCache(string cachePath)
126127

127128
private bool SaveToCache(string cachePath)
128129
{
130+
logger.Trace("SaveToCache: {0}", cachePath);
131+
129132
EnsureCachePath(cachePath);
130133

131134
try
@@ -135,8 +138,7 @@ private bool SaveToCache(string cachePath)
135138
}
136139
catch (Exception ex)
137140
{
138-
logger.Error(SettingsParseError, cachePath);
139-
logger.Error(ex);
141+
logger.Error(ex, "SaveToCache Error");
140142
return false;
141143
}
142144

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using FluentAssertions;
4+
using GitHub.Unity;
5+
using NUnit.Framework;
6+
7+
namespace IntegrationTests
8+
{
9+
class JsonBackedSettingsTests: BaseGitEnvironmentTest
10+
{
11+
[Test, Ignore("For some reason this fails")]
12+
public void UserSettingsTest()
13+
{
14+
InitializeEnvironment(TestRepoMasterCleanSynchronized, true);
15+
16+
var userSettings = new UserSettings(Environment, "GitHubTest");
17+
userSettings.Initialize();
18+
19+
var connectionCacheItems = new[] {
20+
new ConnectionCacheItem { Host = "Host1", Username = "User1" },
21+
new ConnectionCacheItem { Host = "Host2", Username = "User2" }
22+
};
23+
24+
connectionCacheItems.ShouldAllBeEquivalentTo(new[] {
25+
new ConnectionCacheItem { Host = "Host1", Username = "User1" },
26+
new ConnectionCacheItem { Host = "Host2", Username = "User2" }
27+
});
28+
29+
userSettings.Set("connectionCacheItems", connectionCacheItems);
30+
31+
var json = Environment.GetSpecialFolder(System.Environment.SpecialFolder.LocalApplicationData)
32+
.ToNPath()
33+
.Combine("GitHubTest", "settings.json")
34+
.ReadAllText();
35+
36+
json.Should()
37+
.Be(@"{""GitHubUnity"":{""connectionCacheItems"":[{""Host"":""Host1"",""Username"":""User1""},{""Host"":""Host2"",""Username"":""User2""}]}}");
38+
39+
userSettings = new UserSettings(Environment, "GitHubTest");
40+
userSettings.Initialize();
41+
42+
var loadedItems = userSettings.Get<ConnectionCacheItem[]>("connectionCacheItems");
43+
44+
loadedItems.Should().NotBeNull();
45+
connectionCacheItems.ShouldAllBeEquivalentTo(loadedItems);
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)