Skip to content

Commit

Permalink
feat: adds support for relative paths to --filter-file-path (#106)
Browse files Browse the repository at this point in the history
* feat): --filter-file-path to support relative paths

* chore: adds test case
  • Loading branch information
leonardochaia authored Nov 12, 2024
1 parent 5bcf58c commit 60dd23b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
14 changes: 11 additions & 3 deletions src/DotnetAffected.Core/AffectedOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ public AffectedOptions(
string? exclusionRegex = null)
{
RepositoryPath = DetermineRepositoryPath(repositoryPath, filterFilePath);
FilterFilePath = filterFilePath;

// Ensure the provided filter is a rooted path
if (!string.IsNullOrEmpty(filterFilePath))
{
FilterFilePath = Path.IsPathRooted(filterFilePath)
? filterFilePath
: Path.Join(Environment.CurrentDirectory, filterFilePath);
}

FromRef = fromRef ?? string.Empty;
ToRef = toRef ?? string.Empty;
ExclusionRegex = exclusionRegex;
Expand Down Expand Up @@ -76,8 +84,8 @@ private static string DetermineRepositoryPath(string? repositoryPath, string? fi
var filterFileDirectory = Path.GetDirectoryName(filterfilePath);
if (string.IsNullOrWhiteSpace(filterFileDirectory))
{
throw new InvalidOperationException(
$"Failed to determine directory from filter file path path. Ensure the path exists: {filterfilePath}");
// A relative path to a file may be provided, in such case getting the directory name fails.
return Environment.CurrentDirectory;
}

return filterFileDirectory;
Expand Down
34 changes: 20 additions & 14 deletions test/dotnet-affected.Tests/PathGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,30 @@ private class RepositoryPathsClassData : IEnumerable<object[]>
{
public IEnumerator<object[]> GetEnumerator()
{
yield return new object[] { "/home/lchaia/dotnet-affected", "", "/home/lchaia/dotnet-affected", null };
yield return new object[]
{
"/home/lchaia/dotnet-affected", "", "/home/lchaia/dotnet-affected"
"", "/home/lchaia/dotnet-affected/Affected.sln",
Path.GetDirectoryName("/home/lchaia/dotnet-affected/Affected.sln"),
"/home/lchaia/dotnet-affected/Affected.sln"
};
yield return new object[]
{
"", "/home/lchaia/dotnet-affected/Affected.sln",
Path.GetDirectoryName("/home/lchaia/dotnet-affected/Affected.sln")
"/home/lchaia/dotnet-affected", "/home/lchaia/dotnet-affected/subdirectory/other/Affected.sln",
"/home/lchaia/dotnet-affected", "/home/lchaia/dotnet-affected/subdirectory/other/Affected.sln",
};
yield return new object[] { "", "", Environment.CurrentDirectory, null };

yield return new object[]
{
"/home/lchaia/dotnet-affected", "/home/lchaia/dotnet-affected/subdirectory/other/Affected.sln",
"/home/lchaia/dotnet-affected"
"", "Affected.sln", Environment.CurrentDirectory,
Path.Join(Environment.CurrentDirectory, "Affected.sln")
};

yield return new object[]
{
"", "", Environment.CurrentDirectory
"/home/lchaia/dotnet-affected", "Affected.sln", "/home/lchaia/dotnet-affected",
Path.Join(Environment.CurrentDirectory, "Affected.sln")
};
}

Expand All @@ -41,21 +48,20 @@ public IEnumerator<object[]> GetEnumerator()
[ClassData(typeof(RepositoryPathsClassData))]
public void Should_determine_repository_path_correctly(
string repositoryPath,
string solutionPath,
string expected)
string filterFilePath,
string expectedRepository,
string expectedFilterFilePath)
{
var options = new AffectedOptions(repositoryPath, solutionPath);
Assert.Equal(expected, options.RepositoryPath);
var options = new AffectedOptions(repositoryPath, filterFilePath);
Assert.Equal(expectedRepository, options.RepositoryPath);
Assert.Equal(expectedFilterFilePath, options.FilterFilePath);
}

private class OutputDirPathsClassData : IEnumerable<object[]>
{
public IEnumerator<object[]> GetEnumerator()
{
yield return new object[]
{
"/home/lchaia/dotnet-affected", "", "/home/lchaia/dotnet-affected"
};
yield return new object[] { "/home/lchaia/dotnet-affected", "", "/home/lchaia/dotnet-affected" };
yield return new object[]
{
"/home/lchaia/dotnet-affected", "relative/path",
Expand Down

0 comments on commit 60dd23b

Please sign in to comment.