Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit a863cf2

Browse files
author
Steven Kirk
committed
Make IO operations go through Funcs
This is the same pattern used by `ConnectionManager` which allows us to pass in mocked rothko interfaces for testing, but ensure that the rothko dependency isn't loaded at runtime. However in GitHub.Exports we don't have and (possibly) don't want a dependency on rothko so we need to work out what to do there.
1 parent b5e4c22 commit a863cf2

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/GitHub.Exports/Services/UsageTracker.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
using System.ComponentModel.Composition;
33
using System.Diagnostics;
44
using System.Globalization;
5-
using System.IO;
65
using System.Linq;
6+
using System.Text;
77
using System.Windows.Threading;
88
using GitHub.Models;
99
using GitHub.Settings;
@@ -26,6 +26,11 @@ public class UsageTracker : IUsageTracker
2626
readonly DispatcherTimer timer;
2727
readonly string storePath;
2828

29+
Func<string, bool> fileExists;
30+
Func<string, Encoding, string> readAllText;
31+
Action<string, string, Encoding> writeAllText;
32+
Action<string> dirCreate;
33+
2934
[ImportingConstructor]
3035
public UsageTracker(
3136
IProgram program,
@@ -35,6 +40,11 @@ public UsageTracker(
3540
{
3641
var componentModel = serviceProvider.GetService(typeof(SComponentModel)) as IComponentModel;
3742

43+
fileExists = (path) => System.IO.File.Exists(path);
44+
readAllText = (path, encoding) => System.IO.File.ReadAllText(path, encoding);
45+
writeAllText = (path, content, encoding) => System.IO.File.WriteAllText(path, content, encoding);
46+
dirCreate = (path) => System.IO.Directory.CreateDirectory(path);
47+
3848
this.connectionManager = connectionManager;
3949
this.userSettings = userSettings;
4050
this.client = componentModel?.DefaultExportProvider.GetExportedValue<IMetricsService>();
@@ -119,8 +129,8 @@ public void IncrementUpstreamPullRequestCount()
119129

120130
UsageStore LoadUsage()
121131
{
122-
var result = File.Exists(storePath) ?
123-
SimpleJson.DeserializeObject<UsageStore>(File.ReadAllText(storePath)) :
132+
var result = fileExists(storePath) ?
133+
SimpleJson.DeserializeObject<UsageStore>(readAllText(storePath, Encoding.UTF8)) :
124134
new UsageStore { Model = new UsageModel() };
125135

126136
result.Model.Lang = CultureInfo.InstalledUICulture.IetfLanguageTag;
@@ -132,9 +142,9 @@ UsageStore LoadUsage()
132142

133143
void SaveUsage(UsageStore store)
134144
{
135-
Directory.CreateDirectory(Path.GetDirectoryName(storePath));
145+
dirCreate(System.IO.Path.GetDirectoryName(storePath));
136146
var json = SimpleJson.SerializeObject(store);
137-
File.WriteAllText(storePath, json);
147+
writeAllText(storePath, json, Encoding.UTF8);
138148
}
139149

140150
void UpdateTimer(bool initialCall)

0 commit comments

Comments
 (0)