From 45544ff1ec93578eba0853645cc2d2f6537ef787 Mon Sep 17 00:00:00 2001 From: Cyrille DUPUYDAUBY Date: Tue, 16 Jul 2024 10:09:01 +0200 Subject: [PATCH 1/4] feat: add support for azure function --- src/Buildalyzer/Logging/EventProcessor.cs | 3 ++- .../Integration/SimpleProjectsFixture.cs | 21 +++++++++++++++++ .../AzureFunctionProject.csproj | 20 ++++++++++++++++ .../AzureFunctionProject/Function1.cs | 23 +++++++++++++++++++ .../projects/AzureFunctionProject/Program.cs | 20 ++++++++++++++++ tests/projects/AzureFunctionProject/host.json | 11 +++++++++ .../AzureFunctionProject/local.settings.json | 7 ++++++ 7 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 tests/projects/AzureFunctionProject/AzureFunctionProject.csproj create mode 100644 tests/projects/AzureFunctionProject/Function1.cs create mode 100644 tests/projects/AzureFunctionProject/Program.cs create mode 100644 tests/projects/AzureFunctionProject/host.json create mode 100644 tests/projects/AzureFunctionProject/local.settings.json diff --git a/src/Buildalyzer/Logging/EventProcessor.cs b/src/Buildalyzer/Logging/EventProcessor.cs index 9ac7ed26..e217873e 100644 --- a/src/Buildalyzer/Logging/EventProcessor.cs +++ b/src/Buildalyzer/Logging/EventProcessor.cs @@ -146,7 +146,8 @@ private void TargetFinished(object sender, TargetFinishedEventArgs e) private void MessageRaised(object sender, BuildMessageEventArgs e) { AnalyzerResult result = _currentResult.Count == 0 ? null : _currentResult.Peek(); - if (result is object) + // do not process any sub project + if (result is object && (string.IsNullOrEmpty(result.Command) || AnalyzerManager.NormalizePath(e.ProjectFile) == _projectFilePath)) { // Process the command line arguments for the Fsc task if (e.SenderName?.Equals("Fsc", StringComparison.OrdinalIgnoreCase) == true diff --git a/tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs b/tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs index 81cd2cfe..2933f32f 100644 --- a/tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs +++ b/tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs @@ -226,6 +226,27 @@ public void WpfControlLibraryGetsSourceFiles() }.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x)), log.ToString()); } + [Test] + [Platform("win")] + public void AzureFunctionSourceFiles() + { + // Given + StringWriter log = new StringWriter(); + IProjectAnalyzer analyzer = GetProjectAnalyzer(@"AzureFunctionProject\AzureFunctionProject.csproj", log); + + // When + IAnalyzerResults results = analyzer.Build(); + + // Then + IReadOnlyList sourceFiles = results.SingleOrDefault()?.SourceFiles; + sourceFiles.ShouldNotBeNull(log.ToString()); + new[] + { + "Program.cs", + "Function1.cs", + }.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x)), log.ToString()); + } + [Test] public void MultiTargetingBuildAllTargetFrameworksGetsSourceFiles() { diff --git a/tests/projects/AzureFunctionProject/AzureFunctionProject.csproj b/tests/projects/AzureFunctionProject/AzureFunctionProject.csproj new file mode 100644 index 00000000..d1c5ea0b --- /dev/null +++ b/tests/projects/AzureFunctionProject/AzureFunctionProject.csproj @@ -0,0 +1,20 @@ + + + net6.0 + Exe + v4 + + + + + + + + PreserveNewest + + + PreserveNewest + Never + + + diff --git a/tests/projects/AzureFunctionProject/Function1.cs b/tests/projects/AzureFunctionProject/Function1.cs new file mode 100644 index 00000000..21596fa6 --- /dev/null +++ b/tests/projects/AzureFunctionProject/Function1.cs @@ -0,0 +1,23 @@ +using Microsoft.Azure.WebJobs; +using Microsoft.Extensions.Logging; +using System.Threading.Tasks; + +namespace StrykerReplication +{ + public class TestFunction + { + [FunctionName("Function1")] + public async Task Run([CosmosDBTrigger( + databaseName: "databaseName", + collectionName: "collectionName", + ConnectionStringSetting = "", + LeaseCollectionName = "leases")]string input, + ILogger log) + { + if (input != null) + { + log.LogInformation("Document modified"); + } + } + } +} diff --git a/tests/projects/AzureFunctionProject/Program.cs b/tests/projects/AzureFunctionProject/Program.cs new file mode 100644 index 00000000..13b1f971 --- /dev/null +++ b/tests/projects/AzureFunctionProject/Program.cs @@ -0,0 +1,20 @@ +using Microsoft.Extensions.Hosting; + +namespace StrykerReplication +{ + public class Program + { + public static void Main() + { + var host = new HostBuilder() + .ConfigureServices(s => + { + + }) + .Build(); + + host.Run(); + + } + } +} \ No newline at end of file diff --git a/tests/projects/AzureFunctionProject/host.json b/tests/projects/AzureFunctionProject/host.json new file mode 100644 index 00000000..beb2e402 --- /dev/null +++ b/tests/projects/AzureFunctionProject/host.json @@ -0,0 +1,11 @@ +{ + "version": "2.0", + "logging": { + "applicationInsights": { + "samplingSettings": { + "isEnabled": true, + "excludedTypes": "Request" + } + } + } +} \ No newline at end of file diff --git a/tests/projects/AzureFunctionProject/local.settings.json b/tests/projects/AzureFunctionProject/local.settings.json new file mode 100644 index 00000000..30386e36 --- /dev/null +++ b/tests/projects/AzureFunctionProject/local.settings.json @@ -0,0 +1,7 @@ +{ + "IsEncrypted": false, + "Values": { + "AzureWebJobsStorage": "", + "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated" + } +} \ No newline at end of file From 3c975ed3cdb07058d4763ba181568f03766125a5 Mon Sep 17 00:00:00 2001 From: Cyrille DUPUYDAUBY Date: Tue, 16 Jul 2024 15:58:39 +0200 Subject: [PATCH 2/4] misc: address PR reveiw comments --- src/Buildalyzer/Logging/EventProcessor.cs | 48 ++++++++++++----------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/Buildalyzer/Logging/EventProcessor.cs b/src/Buildalyzer/Logging/EventProcessor.cs index e217873e..6ea96b41 100644 --- a/src/Buildalyzer/Logging/EventProcessor.cs +++ b/src/Buildalyzer/Logging/EventProcessor.cs @@ -145,32 +145,36 @@ private void TargetFinished(object sender, TargetFinishedEventArgs e) private void MessageRaised(object sender, BuildMessageEventArgs e) { - AnalyzerResult result = _currentResult.Count == 0 ? null : _currentResult.Peek(); - // do not process any sub project - if (result is object && (string.IsNullOrEmpty(result.Command) || AnalyzerManager.NormalizePath(e.ProjectFile) == _projectFilePath)) + var result = _currentResult.Count == 0 ? null : _currentResult.Peek(); + if (result is not object || !IsRelevant()) { - // Process the command line arguments for the Fsc task - if (e.SenderName?.Equals("Fsc", StringComparison.OrdinalIgnoreCase) == true - && !string.IsNullOrWhiteSpace(e.Message) - && _targetStack.Any(x => x.TargetName == "CoreCompile") - && result.CompilerCommand is null) - { - result.ProcessFscCommandLine(e.Message); - } + return; + } - // Process the command line arguments for the Csc task - if (e is TaskCommandLineEventArgs cmd - && string.Equals(cmd.TaskName, "Csc", StringComparison.OrdinalIgnoreCase)) - { - result.ProcessCscCommandLine(cmd.CommandLine, _targetStack.Any(x => x.TargetName == "CoreCompile")); - } + // Process the command line arguments for the Fsc task + if (e.SenderName?.Equals("Fsc", StringComparison.OrdinalIgnoreCase) == true + && !string.IsNullOrWhiteSpace(e.Message) + && _targetStack.Any(x => x.TargetName == "CoreCompile") + && result.CompilerCommand is null) + { + result.ProcessFscCommandLine(e.Message); + } - if (e is TaskCommandLineEventArgs cmdVbc && - string.Equals(cmdVbc.TaskName, "Vbc", StringComparison.OrdinalIgnoreCase)) - { - result.ProcessVbcCommandLine(cmdVbc.CommandLine); - } + // Process the command line arguments for the Csc task + if (e is TaskCommandLineEventArgs cmd + && string.Equals(cmd.TaskName, "Csc", StringComparison.OrdinalIgnoreCase)) + { + result.ProcessCscCommandLine(cmd.CommandLine, _targetStack.Any(x => x.TargetName == "CoreCompile")); } + + if (e is TaskCommandLineEventArgs cmdVbc && + string.Equals(cmdVbc.TaskName, "Vbc", StringComparison.OrdinalIgnoreCase)) + { + result.ProcessVbcCommandLine(cmdVbc.CommandLine); + } + + bool IsRelevant() => string.IsNullOrEmpty(result.Command) || AnalyzerManager.NormalizePath(e.ProjectFile) == _projectFilePath; + } private void BuildFinished(object sender, BuildFinishedEventArgs e) From 407b9c1a751c2a8a22b2a7aea4e7dfef05eac064 Mon Sep 17 00:00:00 2001 From: Cyrille DUPUYDAUBY Date: Thu, 1 Aug 2024 09:56:07 +0200 Subject: [PATCH 3/4] fix: address review comments --- .../projects/AzureFunctionProject/Program.cs | 22 ++++++++---------- .../{Function1.cs => TestFunction.cs} | 23 +++++++++---------- 2 files changed, 20 insertions(+), 25 deletions(-) rename tests/projects/AzureFunctionProject/{Function1.cs => TestFunction.cs} (52%) diff --git a/tests/projects/AzureFunctionProject/Program.cs b/tests/projects/AzureFunctionProject/Program.cs index 13b1f971..c83d88e1 100644 --- a/tests/projects/AzureFunctionProject/Program.cs +++ b/tests/projects/AzureFunctionProject/Program.cs @@ -1,20 +1,16 @@ using Microsoft.Extensions.Hosting; -namespace StrykerReplication +namespace AzureFunctionProject; + +public class Program { - public class Program + public static void Main() { - public static void Main() - { - var host = new HostBuilder() - .ConfigureServices(s => - { - - }) - .Build(); - - host.Run(); + var host = new HostBuilder() + .ConfigureServices(_ => + {}) + .Build(); - } + host.Run(); } } \ No newline at end of file diff --git a/tests/projects/AzureFunctionProject/Function1.cs b/tests/projects/AzureFunctionProject/TestFunction.cs similarity index 52% rename from tests/projects/AzureFunctionProject/Function1.cs rename to tests/projects/AzureFunctionProject/TestFunction.cs index 21596fa6..68d39b83 100644 --- a/tests/projects/AzureFunctionProject/Function1.cs +++ b/tests/projects/AzureFunctionProject/TestFunction.cs @@ -1,23 +1,22 @@ +using System.Threading.Tasks; using Microsoft.Azure.WebJobs; using Microsoft.Extensions.Logging; -using System.Threading.Tasks; -namespace StrykerReplication +namespace AzureFunctionProject; + +public class TestFunction { - public class TestFunction - { - [FunctionName("Function1")] - public async Task Run([CosmosDBTrigger( + [FunctionName(nameof(TestFunction))] + public async Task Run([CosmosDBTrigger( databaseName: "databaseName", collectionName: "collectionName", ConnectionStringSetting = "", LeaseCollectionName = "leases")]string input, - ILogger log) + ILogger log) + { + if (input != null) { - if (input != null) - { - log.LogInformation("Document modified"); - } + log.LogInformation("Document modified"); } } -} +} \ No newline at end of file From 436a9da657e0adadf768801b6016c727f967c8c2 Mon Sep 17 00:00:00 2001 From: Pablo Monteiro Date: Sun, 8 Sep 2024 01:51:17 -0300 Subject: [PATCH 4/4] fix: testing azure function --- .../Integration/SimpleProjectsFixture.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs b/tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs index 2933f32f..23618fed 100644 --- a/tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs +++ b/tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs @@ -242,9 +242,11 @@ public void AzureFunctionSourceFiles() sourceFiles.ShouldNotBeNull(log.ToString()); new[] { - "Program.cs", - "Function1.cs", - }.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x)), log.ToString()); + "Program", + "TestFunction", + "AssemblyAttributes", + "AssemblyInfo" + }.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x).Split('.').TakeLast(2).First()), log.ToString()); } [Test]