Exemple #1
0
        /// <summary>
        /// Create a new test-suite.
        /// </summary>
        /// <param name="testOutput">
        /// Output for the current test.
        /// </param>
        protected TestBase(ITestOutputHelper testOutput)
        {
            if (testOutput == null)
            {
                throw new ArgumentNullException(nameof(testOutput));
            }

            // We *must* have a synchronisation context for the test, or we'll see random deadlocks.
            SynchronizationContext.SetSynchronizationContext(
                new SynchronizationContext()
                );

            TestOutput = testOutput;

            // Redirect component logging to Serilog.
            LoggerFactory = new LoggerFactory();
            Disposal.Add(LoggerFactory);

            // LoggerFactory.AddDebug(LogLevel);
            // ReSharper disable once VirtualMemberCallInConstructor
            LoggerFactory.AddTestOutput(TestOutput, LogLevel);

            // Ugly hack to get access to the current test.
            CurrentTest = (ITest)
                          TestOutput.GetType()
                          .GetField("test", BindingFlags.NonPublic | BindingFlags.Instance) !
                          .GetValue(TestOutput) !;

            Assert.True(CurrentTest != null, "Cannot retrieve current test from ITestOutputHelper.");

            Log = LoggerFactory.CreateLogger("CurrentTest");

            Disposal.Add(Log.BeginScope("TestDisplayName='{TestName}'", CurrentTest !.DisplayName));
        }
        /// <summary>
        ///     Create a new test-suite.
        /// </summary>
        /// <param name="testOutput">
        ///     Output for the current test.
        /// </param>
        protected TestBase(ITestOutputHelper testOutput)
        {
            if (testOutput == null)
            {
                throw new ArgumentNullException(nameof(testOutput));
            }

            // We *must* have a synchronisation context for the test, or we'll see random deadlocks.
            if (SynchronizationContext.Current == null)
            {
                SynchronizationContext.SetSynchronizationContext(
                    new SynchronizationContext()
                    );
            }

            Disposal.Add(TestCancellationSource);

            TestOutput    = testOutput;
            LoggerFactory = new LoggerFactory().AddTestOutput(TestOutput, MinLogLevel);
            Log           = LoggerFactory.CreateLogger("CurrentTest");

            // Ugly hack to get access to the current test.
            CurrentTest = (ITest)
                          TestOutput.GetType()
                          .GetField("test", BindingFlags.NonPublic | BindingFlags.Instance)
                          .GetValue(TestOutput);

            Assert.True(CurrentTest != null, "Cannot retrieve current test from ITestOutputHelper.");

            Disposal.Add(
                Log.BeginScope("CurrentTest", CurrentTest.DisplayName)
                );
        }
        /// <summary>
        /// Create a <see cref="LanguageClient" /> connected to the test's <see cref="PipeServerProcess" />.
        /// </summary>
        /// <returns>
        /// The <see cref="LanguageClient" />.
        /// </returns>
        protected async Task<(ILanguageClient client, ILanguageServer server)> Initialize(
            Action<LanguageClientOptions> clientOptionsAction,
            Action<LanguageServerOptions> serverOptionsAction
        )
        {
            var clientPipe = new Pipe();
            var serverPipe = new Pipe();
            _client = LanguageClient.PreInit(
                options => {
                    options.WithServices(services => services.AddSingleton(LoggerFactory));
                    options.WithInput(serverPipe.Reader).WithOutput(clientPipe.Writer);
                    options.WithRootPath(WorkspaceRoot);
                    clientOptionsAction(options);
                }
            );
            Disposal.Add(_client);

            _server = Server.LanguageServer.PreInit(
                options => {
                    options.WithServices(services => services.AddSingleton(LoggerFactory));
                    options.WithInput(clientPipe.Reader).WithOutput(serverPipe.Writer);
                    serverOptionsAction(options);
                }
            );
            Disposal.Add(_server);

            await Task.WhenAll(
                _client.Initialize(CancellationToken),
                _server.Initialize(CancellationToken)
            );

            return ( _client, _server );
        }
        /// <summary>
        ///     Create a <see cref="LspConnection"/> that uses the client ends of the the test's <see cref="PipeServerProcess"/> streams.
        /// </summary>
        /// <returns>
        ///     The <see cref="LspConnection"/>.
        /// </returns>
        protected async Task <LspConnection> CreateClientConnection()
        {
            if (!_serverProcess.IsRunning)
            {
                await StartServer();
            }

            await _serverProcess.HasStarted;

            LspConnection connection = new LspConnection(LoggerFactory, input: ServerOutputStream, output: ServerInputStream);

            Disposal.Add(connection);

            return(connection);
        }
            /// <summary>
            ///     Create a new <see cref="WebSocketTestBase"/>.
            /// </summary>
            /// <param name="testOutput">
            ///     Output for the current test.
            /// </param>
            protected WebSocketTestBase(ITestOutputHelper testOutput)
                : base(testOutput)
            {
                int port = Interlocked.Increment(ref NextPort);

                BaseAddress = new Uri($"http://localhost:{port}");
                WebSocketBaseAddress = new Uri($"ws://localhost:{port}");

                Host = WebHost.CreateDefaultBuilder()
                    .UseStartup<Startup>()
                    .ConfigureServices(ConfigureTestServerServices)
                    .ConfigureLogging(ConfigureTestServerLogging)
                    .UseUrls(BaseAddress.AbsoluteUri)
                    .Build();

                Disposal.Add(Host);
            }
        /// <summary>
        ///     Create a <see cref="LanguageClient"/> connected to the test's <see cref="PipeServerProcess"/>.
        /// </summary>
        /// <param name="initialize">
        ///     Automatically initialise the client?
        ///
        ///     Default is <c>true</c>.
        /// </param>
        /// <returns>
        ///     The <see cref="LanguageClient"/>.
        /// </returns>
        protected async Task <LanguageClient> CreateClient(bool initialize = true)
        {
            if (!_serverProcess.IsRunning)
            {
                await StartServer();
            }

            await _serverProcess.HasStarted;

            LanguageClient client = new LanguageClient(LoggerFactory, _serverProcess);

            Disposal.Add(client);

            if (initialize)
            {
                await client.Initialize(WorkspaceRoot);
            }

            return(client);
        }
        /// <summary>
        ///     Create a new test-suite.
        /// </summary>
        /// <param name="testOutput">
        ///     Output for the current test.
        /// </param>
        protected TestBase(ITestOutputHelper testOutput)
        {
            if (testOutput == null)
            {
                throw new ArgumentNullException(nameof(testOutput));
            }

            // We *must* have a synchronisation context for the test, or we'll see random deadlocks.
            SynchronizationContext.SetSynchronizationContext(
                new SynchronizationContext()
                );

            TestOutput = testOutput;

            // Redirect component logging to Serilog.
            Log =
                new LoggerConfiguration()
                .MinimumLevel.Verbose()
                .Enrich.FromLogContext()
                .WriteTo.Debug(
                    restrictedToMinimumLevel: LogEventLevel.Verbose
                    )
                .WriteTo.TestOutput(TestOutput, LogLevelSwitch)
                .CreateLogger();

            // Ugly hack to get access to the current test.
            CurrentTest = (ITest)
                          TestOutput.GetType()
                          .GetField("test", BindingFlags.NonPublic | BindingFlags.Instance)
                          .GetValue(TestOutput);

            Assert.True(CurrentTest != null, "Cannot retrieve current test from ITestOutputHelper.");

            Disposal.Add(
                LogContext.PushProperty("TestName", CurrentTest.DisplayName)
                );
        }
 /// <summary>
 ///     Create a new <see cref="PipeServerTestBase"/>.
 /// </summary>
 /// <param name="testOutput">
 ///     Output for the current test.
 /// </param>
 protected PipeServerTestBase(ITestOutputHelper testOutput)
     : base(testOutput)
 {
     _serverProcess = new NamedPipeServerProcess(Guid.NewGuid().ToString("N"), LoggerFactory);
     Disposal.Add(_serverProcess);
 }