Skip to content

Commit f8d4273

Browse files
committed
C#: Add assets parsing unit test.
1 parent 7bbf1a2 commit f8d4273

File tree

1 file changed

+187
-0
lines changed
  • csharp/extractor/Semmle.Extraction.Tests

1 file changed

+187
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
using Xunit;
2+
using System.Linq;
3+
using Semmle.Extraction.CSharp.DependencyFetching;
4+
5+
namespace Semmle.Extraction.Tests
6+
{
7+
public class AssetsTests
8+
{
9+
private static string FixExpectedPathOnWindows(string path) => path.Replace('\\', '/');
10+
11+
[Fact]
12+
public void TestAssets1()
13+
{
14+
// Setup
15+
var assets = new Assets(new ProgressMonitor(new LoggerStub()));
16+
var json = assetsJson1;
17+
18+
// Execute
19+
var success = assets.TryParse(json, out var dependencies);
20+
21+
// Verify
22+
Assert.True(success);
23+
Assert.Equal(4, dependencies.Count());
24+
25+
var normalizedPaths = dependencies.Select(FixExpectedPathOnWindows);
26+
// Packages references
27+
Assert.Contains("castle.core/4.4.1/lib/netstandard1.5/Castle.Core.dll", normalizedPaths);
28+
Assert.Contains("json.net/1.0.33/lib/netstandard2.0/Json.Net.dll", normalizedPaths);
29+
Assert.Contains("microsoft.aspnetcore.cryptography.internal/6.0.8/lib/net6.0/Microsoft.AspNetCore.Cryptography.Internal.dll", normalizedPaths);
30+
Assert.Contains("humanizer.core/2.8.26/lib/netstandard2.0", normalizedPaths);
31+
}
32+
33+
[Fact]
34+
public void TestAssets2()
35+
{
36+
// Setup
37+
var assets = new Assets(new ProgressMonitor(new LoggerStub()));
38+
var json = assetsJson2;
39+
40+
// Execute
41+
var success = assets.TryParse(json, out var dependencies);
42+
43+
// Verify
44+
Assert.True(success);
45+
Assert.Equal(2, dependencies.Count());
46+
Assert.Contains("microsoft.netframework.referenceassemblies/1.0.3", dependencies);
47+
Assert.Contains("microsoft.netframework.referenceassemblies.net48/1.0.3", dependencies);
48+
}
49+
50+
[Fact]
51+
public void TestAssets3()
52+
{
53+
// Setup
54+
var assets = new Assets(new ProgressMonitor(new LoggerStub()));
55+
var json = "garbage data";
56+
57+
// Execute
58+
var success = assets.TryParse(json, out var dependencies);
59+
60+
// Verify
61+
Assert.False(success);
62+
Assert.Empty(dependencies);
63+
}
64+
65+
private readonly string assetsJson1 = """
66+
{
67+
"version": 3,
68+
"targets": {
69+
"net7.0": {
70+
"Castle.Core/4.4.1": {
71+
"type": "package",
72+
"dependencies": {
73+
"NETStandard.Library": "1.6.1",
74+
"System.Collections.Specialized": "4.3.0",
75+
},
76+
"compile": {
77+
"lib/netstandard1.5/Castle.Core.dll": {
78+
"related": ".xml"
79+
}
80+
},
81+
"runtime": {
82+
"lib/netstandard1.5/Castle.Core.dll": {
83+
"related": ".xml"
84+
}
85+
}
86+
},
87+
"Json.Net/1.0.33": {
88+
"type": "package",
89+
"compile": {
90+
"lib/netstandard2.0/Json.Net.dll": {}
91+
},
92+
"runtime": {
93+
"lib/netstandard2.0/Json.Net.dll": {}
94+
}
95+
},
96+
"MessagePackAnalyzer/2.1.152": {
97+
"type": "package"
98+
},
99+
"Microsoft.AspNetCore.Cryptography.Internal/6.0.8": {
100+
"type": "package",
101+
"compile": {
102+
"lib/net6.0/Microsoft.AspNetCore.Cryptography.Internal.dll": {
103+
"related": ".xml"
104+
}
105+
},
106+
"runtime": {
107+
"lib/net6.0/Microsoft.AspNetCore.Cryptography.Internal.dll": {
108+
"related": ".xml"
109+
}
110+
}
111+
},
112+
"Humanizer.Core/2.8.26": {
113+
"type": "package",
114+
"compile": {
115+
"lib/netstandard2.0/_._": {
116+
"related": ".xml"
117+
}
118+
},
119+
"runtime": {
120+
"lib/netstandard2.0/Humanizer.dll": {
121+
"related": ".xml"
122+
}
123+
}
124+
},
125+
"Nop.Core/4.5.0": {
126+
"type": "project",
127+
"compile": {
128+
"bin/placeholder/Nop.Core.dll": {}
129+
},
130+
"runtime": {
131+
"bin/placeholder/Nop.Core.dll": {}
132+
}
133+
},
134+
}
135+
},
136+
"project": {
137+
"version": "1.0.0",
138+
"frameworks": {
139+
"net7.0": {
140+
"targetAlias": "net7.0",
141+
"downloadDependencies": [
142+
{
143+
"name": "Microsoft.AspNetCore.App.Ref",
144+
"version": "[7.0.2, 7.0.2]"
145+
},
146+
{
147+
"name": "Microsoft.NETCore.App.Ref",
148+
"version": "[7.0.2, 7.0.2]"
149+
}
150+
],
151+
"frameworkReferences": {
152+
"Microsoft.AspNetCore.App": {
153+
"privateAssets": "none"
154+
},
155+
"Microsoft.NETCore.App": {
156+
"privateAssets": "all"
157+
}
158+
}
159+
}
160+
}
161+
}
162+
}
163+
""";
164+
165+
private readonly string assetsJson2 = """
166+
{
167+
"version": 3,
168+
"targets": {
169+
".NETFramework,Version=v4.8": {
170+
"Microsoft.NETFramework.ReferenceAssemblies/1.0.3": {
171+
"type": "package",
172+
"dependencies": {
173+
"Microsoft.NETFramework.ReferenceAssemblies.net48": "1.0.3"
174+
}
175+
},
176+
"Microsoft.NETFramework.ReferenceAssemblies.net48/1.0.3": {
177+
"type": "package",
178+
"build": {
179+
"build/Microsoft.NETFramework.ReferenceAssemblies.net48.targets": {}
180+
}
181+
}
182+
}
183+
}
184+
}
185+
""";
186+
}
187+
}

0 commit comments

Comments
 (0)