Skip to content

Commit

Permalink
Merge branch 'ForceCompilation'
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Makarchuk committed Oct 31, 2024
2 parents d751217 + 7dd8742 commit 4441931
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 5 deletions.
88 changes: 88 additions & 0 deletions clio.tests/Command/PushPkgCommand.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using Autofac;
using Clio.Command;
using Clio.Package;
using Clio.Project;
using FluentAssertions;
using NSubstitute;
using NUnit.Framework;

namespace Clio.Tests.Command;

[TestFixture]
public class PushPkgCommandTestCase : BaseCommandTests<PushPkgOptions>
{

private readonly ICompileConfigurationCommand _compileConfigurationCommand = Substitute.For<ICompileConfigurationCommand>();
private readonly IPackageInstaller _packageInstaller = Substitute.For<IPackageInstaller>();
private readonly IMarketplace _marketplace = Substitute.For<IMarketplace>();

protected override void AdditionalRegistrations(ContainerBuilder containerBuilder) {
base.AdditionalRegistrations(containerBuilder);
containerBuilder.RegisterInstance(_compileConfigurationCommand);
containerBuilder.RegisterInstance(_packageInstaller);
containerBuilder.RegisterInstance(_marketplace);
}

[Test, Category("Unit")]
public void Execute_RunsForceCompilation() {
_compileConfigurationCommand.ClearReceivedCalls();
PushPackageCommand command = Container.Resolve<PushPackageCommand>();
PushPkgOptions options = new() {
ForceCompilation = true
};
_packageInstaller.Install(Arg.Any<string>(), Arg.Any<EnvironmentSettings>(),
Arg.Any<PackageInstallOptions>(), Arg.Any<string>())
.Returns(true);
_compileConfigurationCommand.Execute(Arg.Any<CompileConfigurationOptions>())
.Returns(0);
int result = command.Execute(options);
result.Should().Be(0);
_compileConfigurationCommand.Received(1).Execute(Arg.Any<CompileConfigurationOptions>());
}

[Test, Category("Unit")]
public void Execute_DoesNotRunningCompilation_WhenInstallFails() {
PushPackageCommand command = Container.Resolve<PushPackageCommand>();
PushPkgOptions options = new() {
ForceCompilation = true
};
_packageInstaller.Install(Arg.Any<string>(), Arg.Any<EnvironmentSettings>(),
Arg.Any<PackageInstallOptions>(), Arg.Any<string>())
.Returns(false);
int result = command.Execute(options);
result.Should().Be(1);
_compileConfigurationCommand.DidNotReceive().Execute(Arg.Any<CompileConfigurationOptions>());
}

[Test, Category("Unit")]
public void Execute_DoesNotRunningCompilation_WhenCompilationOptionsFalse() {
PushPackageCommand command = Container.Resolve<PushPackageCommand>();
PushPkgOptions options = new() {
ForceCompilation = false
};
_packageInstaller.Install(Arg.Any<string>(), Arg.Any<EnvironmentSettings>(),
Arg.Any<PackageInstallOptions>(), Arg.Any<string>())
.Returns(true);
int result = command.Execute(options);
result.Should().Be(0);
_compileConfigurationCommand.DidNotReceive().Execute(Arg.Any<CompileConfigurationOptions>());
}

[Test, Category("Unit")]
public void Execute_ReturnsFalse_WhenCompilationFails() {
_compileConfigurationCommand.ClearReceivedCalls();
PushPackageCommand command = Container.Resolve<PushPackageCommand>();
PushPkgOptions options = new() {
ForceCompilation = true
};
_packageInstaller.Install(Arg.Any<string>(), Arg.Any<EnvironmentSettings>(),
Arg.Any<PackageInstallOptions>(), Arg.Any<string>())
.Returns(true);
_compileConfigurationCommand.Execute(Arg.Any<CompileConfigurationOptions>())
.Returns(1);
int result = command.Execute(options);
result.Should().Be(1);
}

}
11 changes: 10 additions & 1 deletion clio/Command/CompileConfigurationCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,18 @@ public bool All {

#endregion

#region Interface: CompileConfigurationCommand

public interface ICompileConfigurationCommand {
int Execute(CompileConfigurationOptions options);

}

#endregion

#region Class: CompileConfigurationCommand

public class CompileConfigurationCommand : RemoteCommand<CompileConfigurationOptions> {
public class CompileConfigurationCommand : RemoteCommand<CompileConfigurationOptions>, ICompileConfigurationCommand {

#region Constants: Private

Expand Down
39 changes: 35 additions & 4 deletions clio/Command/PushPackageCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public class PushPkgOptions : InstallOptions
[Option("id", Required = false, HelpText = "Marketplace application id")]
public IEnumerable<int> MarketplaceIds { get; set; }

[Option("force-compilation", Required = false, HelpText = "Runs compilation after install package")]
public bool ForceCompilation { get; set; }

#endregion

}
Expand All @@ -54,20 +57,23 @@ public class PushPackageCommand : Command<PushPkgOptions>
private readonly EnvironmentSettings _environmentSettings;
private readonly IPackageInstaller _packageInstaller;
private readonly IMarketplace _marketplace;
private readonly ICompileConfigurationCommand _compileConfigurationCommand;
private readonly PackageInstallOptions _packageInstallOptionsDefault = new PackageInstallOptions();
#endregion

#region Constructors: Public

public PushPackageCommand() { } // for tests

public PushPackageCommand(EnvironmentSettings environmentSettings, IPackageInstaller packageInstaller, IMarketplace marketplace)
{
public PushPackageCommand(EnvironmentSettings environmentSettings, IPackageInstaller packageInstaller,
IMarketplace marketplace, ICompileConfigurationCommand compileConfigurationCommand) {
environmentSettings.CheckArgumentNull(nameof(environmentSettings));
packageInstaller.CheckArgumentNull(nameof(packageInstaller));
compileConfigurationCommand.CheckArgumentNull(nameof(compileConfigurationCommand));
_environmentSettings = environmentSettings;
_packageInstaller = packageInstaller;
_marketplace = marketplace;
_compileConfigurationCommand = compileConfigurationCommand;
}

#endregion
Expand All @@ -92,6 +98,15 @@ private PackageInstallOptions ExtractPackageInstallOptions(PushPkgOptions option

#region Methods: Public

/// <summary>
/// Executes the push package command with the specified options.
/// </summary>
/// <param name="options">The options for the push package command.</param>
/// <returns>Returns 0 if the command executed successfully, otherwise returns 1.</returns>
/// <remarks>
/// This method installs a package on a web application. If `MarketplaceIds` are provided, it installs the package
/// for each ID. If `ForceCompilation` is true and the installation is successful, it compiles the configuration.
/// </remarks>
public override int Execute(PushPkgOptions options)
{
PackageInstallOptions packageInstallOptions = ExtractPackageInstallOptions(options);
Expand Down Expand Up @@ -119,6 +134,10 @@ public override int Execute(PushPkgOptions options)
success = _packageInstaller.Install(options.Name, _environmentSettings,
packageInstallOptions, options.ReportPath);
}
if (options.ForceCompilation && success) {
CompileConfigurationOptions compileOptions = CreateFromPushPkgOptions(options);
success &= _compileConfigurationCommand.Execute(compileOptions) == 0;
}
Console.WriteLine(success ? "Done" : "Error");
return success ? 0 : 1;
}
Expand All @@ -128,6 +147,17 @@ public override int Execute(PushPkgOptions options)
return 1;
}
}

private CompileConfigurationOptions CreateFromPushPkgOptions(EnvironmentOptions options) {
return new CompileConfigurationOptions {
Environment = options.Environment,
Login = options.Login,
Password = options.Password,
Uri = options.Uri,
All = true,
};
}

#endregion
}

Expand All @@ -142,8 +172,9 @@ public class InstallGatePkgCommand : PushPackageCommand

#region Constructors: Public
public InstallGatePkgCommand(EnvironmentSettings environmentSettings, IPackageInstaller packageInstaller,
IMarketplace marketplace, IApplication applicatom, ILogger logger)
: base(environmentSettings, packageInstaller, marketplace) {
IMarketplace marketplace, ICompileConfigurationCommand compileConfigurationCommand, IApplication applicatom,
ILogger logger)
: base(environmentSettings, packageInstaller, marketplace, compileConfigurationCommand) {
_application = applicatom;
_logger = logger;
}
Expand Down

0 comments on commit 4441931

Please sign in to comment.