Skip to content

Commit cfea588

Browse files
committed
unit test for an extra case, slight formatting fix, minor refactor
1 parent 29ca5a5 commit cfea588

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

src/Build.UnitTests/BackEnd/IntrinsicTask_Tests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,26 @@ public void ItemKeepDuplicatesFalse()
314314
Assert.Single(group);
315315
}
316316

317+
[Fact]
318+
public void ItemKeepDuplicatesFalseTwoDuplicatesAtOnce()
319+
{
320+
string content = ObjectModelHelpers.CleanupFileContents(@"
321+
<Project ToolsVersion='msbuilddefaulttoolsversion' xmlns='msbuildnamespace'>
322+
<Target Name='t'>
323+
<ItemGroup>
324+
<i1 Include='a1'/>
325+
<i1 Include='a1;a1' KeepDuplicates='false' />
326+
</ItemGroup>
327+
</Target>
328+
</Project>");
329+
IntrinsicTask task = CreateIntrinsicTask(content);
330+
Lookup lookup = LookupHelpers.CreateEmptyLookup();
331+
ExecuteTask(task, lookup);
332+
333+
var group = lookup.GetItems("i1");
334+
Assert.Single(group);
335+
}
336+
317337
[Fact]
318338
public void ItemKeepDuplicatesAsCondition()
319339
{

src/Build/BackEnd/Components/RequestBuilder/IntrinsicTasks/ItemGroupIntrinsicTask.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Collections;
56
using System.Collections.Generic;
67
using System.Collections.Immutable;
78
using System.Linq;
@@ -215,7 +216,7 @@ private void ExecuteAdd(ProjectItemGroupTaskItemInstance child, ItemBucket bucke
215216
FileSystems.Default,
216217
LoggingContext);
217218

218-
Action<List<ProjectItemInstance>> logFunction = null;
219+
Action<IList> logFunction = null;
219220

220221
if (LogTaskInputs && !LoggingContext.LoggingService.OnlyLogCriticalEvents && itemsToAdd?.Count > 0)
221222
{
@@ -232,7 +233,7 @@ private void ExecuteAdd(ProjectItemGroupTaskItemInstance child, ItemBucket bucke
232233
child.Location);
233234
};
234235
}
235-
236+
236237
// Now add the items we created to the lookup.
237238
bucket.Lookup.AddNewItemsOfItemType(child.ItemType, itemsToAdd, !keepDuplicates, logFunction);
238239
// Add in one operation for potential copy-on-write

src/Build/BackEnd/Components/RequestBuilder/Lookup.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Collections;
56
using System.Collections.Generic;
67
using System.Linq;
78
using System.Threading;
@@ -636,7 +637,7 @@ internal void SetProperty(ProjectPropertyInstance property)
636637
/// <summary>
637638
/// Implements a true add, an item that has been created in a batch.
638639
/// </summary>
639-
internal void AddNewItemsOfItemType(string itemType, ICollection<ProjectItemInstance> group, bool doNotAddDuplicates = false, Action<List<ProjectItemInstance>> logFunction = null)
640+
internal void AddNewItemsOfItemType(string itemType, ICollection<ProjectItemInstance> group, bool doNotAddDuplicates = false, Action<IList> logFunction = null)
640641
{
641642
// Adding to outer scope could be easily implemented, but our code does not do it at present
642643
MustNotBeOuterScope();
@@ -658,24 +659,29 @@ internal void AddNewItemsOfItemType(string itemType, ICollection<ProjectItemInst
658659
IEnumerable<ProjectItemInstance> itemsToAdd = group;
659660
if (doNotAddDuplicates)
660661
{
661-
// Remove duplicates from the inputs.
662-
itemsToAdd = itemsToAdd.Distinct(ProjectItemInstance.EqualityComparer);
663662

664663
// Ensure we don't also add any that already exist.
665664
var existingItems = GetItems(itemType);
666665

667666
var existingItemsHashSet = existingItems.ToHashSet(ProjectItemInstance.EqualityComparer);
668-
667+
669668
if (existingItems.Count > 0)
670669
{
671-
670+
672671
var deduplicatedItemsToAdd = new List<ProjectItemInstance>();
673-
foreach (var item in itemsToAdd) {
674-
if (!existingItemsHashSet.Contains(item)) {
672+
foreach (var item in itemsToAdd)
673+
{
674+
if (existingItemsHashSet.Add(item))
675+
{
675676
deduplicatedItemsToAdd.Add(item);
676677
}
677678
}
678-
itemsToAdd = deduplicatedItemsToAdd;
679+
itemsToAdd = deduplicatedItemsToAdd;
680+
}
681+
else
682+
{
683+
// Remove the duplicates in case we're not concerned with the existing items.
684+
itemsToAdd = itemsToAdd.Distinct(ProjectItemInstance.EqualityComparer);
679685
}
680686
}
681687

@@ -687,7 +693,6 @@ internal void AddNewItemsOfItemType(string itemType, ICollection<ProjectItemInst
687693
var groupAsList = group as List<ProjectItemInstance>;
688694
logFunction?.Invoke(groupAsList ?? group.ToList());
689695
}
690-
691696

692697
PrimaryAddTable.ImportItemsOfType(itemType, itemsToAdd);
693698
}

0 commit comments

Comments
 (0)