Skip to content

Commit 540e612

Browse files
committed
Support package references and cut down lock file
1 parent 87f906a commit 540e612

File tree

2 files changed

+104
-6661
lines changed

2 files changed

+104
-6661
lines changed

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

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public void ItHandlesReferenceAndPackageReferenceNameCollisions()
216216
}
217217

218218
// If an assembly is in withResources, it has to be a key in dependencies, even with an empty list.
219-
private static DependencyContext BuildDependencyContextFromDependenciesWithResources(Dictionary<string, List<string>> dependencies, List<string> withResources, List<string> references)
219+
private static DependencyContext BuildDependencyContextFromDependenciesWithResources(Dictionary<string, List<string>> dependencies, List<string> withResources, List<string> references, bool dllReference)
220220
{
221221
string mainProjectName = "simpleApp";
222222
LockFile lockFile = TestLockFiles.GetLockFile(mainProjectName);
@@ -229,43 +229,49 @@ private static DependencyContext BuildDependencyContextFromDependenciesWithResou
229229
[]);
230230
string mainProjectDirectory = Path.GetDirectoryName(mainProject.ProjectPath);
231231

232-
ITaskItem[] referencePaths = references.Select(reference =>
232+
233+
ITaskItem[] referencePaths = dllReference ? references.Select(reference =>
233234
new MockTaskItem($"/usr/Path/{reference}.dll", new Dictionary<string, string> {
234235
{ "CopyLocal", "false" },
235236
{ "FusionName", $"{reference}, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" },
236237
{ "Version", "" },
237-
})).ToArray();
238+
})).ToArray() : [];
238239

239240
ProjectContext projectContext = lockFile.CreateProjectContext(
240-
FrameworkConstants.CommonFrameworks.NetCoreApp10.GetShortFolderName(),
241+
FrameworkConstants.CommonFrameworks.Net10_0.GetShortFolderName(),
241242
runtime: null,
242243
platformLibraryName: Constants.DefaultPlatformLibrary,
243244
runtimeFrameworks: null,
244245
isSelfContained: false);
245246

247+
if (!dllReference)
248+
{
249+
projectContext.LockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup(string.Empty, references));
250+
}
251+
246252
Dictionary<string, SingleProjectInfo> referenceProjectInfos = new();
247253

248254
foreach (KeyValuePair<string, List<string>> kvp in dependencies)
249255
{
250-
projectContext.LockFileTarget.Libraries.Add(
256+
projectContext.LockFileTarget.Libraries = projectContext.LockFileTarget.Libraries.Concat([
251257
new LockFileTargetLibrary()
252258
{
253259
Name = kvp.Key,
254260
Version = new NuGetVersion(4, 0, 0),
255261
Type = withResources.Contains(kvp.Key) ? "project" : "unrealType",
256262
Dependencies = kvp.Value.Select(n => new PackageDependency(n)).ToList()
257-
});
263+
}]).ToList();
258264

259265
if (withResources.Contains(kvp.Key))
260266
{
261267
var fullPath = Path.GetFullPath(Path.Combine(mainProjectDirectory, kvp.Key));
262-
lockFile.Libraries.Add(new LockFileLibrary()
268+
lockFile.Libraries = lockFile.Libraries.Concat([new LockFileLibrary()
263269
{
264270
Name = kvp.Key,
265271
Version = new NuGetVersion(4, 0, 0),
266272
Type = "project",
267273
MSBuildProject = fullPath
268-
});
274+
}]).ToList();
269275

270276
referenceProjectInfos.Add(fullPath, SingleProjectInfo.Create(kvp.Key, kvp.Key, ".dll", "4.0.0",
271277
[new MockTaskItem($"{kvp.Key}.resource", new Dictionary<string, string>() {
@@ -284,86 +290,100 @@ private static DependencyContext BuildDependencyContextFromDependenciesWithResou
284290
.Build();
285291
}
286292

287-
[Fact]
288-
public void DirectReferenceToPackageWithNoAssets()
293+
[Theory]
294+
[InlineData(true)]
295+
[InlineData(false)]
296+
public void DirectReferenceToPackageWithNoAssets(bool dllReference)
289297
{
290-
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources([], [], ["System.A"]);
298+
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources([], [], ["System.A"], dllReference);
291299
Save(dependencyContext);
292300
dependencyContext.RuntimeLibraries.Count.Should().Be(1);
293301
}
294302

295-
[Fact]
296-
public void IndirectReferenceToPackageWithNoAssets()
303+
[Theory]
304+
[InlineData(true)]
305+
[InlineData(false)]
306+
public void IndirectReferenceToPackageWithNoAssets(bool dllReference)
297307
{
298308
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources(new Dictionary<string, List<string>>() {
299309
{ "System.A", ["System.B"] }
300-
}, ["System.A"], ["System.A"]);
310+
}, ["System.A"], ["System.A"], dllReference);
301311
Save(dependencyContext);
302312
dependencyContext.RuntimeLibraries.Count.Should().Be(2);
303313
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.A"));
304314
}
305315

306-
[Fact]
307-
public void PackageWithNoAssetsReferencesPackageWithNoAssets()
316+
[Theory]
317+
[InlineData(true)]
318+
[InlineData(false)]
319+
public void PackageWithNoAssetsReferencesPackageWithNoAssets(bool dllReference)
308320
{
309321
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources(new Dictionary<string, List<string>>() {
310322
{ "System.A", ["System.B"] },
311323
{ "System.B", [] }
312-
}, [], ["System.A"]);
324+
}, [], ["System.A"], dllReference);
313325
Save(dependencyContext);
314326
dependencyContext.RuntimeLibraries.Count.Should().Be(1);
315327
}
316328

317-
[Fact]
318-
public void PackageWithNoAssetsReferencesPackageWithAssets()
329+
[Theory]
330+
[InlineData(true)]
331+
[InlineData(false)]
332+
public void PackageWithNoAssetsReferencesPackageWithAssets(bool dllReference)
319333
{
320334
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources(new Dictionary<string, List<string>>() {
321335
{ "System.A", ["System.B"] },
322336
{ "System.B", [] }
323-
}, ["System.B"], ["System.A"]);
337+
}, ["System.B"], ["System.A"], dllReference);
324338
Save(dependencyContext);
325339
dependencyContext.RuntimeLibraries.Count.Should().Be(3);
326340
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.A"));
327341
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.B"));
328342
}
329343

330-
[Fact]
331-
public void PackageWithNoAssetsReferencesPackageReferencesByOtherPackage()
344+
[Theory]
345+
[InlineData(true)]
346+
[InlineData(false)]
347+
public void PackageWithNoAssetsReferencesPackageReferencesByOtherPackage(bool dllReference)
332348
{
333349
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources(new Dictionary<string, List<string>>()
334350
{
335351
{ "System.A", ["System.B"] },
336352
{ "System.B", [] },
337-
}, ["System.B"], ["System.A", "System.B"]);
353+
}, ["System.B"], ["System.A", "System.B"], dllReference);
338354
Save(dependencyContext);
339355
dependencyContext.RuntimeLibraries.Count.Should().Be(2);
340356
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.B"));
341357
}
342358

343-
[Fact]
344-
public void PackageWithNoAssetsReferencesPackageWithAssetsWithOtherReferencer()
359+
[Theory]
360+
[InlineData(true)]
361+
[InlineData(false)]
362+
public void PackageWithNoAssetsReferencesPackageWithAssetsWithOtherReferencer(bool dllReference)
345363
{
346364
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources(new Dictionary<string, List<string>>()
347365
{
348366
{ "System.A", ["System.B"] },
349367
{ "System.B", [] },
350368
{ "System.C", ["System.B"] }
351-
}, ["System.B", "System.C"], ["System.A", "System.C"]);
369+
}, ["System.B", "System.C"], ["System.A", "System.C"], dllReference);
352370
Save(dependencyContext);
353371
dependencyContext.RuntimeLibraries.Count.Should().Be(3);
354372
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.C"));
355373
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.B"));
356374
}
357375

358-
[Fact]
359-
public void TwoPackagesWithNoAssetsReferencePackageWithAssets()
376+
[Theory]
377+
[InlineData(true)]
378+
[InlineData(false)]
379+
public void TwoPackagesWithNoAssetsReferencePackageWithAssets(bool dllReference)
360380
{
361381
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources(new Dictionary<string, List<string>>()
362382
{
363383
{ "System.A", ["System.B"] },
364384
{ "System.C", ["System.B"] },
365385
{ "System.B", [] }
366-
}, ["System.B"], ["System.A", "System.C"]);
386+
}, ["System.B"], ["System.A", "System.C"], dllReference);
367387
Save(dependencyContext);
368388
dependencyContext.RuntimeLibraries.Count.Should().Be(3);
369389
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.B"));

0 commit comments

Comments
 (0)