public async Task SetsLogLevel(string logLevel, int expected) { // Given string[] args = new[] { "build", "-l", logLevel }; IBootstrapper bootstrapper = App.Bootstrapper.Create(args); bootstrapper.AddCommand <BuildCommand <EngineCommandSettings> >("build"); TestLoggerProvider provider = new TestLoggerProvider { ThrowLogLevel = LogLevel.None }; bootstrapper.ConfigureServices(services => services.AddSingleton <ILoggerProvider>(provider)); bootstrapper.AddPipeline( "Foo", new Core.LogMessage(LogLevel.Trace, "A"), new Core.LogMessage(LogLevel.Debug, "B"), new Core.LogMessage(LogLevel.Information, "C"), new Core.LogMessage(LogLevel.Warning, "D"), new Core.LogMessage(LogLevel.Error, "E"), new Core.LogMessage(LogLevel.Critical, "F")); // When int exitCode = await bootstrapper.RunAsync(); // Then exitCode.ShouldBe((int)ExitCode.Normal); provider.Messages.Count(x => x.FormattedMessage.StartsWith("Foo/Process")).ShouldBe(expected); }
public static IApplicationHost Build(this IApplicationHostBuilder hostBuilder) { //string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); //if (String.IsNullOrWhiteSpace(environment)) // throw new ArgumentNullException("Environment not found in ASPNETCORE_ENVIRONMENT"); var builder = new ConfigurationBuilder() .SetBasePath(Path.Combine(Directory.GetCurrentDirectory())) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) //.AddJsonFile($"appsettings.Development.json", optional: true, reloadOnChange: true) ; IConfiguration configuration = builder.Build(); hostBuilder.ApplicationFramework.Services.AddTransient((sp) => configuration); IBootstrapper bootstrapper = (IBootstrapper)Activator.CreateInstance(hostBuilder.ApplicationFramework.BootstrapperType, new[] { configuration }); bootstrapper.ConfigureServices(hostBuilder.ApplicationFramework.Services); hostBuilder.ApplicationFramework.BuildServiceProvider(); ILoggingBuilder loggingBuilder = hostBuilder.ApplicationFramework.ServiceProvider.GetService <ILoggingBuilder>(); bootstrapper.Configure(hostBuilder, loggingBuilder); return(new DefaultApplicationHost(hostBuilder.ApplicationFramework)); }
/// <summary> /// Runs tests on the bootstrapper with the specified file system and reports the results. /// </summary> /// <param name="bootstrapper">The bootstrapper.</param> /// <param name="throwLogLevel">The log level at which to throw.</param> /// <param name="fileProvider">The file provider to use.</param> /// <returns>Results from running the bootstrapper such as phase outputs and exit code.</returns> public static async Task <BootstrapperTestResult> RunTestAsync(this IBootstrapper bootstrapper, LogLevel throwLogLevel, IFileProvider fileProvider = null) { BootstrapperTestResult results = new BootstrapperTestResult(); // Prevent disposal by the console log provider and instrument with a test logger TestLoggerProvider loggerProvider = new TestLoggerProvider { ThrowLogLevel = throwLogLevel }; bootstrapper.ConfigureServices(services => { services.RemoveAll <ILoggerProvider>(); // The console logger isn't friendly to tests services.AddSingleton <ILoggerProvider>(loggerProvider); }); // Instrument the Bootstrapper GatherDocuments phaseInputs = new GatherDocuments(); GatherDocuments phaseOutputs = new GatherDocuments(); bootstrapper.ConfigureEngine(engine => { results.Engine = engine; // Instrument every pipeline to track inputs and outputs foreach (IPipeline pipeline in engine.Pipelines.Values) { pipeline.InputModules?.Insert(0, phaseInputs); pipeline.InputModules?.Add(phaseOutputs); pipeline.ProcessModules?.Insert(0, phaseInputs); pipeline.ProcessModules?.Add(phaseOutputs); pipeline.PostProcessModules?.Insert(0, phaseInputs); pipeline.PostProcessModules?.Add(phaseOutputs); pipeline.OutputModules?.Insert(0, phaseInputs); pipeline.OutputModules?.Add(phaseOutputs); } // Add the file provider if (fileProvider != null) { engine.FileSystem.RootPath = "/"; engine.FileSystem.FileProvider = fileProvider; } }); // Return the results results.LogMessages = loggerProvider.Messages; results.ExitCode = await bootstrapper.RunAsync(); results.Inputs = phaseInputs.Documents; results.Outputs = phaseOutputs.Documents; return(results); }
public async Task LogsVersion() { // Given string[] args = new[] { "build" }; TestLoggerProvider provider = new TestLoggerProvider(); IBootstrapper bootstrapper = App.Bootstrapper.Create(args); bootstrapper.AddCommand <BuildCommand <EngineCommandSettings> >("build"); bootstrapper.ConfigureServices(services => services.AddSingleton <ILoggerProvider>(provider)); bootstrapper.AddPipeline("Foo"); // When int exitCode = await bootstrapper.RunAsync(); // Then exitCode.ShouldBe((int)ExitCode.Normal); provider.Messages.ShouldContain(x => x.FormattedMessage.StartsWith("Statiq version")); }
public async Task CommandLineSettingTakesPrecedenceOverDefaultSettings() { // Given string[] args = new string[] { "-s", $"{Keys.LinkHideIndexPages}=false" }; IBootstrapper bootstrapper = App.Bootstrapper.CreateDefault(args); TestLoggerProvider provider = new TestLoggerProvider(); bootstrapper.ConfigureServices(services => services.AddSingleton <ILoggerProvider>(provider)); object variable = null; bootstrapper.AddPipeline("Foo", new ExecuteConfig(Config.FromContext(x => variable = x.Settings[Keys.LinkHideIndexPages]))); // When int exitCode = await bootstrapper.RunAsync(); // Then exitCode.ShouldBe((int)ExitCode.Normal); variable.ShouldBe("false"); }
public async Task CatalogsType() { // Given string[] args = new[] { "build", "-l", "Debug" }; TestLoggerProvider provider = new TestLoggerProvider(); IBootstrapper bootstrapper = App.Bootstrapper.Create(args); bootstrapper.AddCommand <BuildCommand <EngineCommandSettings> >("build"); bootstrapper.ConfigureServices(services => services.AddSingleton <ILoggerProvider>(provider)); bootstrapper.AddPipeline("Foo"); // When int exitCode = await bootstrapper.RunAsync(); // Then exitCode.ShouldBe((int)ExitCode.Normal); bootstrapper.ClassCatalog.GetTypesAssignableTo <BootstrapperFixture>().Count().ShouldBe(1); provider.Messages.ShouldContain(x => x.FormattedMessage.StartsWith("Cataloging types in assembly")); }
public async Task CommandLineSettingTakesPrecedenceOverEnvironmentVariables() { // Given string[] args = new string[] { "-s", $"{nameof(CommandLineSettingTakesPrecedenceOverEnvironmentVariables)}=Bar" }; Environment.SetEnvironmentVariable(nameof(CommandLineSettingTakesPrecedenceOverEnvironmentVariables), "Foo"); IBootstrapper bootstrapper = App.Bootstrapper.CreateDefault(args); TestLoggerProvider provider = new TestLoggerProvider(); bootstrapper.ConfigureServices(services => services.AddSingleton <ILoggerProvider>(provider)); object variable = null; bootstrapper.AddPipeline("Foo", new ExecuteConfig(Config.FromContext(x => variable = x.Settings[nameof(CommandLineSettingTakesPrecedenceOverEnvironmentVariables)]))); // When int exitCode = await bootstrapper.RunAsync(); // Then exitCode.ShouldBe((int)ExitCode.Normal); variable.ShouldBe("Bar"); }
public async Task CanReadConfigurationValues() { // Given string[] args = new string[] { }; Environment.SetEnvironmentVariable(nameof(CanReadConfigurationValues), "Foo"); IBootstrapper bootstrapper = App.Bootstrapper.CreateDefault(args); TestLoggerProvider provider = new TestLoggerProvider(); bootstrapper.ConfigureServices(services => services.AddSingleton <ILoggerProvider>(provider)); string variable = null; bootstrapper.ConfigureSettings(x => variable = x[nameof(CanReadConfigurationValues)]); bootstrapper.AddPipeline("Foo"); // When int exitCode = await bootstrapper.RunAsync(); // Then exitCode.ShouldBe((int)ExitCode.Normal); variable.ShouldBe("Foo"); }
public async Task NoPipelinesWarning() { // Given string[] args = new[] { "build" }; TestLoggerProvider provider = new TestLoggerProvider { ThrowLogLevel = LogLevel.None }; IBootstrapper bootstrapper = App.Bootstrapper.Create(args); bootstrapper.AddCommand <BuildCommand <EngineCommandSettings> >("build"); bootstrapper.ConfigureServices(services => services.AddSingleton <ILoggerProvider>(provider)); // When int exitCode = await bootstrapper.RunAsync(); // Then exitCode.ShouldBe((int)ExitCode.Normal); provider.Messages.ShouldContain(x => x.LogLevel == LogLevel.Warning && x.FormattedMessage == "No pipelines are configured or specified for execution."); }
public void Configure(IBootstrapper bootstrapper) => bootstrapper.ConfigureServices(x => x.AddRazor(null));