Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bootstrapper: fixed partial download #1859

Merged
merged 2 commits into from
Aug 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ namespace Paket.Bootstrapper.DownloadStrategies
{
public class GitHubDownloadStrategy : IDownloadStrategy
{
private const int HttpBufferSize = 4096;

public static class Constants
{
public const string PaketReleasesLatestUrl = "https://github.com/fsprojects/Paket/releases/latest";
Expand Down Expand Up @@ -78,10 +76,7 @@ public void DownloadVersion(string latestVersion, string target)
ConsoleImpl.WriteDebug("Starting download from {0}", url);

var tmpFile = BootstrapperHelper.GetTempFile("paket");
using (var fileStream = FileProxy.Create(tmpFile))
{
WebRequestProxy.DownloadFile(url, fileStream, HttpBufferSize);
}
WebRequestProxy.DownloadFile(url, tmpFile);

FileProxy.Copy(tmpFile, target, true);
FileProxy.Delete(tmpFile);
Expand All @@ -103,11 +98,8 @@ public void SelfUpdate(string latestVersion)

string renamedPath = BootstrapperHelper.GetTempFile("oldBootstrapper");
string tmpDownloadPath = BootstrapperHelper.GetTempFile("newBootstrapper");
WebRequestProxy.DownloadFile(url, tmpDownloadPath);

using (var toStream = FileProxy.Create(tmpDownloadPath))
{
WebRequestProxy.DownloadFile(url, toStream, HttpBufferSize);
}
try
{
FileProxy.FileMove(exePath, renamedPath);
Expand Down
1 change: 0 additions & 1 deletion src/Paket.Bootstrapper/HelperProxies/IWebRequestProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ public interface IWebRequestProxy
{
string DownloadString(string address);
void DownloadFile(string url, string targetLocation);
void DownloadFile(string url, Stream stream, int bufferSize);
}
}
13 changes: 0 additions & 13 deletions src/Paket.Bootstrapper/HelperProxies/WebRequestProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,5 @@ public void DownloadFile(string url, string targetLocation)
BootstrapperHelper.PrepareWebClient(Client, url);
Client.DownloadFile(url, targetLocation);
}

public void DownloadFile(string url, Stream stream, int bufferSize)
{
var request = BootstrapperHelper.PrepareWebRequest(url);

using (var httpResponse = (HttpWebResponse)request.GetResponse())
{
using (var responseStream = httpResponse.GetResponseStream())
{
responseStream.CopyTo(stream, bufferSize);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,51 +56,35 @@ public void GetLatestVersion_Prerelease()
public void DownloadVersion()
{
//arrange
var byteArray = Encoding.ASCII.GetBytes("paketExeContent");
var stream = new MemoryStream(byteArray);
var tempFileName = BootstrapperHelper.GetTempFile("paket");

mockWebProxy.Setup(x => x.DownloadFile(It.IsAny<string>(), It.IsAny<Stream>(), It.IsAny<int>()))
.Callback<string, Stream, int>((url, streamIn, bufferIn) => stream.CopyTo(streamIn)).Verifiable();
var buffer = new byte[byteArray.Length];
mockFileProxy.Setup(x => x.Create(tempFileName)).Returns(new MemoryStream(buffer));
mockWebProxy.Setup(x => x.DownloadFile(It.IsAny<string>(), It.IsAny<string>())).Verifiable();

//act
sut.DownloadVersion("2.57.1", "paketExeLocation");

//assert
mockWebProxy.Verify();
mockWebProxy.Verify(x => x.DownloadFile(It.IsAny<string>(), tempFileName));
mockFileProxy.Verify(x => x.Copy(tempFileName, "paketExeLocation", true));
mockFileProxy.Verify(x => x.Delete(tempFileName));
var text = Encoding.ASCII.GetString(buffer);
Assert.That(text, Is.EqualTo("paketExeContent"));
}

[Test]
public void SelfUpdate()
{
//arrange
var byteArray = Encoding.ASCII.GetBytes("paketExeContent");
var stream = new MemoryStream(byteArray);
var tempFileNameNew = BootstrapperHelper.GetTempFile("newBootstrapper");
var tempFileNameOld = BootstrapperHelper.GetTempFile("oldBootstrapper");

mockWebProxy.Setup(x => x.DownloadFile(It.IsAny<string>(), It.IsAny<Stream>(), It.IsAny<int>()))
.Callback<string, Stream, int>((url, streamIn, bufferIn) => stream.CopyTo(streamIn)).Verifiable();
var buffer = new byte[byteArray.Length];
mockFileProxy.Setup(x => x.Create(tempFileNameNew)).Returns(new MemoryStream(buffer));
mockWebProxy.Setup(x => x.DownloadFile(It.IsAny<string>(), It.IsAny<string>())).Verifiable();
mockFileProxy.Setup(x => x.GetLocalFileVersion(It.IsAny<string>())).Returns("2.52.1");

//act
sut.SelfUpdate("2.57.1");

//assert
mockWebProxy.Verify();
mockWebProxy.Verify(x => x.DownloadFile(It.IsAny<string>(), tempFileNameNew));
mockFileProxy.Verify(x => x.FileMove(Assembly.GetAssembly(typeof(GitHubDownloadStrategy)).Location, tempFileNameOld));
mockFileProxy.Verify(x => x.FileMove(tempFileNameNew, Assembly.GetAssembly(typeof(GitHubDownloadStrategy)).Location));

var text = Encoding.ASCII.GetString(buffer);
Assert.That(text, Is.EqualTo("paketExeContent"));
}
}
}