Skip to content

Commit 38fb698

Browse files
CopilotJusterZhu
andauthored
fix: match by RelativePath in DefaultCleanMatcher to handle same-name files in different dirs
Agent-Logs-Url: https://github.com/GeneralLibrary/GeneralUpdate/sessions/6c694de5-ffd1-4c1e-bff3-6dc8faf754fe Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com>
1 parent 572e07f commit 38fb698

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/c#/DifferentialTest/Matchers/MatcherTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,36 @@ public void DefaultCleanMatcher_ReturnsNull_WhenNewFileDoesNotExist()
182182
Assert.Null(result);
183183
}
184184

185+
[Fact]
186+
public void DefaultCleanMatcher_ReturnsCorrectNode_WhenSameNameFilesExistInDifferentDirectories()
187+
{
188+
// Arrange – two old files with the same name but different relative paths
189+
var fileInRoot = Path.Combine(_testDirectory, "a.txt");
190+
var subDir = Path.Combine(_testDirectory, "sub");
191+
Directory.CreateDirectory(subDir);
192+
var fileInSub = Path.Combine(subDir, "a.txt");
193+
File.WriteAllText(fileInRoot, "root");
194+
File.WriteAllText(fileInSub, "sub");
195+
196+
// The new file lives in the sub-directory
197+
var newFile = new FileNode { Name = "a.txt", FullName = fileInSub, RelativePath = "sub/a.txt" };
198+
var oldFileRoot = new FileNode { Name = "a.txt", FullName = fileInRoot, RelativePath = "a.txt" };
199+
var oldFileSub = new FileNode { Name = "a.txt", FullName = fileInSub, RelativePath = "sub/a.txt" };
200+
201+
// oldFileRoot appears first in the list – before the fix FirstOrDefault would
202+
// return oldFileRoot, then the RelativePath guard would return null, hiding oldFileSub.
203+
var leftNodes = new List<FileNode> { oldFileRoot, oldFileSub };
204+
205+
var matcher = new DefaultCleanMatcher();
206+
207+
// Act
208+
var result = matcher.Match(newFile, leftNodes);
209+
210+
// Assert – must find oldFileSub, not be tricked by oldFileRoot
211+
Assert.NotNull(result);
212+
Assert.Same(oldFileSub, result);
213+
}
214+
185215
[Fact]
186216
public void DefaultCleanMatcher_ReturnsNull_WhenLeftNodesIsEmpty()
187217
{

src/c#/GeneralUpdate.Differential/Matchers/DefaultCleanMatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public ComparisonResult Compare(string sourcePath, string targetPath)
3030
/// <inheritdoc/>
3131
public FileNode? Match(FileNode newFile, IEnumerable<FileNode> leftNodes)
3232
{
33-
var oldFile = leftNodes.FirstOrDefault(i => i.Name.Equals(newFile.Name));
33+
var oldFile = leftNodes.FirstOrDefault(i => i.Name.Equals(newFile.Name) && i.RelativePath.Equals(newFile.RelativePath));
3434
if (oldFile is null) return null;
3535
if (!File.Exists(oldFile.FullName)) return null;
3636
if (!File.Exists(newFile.FullName)) return null;

0 commit comments

Comments
 (0)