Skip to content

Commit 60dd23b

Browse files
feat: adds support for relative paths to --filter-file-path (#106)
* feat): --filter-file-path to support relative paths * chore: adds test case
1 parent 5bcf58c commit 60dd23b

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

src/DotnetAffected.Core/AffectedOptions.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ public AffectedOptions(
2525
string? exclusionRegex = null)
2626
{
2727
RepositoryPath = DetermineRepositoryPath(repositoryPath, filterFilePath);
28-
FilterFilePath = filterFilePath;
28+
29+
// Ensure the provided filter is a rooted path
30+
if (!string.IsNullOrEmpty(filterFilePath))
31+
{
32+
FilterFilePath = Path.IsPathRooted(filterFilePath)
33+
? filterFilePath
34+
: Path.Join(Environment.CurrentDirectory, filterFilePath);
35+
}
36+
2937
FromRef = fromRef ?? string.Empty;
3038
ToRef = toRef ?? string.Empty;
3139
ExclusionRegex = exclusionRegex;
@@ -76,8 +84,8 @@ private static string DetermineRepositoryPath(string? repositoryPath, string? fi
7684
var filterFileDirectory = Path.GetDirectoryName(filterfilePath);
7785
if (string.IsNullOrWhiteSpace(filterFileDirectory))
7886
{
79-
throw new InvalidOperationException(
80-
$"Failed to determine directory from filter file path path. Ensure the path exists: {filterfilePath}");
87+
// A relative path to a file may be provided, in such case getting the directory name fails.
88+
return Environment.CurrentDirectory;
8189
}
8290

8391
return filterFileDirectory;

test/dotnet-affected.Tests/PathGenerationTests.cs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,30 @@ private class RepositoryPathsClassData : IEnumerable<object[]>
1414
{
1515
public IEnumerator<object[]> GetEnumerator()
1616
{
17+
yield return new object[] { "/home/lchaia/dotnet-affected", "", "/home/lchaia/dotnet-affected", null };
1718
yield return new object[]
1819
{
19-
"/home/lchaia/dotnet-affected", "", "/home/lchaia/dotnet-affected"
20+
"", "/home/lchaia/dotnet-affected/Affected.sln",
21+
Path.GetDirectoryName("/home/lchaia/dotnet-affected/Affected.sln"),
22+
"/home/lchaia/dotnet-affected/Affected.sln"
2023
};
2124
yield return new object[]
2225
{
23-
"", "/home/lchaia/dotnet-affected/Affected.sln",
24-
Path.GetDirectoryName("/home/lchaia/dotnet-affected/Affected.sln")
26+
"/home/lchaia/dotnet-affected", "/home/lchaia/dotnet-affected/subdirectory/other/Affected.sln",
27+
"/home/lchaia/dotnet-affected", "/home/lchaia/dotnet-affected/subdirectory/other/Affected.sln",
2528
};
29+
yield return new object[] { "", "", Environment.CurrentDirectory, null };
30+
2631
yield return new object[]
2732
{
28-
"/home/lchaia/dotnet-affected", "/home/lchaia/dotnet-affected/subdirectory/other/Affected.sln",
29-
"/home/lchaia/dotnet-affected"
33+
"", "Affected.sln", Environment.CurrentDirectory,
34+
Path.Join(Environment.CurrentDirectory, "Affected.sln")
3035
};
36+
3137
yield return new object[]
3238
{
33-
"", "", Environment.CurrentDirectory
39+
"/home/lchaia/dotnet-affected", "Affected.sln", "/home/lchaia/dotnet-affected",
40+
Path.Join(Environment.CurrentDirectory, "Affected.sln")
3441
};
3542
}
3643

@@ -41,21 +48,20 @@ public IEnumerator<object[]> GetEnumerator()
4148
[ClassData(typeof(RepositoryPathsClassData))]
4249
public void Should_determine_repository_path_correctly(
4350
string repositoryPath,
44-
string solutionPath,
45-
string expected)
51+
string filterFilePath,
52+
string expectedRepository,
53+
string expectedFilterFilePath)
4654
{
47-
var options = new AffectedOptions(repositoryPath, solutionPath);
48-
Assert.Equal(expected, options.RepositoryPath);
55+
var options = new AffectedOptions(repositoryPath, filterFilePath);
56+
Assert.Equal(expectedRepository, options.RepositoryPath);
57+
Assert.Equal(expectedFilterFilePath, options.FilterFilePath);
4958
}
5059

5160
private class OutputDirPathsClassData : IEnumerable<object[]>
5261
{
5362
public IEnumerator<object[]> GetEnumerator()
5463
{
55-
yield return new object[]
56-
{
57-
"/home/lchaia/dotnet-affected", "", "/home/lchaia/dotnet-affected"
58-
};
64+
yield return new object[] { "/home/lchaia/dotnet-affected", "", "/home/lchaia/dotnet-affected" };
5965
yield return new object[]
6066
{
6167
"/home/lchaia/dotnet-affected", "relative/path",

0 commit comments

Comments
 (0)