Skip to content

Commit f065ba9

Browse files
committed
C#: Add unit tests for runtime version fetching.
1 parent 4270425 commit f065ba9

File tree

4 files changed

+98
-5
lines changed

4 files changed

+98
-5
lines changed

csharp/extractor/Semmle.Extraction.CSharp.Standalone/DotNet.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@
55

66
namespace Semmle.BuildAnalyser
77
{
8+
internal interface IDotNet
9+
{
10+
bool RestoreToDirectory(string project, string directory);
11+
bool New(string folder);
12+
bool AddPackage(string folder, string package);
13+
public IList<string> GetListedRuntimes();
14+
}
15+
816
/// <summary>
917
/// Utilities to run the "dotnet" command.
1018
/// </summary>
11-
internal class DotNet
19+
internal class DotNet : IDotNet
1220
{
1321
private const string dotnet = "dotnet";
1422
private readonly ProgressMonitor progressMonitor;

csharp/extractor/Semmle.Extraction.CSharp.Standalone/Properties/AssemblyInfo.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Reflection;
2+
using System.Runtime.CompilerServices;
23
using System.Runtime.InteropServices;
34

45
// General Information about an assembly is controlled through the following
@@ -13,6 +14,9 @@
1314
[assembly: AssemblyTrademark("")]
1415
[assembly: AssemblyCulture("")]
1516

17+
// Expose internals for testing purposes.
18+
[assembly: InternalsVisibleTo("Semmle.Extraction.Tests")]
19+
1620
// Setting ComVisible to false makes the types in this assembly not visible
1721
// to COM components. If you need to access a type in this assembly from
1822
// COM, set the ComVisible attribute to true on that type.

csharp/extractor/Semmle.Extraction.CSharp.Standalone/Runtime.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ internal partial class Runtime
1717
private const string netCoreApp = "Microsoft.NETCore.App";
1818
private const string aspNetCoreApp = "Microsoft.AspNetCore.App";
1919

20-
private readonly DotNet dotNet;
20+
private readonly IDotNet dotNet;
2121
private static string ExecutingRuntime => RuntimeEnvironment.GetRuntimeDirectory();
2222

23-
public Runtime(DotNet dotNet) => this.dotNet = dotNet;
23+
public Runtime(IDotNet dotNet) => this.dotNet = dotNet;
2424

25-
private sealed class RuntimeVersion : IComparable<RuntimeVersion>
25+
internal sealed class RuntimeVersion : IComparable<RuntimeVersion>
2626
{
2727
private readonly string dir;
2828
private Version Version { get; }
@@ -97,7 +97,10 @@ private static Dictionary<string, RuntimeVersion> ParseRuntimes(IList<string> li
9797
return runtimes;
9898
}
9999

100-
private Dictionary<string, RuntimeVersion> GetNewestRuntimes()
100+
/// <summary>
101+
/// Returns a dictionary mapping runtimes to their newest version.
102+
/// </summary>
103+
internal Dictionary<string, RuntimeVersion> GetNewestRuntimes()
101104
{
102105
var listed = dotNet.GetListedRuntimes();
103106
return ParseRuntimes(listed);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using Xunit;
2+
using System.Collections.Generic;
3+
using Semmle.BuildAnalyser;
4+
using Semmle.Extraction.CSharp.Standalone;
5+
6+
namespace Semmle.Extraction.Tests
7+
{
8+
internal class DotNetStub : IDotNet
9+
{
10+
private readonly IList<string> runtimes;
11+
12+
public DotNetStub(IList<string> runtimes) => this.runtimes = runtimes;
13+
14+
public bool AddPackage(string folder, string package) => true;
15+
16+
public bool New(string folder) => true;
17+
18+
public bool RestoreToDirectory(string project, string directory) => true;
19+
20+
public IList<string> GetListedRuntimes() => runtimes;
21+
}
22+
23+
public class RuntimeTests
24+
{
25+
[Fact]
26+
public void TestRuntime1()
27+
{
28+
// Setup
29+
var listedRuntimes = new List<string> {
30+
"Microsoft.AspNetCore.App 5.0.12 [/path/dotnet/shared/Microsoft.AspNetCore.App]",
31+
"Microsoft.AspNetCore.App 6.0.4 [/path/dotnet/shared/Microsoft.AspNetCore.App]",
32+
"Microsoft.AspNetCore.App 7.0.0 [/path/dotnet/shared/Microsoft.AspNetCore.App]",
33+
"Microsoft.AspNetCore.App 7.0.2 [/path/dotnet/shared/Microsoft.AspNetCore.App]",
34+
"Microsoft.NETCore.App 5.0.12 [/path/dotnet/shared/Microsoft.NETCore.App]",
35+
"Microsoft.NETCore.App 6.0.4 [/path/dotnet/shared/Microsoft.NETCore.App]",
36+
"Microsoft.NETCore.App 7.0.0 [/path/dotnet/shared/Microsoft.NETCore.App]",
37+
"Microsoft.NETCore.App 7.0.2 [/path/dotnet/shared/Microsoft.NETCore.App]"
38+
};
39+
var dotnet = new DotNetStub(listedRuntimes);
40+
var runtime = new Runtime(dotnet);
41+
42+
// Execute
43+
var runtimes = runtime.GetNewestRuntimes();
44+
45+
// Verify
46+
Assert.Equal(2, runtimes.Count);
47+
48+
Assert.True(runtimes.TryGetValue("Microsoft.AspNetCore.App", out var aspNetCoreApp));
49+
Assert.Equal("/path/dotnet/shared/Microsoft.AspNetCore.App/7.0.2", aspNetCoreApp.FullPath);
50+
51+
Assert.True(runtimes.TryGetValue("Microsoft.NETCore.App", out var netCoreApp));
52+
Assert.Equal("/path/dotnet/shared/Microsoft.NETCore.App/7.0.2", netCoreApp.FullPath);
53+
}
54+
55+
[Fact]
56+
public void TestRuntime2()
57+
{
58+
// Setup
59+
var listedRuntimes = new List<string>
60+
{
61+
"Microsoft.NETCore.App 7.0.2 [/path/dotnet/shared/Microsoft.NETCore.App]",
62+
"Microsoft.NETCore.App 8.0.0-preview.5.23280.8 [/path/dotnet/shared/Microsoft.NETCore.App]",
63+
"Microsoft.NETCore.App 8.0.0-preview.5.43280.8 [/path/dotnet/shared/Microsoft.NETCore.App]"
64+
};
65+
var dotnet = new DotNetStub(listedRuntimes);
66+
var runtime = new Runtime(dotnet);
67+
68+
// Execute
69+
var runtimes = runtime.GetNewestRuntimes();
70+
71+
// Verify
72+
Assert.Single(runtimes);
73+
74+
Assert.True(runtimes.TryGetValue("Microsoft.NETCore.App", out var netCoreApp));
75+
Assert.Equal("/path/dotnet/shared/Microsoft.NETCore.App/8.0.0-preview.5.43280.8", netCoreApp.FullPath);
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)