diff --git a/clio.tests/Command/PushPkgCommand.Tests.cs b/clio.tests/Command/PushPkgCommand.Tests.cs new file mode 100644 index 00000000..f201af85 --- /dev/null +++ b/clio.tests/Command/PushPkgCommand.Tests.cs @@ -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 +{ + + private readonly ICompileConfigurationCommand _compileConfigurationCommand = Substitute.For(); + private readonly IPackageInstaller _packageInstaller = Substitute.For(); + private readonly IMarketplace _marketplace = Substitute.For(); + + 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(); + PushPkgOptions options = new() { + ForceCompilation = true + }; + _packageInstaller.Install(Arg.Any(), Arg.Any(), + Arg.Any(), Arg.Any()) + .Returns(true); + _compileConfigurationCommand.Execute(Arg.Any()) + .Returns(0); + int result = command.Execute(options); + result.Should().Be(0); + _compileConfigurationCommand.Received(1).Execute(Arg.Any()); + } + + [Test, Category("Unit")] + public void Execute_DoesNotRunningCompilation_WhenInstallFails() { + PushPackageCommand command = Container.Resolve(); + PushPkgOptions options = new() { + ForceCompilation = true + }; + _packageInstaller.Install(Arg.Any(), Arg.Any(), + Arg.Any(), Arg.Any()) + .Returns(false); + int result = command.Execute(options); + result.Should().Be(1); + _compileConfigurationCommand.DidNotReceive().Execute(Arg.Any()); + } + + [Test, Category("Unit")] + public void Execute_DoesNotRunningCompilation_WhenCompilationOptionsFalse() { + PushPackageCommand command = Container.Resolve(); + PushPkgOptions options = new() { + ForceCompilation = false + }; + _packageInstaller.Install(Arg.Any(), Arg.Any(), + Arg.Any(), Arg.Any()) + .Returns(true); + int result = command.Execute(options); + result.Should().Be(0); + _compileConfigurationCommand.DidNotReceive().Execute(Arg.Any()); + } + + [Test, Category("Unit")] + public void Execute_ReturnsFalse_WhenCompilationFails() { + _compileConfigurationCommand.ClearReceivedCalls(); + PushPackageCommand command = Container.Resolve(); + PushPkgOptions options = new() { + ForceCompilation = true + }; + _packageInstaller.Install(Arg.Any(), Arg.Any(), + Arg.Any(), Arg.Any()) + .Returns(true); + _compileConfigurationCommand.Execute(Arg.Any()) + .Returns(1); + int result = command.Execute(options); + result.Should().Be(1); + } + +} \ No newline at end of file diff --git a/clio/Command/CompileConfigurationCommand.cs b/clio/Command/CompileConfigurationCommand.cs index 2d2a25dc..71a61447 100644 --- a/clio/Command/CompileConfigurationCommand.cs +++ b/clio/Command/CompileConfigurationCommand.cs @@ -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 { + public class CompileConfigurationCommand : RemoteCommand, ICompileConfigurationCommand { #region Constants: Private diff --git a/clio/Command/PushPackageCommand.cs b/clio/Command/PushPackageCommand.cs index 4ecded4e..f5484e51 100644 --- a/clio/Command/PushPackageCommand.cs +++ b/clio/Command/PushPackageCommand.cs @@ -40,6 +40,9 @@ public class PushPkgOptions : InstallOptions [Option("id", Required = false, HelpText = "Marketplace application id")] public IEnumerable MarketplaceIds { get; set; } + [Option("force-compilation", Required = false, HelpText = "Runs compilation after install package")] + public bool ForceCompilation { get; set; } + #endregion } @@ -54,6 +57,7 @@ public class PushPackageCommand : Command private readonly EnvironmentSettings _environmentSettings; private readonly IPackageInstaller _packageInstaller; private readonly IMarketplace _marketplace; + private readonly ICompileConfigurationCommand _compileConfigurationCommand; private readonly PackageInstallOptions _packageInstallOptionsDefault = new PackageInstallOptions(); #endregion @@ -61,13 +65,15 @@ public class PushPackageCommand : Command 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 @@ -92,6 +98,15 @@ private PackageInstallOptions ExtractPackageInstallOptions(PushPkgOptions option #region Methods: Public + /// + /// Executes the push package command with the specified options. + /// + /// The options for the push package command. + /// Returns 0 if the command executed successfully, otherwise returns 1. + /// + /// 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. + /// public override int Execute(PushPkgOptions options) { PackageInstallOptions packageInstallOptions = ExtractPackageInstallOptions(options); @@ -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; } @@ -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 } @@ -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; }