Fix bug in SimpleWebSource and add tests

This commit is contained in:
Caelan Sayler
2022-03-18 12:04:13 +00:00
parent 6c19593f22
commit da50aa829a
6 changed files with 81 additions and 29 deletions

View File

@@ -15,24 +15,6 @@ using Xunit;
namespace Squirrel.Tests
{
public class FakeUrlDownloader : Sources.IFileDownloader
{
public Task<byte[]> DownloadBytes(string url, string auth, string acc)
{
return Task.FromResult(new byte[0]);
}
public Task DownloadFile(string url, string targetFile, Action<int> progress, string auth, string acc)
{
return Task.CompletedTask;
}
public Task<string> DownloadString(string url, string auth, string acc)
{
return Task.FromResult("");
}
}
public class ApplyReleasesTests : IEnableLogger
{
[Fact]
@@ -481,7 +463,7 @@ namespace Squirrel.Tests
"Squirrel.Core.1.1.0.0-delta.nupkg"
}.ForEach(x => File.Copy(IntegrationTestHelper.GetPath("fixtures", x), Path.Combine(tempDir, "theApp", "packages", x)));
var urlDownloader = new FakeUrlDownloader();
var urlDownloader = new FakeDownloader();
var fixture = new UpdateManager.ApplyReleasesImpl(appDir);
var baseEntry = ReleaseEntry.GenerateFromFile(Path.Combine(tempDir, "theApp", "packages", "Squirrel.Core.1.0.0.0-full.nupkg"));

View File

@@ -1,7 +1,8 @@
using System;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Squirrel.Tests.TestHelpers;
using Xunit;
@@ -123,5 +124,24 @@ namespace Squirrel.Tests
{
throw new NotImplementedException();
}
[Theory]
[InlineData(@"94689fede03fed7ab59c24337673a27837f0c3ec MyCoolApp-1.0.nupkg 1004502", "MyCoolApp", null)]
[InlineData(@"0000000000000000000000000000000000000000 https://www.test.org/Folder/MyCoolApp-1.2-delta.nupkg?query=param 1231953", "MyCoolApp", "https://www.test.org/Folder")]
public async Task WebSourceRequestsExpectedUrls(string releaseEntry, string releaseName, string baseUrl)
{
baseUrl = baseUrl ?? "https://example.com/files";
var dl = new FakeDownloader();
var source = new Sources.SimpleWebSource("https://example.com/files", dl);
dl.MockedResponseBytes = Encoding.UTF8.GetBytes(releaseEntry);
var releases = await source.GetReleaseFeed(null, null);
Assert.True(releases.Count() == 1);
Assert.Equal(releaseName, releases[0].PackageName);
Assert.Equal("https://example.com/files/RELEASES", new Uri(dl.LastUrl).GetLeftPart(UriPartial.Path));
await source.DownloadReleaseEntry(releases[0], "test", null);
Assert.Equal(baseUrl + "/" + releases[0].Filename, new Uri(dl.LastUrl).GetLeftPart(UriPartial.Path));
}
}
}

View File

@@ -39,8 +39,8 @@ namespace Squirrel.Tests
[Theory]
[InlineData(@"94689fede03fed7ab59c24337673a27837f0c3ec My-Cool-App-1.0-full.nupkg 1004502", "My-Cool-App")]
[InlineData(@"94689fede03fed7ab59c24337673a27837f0c3ec My-Cool-App-1.1.nupkg 1004502", "My-Cool-App")]
[InlineData(@"94689fede03fed7ab59c24337673a27837f0c3ec http://test.org/Folder/My-Cool-App-1.2.nupkg?query=param 1231953", "My-Cool-App")]
[InlineData(@"94689fede03fed7ab59c24337673a27837f0c3ec My.Cool-App-1.1.nupkg 1004502", "My.Cool-App")]
[InlineData(@"94689fede03fed7ab59c24337673a27837f0c3ec http://test.org/Folder/My.Cool-App-1.2.nupkg?query=param 1231953", "My.Cool-App")]
public void ParseValidReleaseEntryLinesWithDashes(string releaseEntry, string packageName)
{
var fixture = ReleaseEntry.ParseReleaseEntry(releaseEntry);

View File

@@ -0,0 +1,42 @@
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace Squirrel.Tests
{
public class FakeDownloader : Sources.IFileDownloader
{
public string LastUrl { get; private set; }
public string LastLocalFile { get; private set; }
public string LastAuthHeader { get; private set; }
public string LastAcceptHeader { get; private set; }
public byte[] MockedResponseBytes { get; set; } = new byte[0];
public bool WriteMockLocalFile { get; set; } = false;
public Task<byte[]> DownloadBytes(string url, string auth, string acc)
{
LastUrl = url;
LastAuthHeader = auth;
LastAcceptHeader = acc;
return Task.FromResult(MockedResponseBytes);
}
public async Task DownloadFile(string url, string targetFile, Action<int> progress, string auth, string acc)
{
LastLocalFile = targetFile;
var resp = await DownloadBytes(url, auth, acc);
progress?.Invoke(25);
progress?.Invoke(50);
progress?.Invoke(75);
progress?.Invoke(100);
if (WriteMockLocalFile)
File.WriteAllBytes(targetFile, resp);
}
public async Task<string> DownloadString(string url, string auth, string acc)
{
return Encoding.UTF8.GetString(await DownloadBytes(url, auth, acc));
}
}
}

View File

@@ -172,7 +172,7 @@ namespace Squirrel.Tests
// check for an update
UpdateInfo updateInfo;
using (var mgr = new UpdateManager(remotePackages, "theApp", tempDir, new FakeUrlDownloader())) {
using (var mgr = new UpdateManager(remotePackages, "theApp", tempDir, new FakeDownloader())) {
updateInfo = await mgr.CheckForUpdate();
}
@@ -217,7 +217,7 @@ namespace Squirrel.Tests
ReleaseEntry.BuildReleasesFile(remotePackages);
UpdateInfo updateInfo;
using (var mgr = new UpdateManager(remotePackages, "theApp", tempDir, new FakeUrlDownloader())) {
using (var mgr = new UpdateManager(remotePackages, "theApp", tempDir, new FakeDownloader())) {
updateInfo = await mgr.CheckForUpdate();
}
@@ -258,7 +258,7 @@ namespace Squirrel.Tests
await fixture.updateLocalReleasesFile();
ReleaseEntry.BuildReleasesFile(remotePackages);
using (var mgr = new UpdateManager(remotePackages, "theApp", tempDir, new FakeUrlDownloader())) {
using (var mgr = new UpdateManager(remotePackages, "theApp", tempDir, new FakeDownloader())) {
UpdateInfo updateInfo;
updateInfo = await mgr.CheckForUpdate();
Assert.True(updateInfo.ReleasesToApply.First().IsDelta);