Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ public static IResourceBuilder<TResource> WithProjectDependencies<TResource>(
}

/// <summary>
/// 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.
/// </summary>
/// <param name="parentResource">The parent Perl application resource.</param>
/// <param name="packageManager">The selected package manager annotation.</param>
Expand All @@ -318,18 +318,28 @@ private static string ResolveInstallerCommand(
PerlPackageManagerAnnotation packageManager,
IResourceBuilder<PerlModuleInstallerResource> installerBuilder)
{
if (!parentResource.TryGetLastAnnotation<PerlbrewEnvironmentAnnotation>(out var perlbrewAnnotation) ||
perlbrewAnnotation.Environment is not { } perlbrewEnvironment)
var command = packageManager.ExecutableName;
if (parentResource.TryGetLastAnnotation<PerlbrewEnvironmentAnnotation>(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;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ private static IResourceBuilder<PerlAppResource> 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.
Expand Down
6 changes: 6 additions & 0 deletions src/CommunityToolkit.Aspire.Hosting.Perl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public void PublishMode_CollidingModuleNames_RetainsRequestsWithoutInstallers()
resource.Resource.Annotations.OfType<PerlRequiredModuleAnnotation>(),
annotation => Assert.Equal("Foo88Bar", annotation.Name),
annotation => Assert.Equal("Foo::Bar", annotation.Name));
#pragma warning disable ASPIRECOMMAND001
Assert.DoesNotContain(
resource.Resource.Annotations.OfType<RequiredCommandAnnotation>(),
annotation => annotation.Command == "cpan");
#pragma warning restore ASPIRECOMMAND001
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DistributedApplicationModel>();
var resource = Assert.Single(appModel.Resources.OfType<PerlAppResource>());
Assert.Empty(appModel.Resources.OfType<PerlModuleInstallerResource>());

#pragma warning disable ASPIRECOMMAND001
var annotations = resource.Annotations.OfType<RequiredCommandAnnotation>().ToList();
var annotation = Assert.Single(resource.Annotations.OfType<RequiredCommandAnnotation>());
#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<DistributedApplicationModel>();
var resource = Assert.Single(appModel.Resources.OfType<PerlAppResource>());
var installer = Assert.Single(appModel.Resources.OfType<PerlModuleInstallerResource>());
var expectedCommand = useCpanMinus ? "cpanm" : "cpan";

#pragma warning disable ASPIRECOMMAND001
Assert.DoesNotContain(resource.Resource.Annotations.OfType<RequiredCommandAnnotation>(), a => a.Command == "cpan");
var annotation = Assert.Single(installer.Annotations.OfType<RequiredCommandAnnotation>());
if (useCpanMinus)
{
Assert.DoesNotContain(
appModel.Resources.SelectMany(r => r.Annotations.OfType<RequiredCommandAnnotation>()),
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<WaitAnnotation>());
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<DistributedApplicationModel>();
var installer = Assert.Single(appModel.Resources.OfType<PerlModuleInstallerResource>());
var expectedCommand = useCarton ? "carton" : "cpanm";

#pragma warning disable ASPIRECOMMAND001
Assert.DoesNotContain(
appModel.Resources.SelectMany(r => r.Annotations.OfType<RequiredCommandAnnotation>()),
a => a.Command == "cpan");
var annotation = Assert.Single(installer.Annotations.OfType<RequiredCommandAnnotation>());
#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<DistributedApplicationModel>();
var installer = Assert.Single(appModel.Resources.OfType<PerlModuleInstallerResource>());

#pragma warning disable ASPIRECOMMAND001
var annotations = resource.Annotations.OfType<RequiredCommandAnnotation>().ToList();
Assert.DoesNotContain(
appModel.Resources.SelectMany(r => r.Annotations.OfType<RequiredCommandAnnotation>()),
a => a.Command == "cpan");
var annotation = Assert.Single(installer.Annotations.OfType<RequiredCommandAnnotation>());
#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]
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<IInteractionService>(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<IDistributedApplicationEventingSubscriber>(),
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<DistributedApplicationModel>();
var installer = Assert.Single(appModel.Resources.OfType<PerlModuleInstallerResource>());
#pragma warning disable ASPIRECOMMAND001
var annotation = Assert.Single(installer.Annotations.OfType<RequiredCommandAnnotation>());
#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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,9 @@ public void WithPerlbrewEnvironment_AddsPerlbrewRequiredCommand()
var annotations = resource.Annotations.OfType<RequiredCommandAnnotation>().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/");
}

Expand Down
Loading