C#: Add unit tests for runtime version fetching.

This commit is contained in:
Michael Nebel
2023-07-07 13:03:01 +02:00
parent 4270425f14
commit f065ba9aa1
4 changed files with 98 additions and 5 deletions

View File

@@ -5,10 +5,18 @@ using Semmle.Util;
namespace Semmle.BuildAnalyser
{
internal interface IDotNet
{
bool RestoreToDirectory(string project, string directory);
bool New(string folder);
bool AddPackage(string folder, string package);
public IList<string> GetListedRuntimes();
}
/// <summary>
/// Utilities to run the "dotnet" command.
/// </summary>
internal class DotNet
internal class DotNet : IDotNet
{
private const string dotnet = "dotnet";
private readonly ProgressMonitor progressMonitor;

View File

@@ -1,4 +1,5 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
@@ -13,6 +14,9 @@ using System.Runtime.InteropServices;
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Expose internals for testing purposes.
[assembly: InternalsVisibleTo("Semmle.Extraction.Tests")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.

View File

@@ -17,12 +17,12 @@ namespace Semmle.Extraction.CSharp.Standalone
private const string netCoreApp = "Microsoft.NETCore.App";
private const string aspNetCoreApp = "Microsoft.AspNetCore.App";
private readonly DotNet dotNet;
private readonly IDotNet dotNet;
private static string ExecutingRuntime => RuntimeEnvironment.GetRuntimeDirectory();
public Runtime(DotNet dotNet) => this.dotNet = dotNet;
public Runtime(IDotNet dotNet) => this.dotNet = dotNet;
private sealed class RuntimeVersion : IComparable<RuntimeVersion>
internal sealed class RuntimeVersion : IComparable<RuntimeVersion>
{
private readonly string dir;
private Version Version { get; }
@@ -97,7 +97,10 @@ namespace Semmle.Extraction.CSharp.Standalone
return runtimes;
}
private Dictionary<string, RuntimeVersion> GetNewestRuntimes()
/// <summary>
/// Returns a dictionary mapping runtimes to their newest version.
/// </summary>
internal Dictionary<string, RuntimeVersion> GetNewestRuntimes()
{
var listed = dotNet.GetListedRuntimes();
return ParseRuntimes(listed);

View File

@@ -0,0 +1,78 @@
using Xunit;
using System.Collections.Generic;
using Semmle.BuildAnalyser;
using Semmle.Extraction.CSharp.Standalone;
namespace Semmle.Extraction.Tests
{
internal class DotNetStub : IDotNet
{
private readonly IList<string> runtimes;
public DotNetStub(IList<string> runtimes) => this.runtimes = runtimes;
public bool AddPackage(string folder, string package) => true;
public bool New(string folder) => true;
public bool RestoreToDirectory(string project, string directory) => true;
public IList<string> GetListedRuntimes() => runtimes;
}
public class RuntimeTests
{
[Fact]
public void TestRuntime1()
{
// Setup
var listedRuntimes = new List<string> {
"Microsoft.AspNetCore.App 5.0.12 [/path/dotnet/shared/Microsoft.AspNetCore.App]",
"Microsoft.AspNetCore.App 6.0.4 [/path/dotnet/shared/Microsoft.AspNetCore.App]",
"Microsoft.AspNetCore.App 7.0.0 [/path/dotnet/shared/Microsoft.AspNetCore.App]",
"Microsoft.AspNetCore.App 7.0.2 [/path/dotnet/shared/Microsoft.AspNetCore.App]",
"Microsoft.NETCore.App 5.0.12 [/path/dotnet/shared/Microsoft.NETCore.App]",
"Microsoft.NETCore.App 6.0.4 [/path/dotnet/shared/Microsoft.NETCore.App]",
"Microsoft.NETCore.App 7.0.0 [/path/dotnet/shared/Microsoft.NETCore.App]",
"Microsoft.NETCore.App 7.0.2 [/path/dotnet/shared/Microsoft.NETCore.App]"
};
var dotnet = new DotNetStub(listedRuntimes);
var runtime = new Runtime(dotnet);
// Execute
var runtimes = runtime.GetNewestRuntimes();
// Verify
Assert.Equal(2, runtimes.Count);
Assert.True(runtimes.TryGetValue("Microsoft.AspNetCore.App", out var aspNetCoreApp));
Assert.Equal("/path/dotnet/shared/Microsoft.AspNetCore.App/7.0.2", aspNetCoreApp.FullPath);
Assert.True(runtimes.TryGetValue("Microsoft.NETCore.App", out var netCoreApp));
Assert.Equal("/path/dotnet/shared/Microsoft.NETCore.App/7.0.2", netCoreApp.FullPath);
}
[Fact]
public void TestRuntime2()
{
// Setup
var listedRuntimes = new List<string>
{
"Microsoft.NETCore.App 7.0.2 [/path/dotnet/shared/Microsoft.NETCore.App]",
"Microsoft.NETCore.App 8.0.0-preview.5.23280.8 [/path/dotnet/shared/Microsoft.NETCore.App]",
"Microsoft.NETCore.App 8.0.0-preview.5.43280.8 [/path/dotnet/shared/Microsoft.NETCore.App]"
};
var dotnet = new DotNetStub(listedRuntimes);
var runtime = new Runtime(dotnet);
// Execute
var runtimes = runtime.GetNewestRuntimes();
// Verify
Assert.Single(runtimes);
Assert.True(runtimes.TryGetValue("Microsoft.NETCore.App", out var netCoreApp));
Assert.Equal("/path/dotnet/shared/Microsoft.NETCore.App/8.0.0-preview.5.43280.8", netCoreApp.FullPath);
}
}
}