Skip to content

Commit cfdb3a6

Browse files
fix(predictions): normalize paths outside of project dir (#94)
Fixes #93
1 parent 6a83d2f commit cfdb3a6

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

src/DotnetAffected.Core/Prediction/ProjectInputFilesCollector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ public void AddInputFile(string path, ProjectInstance projectInstance, string pr
2525
// Make the path absolute if needed.
2626
if (!Path.IsPathRooted(path))
2727
{
28-
path = Path.GetFullPath(Path.Combine(projectInstance.Directory, path));
28+
path = Path.Combine(projectInstance.Directory, path);
2929
}
3030
else if (!path.StartsWith(_repositoryPath))
3131
{
3232
// ignore files outside the project's directory
3333
return;
3434
}
3535

36-
this.AllFiles.Add(path);
36+
this.AllFiles.Add(Path.GetFullPath(path));
3737
}
3838

3939
public void AddInputDirectory(string path, ProjectInstance projectInstance, string predictorName)

test/DotnetAffected.Core.Tests/ChangedProjectsPredictorTests.cs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ public async Task FindProjectsForFilePaths_ShouldFindSingleProject()
2828
await this.Repository.CreateTextFileAsync(targetFilePath, "// Initial content");
2929

3030
// Act
31-
var projects = this.Provider.GetReferencingProjects(new[]
32-
{
33-
targetFilePath,
34-
})
31+
var projects = this.Provider.GetReferencingProjects(new[] { targetFilePath, })
3532
.ToArray();
3633

3734
// Assert
@@ -55,10 +52,7 @@ public async Task FindProjectsForFilePaths_WithMultipleFiles_ShouldFindSinglePro
5552
await this.Repository.CreateTextFileAsync(targetFilePath2, "// Other content");
5653

5754
// Act
58-
var projects = this.Provider.GetReferencingProjects(new[]
59-
{
60-
targetFilePath, targetFilePath2
61-
})
55+
var projects = this.Provider.GetReferencingProjects(new[] { targetFilePath, targetFilePath2 })
6256
.ToArray();
6357

6458
// Assert
@@ -82,10 +76,7 @@ public async Task FindProjectsForFilePaths_ShouldFindMultipleProject()
8276
await this.Repository.CreateTextFileAsync(targetFilePath2, "// Initial content");
8377

8478
// Act
85-
var projects = this.Provider.GetReferencingProjects(new[]
86-
{
87-
targetFilePath, targetFilePath2
88-
})
79+
var projects = this.Provider.GetReferencingProjects(new[] { targetFilePath, targetFilePath2 })
8980
.OrderBy(p => p.GetProjectName())
9081
.ToArray();
9182

@@ -97,5 +88,34 @@ public async Task FindProjectsForFilePaths_ShouldFindMultipleProject()
9788
p => Assert.Equal(project1Node, p),
9889
p => Assert.Equal(project2Node, p));
9990
}
91+
92+
/// <summary>
93+
/// Covers the case where the project references a file outside the project directory
94+
/// using paths that are not normalized.
95+
/// affected should compare paths normmalized (Path.GetFullPath) so that the comparison works.
96+
/// </summary>
97+
[Fact]
98+
public async Task FindProjectsWithNonFullPathFilePaths_ShouldFindSingleProject()
99+
{
100+
// Arrange
101+
var project1 = Repository.CreateCsProject("context/Project1", p =>
102+
{
103+
p.AddItem("None", Path.Combine(Repository.Path, "context/Project1", "../../subdir/file.json"));
104+
});
105+
106+
var targetFilePath = Path.Join(this.Repository.Path, "/subdir/", "file.json");
107+
Directory.CreateDirectory(Path.Join(this.Repository.Path, "/subdir/"));
108+
await this.Repository.CreateTextFileAsync(targetFilePath, "// Initial content");
109+
110+
// Act
111+
var projects = this.Provider.GetReferencingProjects(new[] { targetFilePath, })
112+
.ToArray();
113+
114+
// Assert
115+
Assert.Single(projects);
116+
117+
var project1Node = Graph.FindNodeByPath(project1.FullPath);
118+
Assert.Equal(project1Node, projects.Single());
119+
}
100120
}
101121
}

0 commit comments

Comments
 (0)