Skip to content

Commit

Permalink
AllRegistries TestCaseSource
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaina committed Dec 17, 2024
1 parent bce6cf8 commit b125686
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 268 deletions.
35 changes: 11 additions & 24 deletions src/UnityNuGet.Server/RegistryCacheInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ public class RegistryCacheInitializer(IConfiguration configuration, IHostEnviron
{
private readonly IConfiguration _configuration = configuration;
private readonly IHostEnvironment _hostEnvironment = hostEnvironment;
private readonly ILoggerFactory _loggerFactory = loggerFactory;
private readonly ILoggerFactory _loggerFactory = loggerFactory;
private readonly RegistryOptions _registryOptions = registryOptionsAccessor.Value;
private readonly RegistryCacheSingleton _registryCacheSingleton = registryCacheSingleton;

public Task StartAsync(CancellationToken cancellationToken)
{
{
ILogger logger = _loggerFactory.CreateLogger("NuGet");
var loggerRedirect = new NuGetRedirectLogger(logger);

Uri uri = _registryOptions.RootHttpUrl!;

bool isDevelopment = _hostEnvironment.IsDevelopment();
bool isDevelopment = _hostEnvironment.IsDevelopment();

if (isDevelopment)
{
Expand All @@ -42,27 +42,14 @@ public Task StartAsync(CancellationToken cancellationToken)
}

// Get the current directory from registry options (prepend binary folder in dev)
string unityPackageFolder;

if (Path.IsPathRooted(_registryOptions.RegistryFilePath))
{
unityPackageFolder = _registryOptions.RootPersistentFolder!;
}
else
{
string currentDirectory;

if (isDevelopment)
{
currentDirectory = Path.GetDirectoryName(AppContext.BaseDirectory)!;
}
else
{
currentDirectory = Directory.GetCurrentDirectory();
}

unityPackageFolder = Path.Combine(currentDirectory, _registryOptions.RootPersistentFolder!);
}
string unityPackageFolder = _registryOptions.RootPersistentFolder!;
if (!Path.IsPathRooted(_registryOptions.RegistryFilePath))
{
string currentDirectory = isDevelopment
? Path.GetDirectoryName(AppContext.BaseDirectory)!
: Directory.GetCurrentDirectory();
unityPackageFolder = Path.Combine(currentDirectory, unityPackageFolder);
}

logger.LogInformation("Using Unity Package folder `{UnityPackageFolder}`", unityPackageFolder);

Expand Down
23 changes: 12 additions & 11 deletions src/UnityNuGet.Server/RegistryCacheReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace UnityNuGet.Server
/// <summary>
/// Main class that stores relevant messages that may appear during the build of the Unity packages with <see cref="RegistryCache"/>.
/// </summary>
public class RegistryCacheReport(RegistryCacheSingleton registryCacheSingleton, IOptions<RegistryOptions> registryOptionsAccessor)
public class RegistryCacheReport(RegistryCacheSingleton registryCacheSingleton, IOptions<RegistryOptions> registryOptionsAccessor)
{
private readonly RegistryCacheSingleton _registryCacheSingleton = registryCacheSingleton;
private readonly RegistryOptions _registryOptions = registryOptionsAccessor.Value;
private readonly RegistryOptions _registryOptions = registryOptionsAccessor.Value;

private readonly List<string> _informationMessages = [];
private readonly List<string> _warningMessages = [];
Expand All @@ -30,10 +30,12 @@ public double Progress
{
get
{
int currentIndex = _registryCacheSingleton.ProgressPackageIndex;
int totalCount = _registryCacheSingleton.ProgressTotalPackageCount;
double percent = totalCount != 0 ? (double)currentIndex * 100 / totalCount : 0;
if(totalCount == 0)
return 0;

int currentIndex = _registryCacheSingleton.ProgressPackageIndex;
double percent = (double)currentIndex * 100 / totalCount;
return percent;
}
}
Expand All @@ -42,14 +44,13 @@ public TimeSpan? TimeRemainingForNextUpdate
{
get
{
if (_errorMessages.Count == 0)
{
return _lastUpdate != null ? _lastUpdate.Value.Add(_registryOptions.UpdateInterval) - DateTime.UtcNow : null;
}
else
{
if (_errorMessages.Count > 0)
return TimeSpan.FromSeconds(0);
}

if(_lastUpdate == null)
return null;

return _lastUpdate.Value.Add(_registryOptions.UpdateInterval) - DateTime.UtcNow;
}
}

Expand Down
248 changes: 124 additions & 124 deletions src/UnityNuGet.Tests/PlatformDefinitionTests.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;

namespace UnityNuGet.Tests
{
public class PlatformDefinitionTests
{
[Test]
public void CanFindDefinitions()
{
var platformDefs = PlatformDefinition.CreateAllPlatforms();

// Look-up by OS should return the most general configuration
PlatformDefinition? win = platformDefs.Find(UnityOs.Windows);
Assert.That(win, Is.Not.Null);
Assert.That(win!.Cpu, Is.EqualTo(UnityCpu.AnyCpu));

// Look-up explicit configuration
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;

namespace UnityNuGet.Tests
{
public class PlatformDefinitionTests
{
[Test]
public void CanFindDefinitions()
{
var platformDefs = PlatformDefinition.CreateAllPlatforms();

// Look-up by OS should return the most general configuration
PlatformDefinition? win = platformDefs.Find(UnityOs.Windows);
Assert.That(win, Is.Not.Null);
Assert.That(win!.Cpu, Is.EqualTo(UnityCpu.AnyCpu));

// Look-up explicit configuration
PlatformDefinition? win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64);
Assert.Multiple(() =>
{
Expand All @@ -29,116 +29,116 @@ public void CanFindDefinitions()
{
Assert.That(win64?.Cpu, Is.EqualTo(UnityCpu.X64));
Assert.That(win.Children, Does.Contain(win64));
});

// Look-up invalid configuration
PlatformDefinition? and = platformDefs.Find(UnityOs.Android, UnityCpu.None);
Assert.That(and, Is.Null);
}

[Test]
public void RemainingPlatforms_NoneVisited()
{
var platformDefs = PlatformDefinition.CreateAllPlatforms();
var visited = new HashSet<PlatformDefinition>();

// If no platform was visited, the remaining platforms should be the (AnyOS, AnyCPU) config.
HashSet<PlatformDefinition> remaining = platformDefs.GetRemainingPlatforms(visited);
});

// Look-up invalid configuration
PlatformDefinition? and = platformDefs.Find(UnityOs.Android, UnityCpu.None);
Assert.That(and, Is.Null);
}

[Test]
public void RemainingPlatforms_NoneVisited()
{
var platformDefs = PlatformDefinition.CreateAllPlatforms();
var visited = new HashSet<PlatformDefinition>();

// If no platform was visited, the remaining platforms should be the (AnyOS, AnyCPU) config.
HashSet<PlatformDefinition> remaining = platformDefs.GetRemainingPlatforms(visited);
Assert.That(remaining, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(remaining, Has.Count.EqualTo(1));
Assert.That(platformDefs, Is.EqualTo(remaining.First()));
});
}

[Test]
public void RemainingPlatforms_OneVisited()
{
var platformDefs = PlatformDefinition.CreateAllPlatforms();
foreach (PlatformDefinition child in platformDefs.Children)
{
var visited = new HashSet<PlatformDefinition>() { child };
HashSet<PlatformDefinition> remaining = platformDefs.GetRemainingPlatforms(visited);

// We should get all other children, except the one already visited
Assert.That(remaining.Count + 1, Is.EqualTo(platformDefs.Children.Count));
foreach (PlatformDefinition r in remaining)
});
}

[Test]
public void RemainingPlatforms_OneVisited()
{
var platformDefs = PlatformDefinition.CreateAllPlatforms();

foreach (var child in platformDefs.Children)
{
var visited = new HashSet<PlatformDefinition>() { child };
HashSet<PlatformDefinition> remaining = platformDefs.GetRemainingPlatforms(visited);

// We should get all other children, except the one already visited
Assert.That(remaining.Count + 1, Is.EqualTo(platformDefs.Children.Count));
foreach (PlatformDefinition r in remaining)
{
Assert.Multiple(() =>
{
Assert.That(child, Is.Not.EqualTo(r));
Assert.That(platformDefs.Children, Does.Contain(r));
});
}
}
}

[Test]
public void RemainingPlatforms_LeafVisited()
{
var platformDefs = PlatformDefinition.CreateAllPlatforms();
PlatformDefinition? win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64);
var visited = new HashSet<PlatformDefinition>() { win64! };

// The remaining platforms should be all non-windows, as well as all !x64 windows
var expected = platformDefs.Children
.Except([win64!.Parent])
.Concat(
win64.Parent!.Children
.Except([win64]))
.ToHashSet();
HashSet<PlatformDefinition> actual = platformDefs.GetRemainingPlatforms(visited);
Assert.That(expected.SetEquals(actual), Is.True);
}

[TestCase("")]
[TestCase("base")]
public void TestConfigPath_Root(string basePath)
{
var platformDefs = PlatformDefinition.CreateAllPlatforms();
var file = new PlatformFile("a/b/c.dll", platformDefs);

// We don't use extra paths for the (AnyOS, AnyCPU) configuration
string actual = file.GetDestinationPath(basePath);
string expected = Path.Combine(
basePath,
Path.GetFileName(file.SourcePath));
Assert.That(expected, Is.EqualTo(actual));
}

[TestCase("")]
[TestCase("base")]
public void TestConfigPath_OsOnly(string basePath)
{
var platformDefs = PlatformDefinition.CreateAllPlatforms();
PlatformDefinition? win = platformDefs.Find(UnityOs.Windows);
var file = new PlatformFile("a/b/c.dll", win!);

string actual = file.GetDestinationPath(basePath);
string expected = Path.Combine(
basePath,
"Windows",
Path.GetFileName(file.SourcePath));
Assert.That(expected, Is.EqualTo(actual));
}

[TestCase("")]
[TestCase("base")]
public void TestConfigPath_Full(string basePath)
{
var platformDefs = PlatformDefinition.CreateAllPlatforms();
PlatformDefinition? win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64);
var file = new PlatformFile("a/b/c.dll", win64!);

string actual = file.GetDestinationPath(basePath);
string expected = Path.Combine(
basePath,
"Windows",
"x86_64",
Path.GetFileName(file.SourcePath));
Assert.That(expected, Is.EqualTo(actual));
}
}
}
});
}
}
}

[Test]
public void RemainingPlatforms_LeafVisited()
{
var platformDefs = PlatformDefinition.CreateAllPlatforms();
PlatformDefinition? win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64);
var visited = new HashSet<PlatformDefinition>() { win64! };

// The remaining platforms should be all non-windows, as well as all !x64 windows
var expected = platformDefs.Children
.Except([win64!.Parent])
.Concat(
win64.Parent!.Children
.Except([win64]))
.ToHashSet();
HashSet<PlatformDefinition> actual = platformDefs.GetRemainingPlatforms(visited);
Assert.That(expected.SetEquals(actual), Is.True);
}

[TestCase("")]
[TestCase("base")]
public void TestConfigPath_Root(string basePath)
{
var platformDefs = PlatformDefinition.CreateAllPlatforms();
var file = new PlatformFile("a/b/c.dll", platformDefs);

// We don't use extra paths for the (AnyOS, AnyCPU) configuration
string actual = file.GetDestinationPath(basePath);
string expected = Path.Combine(
basePath,
Path.GetFileName(file.SourcePath));
Assert.That(expected, Is.EqualTo(actual));
}

[TestCase("")]
[TestCase("base")]
public void TestConfigPath_OsOnly(string basePath)
{
var platformDefs = PlatformDefinition.CreateAllPlatforms();
PlatformDefinition? win = platformDefs.Find(UnityOs.Windows);
var file = new PlatformFile("a/b/c.dll", win!);

string actual = file.GetDestinationPath(basePath);
string expected = Path.Combine(
basePath,
"Windows",
Path.GetFileName(file.SourcePath));
Assert.That(expected, Is.EqualTo(actual));
}

[TestCase("")]
[TestCase("base")]
public void TestConfigPath_Full(string basePath)
{
var platformDefs = PlatformDefinition.CreateAllPlatforms();
PlatformDefinition? win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64);
var file = new PlatformFile("a/b/c.dll", win64!);

string actual = file.GetDestinationPath(basePath);
string expected = Path.Combine(
basePath,
"Windows",
"x86_64",
Path.GetFileName(file.SourcePath));
Assert.That(expected, Is.EqualTo(actual));
}
}
}
2 changes: 1 addition & 1 deletion src/UnityNuGet.Tests/RegistryCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace UnityNuGet.Tests
{
public class RegistryCacheTests
{
[Test]
[Test,Order(99)]
public async Task TestBuild()
{
bool errorsTriggered = false;
Expand Down
Loading

0 comments on commit b125686

Please sign in to comment.