diff --git a/src/CommunityToolkit.Aspire.Hosting.Perl/PerlAppResourceBuilderExtensions.PackageManager.cs b/src/CommunityToolkit.Aspire.Hosting.Perl/PerlAppResourceBuilderExtensions.PackageManager.cs index e037fa263..0faa5b9c5 100644 --- a/src/CommunityToolkit.Aspire.Hosting.Perl/PerlAppResourceBuilderExtensions.PackageManager.cs +++ b/src/CommunityToolkit.Aspire.Hosting.Perl/PerlAppResourceBuilderExtensions.PackageManager.cs @@ -306,8 +306,8 @@ public static IResourceBuilder WithProjectDependencies( } /// - /// Resolves the installer command executable and applies perlbrew environment - /// variables to the installer when perlbrew is configured. + /// Resolves the installer executable, registers its required-command check, + /// and applies perlbrew environment variables when configured. /// /// The parent Perl application resource. /// The selected package manager annotation. @@ -318,18 +318,28 @@ private static string ResolveInstallerCommand( PerlPackageManagerAnnotation packageManager, IResourceBuilder installerBuilder) { - if (!parentResource.TryGetLastAnnotation(out var perlbrewAnnotation) || - perlbrewAnnotation.Environment is not { } perlbrewEnvironment) + var command = packageManager.ExecutableName; + if (parentResource.TryGetLastAnnotation(out var perlbrewAnnotation) && + perlbrewAnnotation.Environment is { } perlbrewEnvironment) { - return packageManager.ExecutableName; + installerBuilder.WithEnvironment(context => + { + ApplyPerlbrewEnvironment(context.EnvironmentVariables, perlbrewEnvironment); + }); + + command = perlbrewEnvironment.GetExecutable(command); } - installerBuilder.WithEnvironment(context => + var helpLink = packageManager.PackageManager switch { - ApplyPerlbrewEnvironment(context.EnvironmentVariables, perlbrewEnvironment); - }); + PerlPackageManager.Cpan => "https://metacpan.org/pod/CPAN", + PerlPackageManager.Cpanm => "https://cpanmin.us/", + PerlPackageManager.Carton => "https://metacpan.org/pod/Carton", + _ => throw new NotSupportedException($"Package manager '{packageManager.PackageManager}' is not supported.") + }; + installerBuilder.WithRequiredCommand(command, helpLink); - return perlbrewEnvironment.GetExecutable(packageManager.ExecutableName); + return command; } /// diff --git a/src/CommunityToolkit.Aspire.Hosting.Perl/PerlAppResourceBuilderExtensions.cs b/src/CommunityToolkit.Aspire.Hosting.Perl/PerlAppResourceBuilderExtensions.cs index 254cdbabc..ffe9adeb2 100644 --- a/src/CommunityToolkit.Aspire.Hosting.Perl/PerlAppResourceBuilderExtensions.cs +++ b/src/CommunityToolkit.Aspire.Hosting.Perl/PerlAppResourceBuilderExtensions.cs @@ -216,7 +216,6 @@ private static IResourceBuilder AddPerlAppCore( resourceBuilder.WithOtlpExporterIfEndpointAvailable(OtlpProtocol.HttpJson); resourceBuilder.WithRequiredCommand("perl", "https://www.perl.org/get.html"); - resourceBuilder.WithRequiredCommand("cpan", "https://metacpan.org/pod/CPAN"); // OpenTelemetry::Exporter::OTLP supports HTTP/JSON and HTTP/protobuf, not gRPC. // HTTP/JSON avoids requiring Google::ProtocolBuffers::Dynamic for protobuf encoding, which can be difficult (though not impossible)to get on windows, specifically. diff --git a/src/CommunityToolkit.Aspire.Hosting.Perl/README.md b/src/CommunityToolkit.Aspire.Hosting.Perl/README.md index 70daefffb..726f10bf4 100644 --- a/src/CommunityToolkit.Aspire.Hosting.Perl/README.md +++ b/src/CommunityToolkit.Aspire.Hosting.Perl/README.md @@ -142,6 +142,12 @@ While I highly recommend you use cpanm or Carton, the integration aims to suppor > `WithProjectDependencies()` is called, since `cpan` does not support `--installdeps`. > `WithLocalLib()` will also currently swap to `cpanm` because it wasn't clear to me at time of release how to integrate it with cpan. +In run mode, `cpan` is required only by individual-package installers that use the default +package manager. Scripts without dependency installation, cpanm installers, and Carton +installers do not require `cpan`. Each installer checks its final selected command before +starting, using its resolved perlbrew path when configured. Missing commands produce +warnings and installation links; they do not prevent an attempt to start the resource. + In run mode, calling `WithPackage()` repeatedly for the same module on the same resource reuses one installer and one completion dependency, including for names such as `Foo::Bar`. The installer keeps the `force` and `skipTest` values from the first call. diff --git a/tests/CommunityToolkit.Aspire.Hosting.Perl.Tests/PublishModeWiringTests.cs b/tests/CommunityToolkit.Aspire.Hosting.Perl.Tests/PublishModeWiringTests.cs index 752abc447..2cef4977f 100644 --- a/tests/CommunityToolkit.Aspire.Hosting.Perl.Tests/PublishModeWiringTests.cs +++ b/tests/CommunityToolkit.Aspire.Hosting.Perl.Tests/PublishModeWiringTests.cs @@ -40,6 +40,11 @@ public void PublishMode_CollidingModuleNames_RetainsRequestsWithoutInstallers() resource.Resource.Annotations.OfType(), annotation => Assert.Equal("Foo88Bar", annotation.Name), annotation => Assert.Equal("Foo::Bar", annotation.Name)); +#pragma warning disable ASPIRECOMMAND001 + Assert.DoesNotContain( + resource.Resource.Annotations.OfType(), + annotation => annotation.Command == "cpan"); +#pragma warning restore ASPIRECOMMAND001 } [Fact] diff --git a/tests/CommunityToolkit.Aspire.Hosting.Perl.Tests/RegistrationAndCommandTests.cs b/tests/CommunityToolkit.Aspire.Hosting.Perl.Tests/RegistrationAndCommandTests.cs index 105e64d4f..adcc0b3f6 100644 --- a/tests/CommunityToolkit.Aspire.Hosting.Perl.Tests/RegistrationAndCommandTests.cs +++ b/tests/CommunityToolkit.Aspire.Hosting.Perl.Tests/RegistrationAndCommandTests.cs @@ -6,44 +6,148 @@ namespace CommunityToolkit.Aspire.Hosting.Perl.Tests; public class RegistrationAndCommandTests { - [Fact] - public void AddPerlScriptAddsRequiredCommandAnnotation() + [Theory] + [InlineData("script")] + [InlineData("api")] + [InlineData("module")] + [InlineData("executable")] + public void AddPerlApp_WithoutDependencies_OnlyRequiresPerl(string entrypoint) { + using var tempDir = new TempDirectory(); var builder = DistributedApplication.CreateBuilder(); - builder.AddPerlScript("perl-app", "scripts", "app.pl"); + _ = entrypoint switch + { + "script" => builder.AddPerlScript("perl-app", tempDir.Path, "app.pl"), + "api" => builder.AddPerlApi("perl-app", tempDir.Path, "app.pl"), + "module" => builder.AddPerlModule("perl-app", tempDir.Path, "App::Main"), + "executable" => builder.AddPerlExecutable("perl-app", tempDir.Path, "app"), + _ => throw new ArgumentOutOfRangeException(nameof(entrypoint)) + }; using var app = builder.Build(); var appModel = app.Services.GetRequiredService(); var resource = Assert.Single(appModel.Resources.OfType()); + Assert.Empty(appModel.Resources.OfType()); #pragma warning disable ASPIRECOMMAND001 - var annotations = resource.Annotations.OfType().ToList(); + var annotation = Assert.Single(resource.Annotations.OfType()); #pragma warning restore ASPIRECOMMAND001 - Assert.Equal(2, annotations.Count); - Assert.Contains(annotations, a => a.Command == "perl" && a.HelpLink == "https://www.perl.org/get.html"); - Assert.Contains(annotations, a => a.Command == "cpan" && a.HelpLink == "https://metacpan.org/pod/CPAN"); + Assert.Equal("perl", annotation.Command); + Assert.Equal("https://www.perl.org/get.html", annotation.HelpLink); } - [Fact] - public void AddPerlApiAddsRequiredCommandAnnotation() + [Theory] + [InlineData(false, false)] + [InlineData(true, false)] + [InlineData(true, true)] + public async Task WithPackage_RequiresSelectedCommandOnInstaller(bool useCpanMinus, bool packageFirst) { + using var tempDir = new TempDirectory(); var builder = DistributedApplication.CreateBuilder(); + var resource = builder.AddPerlScript("perl-app", tempDir.Path, "app.pl"); + + if (useCpanMinus && !packageFirst) + { + resource.WithCpanMinus(); + } - builder.AddPerlApi("perl-api", "api", "server.pl"); + resource.WithPackage("DBI"); + + if (useCpanMinus && packageFirst) + { + resource.WithCpanMinus(); + } using var app = builder.Build(); + await LoggingTestHelper.PublishBeforeStartAndCollectLogsAsync(builder, app, resource.Resource.Name); var appModel = app.Services.GetRequiredService(); - var resource = Assert.Single(appModel.Resources.OfType()); + var installer = Assert.Single(appModel.Resources.OfType()); + var expectedCommand = useCpanMinus ? "cpanm" : "cpan"; + +#pragma warning disable ASPIRECOMMAND001 + Assert.DoesNotContain(resource.Resource.Annotations.OfType(), a => a.Command == "cpan"); + var annotation = Assert.Single(installer.Annotations.OfType()); + if (useCpanMinus) + { + Assert.DoesNotContain( + appModel.Resources.SelectMany(r => r.Annotations.OfType()), + a => a.Command == "cpan"); + } +#pragma warning restore ASPIRECOMMAND001 + Assert.Equal(expectedCommand, installer.Command); + Assert.Equal(expectedCommand, annotation.Command); + Assert.Equal(useCpanMinus ? "https://cpanmin.us/" : "https://metacpan.org/pod/CPAN", annotation.HelpLink); + var wait = Assert.Single(resource.Resource.Annotations.OfType()); + Assert.Same(installer, wait.Resource); + Assert.Equal(WaitType.WaitForCompletion, wait.WaitType); + } + + [Theory] + [InlineData(false, false)] + [InlineData(true, false)] + [InlineData(true, true)] + public async Task WithProjectDependencies_RequiresSelectedCommandOnInstaller(bool useCarton, bool dependenciesFirst) + { + using var tempDir = new TempDirectory(); + var builder = DistributedApplication.CreateBuilder(); + var resource = builder.AddPerlScript("perl-app", tempDir.Path, "app.pl"); + + if (useCarton && !dependenciesFirst) + { + resource.WithCarton(); + } + + resource.WithProjectDependencies(); + + if (useCarton && dependenciesFirst) + { + resource.WithCarton(); + } + + using var app = builder.Build(); + await LoggingTestHelper.PublishBeforeStartAndCollectLogsAsync(builder, app, resource.Resource.Name); + + var appModel = app.Services.GetRequiredService(); + var installer = Assert.Single(appModel.Resources.OfType()); + var expectedCommand = useCarton ? "carton" : "cpanm"; + +#pragma warning disable ASPIRECOMMAND001 + Assert.DoesNotContain( + appModel.Resources.SelectMany(r => r.Annotations.OfType()), + a => a.Command == "cpan"); + var annotation = Assert.Single(installer.Annotations.OfType()); +#pragma warning restore ASPIRECOMMAND001 + Assert.Equal(expectedCommand, installer.Command); + Assert.Equal(expectedCommand, annotation.Command); + Assert.Equal(useCarton ? "https://metacpan.org/pod/Carton" : "https://cpanmin.us/", annotation.HelpLink); + } + + [Fact] + public async Task AddPerlScript_WithCpanfile_RequiresCpanmWithoutCpan() + { + using var tempDir = new TempDirectory(); + File.WriteAllText(Path.Combine(tempDir.Path, "cpanfile"), "requires 'DBI';"); + var builder = DistributedApplication.CreateBuilder(); + var resource = builder.AddPerlScript("perl-app", tempDir.Path, "app.pl"); + + using var app = builder.Build(); + await LoggingTestHelper.PublishBeforeStartAndCollectLogsAsync(builder, app, resource.Resource.Name); + + var appModel = app.Services.GetRequiredService(); + var installer = Assert.Single(appModel.Resources.OfType()); #pragma warning disable ASPIRECOMMAND001 - var annotations = resource.Annotations.OfType().ToList(); + Assert.DoesNotContain( + appModel.Resources.SelectMany(r => r.Annotations.OfType()), + a => a.Command == "cpan"); + var annotation = Assert.Single(installer.Annotations.OfType()); #pragma warning restore ASPIRECOMMAND001 - Assert.Equal(2, annotations.Count); - Assert.Contains(annotations, a => a.Command == "perl"); - Assert.Contains(annotations, a => a.Command == "cpan"); + Assert.Equal("cpanm", installer.Command); + Assert.Equal("cpanm", annotation.Command); + Assert.Equal("https://cpanmin.us/", annotation.HelpLink); } [Fact] diff --git a/tests/CommunityToolkit.Aspire.Hosting.Perl.Tests/ValidationMessagingTests.cs b/tests/CommunityToolkit.Aspire.Hosting.Perl.Tests/ValidationMessagingTests.cs index 8820a653b..263e51281 100644 --- a/tests/CommunityToolkit.Aspire.Hosting.Perl.Tests/ValidationMessagingTests.cs +++ b/tests/CommunityToolkit.Aspire.Hosting.Perl.Tests/ValidationMessagingTests.cs @@ -1,5 +1,6 @@ using Aspire.Hosting; using Aspire.Hosting.ApplicationModel; +using Aspire.Hosting.Lifecycle; using CommunityToolkit.Aspire.Hosting.Perl; using CommunityToolkit.Aspire.Hosting.Perl.Annotations; using CommunityToolkit.Aspire.Testing; @@ -27,13 +28,59 @@ public void WithPerlbrewEnvironment_OnWindows_DoesNotRegisterPerlbrewRequiredCom #pragma warning restore ASPIRECOMMAND001 // Windows pathway uses direct interaction service in startup events, not command validation. - Assert.Equal(2, requiredCommands.Count); + Assert.Single(requiredCommands); Assert.Contains(requiredCommands, a => a.Command == "perl"); - Assert.Contains(requiredCommands, a => a.Command == "cpan"); + Assert.DoesNotContain(requiredCommands, a => a.Command == "cpan"); Assert.DoesNotContain(requiredCommands, a => a.Command == "perlbrew"); Assert.DoesNotContain(requiredCommands, a => a.HelpLink == "https://github.com/stevieb9/berrybrew"); } + [Fact] + public async Task InstallerRequiredCommand_UsesResolvedPathAndIsValidatedBeforeResourceStarts() + { + using var tempDir = new TempDirectory(); + var builder = DistributedApplication.CreateBuilder(new DistributedApplicationOptions + { + DisableDashboard = true + }); + var interactionService = new RecordingInteractionService(); +#pragma warning disable ASPIREINTERACTION001 + builder.Services.AddSingleton(interactionService); +#pragma warning restore ASPIREINTERACTION001 + + var environment = new PerlbrewEnvironment(tempDir.Path, "perl-5.38.0"); + var resource = builder.AddPerlScript("perl-app", tempDir.Path, "app.pl") + .WithAnnotation(new PerlbrewEnvironmentAnnotation(environment.Version, null) + { + Environment = environment + }) + .WithPackage("DBI"); + + using var app = builder.Build(); + var validatorSubscriber = Assert.Single( + app.Services.GetServices(), + subscriber => subscriber.GetType().Name == "RequiredCommandValidationEventingSubscriber"); + await validatorSubscriber.SubscribeAsync(builder.Eventing, builder.ExecutionContext, TestContext.Current.CancellationToken); + + await LoggingTestHelper.PublishBeforeStartAndCollectLogsAsync(builder, app, resource.Resource.Name); + + var appModel = app.Services.GetRequiredService(); + var installer = Assert.Single(appModel.Resources.OfType()); +#pragma warning disable ASPIRECOMMAND001 + var annotation = Assert.Single(installer.Annotations.OfType()); +#pragma warning restore ASPIRECOMMAND001 + Assert.Equal(environment.GetExecutable("cpan"), installer.Command); + Assert.Equal(installer.Command, annotation.Command); + Assert.False(interactionService.PromptNotificationCalled); + + await builder.Eventing.PublishAsync( + new BeforeResourceStartedEvent(installer, app.Services), TestContext.Current.CancellationToken); + + Assert.True(interactionService.PromptNotificationCalled); + Assert.Contains(installer.Command, interactionService.LastMessage); + Assert.Equal("https://metacpan.org/pod/CPAN", interactionService.LastOptions?.LinkUrl); + } + [Fact, RequiresWindows] public async Task WithPerlbrewEnvironment_OnWindows_PromptsExpectedNotification() { diff --git a/tests/CommunityToolkit.Aspire.Hosting.Perl.Tests/WithPerlbrewExtensionTests.cs b/tests/CommunityToolkit.Aspire.Hosting.Perl.Tests/WithPerlbrewExtensionTests.cs index db08d68da..c9fc48f19 100644 --- a/tests/CommunityToolkit.Aspire.Hosting.Perl.Tests/WithPerlbrewExtensionTests.cs +++ b/tests/CommunityToolkit.Aspire.Hosting.Perl.Tests/WithPerlbrewExtensionTests.cs @@ -118,10 +118,9 @@ public void WithPerlbrewEnvironment_AddsPerlbrewRequiredCommand() var annotations = resource.Annotations.OfType().ToList(); #pragma warning restore ASPIRECOMMAND001 - // Should have "perl" (from AddPerlAppCore), "cpan" (from AddPerlAppCore), and "perlbrew" (from WithPerlbrewEnvironment) - Assert.Equal(3, annotations.Count); + Assert.Equal(2, annotations.Count); Assert.Contains(annotations, a => a.Command == "perl"); - Assert.Contains(annotations, a => a.Command == "cpan"); + Assert.DoesNotContain(annotations, a => a.Command == "cpan"); Assert.Contains(annotations, a => a.Command == "perlbrew" && a.HelpLink == "https://perlbrew.pl/"); }