Skip to content

Commit b76f793

Browse files
committed
C#: Add some Feed manager unit tests.
1 parent 53972f5 commit b76f793

3 files changed

Lines changed: 203 additions & 9 deletions

File tree

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ internal class DotNetStub : IDotNet
88
{
99
private readonly IList<string> runtimes;
1010
private readonly IList<string> sdks;
11+
private readonly IList<string> nugetFeedsFromConfig;
12+
private readonly IList<string> nugetFeedsFromFolder;
1113

12-
public DotNetStub(IList<string> runtimes, IList<string> sdks)
14+
public DotNetStub(IList<string> runtimes, IList<string> sdks, IList<string> nugetFeedsFromConfig, IList<string> nugetFeedsFromFolder)
1315
{
1416
this.runtimes = runtimes;
1517
this.sdks = sdks;
18+
this.nugetFeedsFromConfig = nugetFeedsFromConfig;
19+
this.nugetFeedsFromFolder = nugetFeedsFromFolder;
1620
}
1721
public bool AddPackage(string folder, string package) => true;
1822

@@ -26,8 +30,8 @@ public DotNetStub(IList<string> runtimes, IList<string> sdks)
2630

2731
public bool Exec(List<string> execArgs) => true;
2832

29-
public IList<string> GetNugetFeeds(string nugetConfig) => [];
33+
public IList<string> GetNugetFeeds(string nugetConfig) => nugetFeedsFromConfig;
3034

31-
public IList<string> GetNugetFeedsFromFolder(string folderPath) => [];
35+
public IList<string> GetNugetFeedsFromFolder(string folderPath) => nugetFeedsFromFolder;
3236
}
3337
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
using Xunit;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using Semmle.Extraction.CSharp.DependencyFetching;
7+
8+
namespace Semmle.Extraction.Tests
9+
{
10+
public class DependabotProxyStub : IDependabotProxy
11+
{
12+
public string Address { get; } = "";
13+
public HashSet<string> RegistryURLs { get; } = ["https://example.com/registry1", "https://example.com/registry2"];
14+
public string? CertificatePath { get; } = null;
15+
public System.Security.Cryptography.X509Certificates.X509Certificate2? Certificate { get; } = null;
16+
17+
public void Dispose() { }
18+
}
19+
20+
public class FeedManagerIOStub : IFeedManagerIO
21+
{
22+
private readonly List<string> unreachableFeeds;
23+
24+
public FeedManagerIOStub(List<string> unreachableFeeds)
25+
{
26+
this.unreachableFeeds = unreachableFeeds;
27+
}
28+
29+
public string? GetDirectoryName(string path)
30+
{
31+
return "/path/to/folder";
32+
}
33+
34+
public bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount)
35+
{
36+
return !unreachableFeeds.Contains(feed);
37+
}
38+
}
39+
40+
public class FileProviderStub : IFileProvider
41+
{
42+
public DirectoryInfo SourceDir { get; } = new DirectoryInfo("/path/to/source");
43+
public IEnumerable<string> SmallNonBinary { get; } = Enumerable.Empty<string>();
44+
public IEnumerable<string> Sources { get; } = Enumerable.Empty<string>();
45+
public ICollection<string> Projects { get; } = new List<string>();
46+
public ICollection<string> Solutions { get; } = new List<string>();
47+
public IEnumerable<string> Dlls { get; } = Enumerable.Empty<string>();
48+
public ICollection<string> NugetConfigs { get; } = ["/path/to/nuget.config"];
49+
public ICollection<string> NugetExes { get; } = new List<string>();
50+
public string? RootNugetConfig { get; } = null;
51+
public IEnumerable<string> GlobalJsons { get; } = Enumerable.Empty<string>();
52+
public ICollection<string> PackagesConfigs { get; } = new List<string>();
53+
public ICollection<string> RazorViews { get; } = new List<string>();
54+
public ICollection<string> Resources { get; } = new List<string>();
55+
}
56+
57+
public class FeedManagerTests
58+
{
59+
private static FeedManager MakeFeedManager()
60+
{
61+
var logger = new LoggerStub();
62+
var dotnet = new DotNetStub([], [], ["E https://feed.from/config"], ["E https://feed.from/folder1", "E https://feed.from/folder2", "D https://feed.from/folder3"]);
63+
var dependabotProxy = new DependabotProxyStub();
64+
var fileProvider = new FileProviderStub();
65+
var feedManagerIo = new FeedManagerIOStub(["https://example.com/registry1", "https://feed.from/folder2"]);
66+
return new FeedManager(logger, dotnet, dependabotProxy, fileProvider, feedManagerIo);
67+
}
68+
69+
[Fact]
70+
public void TestExplicitFeeds()
71+
{
72+
// Setup
73+
var feedManager = MakeFeedManager();
74+
75+
// Execute
76+
var actualFeeds = feedManager.ExplicitFeeds;
77+
78+
// Verify
79+
Assert.Equal([
80+
"https://example.com/registry1",
81+
"https://example.com/registry2",
82+
"https://feed.from/config"
83+
], actualFeeds);
84+
}
85+
86+
[Fact]
87+
public void TestInheritedFeeds()
88+
{
89+
// Setup
90+
var feedManager = MakeFeedManager();
91+
92+
// Execute
93+
var inherited = feedManager.InheritedFeeds;
94+
95+
// Verify
96+
Assert.Equal([
97+
"https://feed.from/folder1",
98+
"https://feed.from/folder2"
99+
], inherited);
100+
}
101+
102+
[Fact]
103+
public void TestAllFeeds()
104+
{
105+
// Setup
106+
var feedManager = MakeFeedManager();
107+
108+
// Execute
109+
var all = feedManager.AllFeeds;
110+
111+
// Verify
112+
Assert.Equal([
113+
"https://example.com/registry1",
114+
"https://example.com/registry2",
115+
"https://feed.from/config",
116+
"https://feed.from/folder1",
117+
"https://feed.from/folder2"
118+
], all);
119+
}
120+
121+
[Fact]
122+
public void TestReachableFeeds()
123+
{
124+
// Setup
125+
var feedManager = MakeFeedManager();
126+
127+
// Execute
128+
var reachableFeeds = feedManager.ReachableFeeds;
129+
130+
// Verify
131+
Assert.Equal([
132+
"https://example.com/registry2",
133+
"https://feed.from/config",
134+
"https://feed.from/folder1"
135+
], reachableFeeds);
136+
}
137+
138+
[Fact]
139+
public void TestReachableExplicitFeeds()
140+
{
141+
// Setup
142+
var feedManager = MakeFeedManager();
143+
144+
// Execute
145+
var reachableFeeds = feedManager.ReachableExplicitFeeds;
146+
147+
// Verify
148+
Assert.Equal([
149+
"https://example.com/registry2",
150+
"https://feed.from/config"
151+
], reachableFeeds);
152+
}
153+
154+
[Fact]
155+
public void TestReachableFallbackFeeds()
156+
{
157+
// Setup
158+
var feedManager = MakeFeedManager();
159+
160+
// Execute
161+
var reachableFallback = feedManager.ReachableFallbackFeeds;
162+
163+
// Verify
164+
Assert.Equal([
165+
"https://example.com/registry2",
166+
"https://feed.from/config",
167+
"https://api.nuget.org/v3/index.json"
168+
], reachableFallback);
169+
}
170+
171+
[Fact]
172+
public void TestFeedsToUse()
173+
{
174+
// Setup
175+
var feedManager = MakeFeedManager();
176+
177+
// Execute
178+
var feedsToUse = feedManager.FeedsToUse("/path/to/packages.config").ToHashSet();
179+
180+
// Verify
181+
Assert.Equal([
182+
"https://example.com/registry2",
183+
"https://feed.from/folder1"
184+
], feedsToUse);
185+
}
186+
}
187+
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public class RuntimeTests
99
{
1010
private static string FixExpectedPathOnWindows(string path) => path.Replace('\\', '/');
1111

12+
private static DotNetStub MakeDotNetStub(IList<string> listedRuntimes) => new DotNetStub(listedRuntimes, null!, [], []);
13+
1214
[Fact]
1315
public void TestRuntime1()
1416
{
@@ -23,7 +25,7 @@ public void TestRuntime1()
2325
"Microsoft.NETCore.App 7.0.0 [/path/dotnet/shared/Microsoft.NETCore.App]",
2426
"Microsoft.NETCore.App 7.0.2 [/path/dotnet/shared/Microsoft.NETCore.App]"
2527
};
26-
var dotnet = new DotNetStub(listedRuntimes, null!);
28+
var dotnet = MakeDotNetStub(listedRuntimes);
2729
var runtime = new Runtime(dotnet);
2830

2931
// Execute
@@ -49,7 +51,7 @@ public void TestRuntime2()
4951
"Microsoft.NETCore.App 8.0.0-preview.5.43280.8 [/path/dotnet/shared/Microsoft.NETCore.App]",
5052
"Microsoft.NETCore.App 8.0.0-preview.5.23280.8 [/path/dotnet/shared/Microsoft.NETCore.App]"
5153
};
52-
var dotnet = new DotNetStub(listedRuntimes, null!);
54+
var dotnet = new DotNetStub(listedRuntimes, null!, [], []);
5355
var runtime = new Runtime(dotnet);
5456

5557
// Execute
@@ -72,7 +74,7 @@ public void TestRuntime3()
7274
"Microsoft.NETCore.App 8.0.0-rc.4.43280.8 [/path/dotnet/shared/Microsoft.NETCore.App]",
7375
"Microsoft.NETCore.App 8.0.0-preview.5.23280.8 [/path/dotnet/shared/Microsoft.NETCore.App]"
7476
};
75-
var dotnet = new DotNetStub(listedRuntimes, null!);
77+
var dotnet = MakeDotNetStub(listedRuntimes);
7678
var runtime = new Runtime(dotnet);
7779

7880
// Execute
@@ -101,7 +103,7 @@ public void TestRuntime4()
101103
@"Microsoft.WindowsDesktop.App 6.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]",
102104
@"Microsoft.WindowsDesktop.App 7.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]"
103105
};
104-
var dotnet = new DotNetStub(listedRuntimes, null!);
106+
var dotnet = MakeDotNetStub(listedRuntimes);
105107
var runtime = new Runtime(dotnet);
106108

107109
// Execute
@@ -122,6 +124,7 @@ public class SdkTests
122124
{
123125
private static string FixExpectedPathOnWindows(string path) => path.Replace('\\', '/');
124126

127+
private static DotNetStub MakeDotNetStub(IList<string> listedSdks) => new DotNetStub(null!, listedSdks, [], []);
125128
[Fact]
126129
public void TestSdk1()
127130
{
@@ -136,7 +139,7 @@ public void TestSdk1()
136139
"6.0.102 [/usr/local/share/dotnet/sdk6]",
137140
"6.0.301 [/usr/local/share/dotnet/sdk7]",
138141
};
139-
var dotnet = new DotNetStub(null!, listedSdks);
142+
var dotnet = MakeDotNetStub(listedSdks);
140143
var sdk = new Sdk(dotnet, new LoggerStub());
141144

142145
// Execute
@@ -158,7 +161,7 @@ public void TestSdk2()
158161
"8.0.100-preview.7.23376.3 [/usr/local/share/dotnet/sdk3]",
159162
"7.0.400 [/usr/local/share/dotnet/sdk4]",
160163
};
161-
var dotnet = new DotNetStub(null!, listedSdks);
164+
var dotnet = MakeDotNetStub(listedSdks);
162165
var sdk = new Sdk(dotnet, new LoggerStub());
163166

164167
// Execute

0 commit comments

Comments
 (0)