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

Commit 6cf8003

Browse files
Fixing tests and adding functionality to optionally download files
1 parent f28b151 commit 6cf8003

File tree

3 files changed

+106
-61
lines changed

3 files changed

+106
-61
lines changed

src/GitHub.Api/Installer/GitInstaller.cs

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

1414
public const string GitExtractedMD5 = "e6cfc0c294a2312042f27f893dfc9c0a";
15-
public const string GitLfsExtractedMD5 = "ae968b69fbf42dff72311040d24a";
15+
public const string GitLfsExtractedMD5 = "36e3ae968b69fbf42dff72311040d24a";
1616

1717
public const string WindowsGitLfsExecutableMD5 = "177bb14d0c08f665a24f0d5516c3b080";
1818
public const string MacGitLfsExecutableMD5 = "f81a1a065a26a4123193e8fd96c561ad";
@@ -65,18 +65,27 @@ class GitInstaller
6565
private readonly IZipHelper sharpZipLibHelper;
6666
private readonly CancellationToken cancellationToken;
6767
private readonly GitInstallDetails installDetails;
68+
private NPath gitArchiveFilePath;
69+
private NPath gitLfsArchivePath;
6870

6971
public GitInstaller(IEnvironment environment, CancellationToken cancellationToken, GitInstallDetails installDetails)
70-
: this(environment, ZipHelper.Instance, cancellationToken, installDetails)
72+
: this(environment, ZipHelper.Instance, cancellationToken, installDetails, null, null)
7173
{
7274
}
7375

74-
public GitInstaller(IEnvironment environment, IZipHelper sharpZipLibHelper, CancellationToken cancellationToken, GitInstallDetails installDetails)
76+
public GitInstaller(IEnvironment environment, CancellationToken cancellationToken, GitInstallDetails installDetails, NPath gitArchiveFilePath, NPath gitLfsArchivePath)
77+
: this(environment, ZipHelper.Instance, cancellationToken, installDetails, gitArchiveFilePath, gitLfsArchivePath)
78+
{
79+
}
80+
81+
public GitInstaller(IEnvironment environment, IZipHelper sharpZipLibHelper, CancellationToken cancellationToken, GitInstallDetails installDetails, NPath gitArchiveFilePath, NPath gitLfsArchivePath)
7582
{
7683
this.environment = environment;
7784
this.sharpZipLibHelper = sharpZipLibHelper;
7885
this.cancellationToken = cancellationToken;
7986
this.installDetails = installDetails;
87+
this.gitArchiveFilePath = gitArchiveFilePath;
88+
this.gitLfsArchivePath = gitLfsArchivePath;
8089
}
8190

8291
public void SetupGitIfNeeded(ActionTask<NPath> onSuccess, ITask onFailure)
@@ -90,8 +99,7 @@ public void SetupGitIfNeeded(ActionTask<NPath> onSuccess, ITask onFailure)
9099
}
91100

92101
new FuncTask<bool>(cancellationToken, IsGitExtracted)
93-
.Then((success, isPortableGitExtracted) => {
94-
102+
.Finally((success, ex, isPortableGitExtracted) => {
95103
Logger.Trace("IsPortableGitExtracted: {0}", isPortableGitExtracted);
96104

97105
if (isPortableGitExtracted)
@@ -104,53 +112,33 @@ public void SetupGitIfNeeded(ActionTask<NPath> onSuccess, ITask onFailure)
104112
}
105113
else
106114
{
107-
var tempZipPath = NPath.CreateTempDirectory("git_zip_paths");
108-
var gitArchivePath = tempZipPath.Combine("git.zip");
109-
var gitLfsArchivePath = tempZipPath.Combine("git-lfs.zip");
110-
var gitExtractPath = tempZipPath.Combine("git").CreateDirectory();
111-
var gitLfsExtractPath = tempZipPath.Combine("git-lfs").CreateDirectory();
112-
113-
var downloadGitMd5Task = new DownloadTextTask(CancellationToken.None,
114-
"https://ghfvs-installer.github.com/unity/portable_git/git.zip.MD5.txt");
115-
116-
var downloadGitTask = new DownloadTask(CancellationToken.None, environment.FileSystem,
117-
"https://ghfvs-installer.github.com/unity/portable_git/git.zip", gitArchivePath, retryCount: 1);
118-
119-
var downloadGitLfsMd5Task = new DownloadTextTask(CancellationToken.None,
120-
"https://ghfvs-installer.github.com/unity/portable_git/git-lfs.zip.MD5.txt");
121-
122-
var downloadGitLfsTask = new DownloadTask(CancellationToken.None, environment.FileSystem,
123-
"https://ghfvs-installer.github.com/unity/portable_git/git-lfs.zip", gitLfsArchivePath, retryCount: 1);
124-
125-
downloadGitMd5Task
126-
.Then((b, s) =>
127-
{
128-
downloadGitTask.ValidationHash = s;
129-
})
130-
.Then(downloadGitTask)
131-
.Then(downloadGitLfsMd5Task)
132-
.Then((b, s) =>
133-
{
134-
downloadGitLfsTask.ValidationHash = s;
135-
})
136-
.Then(downloadGitLfsTask)
137-
.Then(new UnzipTask(cancellationToken, gitArchivePath, gitExtractPath, sharpZipLibHelper, environment.FileSystem, GitInstallDetails.GitExtractedMD5))
138-
.Then(new UnzipTask(cancellationToken, gitLfsArchivePath, gitLfsExtractPath, sharpZipLibHelper, environment.FileSystem, GitInstallDetails.GitLfsExtractedMD5))
115+
ITask downloadFilesTask1 = null;
116+
if (gitArchiveFilePath == null || gitLfsArchivePath == null)
117+
{
118+
downloadFilesTask1 = CreateDownloadTask();
119+
}
120+
121+
var tempZipExtractPath1 = NPath.CreateTempDirectory("git_zip_extract_zip_paths");
122+
var gitExtractPath1 = tempZipExtractPath1.Combine("git").CreateDirectory();
123+
var gitLfsExtractPath1 = tempZipExtractPath1.Combine("git-lfs").CreateDirectory();
124+
125+
var resultTask1 = new UnzipTask(cancellationToken, gitArchiveFilePath, gitExtractPath1, sharpZipLibHelper, environment.FileSystem, GitInstallDetails.GitExtractedMD5)
126+
.Then(new UnzipTask(cancellationToken, gitLfsArchivePath, gitLfsExtractPath1, sharpZipLibHelper, environment.FileSystem, GitInstallDetails.GitLfsExtractedMD5))
139127
.Then(() => {
140-
var targetGitLfsExecPath = installDetails.GetGitLfsExecPath(gitExtractPath);
141-
var extractGitLfsExePath = gitLfsExtractPath.Combine(installDetails.GitLfsExec);
142-
extractGitLfsExePath.Move(targetGitLfsExecPath);
128+
var targetGitLfsExecPath1 = installDetails.GetGitLfsExecPath(gitExtractPath1);
129+
var extractGitLfsExePath1 = gitLfsExtractPath1.Combine(installDetails.GitLfsExec);
130+
extractGitLfsExePath1.Move(targetGitLfsExecPath1);
143131

144-
Logger.Trace("Moving tempDirectory:\"{0}\" to extractTarget:\"{1}\"", gitExtractPath,
132+
Logger.Trace("Moving tempDirectory:\"{0}\" to extractTarget:\"{1}\"", gitExtractPath1,
145133
installDetails.GitInstallPath);
146134

147135
installDetails.GitInstallPath.EnsureParentDirectoryExists();
148-
gitExtractPath.Move(installDetails.GitInstallPath);
149-
150-
Logger.Trace("Deleting tempZipPath:\"{0}\"", tempZipPath);
151-
tempZipPath.DeleteIfExists();
136+
gitExtractPath1.Move(installDetails.GitInstallPath);
152137

153-
}).Finally((b, exception) => {
138+
Logger.Trace("Deleting tempZipPath:\"{0}\"", tempZipExtractPath1);
139+
tempZipExtractPath1.DeleteIfExists();
140+
})
141+
.Finally((b, exception) => {
154142
if (b)
155143
{
156144
Logger.Trace("SetupGitIfNeeded: Success");
@@ -165,12 +153,48 @@ public void SetupGitIfNeeded(ActionTask<NPath> onSuccess, ITask onFailure)
165153

166154
onFailure.Start();
167155
}
168-
}).Start();
156+
});
157+
158+
if (downloadFilesTask1 != null)
159+
{
160+
resultTask1 = downloadFilesTask1.Then(resultTask1);
161+
}
162+
163+
resultTask1.Start();
169164
}
170165

171166
}).Start();
172167
}
173168

169+
private ITask CreateDownloadTask()
170+
{
171+
var tempZipPath = NPath.CreateTempDirectory("git_zip_paths");
172+
gitArchiveFilePath = tempZipPath.Combine("git.zip");
173+
gitLfsArchivePath = tempZipPath.Combine("git-lfs.zip");
174+
175+
var downloadGitMd5Task = new DownloadTextTask(CancellationToken.None,
176+
"https://ghfvs-installer.github.com/unity/portable_git/git.zip.MD5.txt");
177+
178+
var downloadGitTask = new DownloadTask(CancellationToken.None, environment.FileSystem,
179+
"https://ghfvs-installer.github.com/unity/portable_git/git.zip", gitArchiveFilePath, retryCount: 1);
180+
181+
var downloadGitLfsMd5Task = new DownloadTextTask(CancellationToken.None,
182+
"https://ghfvs-installer.github.com/unity/portable_git/git-lfs.zip.MD5.txt");
183+
184+
var downloadGitLfsTask = new DownloadTask(CancellationToken.None, environment.FileSystem,
185+
"https://ghfvs-installer.github.com/unity/portable_git/git-lfs.zip", gitLfsArchivePath, retryCount: 1);
186+
187+
return downloadGitMd5Task.Then((b, s) => {
188+
downloadGitTask.ValidationHash = s;
189+
})
190+
.Then(downloadGitTask)
191+
.Then(downloadGitLfsMd5Task)
192+
.Then((b, s) => {
193+
downloadGitLfsTask.ValidationHash = s;
194+
})
195+
.Then(downloadGitLfsTask);
196+
}
197+
174198
private bool IsGitExtracted()
175199
{
176200
if (!installDetails.GitInstallPath.DirectoryExists())

src/tests/IntegrationTests/BasePlatformIntegrationTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected void InitializePlatform(NPath repoPath, NPath environmentPath, bool en
4040
var gitArchivePath = AssemblyResources.ToFile(ResourceType.Platform, "git.zip", zipArchivesPath, Environment);
4141
var gitLfsArchivePath = AssemblyResources.ToFile(ResourceType.Platform, "git-lfs.zip", zipArchivesPath, Environment);
4242

43-
var gitInstaller = new GitInstaller(Environment, CancellationToken.None, installDetails);
43+
var gitInstaller = new GitInstaller(Environment, CancellationToken.None, installDetails, gitArchivePath, gitLfsArchivePath);
4444

4545
NPath result = null;
4646
Exception ex = null;
Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
using System;
12
using System.Threading;
2-
using FluentAssertions;
33
using GitHub.Unity;
44
using NSubstitute;
55
using NUnit.Framework;
@@ -19,18 +19,39 @@ public void GitInstallTest()
1919

2020
var gitInstallationPath = TestBasePath.Combine("GitInstall").CreateDirectory();
2121

22-
var gitInstallDetails = new GitInstallDetails(gitInstallationPath, DefaultEnvironment.OnWindows);
23-
// var gitInstallTask = new PortableGitInstallTask(CancellationToken.None, Environment, gitInstallDetails);
24-
//
25-
// gitInstallTask.Start().Wait();
26-
//
27-
// Environment.FileSystem.CalculateFolderMD5(gitInstallDetails.GitInstallPath).Should().Be(PortableGitInstallDetails.ExtractedMD5);
28-
// Environment.FileSystem.CalculateFolderMD5(gitInstallDetails.GitInstallPath, false).Should().Be(PortableGitInstallDetails.FileListMD5);
29-
//
30-
// new PortableGitInstallTask(CancellationToken.None, Environment, gitInstallDetails)
31-
// .Then(new PortableGitInstallTask(CancellationToken.None, Environment, gitInstallDetails))
32-
// .Start()
33-
// .Wait();
22+
var installDetails = new GitInstallDetails(gitInstallationPath, DefaultEnvironment.OnWindows);
23+
24+
var zipArchivesPath = TestBasePath.Combine("ZipArchives").CreateDirectory();
25+
var gitArchivePath = AssemblyResources.ToFile(ResourceType.Platform, "git.zip", zipArchivesPath, Environment);
26+
var gitLfsArchivePath = AssemblyResources.ToFile(ResourceType.Platform, "git-lfs.zip", zipArchivesPath, Environment);
27+
28+
var gitInstaller = new GitInstaller(Environment, CancellationToken.None, installDetails, gitArchivePath, gitLfsArchivePath);
29+
30+
var autoResetEvent = new AutoResetEvent(false);
31+
32+
NPath result = null;
33+
Exception ex = null;
34+
35+
gitInstaller.SetupGitIfNeeded(new ActionTask<NPath>(CancellationToken.None, (b, path) => {
36+
result = path;
37+
autoResetEvent.Set();
38+
}),
39+
new ActionTask(CancellationToken.None, (b, exception) => {
40+
ex = exception;
41+
autoResetEvent.Set();
42+
}));
43+
44+
autoResetEvent.WaitOne();
45+
46+
if (result == null)
47+
{
48+
if (ex != null)
49+
{
50+
throw ex;
51+
}
52+
53+
throw new Exception("Did not install git");
54+
}
3455
}
3556
}
3657
}

0 commit comments

Comments
 (0)