Skip to content

Commit

Permalink
Applier prep
Browse files Browse the repository at this point in the history
Also added Azure only testing logic & test area for using updater (to actually make)
  • Loading branch information
Azyyyyyy committed Jan 28, 2024
1 parent 68f7f6f commit e821c01
Show file tree
Hide file tree
Showing 21 changed files with 229 additions and 52 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ Tiny Update is an updater that focuses on being easy to use while being feature
Because when I started this project, I was still in college and have learnt a lot since then and the .NET ecosystem is not the same as back then (Very much in a good way!)

### What is currently missing?
* Package Creation
* Applying updates
* Hard link support
* Update package retrieval
* Cli for making updates
* Staging
* RELEASE file creation/reading

*its likely that they is more missing but can't remeber at the time of creating the readme*
*its likely that they is more missing but can't remember at the time of creating the readme*
20 changes: 20 additions & 0 deletions TinyUpdate.sln
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TinyUpdate.MicrosoftStore",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TinyUpdate.Azure", "src\Clients\TinyUpdate.Azure\TinyUpdate.Azure.csproj", "{35DBC016-62BB-41B4-B2F8-1153E1ACB5AA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TinyUpdate.Updaters.Tests", "tests\TinyUpdate.Updaters.Tests\TinyUpdate.Updaters.Tests.csproj", "{A770334A-238E-4150-9C1B-064DD9136073}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TinyUpdate.Clients.Tests", "tests\TinyUpdate.Clients.Tests\TinyUpdate.Clients.Tests.csproj", "{E101DF04-54F0-4A00-A3AF-03E92028A6AA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleApplication", "src\ExampleApplication\ExampleApplication.csproj", "{B24E6FD4-EA62-48DD-9146-72AA2D73D38F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -84,6 +90,18 @@ Global
{35DBC016-62BB-41B4-B2F8-1153E1ACB5AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{35DBC016-62BB-41B4-B2F8-1153E1ACB5AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{35DBC016-62BB-41B4-B2F8-1153E1ACB5AA}.Release|Any CPU.Build.0 = Release|Any CPU
{A770334A-238E-4150-9C1B-064DD9136073}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A770334A-238E-4150-9C1B-064DD9136073}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A770334A-238E-4150-9C1B-064DD9136073}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A770334A-238E-4150-9C1B-064DD9136073}.Release|Any CPU.Build.0 = Release|Any CPU
{E101DF04-54F0-4A00-A3AF-03E92028A6AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E101DF04-54F0-4A00-A3AF-03E92028A6AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E101DF04-54F0-4A00-A3AF-03E92028A6AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E101DF04-54F0-4A00-A3AF-03E92028A6AA}.Release|Any CPU.Build.0 = Release|Any CPU
{B24E6FD4-EA62-48DD-9146-72AA2D73D38F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B24E6FD4-EA62-48DD-9146-72AA2D73D38F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B24E6FD4-EA62-48DD-9146-72AA2D73D38F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B24E6FD4-EA62-48DD-9146-72AA2D73D38F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{4B3D4412-9AF3-4D5A-B013-3462B46E815F} = {63F2556D-A89A-4552-84C7-D2BE30718485}
Expand All @@ -101,5 +119,7 @@ Global
{81B0A72E-04FA-4A4F-856F-68C22FF2D945} = {0BD356C4-B4F2-45D6-A94F-77ED1ECEEE11}
{F364F894-0AF5-4A42-A6D3-9B6A8C10E45B} = {EE289D98-EE1C-4D17-9BB0-B4981E26A49D}
{35DBC016-62BB-41B4-B2F8-1153E1ACB5AA} = {EE289D98-EE1C-4D17-9BB0-B4981E26A49D}
{A770334A-238E-4150-9C1B-064DD9136073} = {0BD356C4-B4F2-45D6-A94F-77ED1ECEEE11}
{E101DF04-54F0-4A00-A3AF-03E92028A6AA} = {0BD356C4-B4F2-45D6-A94F-77ED1ECEEE11}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="SharpCompress" Version="0.35.0"/>
<PackageReference Include="SharpCompress" Version="0.36.0" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions src/ExampleApplication/ExampleApplication.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Deltas\TinyUpdate.Delta.BSDiff\TinyUpdate.Delta.BSDiff.csproj" />
<ProjectReference Include="..\Deltas\TinyUpdate.Delta.MSDelta\TinyUpdate.Delta.MSDelta.csproj" />
<ProjectReference Include="..\Packages\TinyUpdate.TUUP\TinyUpdate.TUUP.csproj" />
<ProjectReference Include="..\Updaters\TinyUpdate.Desktop\TinyUpdate.Desktop.csproj" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions src/ExampleApplication/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// See https://aka.ms/new-console-template for more information

//TODO: When local client is made, add it to this for testing

using TinyUpdate.Core;

Console.WriteLine("Hello, World! In Test Runner?: " + Testing.InTestRunner);
2 changes: 1 addition & 1 deletion src/Packages/TinyUpdate.TUUP/TinyUpdate.TUUP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="TestableIO.System.IO.Abstractions" Version="20.0.4" />
<PackageReference Include="TestableIO.System.IO.Abstractions" Version="20.0.15" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions src/TinyUpdate.Core/Abstract/ReleaseEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ protected ReleaseEntry(SemanticVersion? previousVersion, SemanticVersion newVers
/// <summary>
/// What version this update package was created against
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault),
JsonConverter(typeof(SemanticVersionConverter))]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
[JsonConverter(typeof(SemanticVersionConverter))]
public SemanticVersion? PreviousVersion { get; }

/// <summary>
Expand Down
33 changes: 33 additions & 0 deletions src/TinyUpdate.Core/Testing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Reflection;

namespace TinyUpdate.Core;

public static class Testing
{
public static bool InTestRunner
{
get
{
var nunitAssemblyName = Assembly.GetExecutingAssembly().GetReferencedAssemblies()
.FirstOrDefault(x => x.Name == "nunit.framework");
if (nunitAssemblyName == null)
{
return false;
}

var contextType = Assembly.Load(nunitAssemblyName).GetType("NUnit.Framework.Internal.TestExecutionContext");
var currentContextProperty = contextType?.GetProperty("CurrentContext", BindingFlags.Public | BindingFlags.Static);
var currentContext = currentContextProperty?.GetValue(null);

var startTicksProperty = currentContext?.GetType().GetProperty("StartTicks", BindingFlags.Public | BindingFlags.Instance);
var startTicksObj = startTicksProperty?.GetValue(currentContext);

if (startTicksObj is long startTicks)
{
return startTicks > 0;
}

return false;
}
}
}
86 changes: 54 additions & 32 deletions src/Updaters/TinyUpdate.Desktop/DesktopApplier.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.Extensions.Logging;
using System.Diagnostics;
using System.IO.Abstractions;
using Microsoft.Extensions.Logging;
using TinyUpdate.Core.Abstract.Delta;
using TinyUpdate.Core.Abstract.UpdatePackage;
using TinyUpdate.Desktop.Abstract;
Expand All @@ -11,12 +13,14 @@ public class DesktopApplier : IUpdateApplier
private readonly ILogger<DesktopApplier> _logger;
private readonly IDeltaManager _deltaManager;
private readonly INative? _native;
public DesktopApplier(ILogger<DesktopApplier> logger, IHasher hasher, INative? native, IDeltaManager deltaManager)
private readonly IFileSystem _fileSystem;
public DesktopApplier(ILogger<DesktopApplier> logger, IHasher hasher, INative? native, IDeltaManager deltaManager, IFileSystem fileSystem)
{
_logger = logger;
_hasher = hasher;
_native = native;
_deltaManager = deltaManager;
_fileSystem = fileSystem;
}

public bool SupportedOS() => OperatingSystem.IsWindows() || OperatingSystem.IsLinux() || OperatingSystem.IsMacOS();
Expand Down Expand Up @@ -56,22 +60,59 @@ public Task<bool> Cleanup()
public Task<bool> ApplyUpdate(IUpdatePackage updatePackage, string applicationLocation, IProgress<double>? progress = null)
{
var newVersionLocation = Path.Combine(applicationLocation, updatePackage.ReleaseEntry.NewVersion.ToString());
var previousVersionLocation = Path.Combine(applicationLocation, updatePackage.ReleaseEntry.PreviousVersion.ToString());
string? previousVersionLocation = null;
if (updatePackage.ReleaseEntry.IsDelta)
{
previousVersionLocation = Path.Combine(applicationLocation, updatePackage.ReleaseEntry.PreviousVersion.ToString());
}

return ApplyUpdate(updatePackage, previousVersionLocation, newVersionLocation, progress);
}

private async Task<bool> ApplyUpdate(IUpdatePackage updatePackage, string previousVersionLocation,
private async Task<bool> ApplyUpdate(IUpdatePackage updatePackage, string? previousVersionLocation,
string newVersionLocation, IProgress<double>? progress)
{
double progressTotal = 0;
var ind = (double)1 / updatePackage.FileCount;


if (updatePackage.ReleaseEntry.IsDelta
&& string.IsNullOrWhiteSpace(previousVersionLocation))
{
_logger.LogError("We have not been given the previous version location");
return false;
}

//Create all the directories beforehand
foreach (var directory in updatePackage.Directories)
{
Directory.CreateDirectory(Path.Combine(newVersionLocation, directory));
_fileSystem.Directory.CreateDirectory(Path.Combine(newVersionLocation, directory));
}

foreach (var newFile in updatePackage.NewFiles)
{
var newPath = Path.Combine(newVersionLocation, newFile.Location);
await using var newFileStream = _fileSystem.File.Open(newPath, new FileStreamOptions
{
PreallocationSize = newFile.Filesize,
Mode = FileMode.Create
});

await newFile.Stream.CopyToAsync(newFileStream);

Check warning on line 100 in src/Updaters/TinyUpdate.Desktop/DesktopApplier.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 100 in src/Updaters/TinyUpdate.Desktop/DesktopApplier.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 100 in src/Updaters/TinyUpdate.Desktop/DesktopApplier.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Dereference of a possibly null reference.

Check warning on line 100 in src/Updaters/TinyUpdate.Desktop/DesktopApplier.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Dereference of a possibly null reference.
await newFile.Stream.DisposeAsync();

newFileStream.Seek(0, SeekOrigin.Begin);
if (!CheckFile(newFileStream, newFile.Hash, newFile.Filesize, newPath))
{
return false;
}
UpdateProgress();
}

//If this isn't a delta update then we'll only have new files
if (!updatePackage.ReleaseEntry.IsDelta)
{
Debug.Assert(updatePackage.FileCount != updatePackage.NewFiles.Count, "Non delta update isn't all new files");
return true;
}

foreach (var movedFile in updatePackage.MovedFiles)
Expand Down Expand Up @@ -99,34 +140,15 @@ private async Task<bool> ApplyUpdate(IUpdatePackage updatePackage, string previo
}
UpdateProgress();
}

foreach (var newFile in updatePackage.NewFiles)
{
var newPath = Path.Combine(newVersionLocation, newFile.Location);
await using var newFileStream = File.Open(newPath, new FileStreamOptions
{
PreallocationSize = newFile.Filesize,
Mode = FileMode.Create
});

await newFile.Stream.CopyToAsync(newFileStream);
await newFile.Stream.DisposeAsync();

newFileStream.Seek(0, SeekOrigin.Begin);
if (!CheckFile(newFileStream, newFile.Hash, newFile.Filesize, newPath))
{
return false;
}
UpdateProgress();
}


foreach (var deltaFile in updatePackage.DeltaFiles)
{
Debug.Assert(previousVersionLocation != null, nameof(previousVersionLocation) + " != null");
var sourcePath = Path.Combine(previousVersionLocation, deltaFile.Location);
var targetPath = Path.Combine(newVersionLocation, deltaFile.Location);

await using var sourceStream = File.OpenRead(sourcePath);
await using var targetStream = File.Open(targetPath, new FileStreamOptions
await using var sourceStream = _fileSystem.File.OpenRead(sourcePath);
await using var targetStream = _fileSystem.File.Open(targetPath, new FileStreamOptions
{
PreallocationSize = deltaFile.Filesize,
Mode = FileMode.Create
Expand Down Expand Up @@ -166,16 +188,16 @@ bool ProcessHardLinkableFile(string previousRelativePath, string newRelativePath
//Attempt to create the file as a hard link
if (_native?.CreateHardLink(previousPath, newPath) ?? false)
{
using (fileStream = File.OpenRead(newPath))
using (fileStream = _fileSystem.File.OpenRead(newPath))
{
return CheckFile(fileStream, expectedHash, expectedFilesize, newPath);
}
}

//We failed, we'll copy the file as a backup
_logger.LogWarning("Was unable to hard link {PreviousPath} to {NewPath}, going to copy file", previousPath, newPath);
File.Copy(previousPath, newPath, true);
using (fileStream = File.OpenRead(newPath))
_fileSystem.File.Copy(previousPath, newPath, true);
using (fileStream = _fileSystem.File.OpenRead(newPath))
{
return CheckFile(fileStream, expectedHash, expectedFilesize, newPath);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Updaters/TinyUpdate.Desktop/TinyUpdate.Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
<ProjectReference Include="..\..\TinyUpdate.Core\TinyUpdate.Core.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="TestableIO.System.IO.Abstractions" Version="20.0.15" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions tests/TinyUpdate.Clients.Tests/AzureClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using TinyUpdate.Core.Tests.Attributes;

namespace TinyUpdate.Clients.Tests;

[AzureTest]
public class AzureClientTests
{
[Test]
public void Test()
{
Assert.Pass();
}
}
1 change: 1 addition & 0 deletions tests/TinyUpdate.Clients.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
23 changes: 23 additions & 0 deletions tests/TinyUpdate.Clients.Tests/TinyUpdate.Clients.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Clients\TinyUpdate.Azure\TinyUpdate.Azure.csproj" />
<ProjectReference Include="..\TinyUpdate.Core.Tests\TinyUpdate.Core.Tests.csproj" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions tests/TinyUpdate.Core.Tests/Attributes/AzureTestAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;

namespace TinyUpdate.Core.Tests.Attributes;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class AzureTestAttribute : Attribute, IApplyToTest
{
public void ApplyToTest(Test test)
{
if (Environment.GetEnvironmentVariable("AZURE_DEVOPS_EXT_PAT") == null)
{
test.RunState = RunState.Skipped;
test.Properties.Set(PropertyNames.SkipReason, "Unable to run Azure Test without AZURE_DEVOPS_EXT_PAT set");
}
}
}
4 changes: 2 additions & 2 deletions tests/TinyUpdate.Core.Tests/TinyUpdate.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.10.0">
<PackageReference Include="NUnit.Analyzers" Version="4.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="20.0.4" />
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="20.0.15" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 0 additions & 5 deletions tests/TinyUpdate.Delta.Tests/TinyUpdate.Delta.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.10.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
</ItemGroup>

Expand Down
Loading

0 comments on commit e821c01

Please sign in to comment.