Skip to content

Commit d60055b

Browse files
committed
C#: Add unit tests for DotNet.
1 parent f00b6e2 commit d60055b

File tree

3 files changed

+251
-9
lines changed

3 files changed

+251
-9
lines changed
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
using Xunit;
2+
using System.Collections.Generic;
3+
using Semmle.Extraction.CSharp.DependencyFetching;
4+
using System;
5+
using System.Linq;
6+
7+
namespace Semmle.Extraction.Tests
8+
{
9+
internal class DotnetCommandStub : IDotnetCommand
10+
{
11+
private readonly IList<string> output;
12+
private string lastArgs = "";
13+
public bool Success { get; set; } = true;
14+
15+
public DotnetCommandStub(IList<string> output)
16+
{
17+
this.output = output;
18+
}
19+
20+
public string Exec => "dotnet";
21+
22+
public bool RunCommand(string args)
23+
{
24+
lastArgs = args;
25+
return Success;
26+
}
27+
28+
public bool RunCommand(string args, out IList<string> output)
29+
{
30+
lastArgs = args;
31+
output = this.output;
32+
return Success;
33+
}
34+
35+
public string GetLastArgs() => lastArgs;
36+
}
37+
38+
public class DotNetTests
39+
{
40+
41+
private static IDotNet MakeDotnet(IDotnetCommand dotnetCommand) =>
42+
new DotNet(dotnetCommand, new ProgressMonitor(new LoggerStub()));
43+
44+
private static IList<string> MakeDotnetRestoreOutput() =>
45+
new List<string> {
46+
" Determining projects to restore...",
47+
" Restored /path/to/project.csproj (in 1.23 sec).",
48+
" Other output...",
49+
" More output...",
50+
" Restored /path/to/project2.csproj (in 4.56 sec).",
51+
" Other output...",
52+
};
53+
54+
private static IList<string> MakeDotnetListRuntimesOutput() =>
55+
new List<string> {
56+
"Microsoft.AspNetCore.App 7.0.2 [/path/dotnet/shared/Microsoft.AspNetCore.App]",
57+
"Microsoft.NETCore.App 7.0.2 [/path/dotnet/shared/Microsoft.NETCore.App]"
58+
};
59+
60+
[Fact]
61+
public void TestDotnetInfo()
62+
{
63+
// Setup
64+
var dotnetCommand = new DotnetCommandStub(new List<string>());
65+
66+
// Execute
67+
var _ = MakeDotnet(dotnetCommand);
68+
69+
// Verify
70+
var lastArgs = dotnetCommand.GetLastArgs();
71+
Assert.Equal("--info", lastArgs);
72+
}
73+
74+
[Fact]
75+
public void TestDotnetInfoFailure()
76+
{
77+
// Setup
78+
var dotnetCommand = new DotnetCommandStub(new List<string>()) { Success = false };
79+
80+
// Execute
81+
try
82+
{
83+
var _ = MakeDotnet(dotnetCommand);
84+
}
85+
86+
// Verify
87+
catch (Exception e)
88+
{
89+
Assert.Equal("dotnet --info failed.", e.Message);
90+
return;
91+
}
92+
Assert.Fail("Expected exception");
93+
}
94+
95+
[Fact]
96+
public void TestDotnetRestoreProjectToDirectory1()
97+
{
98+
// Setup
99+
var dotnetCommand = new DotnetCommandStub(new List<string>());
100+
var dotnet = MakeDotnet(dotnetCommand);
101+
102+
// Execute
103+
dotnet.RestoreProjectToDirectory("myproject.csproj", "mypackages", out var _);
104+
105+
// Verify
106+
var lastArgs = dotnetCommand.GetLastArgs();
107+
Assert.Equal("restore --no-dependencies \"myproject.csproj\" --packages \"mypackages\" /p:DisableImplicitNuGetFallbackFolder=true", lastArgs);
108+
}
109+
110+
[Fact]
111+
public void TestDotnetRestoreProjectToDirectory2()
112+
{
113+
// Setup
114+
var dotnetCommand = new DotnetCommandStub(new List<string>());
115+
var dotnet = MakeDotnet(dotnetCommand);
116+
117+
// Execute
118+
dotnet.RestoreProjectToDirectory("myproject.csproj", "mypackages", out var _, "myconfig.config");
119+
120+
// Verify
121+
var lastArgs = dotnetCommand.GetLastArgs();
122+
Assert.Equal("restore --no-dependencies \"myproject.csproj\" --packages \"mypackages\" /p:DisableImplicitNuGetFallbackFolder=true --configfile \"myconfig.config\"", lastArgs);
123+
}
124+
125+
[Fact]
126+
public void TestDotnetRestoreSolutionToDirectory1()
127+
{
128+
// Setup
129+
var dotnetCommand = new DotnetCommandStub(MakeDotnetRestoreOutput());
130+
var dotnet = MakeDotnet(dotnetCommand);
131+
132+
// Execute
133+
dotnet.RestoreSolutionToDirectory("mysolution.sln", "mypackages", out var projects);
134+
135+
// Verify
136+
var lastArgs = dotnetCommand.GetLastArgs();
137+
Assert.Equal("restore --no-dependencies \"mysolution.sln\" --packages \"mypackages\" /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal", lastArgs);
138+
Assert.Equal(2, projects.Count());
139+
Assert.Contains("/path/to/project.csproj", projects);
140+
Assert.Contains("/path/to/project2.csproj", projects);
141+
}
142+
143+
[Fact]
144+
public void TestDotnetRestoreSolutionToDirectory2()
145+
{
146+
// Setup
147+
var dotnetCommand = new DotnetCommandStub(MakeDotnetRestoreOutput());
148+
var dotnet = MakeDotnet(dotnetCommand);
149+
dotnetCommand.Success = false;
150+
151+
// Execute
152+
dotnet.RestoreSolutionToDirectory("mysolution.sln", "mypackages", out var projects);
153+
154+
// Verify
155+
var lastArgs = dotnetCommand.GetLastArgs();
156+
Assert.Equal("restore --no-dependencies \"mysolution.sln\" --packages \"mypackages\" /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal", lastArgs);
157+
Assert.Empty(projects);
158+
}
159+
160+
[Fact]
161+
public void TestDotnetNew()
162+
{
163+
// Setup
164+
var dotnetCommand = new DotnetCommandStub(new List<string>());
165+
var dotnet = MakeDotnet(dotnetCommand);
166+
167+
// Execute
168+
dotnet.New("myfolder");
169+
170+
// Verify
171+
var lastArgs = dotnetCommand.GetLastArgs();
172+
Assert.Equal("new console --no-restore --output \"myfolder\"", lastArgs);
173+
}
174+
175+
[Fact]
176+
public void TestDotnetAddPackage()
177+
{
178+
// Setup
179+
var dotnetCommand = new DotnetCommandStub(new List<string>());
180+
var dotnet = MakeDotnet(dotnetCommand);
181+
182+
// Execute
183+
dotnet.AddPackage("myfolder", "mypackage");
184+
185+
// Verify
186+
var lastArgs = dotnetCommand.GetLastArgs();
187+
Assert.Equal("add \"myfolder\" package \"mypackage\" --no-restore", lastArgs);
188+
}
189+
190+
[Fact]
191+
public void TestDotnetGetListedRuntimes1()
192+
{
193+
// Setup
194+
var dotnetCommand = new DotnetCommandStub(MakeDotnetListRuntimesOutput());
195+
var dotnet = MakeDotnet(dotnetCommand);
196+
197+
// Execute
198+
var runtimes = dotnet.GetListedRuntimes();
199+
200+
// Verify
201+
var lastArgs = dotnetCommand.GetLastArgs();
202+
Assert.Equal("--list-runtimes", lastArgs);
203+
Assert.Equal(2, runtimes.Count);
204+
Assert.Contains("Microsoft.AspNetCore.App 7.0.2 [/path/dotnet/shared/Microsoft.AspNetCore.App]", runtimes);
205+
Assert.Contains("Microsoft.NETCore.App 7.0.2 [/path/dotnet/shared/Microsoft.NETCore.App]", runtimes);
206+
}
207+
208+
[Fact]
209+
public void TestDotnetGetListedRuntimes2()
210+
{
211+
// Setup
212+
var dotnetCommand = new DotnetCommandStub(MakeDotnetListRuntimesOutput());
213+
var dotnet = MakeDotnet(dotnetCommand);
214+
dotnetCommand.Success = false;
215+
216+
// Execute
217+
var runtimes = dotnet.GetListedRuntimes();
218+
219+
// Verify
220+
var lastArgs = dotnetCommand.GetLastArgs();
221+
Assert.Equal("--list-runtimes", lastArgs);
222+
Assert.Empty(runtimes);
223+
}
224+
225+
[Fact]
226+
public void TestDotnetExec()
227+
{
228+
// Setup
229+
var dotnetCommand = new DotnetCommandStub(new List<string>());
230+
var dotnet = MakeDotnet(dotnetCommand);
231+
232+
// Execute
233+
dotnet.Exec("myarg1 myarg2");
234+
235+
// Verify
236+
var lastArgs = dotnetCommand.GetLastArgs();
237+
Assert.Equal("exec myarg1 myarg2", lastArgs);
238+
}
239+
}
240+
}

csharp/extractor/Semmle.Extraction.Tests/FileContent.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
using Xunit;
22
using System.Collections.Generic;
3-
using Semmle.Util.Logging;
43
using Semmle.Extraction.CSharp.DependencyFetching;
54

65
namespace Semmle.Extraction.Tests
76
{
8-
9-
internal class LoggerStub : ILogger
10-
{
11-
public void Log(Severity severity, string message) { }
12-
13-
public void Dispose() { }
14-
}
15-
167
internal class UnsafeFileReaderStub : IUnsafeFileReader
178
{
189
private readonly List<string> lines;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Semmle.Util.Logging;
2+
3+
namespace Semmle.Extraction.Tests
4+
{
5+
internal class LoggerStub : ILogger
6+
{
7+
public void Log(Severity severity, string message) { }
8+
9+
public void Dispose() { }
10+
}
11+
}

0 commit comments

Comments
 (0)