From e7e17271bb6aeb5c2ea1cb198c12d3b76271ccb2 Mon Sep 17 00:00:00 2001 From: codenano077 Date: Mon, 25 May 2026 20:37:19 +0530 Subject: [PATCH] Add FindFilesInDirectory alias to FileAliases --- .../Unit/IO/FileAliasesTests.cs | 70 +++++++++++++++++++ src/Cake.Common/IO/FileAliases.cs | 31 ++++++++ 2 files changed, 101 insertions(+) diff --git a/src/Cake.Common.Tests/Unit/IO/FileAliasesTests.cs b/src/Cake.Common.Tests/Unit/IO/FileAliasesTests.cs index 6e836dc451..bdba58fbd6 100644 --- a/src/Cake.Common.Tests/Unit/IO/FileAliasesTests.cs +++ b/src/Cake.Common.Tests/Unit/IO/FileAliasesTests.cs @@ -1279,5 +1279,75 @@ public void Should_Expand_Existing_Environment_Variables() Assert.Equal("/bar/baz.qux", result.FullPath); } } + + public sealed class TheFindFilesInDirectory + { + [Fact] + public void Should_Throw_If_Context_Is_Null() + { + // Given + var directory = new DirectoryPath("./tests"); + + // when + var result = Record.Exception(() => + FileAliases.FindFilesInDirectory(null, directory, "*.csproj")); + + // Then + AssertEx.IsArgumentNullException(result, "context"); + } + + [Fact] + public void Should_Throw_If_Directory_Path_Is_Null() + { + // Given + var context = Substitute.For(); + + // When + var result = Record.Exception(() => + FileAliases.FindFilesInDirectory(context, null, "*.csproj")); + + // Then + AssertEx.IsArgumentNullException(result, "directoryPath"); + } + + [Fact] + public void Should_Throw_If_Pattern_Is_Null() + { + // Given + var context = Substitute.For(); + var directory = new DirectoryPath("./tests"); + + // When + var result = Record.Exception(() => + FileAliases.FindFilesInDirectory(context, directory, null)); + + // Then + AssertEx.IsArgumentNullException(result, "pattern"); + } + + [Fact] + public void Should_Return_Files_Matching_Pattern() + { + // Given + var environment = FakeEnvironment.CreateUnixEnvironment(); + var fileSystem = new FakeFileSystem(environment); + fileSystem.CreateFile("/Working/tests/project.csproj"); + fileSystem.CreateFile("/Working/tests/other.txt"); + + var context = Substitute.For(); + context.FileSystem.Returns(fileSystem); + context.Environment.Returns(environment); + + // When + var result = FileAliases.FindFilesInDirectory( + context, + new DirectoryPath("/Working/tests"), + "*.csproj").ToList(); + + // Then + Assert.Single(result); + Assert.Equal("/Working/tests/project.csproj", result[0].FullPath); + } + } } } \ No newline at end of file diff --git a/src/Cake.Common/IO/FileAliases.cs b/src/Cake.Common/IO/FileAliases.cs index 8d1d6ff631..06c2bb08c8 100644 --- a/src/Cake.Common/IO/FileAliases.cs +++ b/src/Cake.Common/IO/FileAliases.cs @@ -428,5 +428,36 @@ public static FilePath ExpandEnvironmentVariables(this ICakeContext context, Fil return filePath.ExpandEnvironmentVariables(context.Environment); } + + /// + /// Finds all files mactching the specified pattern within a directory + /// + /// The context. + /// The directory path to search in. + /// The file pattern to match (eg:"*.csproj"). + /// The search scope (current directory or recursive). + /// List of files matching the pattern. + /// + /// + /// var files = FindFilesInDirectory("./src", "*.csproj", SearchScope.Recursive); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Find")] + public static IEnumerable FindFilesInDirectory( + this ICakeContext context, + DirectoryPath directoryPath, + string pattern, + SearchScope scope = SearchScope.Recursive) + { + ArgumentNullException.ThrowIfNull(context); + ArgumentNullException.ThrowIfNull(directoryPath); + ArgumentNullException.ThrowIfNull(pattern); + + var directory = context.FileSystem.GetDirectory( + directoryPath.MakeAbsolute(context.Environment)); + + return directory.GetFiles(pattern, scope).Select(f => f.Path); + } } } \ No newline at end of file