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

Commit f28b151

Browse files
Merge branch 'enhancements/async-git-setup' into enhancements/git-setup-download
# Conflicts: # src/GitHub.Api/Installer/GitInstaller.cs # src/tests/IntegrationTests/BasePlatformIntegrationTest.cs # src/tests/IntegrationTests/Download/DownloadTaskTests.cs
2 parents 7fdd946 + af0e245 commit f28b151

File tree

7 files changed

+154
-47
lines changed

7 files changed

+154
-47
lines changed

src/GitHub.Api/Application/ApplicationManagerBase.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ protected void Initialize()
3939
Logging.TracingEnabled = UserSettings.Get(Constants.TraceLoggingKey, false);
4040
ProcessManager = new ProcessManager(Environment, Platform.GitEnvironment, CancellationToken);
4141
Platform.Initialize(ProcessManager, TaskManager);
42-
ITaskManager taskManager = TaskManager;
43-
GitClient = new GitClient(Environment, ProcessManager, taskManager.Token);
42+
GitClient = new GitClient(Environment, ProcessManager, TaskManager.Token);
4443
SetupMetrics();
4544
}
4645

@@ -51,6 +50,7 @@ public void Run(bool firstRun)
5150
var afterGitSetup = new ActionTask(CancellationToken, RestartRepository)
5251
.ThenInUI(InitializeUI);
5352

53+
//GitClient.GetConfig cannot be called until there is a git path set so it is wrapped in an ActionTask
5454
var windowsCredentialSetup = new ActionTask(CancellationToken, () => {
5555
GitClient.GetConfig("credential.helper", GitConfigSource.Global).Then((b, credentialHelper) => {
5656
if (!string.IsNullOrEmpty(credentialHelper))
@@ -68,13 +68,10 @@ public void Run(bool firstRun)
6868
}).Start();
6969
});
7070

71-
var afterPathDetermined = new ActionTask<NPath>(CancellationToken, (b1, path) => {
72-
71+
var afterPathDetermined = new ActionTask<NPath>(CancellationToken, (b, path) => {
7372
Logger.Trace("Setting Environment git path: {0}", path);
7473
Environment.GitExecutablePath = path;
75-
7674
}).ThenInUI(() => {
77-
7875
Environment.User.Initialize(GitClient);
7976

8077
if (Environment.IsWindows)
@@ -87,20 +84,35 @@ public void Run(bool firstRun)
8784
}
8885
});
8986

87+
var findExecTask = new FindExecTask("git", CancellationToken)
88+
.Finally((b, ex, path) => {
89+
if (b && path != null)
90+
{
91+
Logger.Trace("FindExecTask Success: {0}", path);
92+
93+
new FuncTask<NPath>(CancellationToken, () => path)
94+
.Then(afterPathDetermined)
95+
.Start();
96+
}
97+
else
98+
{
99+
Logger.Warning("FindExecTask Failure");
100+
Logger.Error("Git not found");
101+
}
102+
});
103+
90104
var applicationDataPath = Environment.GetSpecialFolder(System.Environment.SpecialFolder.LocalApplicationData).ToNPath();
91105
var installDetails = new GitInstallDetails(applicationDataPath, true);
92106

93107
var gitInstaller = new GitInstaller(Environment, CancellationToken, installDetails);
94-
gitInstaller.SetupGitIfNeeded(new ActionTask<NPath>(CancellationToken, (b, s) => {
95-
96-
Logger.Trace("Success: {0}", s);
97-
98-
new FuncTask<NPath>(CancellationToken, () => s)
108+
gitInstaller.SetupGitIfNeeded(new ActionTask<NPath>(CancellationToken, (b, path) => {
109+
Logger.Trace("GitInstaller Success: {0}", path);
110+
new FuncTask<NPath>(CancellationToken, () => path)
99111
.Then(afterPathDetermined)
100112
.Start();
101-
102113
}), new ActionTask(CancellationToken, () => {
103-
Logger.Trace("Failure");
114+
Logger.Warning("GitInstaller Failure");
115+
findExecTask.Start();
104116
}) );
105117
}
106118

src/GitHub.Api/Installer/GitInstaller.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ class GitInstallDetails
1111
public string GitLfsExec { get; }
1212
public NPath GitLfsExecPath { get; }
1313

14-
public const string ExtractedMD5 = "65fd0575d3b47d8207b9e19d02faca4f";
14+
public const string GitExtractedMD5 = "e6cfc0c294a2312042f27f893dfc9c0a";
15+
public const string GitLfsExtractedMD5 = "ae968b69fbf42dff72311040d24a";
16+
17+
public const string WindowsGitLfsExecutableMD5 = "177bb14d0c08f665a24f0d5516c3b080";
18+
public const string MacGitLfsExecutableMD5 = "f81a1a065a26a4123193e8fd96c561ad";
19+
1520
public const string FileListMD5 = "a152a216b2e76f6c127053251187a278";
1621

1722
private const string PackageVersion = "f02737a78695063deace08e96d5042710d3e32db";
@@ -81,6 +86,7 @@ public void SetupGitIfNeeded(ActionTask<NPath> onSuccess, ITask onFailure)
8186
if (!environment.IsWindows)
8287
{
8388
onFailure.Start();
89+
return;
8490
}
8591

8692
new FuncTask<bool>(cancellationToken, IsGitExtracted)
@@ -128,24 +134,17 @@ public void SetupGitIfNeeded(ActionTask<NPath> onSuccess, ITask onFailure)
128134
downloadGitLfsTask.ValidationHash = s;
129135
})
130136
.Then(downloadGitLfsTask)
131-
.Then(new UnzipTask(cancellationToken, gitArchivePath, gitExtractPath, sharpZipLibHelper))
132-
.Then(new UnzipTask(cancellationToken, gitLfsArchivePath, gitLfsExtractPath, sharpZipLibHelper))
137+
.Then(new UnzipTask(cancellationToken, gitArchivePath, gitExtractPath, sharpZipLibHelper, environment.FileSystem, GitInstallDetails.GitExtractedMD5))
138+
.Then(new UnzipTask(cancellationToken, gitLfsArchivePath, gitLfsExtractPath, sharpZipLibHelper, environment.FileSystem, GitInstallDetails.GitLfsExtractedMD5))
133139
.Then(() => {
134140
var targetGitLfsExecPath = installDetails.GetGitLfsExecPath(gitExtractPath);
135141
var extractGitLfsExePath = gitLfsExtractPath.Combine(installDetails.GitLfsExec);
136142
extractGitLfsExePath.Move(targetGitLfsExecPath);
137143

138-
var extractedMD5 = environment.FileSystem.CalculateFolderMD5(gitExtractPath);
139-
if (!extractedMD5.Equals(GitInstallDetails.ExtractedMD5, StringComparison.InvariantCultureIgnoreCase))
140-
{
141-
Logger.Warning("MD5 {0} does not match expected {1}", extractedMD5, GitInstallDetails.ExtractedMD5);
142-
Logger.Warning("Failed PortableGitInstallTask");
143-
throw new Exception();
144-
}
145-
146144
Logger.Trace("Moving tempDirectory:\"{0}\" to extractTarget:\"{1}\"", gitExtractPath,
147145
installDetails.GitInstallPath);
148146

147+
installDetails.GitInstallPath.EnsureParentDirectoryExists();
149148
gitExtractPath.Move(installDetails.GitInstallPath);
150149

151150
Logger.Trace("Deleting tempZipPath:\"{0}\"", tempZipPath);
@@ -187,6 +186,14 @@ private bool IsGitExtracted()
187186
return false;
188187
}
189188

189+
var calculateMd5 = environment.FileSystem.CalculateFileMD5(installDetails.GitLfsExecPath);
190+
var md5 = environment.IsWindows ? GitInstallDetails.WindowsGitLfsExecutableMD5 : GitInstallDetails.MacGitLfsExecutableMD5;
191+
if (md5.Equals(calculateMd5, StringComparison.InvariantCultureIgnoreCase))
192+
{
193+
Logger.Trace("{0} has MD5 {1} Excepted {2}", installDetails.GitLfsExecPath, calculateMd5, md5);
194+
return false;
195+
}
196+
190197
Logger.Trace("Git Present");
191198
return true;
192199
}

src/GitHub.Api/Installer/UnzipTask.cs

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,27 @@ namespace GitHub.Unity
77
class UnzipTask: TaskBase
88
{
99
private readonly string archiveFilePath;
10-
private readonly string extractedPath;
10+
private readonly NPath extractedPath;
1111
private readonly IZipHelper zipHelper;
12+
private readonly IFileSystem fileSystem;
13+
private readonly string expectedMD5;
1214
private readonly IProgress<float> zipFileProgress;
1315
private readonly IProgress<long> estimatedDurationProgress;
1416

15-
public UnzipTask(CancellationToken token, string archiveFilePath, string extractedPath,
16-
IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null) :
17-
this(token, archiveFilePath, extractedPath, ZipHelper.Instance, zipFileProgress, estimatedDurationProgress)
17+
public UnzipTask(CancellationToken token, string archiveFilePath, NPath extractedPath, IFileSystem fileSystem, string expectedMD5 = null, IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null) :
18+
this(token, archiveFilePath, extractedPath, ZipHelper.Instance, fileSystem, expectedMD5, zipFileProgress, estimatedDurationProgress)
1819
{
1920

2021
}
2122

22-
public UnzipTask(CancellationToken token, string archiveFilePath, string extractedPath, IZipHelper zipHelper, IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null)
23+
public UnzipTask(CancellationToken token, string archiveFilePath, NPath extractedPath, IZipHelper zipHelper, IFileSystem fileSystem, string expectedMD5 = null, IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null)
2324
: base(token)
2425
{
2526
this.archiveFilePath = archiveFilePath;
2627
this.extractedPath = extractedPath;
2728
this.zipHelper = zipHelper;
29+
this.fileSystem = fileSystem;
30+
this.expectedMD5 = expectedMD5;
2831
this.zipFileProgress = zipFileProgress;
2932
this.estimatedDurationProgress = estimatedDurationProgress;
3033
}
@@ -33,16 +36,43 @@ protected override void Run(bool success)
3336
{
3437
base.Run(success);
3538

36-
UnzipArchive();
37-
}
38-
39-
private void UnzipArchive()
40-
{
4139
Logger.Trace("Unzip File: {0} to Path: {1}", archiveFilePath, extractedPath);
4240

43-
zipHelper.Extract(archiveFilePath, extractedPath, Token, zipFileProgress, estimatedDurationProgress);
41+
try
42+
{
43+
zipHelper.Extract(archiveFilePath, extractedPath, Token, zipFileProgress, estimatedDurationProgress);
44+
}
45+
catch (Exception ex)
46+
{
47+
var message = "Error Unzipping file";
48+
49+
Logger.Error(ex, message);
50+
throw new UnzipTaskException(message);
51+
}
52+
53+
if (expectedMD5 != null)
54+
{
55+
var calculatedMD5 = fileSystem.CalculateFolderMD5(extractedPath);
56+
if (!calculatedMD5.Equals(expectedMD5, StringComparison.InvariantCultureIgnoreCase))
57+
{
58+
extractedPath.DeleteIfExists();
59+
60+
var message = $"Extracted MD5: {calculatedMD5} Does not match expected: {expectedMD5}";
61+
Logger.Error(message);
62+
63+
throw new UnzipTaskException(message);
64+
}
65+
}
4466

4567
Logger.Trace("Completed Unzip");
4668
}
4769
}
70+
71+
public class UnzipTaskException : Exception {
72+
public UnzipTaskException(string message) : base(message)
73+
{ }
74+
75+
public UnzipTaskException(string message, Exception innerException) : base(message, innerException)
76+
{ }
77+
}
4878
}

src/tests/IntegrationTests/BasePlatformIntegrationTest.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
using System;
12
using System.IO;
23
using System.Threading;
34
using System.Threading.Tasks;
45
using GitHub.Unity;
56
using NSubstitute;
7+
using Octokit;
68

79
namespace IntegrationTests
810
{
@@ -29,19 +31,43 @@ protected void InitializePlatform(NPath repoPath, NPath environmentPath, bool en
2931

3032
if (setupGit)
3133
{
34+
var autoResetEvent = new AutoResetEvent(false);
35+
3236
var applicationDataPath = Environment.GetSpecialFolder(System.Environment.SpecialFolder.LocalApplicationData).ToNPath();
3337
var installDetails = new GitInstallDetails(applicationDataPath, true);
3438

3539
var zipArchivesPath = TestBasePath.Combine("ZipArchives").CreateDirectory();
3640
var gitArchivePath = AssemblyResources.ToFile(ResourceType.Platform, "git.zip", zipArchivesPath, Environment);
3741
var gitLfsArchivePath = AssemblyResources.ToFile(ResourceType.Platform, "git-lfs.zip", zipArchivesPath, Environment);
3842

39-
//var gitInstallTask = new PortableGitInstallTask(CancellationToken.None, Environment, gitArchivePath, gitLfsArchivePath, installDetails);
43+
var gitInstaller = new GitInstaller(Environment, CancellationToken.None, installDetails);
44+
45+
NPath result = null;
46+
Exception ex = null;
47+
48+
gitInstaller.SetupGitIfNeeded(new ActionTask<NPath>(CancellationToken.None, (b, path) => {
49+
result = path;
50+
autoResetEvent.Set();
51+
}),
52+
new ActionTask(CancellationToken.None, (b, exception) => {
53+
ex = exception;
54+
autoResetEvent.Set();
55+
}));
56+
57+
autoResetEvent.WaitOne();
58+
59+
if (result == null)
60+
{
61+
if (ex != null)
62+
{
63+
throw ex;
64+
}
4065

41-
//var installPath = gitInstallTask.Start().Result;
42-
//Environment.GitExecutablePath = installPath;
66+
throw new Exception("Did not install git");
67+
}
4368

44-
//GitClient = new GitClient(Environment, ProcessManager, TaskManager.Token);
69+
Environment.GitExecutablePath = result;
70+
GitClient = new GitClient(Environment, ProcessManager, TaskManager.Token);
4571
}
4672
}
4773
}

src/tests/IntegrationTests/Download/DownloadTaskTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public async Task TestDownloadTask()
3232
Logger.Trace("File size {0} bytes", downloadPathBytes.Length);
3333

3434
var md5Sum = fileSystem.CalculateFileMD5(downloadPath);
35-
md5Sum.Should().Be(TestDownloadMD5.ToUpperInvariant());
35+
md5Sum.Should().Be(TestDownloadMD5);
3636

3737
var random = new Random();
3838
var takeCount = random.Next(downloadPathBytes.Length);
@@ -49,7 +49,7 @@ public async Task TestDownloadTask()
4949
Logger.Trace("File size {0} Bytes", downloadHalfPathBytes.Length);
5050

5151
md5Sum = fileSystem.CalculateFileMD5(downloadPath);
52-
md5Sum.Should().Be(TestDownloadMD5.ToUpperInvariant());
52+
md5Sum.Should().Be(TestDownloadMD5);
5353
}
5454

5555
[Test]

src/tests/IntegrationTests/SetUpFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void Setup()
1414

1515
Logging.LogAdapter = new MultipleLogAdapter(
1616
new FileLogAdapter($"..\\{DateTime.UtcNow.ToString("yyyyMMddHHmmss")}-integration-tests.log")
17-
//, new ConsoleLogAdapter()
17+
, new ConsoleLogAdapter()
1818
);
1919
}
2020
}

src/tests/IntegrationTests/UnzipTaskTests.cs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using System;
12
using System.Threading;
23
using System.Threading.Tasks;
4+
using FluentAssertions;
35
using GitHub.Unity;
46
using Microsoft.Win32.SafeHandles;
57
using NSubstitute;
@@ -12,7 +14,7 @@ namespace IntegrationTests
1214
class UnzipTaskTests : BaseTaskManagerTest
1315
{
1416
[Test]
15-
public void UnzipTest()
17+
public void TaskSucceeds()
1618
{
1719
InitializeTaskManager();
1820

@@ -22,14 +24,11 @@ public void UnzipTest()
2224
var destinationPath = TestBasePath.Combine("git_zip").CreateDirectory();
2325
var archiveFilePath = AssemblyResources.ToFile(ResourceType.Platform, "git.zip", destinationPath, Environment);
2426

25-
Logger.Trace("ArchiveFilePath: {0}", archiveFilePath);
26-
Logger.Trace("TestBasePath: {0}", TestBasePath);
27-
2827
var extractedPath = TestBasePath.Combine("git_zip_extracted").CreateDirectory();
2928

3029
var zipProgress = 0;
3130
Logger.Trace("Pct Complete {0}%", zipProgress);
32-
var unzipTask = new UnzipTask(CancellationToken.None, archiveFilePath, extractedPath,
31+
var unzipTask = new UnzipTask(CancellationToken.None, archiveFilePath, extractedPath, Environment.FileSystem, GitInstallDetails.GitExtractedMD5,
3332
new Progress<float>(zipFileProgress => {
3433
var zipFileProgressInteger = (int) (zipFileProgress * 100);
3534
if (zipProgress != zipFileProgressInteger)
@@ -40,6 +39,39 @@ public void UnzipTest()
4039
}));
4140

4241
unzipTask.Start().Wait();
42+
43+
extractedPath.DirectoryExists().Should().BeTrue();
44+
}
45+
46+
[Test]
47+
public void TaskFailsWhenMD5Incorect()
48+
{
49+
InitializeTaskManager();
50+
51+
var cacheContainer = Substitute.For<ICacheContainer>();
52+
Environment = new IntegrationTestEnvironment(cacheContainer, TestBasePath, SolutionDirectory);
53+
54+
var destinationPath = TestBasePath.Combine("git_zip").CreateDirectory();
55+
var archiveFilePath = AssemblyResources.ToFile(ResourceType.Platform, "git.zip", destinationPath, Environment);
56+
57+
var extractedPath = TestBasePath.Combine("git_zip_extracted").CreateDirectory();
58+
59+
60+
var failed = false;
61+
Exception exception = null;
62+
63+
var unzipTask = new UnzipTask(CancellationToken.None, archiveFilePath, extractedPath, Environment.FileSystem, "AABBCCDD")
64+
.Finally((b, ex) => {
65+
failed = true;
66+
exception = ex;
67+
});
68+
69+
unzipTask.Start().Wait();
70+
71+
extractedPath.DirectoryExists().Should().BeFalse();
72+
failed.Should().BeTrue();
73+
exception.Should().NotBeNull();
74+
exception.Should().BeOfType<UnzipTaskException>();
4375
}
4476
}
4577
}

0 commit comments

Comments
 (0)