Skip to content

Commit e73d3df

Browse files
committed
Add unit tests
1 parent 662d157 commit e73d3df

File tree

2 files changed

+6781
-43
lines changed

2 files changed

+6781
-43
lines changed

src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenADependencyContextBuilder.cs

Lines changed: 137 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,6 @@ public static IEnumerable<object[]> ProjectData
142142
new object[] { "dotnet.new", "1.0.0", null, "dotnet.new.resources", null, dotnetNewSatelliteAssemblies, null, null, null },
143143
new object[] { "simple.dependencies", "1.0.0", null, "simple.dependencies", null, null, null, null, resolvedNuGetFiles },
144144
};
145-
146-
147-
148145
}
149146
}
150147

@@ -220,10 +217,10 @@ public void ItHandlesReferenceAndPackageReferenceNameCollisions()
220217
.Contain(c => c.Name == "System.Collections.NonGeneric.Reference.Reference" && c.Type == "referenceassembly");
221218
}
222219

223-
[Fact]
224-
public void ItHandlesReferencesThatCannotBeRemovedProperly()
220+
// If an assembly is in withResources, it has to be a key in dependencies, even with an empty list.
221+
private DependencyContext BuildDependencyContextFromDependenciesWithResources(Dictionary<string, List<string>> dependencies, List<string> withResources, List<string> directReferences)
225222
{
226-
string mainProjectName = "simple.dependencies";
223+
string mainProjectName = "simpleApp";
227224
LockFile lockFile = TestLockFiles.GetLockFile(mainProjectName);
228225

229226
SingleProjectInfo mainProject = SingleProjectInfo.Create(
@@ -232,34 +229,14 @@ public void ItHandlesReferencesThatCannotBeRemovedProperly()
232229
".dll",
233230
"1.0.0",
234231
[]);
232+
string mainProjectDirectory = Path.GetDirectoryName(mainProject.ProjectPath);
235233

236-
ITaskItem[] referencePaths = new ITaskItem[]
237-
{
238-
new MockTaskItem(
239-
"/usr/Path/System.NotConflicting.dll",
240-
new Dictionary<string, string>
241-
{
242-
{ "CopyLocal", "false" },
243-
{ "FusionName", "System.NotConflicting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" },
244-
{ "Version", "" },
245-
}),
246-
new MockTaskItem(
247-
"/usr/Path/System.Collections.NonGeneric.dll",
248-
new Dictionary<string, string>
249-
{
250-
{ "CopyLocal", "false" },
251-
{ "FusionName", "System.Collections.NonGeneric, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" },
252-
{ "Version", "" },
253-
}),
254-
new MockTaskItem(
255-
"/usr/Path/System.Collections.NonGeneric.Reference.dll",
256-
new Dictionary<string, string>
257-
{
258-
{ "CopyLocal", "false" },
259-
{ "FusionName", "System.Collections.NonGeneric.Reference, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" },
260-
{ "Version", "" },
261-
}),
262-
};
234+
ITaskItem[] referencePaths = directReferences.Select(reference =>
235+
new MockTaskItem($"/usr/Path/{reference}.dll", new Dictionary<string, string> {
236+
{ "CopyLocal", "false" },
237+
{ "FusionName", $"{reference}, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" },
238+
{ "Version", "" },
239+
})).ToArray();
263240

264241
ProjectContext projectContext = lockFile.CreateProjectContext(
265242
FrameworkConstants.CommonFrameworks.NetCoreApp10.GetShortFolderName(),
@@ -268,30 +245,147 @@ public void ItHandlesReferencesThatCannotBeRemovedProperly()
268245
runtimeFrameworks: null,
269246
isSelfContained: false);
270247

248+
Dictionary<string, SingleProjectInfo> referenceProjectInfos = new();
249+
250+
foreach (KeyValuePair<string, List<string>> kvp in dependencies)
251+
{
252+
projectContext.LockFileTarget.Libraries.Add(
253+
new LockFileTargetLibrary()
254+
{
255+
Name = kvp.Key,
256+
Version = new NuGetVersion(4, 0, 0),
257+
Type = withResources.Contains(kvp.Key) ? "project" : "unrealType",
258+
Dependencies = kvp.Value.Select(n => new PackageDependency(n)).ToList()
259+
});
260+
261+
if (withResources.Contains(kvp.Key))
262+
{
263+
var fullPath = Path.GetFullPath(Path.Combine(mainProjectDirectory, kvp.Key));
264+
lockFile.Libraries.Add(new LockFileLibrary()
265+
{
266+
Name = kvp.Key,
267+
Version = new NuGetVersion(4, 0, 0),
268+
Type = "project",
269+
MSBuildProject = fullPath
270+
});
271+
272+
referenceProjectInfos.Add(fullPath, SingleProjectInfo.Create(kvp.Key, kvp.Key, ".dll", "4.0.0",
273+
[new MockTaskItem($"{kvp.Key}.resource", new Dictionary<string, string>() {
274+
{ "Culture", "en-us" },
275+
{ "TargetPath", $"{kvp.Key}.resource" }
276+
})]));
277+
}
278+
}
279+
271280
CompilationOptions compilationOptions = CreateCompilationOptions();
272281

273-
IEnumerable<ReferenceInfo> directReferences =
282+
/* IEnumerable<ReferenceInfo> references =
274283
ReferenceInfo.CreateDirectReferenceInfos(
275284
referencePaths,
276285
[],
277286
lockFileLookup: new LockFileLookup(lockFile),
278287
i => true,
279288
true);
280-
281-
DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, includeRuntimeFileVersions: false, runtimeGraph: null, projectContext: projectContext, libraryLookup: new LockFileLookup(lockFile))
289+
*/
290+
return new DependencyContextBuilder(mainProject, includeRuntimeFileVersions: false, runtimeGraph: null, projectContext: projectContext, libraryLookup: new LockFileLookup(lockFile))
282291
.WithReferenceAssemblies(ReferenceInfo.CreateReferenceInfos(referencePaths))
283292
.WithCompilationOptions(compilationOptions)
284-
.WithDirectReferences(directReferences)
293+
// .WithDirectReferences(references)
294+
.WithReferenceProjectInfos(referenceProjectInfos)
285295
.Build();
296+
}
286297

287-
// ensure the DependencyContext can be written out successfully - it has no duplicate dependency names
298+
[Fact]
299+
public void DirectReferenceToPackageWithNoAssets()
300+
{
301+
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources([], [], ["System.A"]);
302+
Save(dependencyContext);
303+
dependencyContext.RuntimeLibraries.Count.Should().Be(1);
304+
}
305+
306+
[Fact]
307+
public void IndirectReferenceToPackageWithNoAssets()
308+
{
309+
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources(new Dictionary<string, List<string>>() {
310+
{ "System.A", ["System.B"] }
311+
}, ["System.A"], ["System.A"]);
288312
Save(dependencyContext);
313+
dependencyContext.RuntimeLibraries.Count.Should().Be(2);
314+
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.A"));
315+
}
289316

290-
dependencyContext.RuntimeLibraries.Count.Should().Be(4);
291-
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("simple.dependencies"));
292-
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.NotConflicting"));
293-
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.Collections.NonGeneric.Reference"));
294-
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.Collections.NonGeneric.Reference.Reference"));
317+
[Fact]
318+
public void PackageWithNoAssetsReferencesPackageWithNoAssets()
319+
{
320+
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources(new Dictionary<string, List<string>>() {
321+
{ "System.A", ["System.B"] },
322+
{ "System.B", [] }
323+
}, [], ["System.A"]);
324+
Save(dependencyContext);
325+
dependencyContext.RuntimeLibraries.Count.Should().Be(1);
326+
}
327+
328+
[Fact]
329+
public void PackageWithNoAssetsReferencesPackageWithAssets()
330+
{
331+
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources(new Dictionary<string, List<string>>() {
332+
{ "System.A", ["System.B"] },
333+
{ "System.B", [] }
334+
}, ["System.B"], ["System.A"]);
335+
Save(dependencyContext);
336+
dependencyContext.RuntimeLibraries.Count.Should().Be(3);
337+
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.A"));
338+
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.B"));
339+
}
340+
341+
[Fact]
342+
public void PackageWithNoAssetsReferencesPackageReferencesByOtherPackage()
343+
{
344+
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources(new Dictionary<string, List<string>>()
345+
{
346+
{ "System.A", ["System.B"] },
347+
{ "System.B", [] },
348+
}, ["System.B"], ["System.A", "System.B"]);
349+
Save(dependencyContext);
350+
dependencyContext.RuntimeLibraries.Count.Should().Be(2);
351+
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.B"));
352+
}
353+
354+
[Fact]
355+
public void PackageWithNoAssetsReferencesPackageWithAssetsWithOtherReferencer()
356+
{
357+
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources(new Dictionary<string, List<string>>()
358+
{
359+
{ "System.A", ["System.B"] },
360+
{ "System.B", [] },
361+
{ "System.C", ["System.B"] }
362+
}, ["System.B", "System.C"], ["System.A", "System.C"]);
363+
Save(dependencyContext);
364+
dependencyContext.RuntimeLibraries.Count.Should().Be(3);
365+
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.C"));
366+
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.B"));
367+
}
368+
369+
[Fact]
370+
public void TwoPackagesWithNoAssetsReferencePackageWithAssets()
371+
{
372+
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources(new Dictionary<string, List<string>>()
373+
{
374+
{ "System.A", ["System.B"] },
375+
{ "System.C", ["System.B"] },
376+
{ "System.B", [] }
377+
}, ["System.B"], ["System.A", "System.C"]);
378+
Save(dependencyContext);
379+
dependencyContext.RuntimeLibraries.Count.Should().Be(3);
380+
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.B"));
381+
if (dependencyContext.RuntimeLibraries.Any(x => x.Name.Equals("System.A")))
382+
{
383+
dependencyContext.RuntimeLibraries.Should().NotContain(x => x.Name.Equals("System.C"));
384+
}
385+
else
386+
{
387+
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.C"));
388+
}
295389
}
296390

297391
private DependencyContext BuildDependencyContextWithReferenceAssemblies(bool useCompilationOptions)

0 commit comments

Comments
 (0)