Files
sqltoolsservice/test/Microsoft.SqlTools.ServiceLayer.TestDriver/Driver/ServiceTestDriver.cs
Aditya Bist eb4f2f2b91 port batch parser wrapper (#232)
* Initial commit for GitHub IO pages

* Add initial doxfx content

* Update manifest.json

* Update manifest.json

* Set theme jekyll-theme-cayman

* Set theme jekyll-theme-slate

* Set theme jekyll-theme-minimal

* Set theme jekyll-theme-tactile

* Clear out theme setting

* Remove API docs

* Revert "Adding Milliseconds to DateTime fields (#173)" (#197)

This reverts commit 431dfa4156.

* ported new BatchParser

* added BatchParser tests

* fixing merge conflicts

* fix build issues

* cleaned code and addressed comments from code review

* addressed code review and made BatchParser logic more efficient

* fixed batch parser tests

* changed class name to fix build issues

* fixed merge conflicts

* added path for lab mode baseline tests

* changed env path for lab mode

* added env variable to appveyor

* testing env variable for appveyor

* fixed lab build

* debug appveyor build

* testing changes for appveyor

* changed trace env path

* debugging appveyor build

* changed baseline env path

* debugging

* debugging

* debugging

* switched on trace flag

* debugging

* debugging

* changed build config

* changed baseline files

* checking baseline output

* changed baseline files

* debug baseline tests

* debugging baseline

* debugging

* debugging

* debug

* debugging

* testing baseline format

* debug

* debug

* debug

* debug

* debug

* newline debug

* changed baseline file

* debug

* test

* try new way to read

* added execution engine tests

* change test

* testing file encoding

* moved execution engine tests to integration

* try compare without spaces

* removed no op test

* added env variable for travis

* put batch parser tests to integration too

* put batch parser in integration

* try new baseline string match

* compare baseline test logic changed

* changed baseline logic as well as cleaned tests

* fix build for travis CI

* fix travis CI issues

* fixed highlighting bugs on vscode

* code review changes

* ported new BatchParser

* added BatchParser tests

* Initial commit for GitHub IO pages

* Add initial doxfx content

* Update manifest.json

* Update manifest.json

* Set theme jekyll-theme-cayman

* Set theme jekyll-theme-slate

* Set theme jekyll-theme-minimal

* Set theme jekyll-theme-tactile

* Clear out theme setting

* Remove API docs

* Revert "Adding Milliseconds to DateTime fields (#173)" (#197)

This reverts commit 431dfa4156.

* fixing merge conflicts

* fix build issues

* cleaned code and addressed comments from code review

* addressed code review and made BatchParser logic more efficient

* fixed batch parser tests

* changed class name to fix build issues

* fixed merge conflicts

* added path for lab mode baseline tests

changed env path for lab mode

added env variable to appveyor

testing env variable for appveyor

fixed lab build

debug appveyor build

testing changes for appveyor

changed trace env path

debugging appveyor build

changed baseline env path

debugging

debugging

debugging

switched on trace flag

debugging

debugging

changed build config

changed baseline files

checking baseline output

changed baseline files

debug baseline tests

debugging baseline

debugging

debugging

debug

debugging

testing baseline format

debug

debug

debug

debug

debug

newline debug

changed baseline file

debug

test

try new way to read

added execution engine tests

change test

testing file encoding

moved execution engine tests to integration

try compare without spaces

removed no op test

added env variable for travis

* put batch parser tests to integration too

* put batch parser in integration

try new baseline string match

* compare baseline test logic changed

* changed baseline logic as well as cleaned tests

* fix build for travis CI

* fix travis CI issues

* fixed highlighting bugs on vscode

* code review changes

* fixed filestream writer test

* added localization string

* added localization string

* generated new string files again

* code review changes
2017-02-10 16:51:26 -08:00

168 lines
6.8 KiB
C#

//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
//
// The following is based upon code from PowerShell Editor Services
// License: https://github.com/PowerShell/PowerShellEditorServices/blob/develop/LICENSE
//
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Channel;
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts.ExecuteRequests;
namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Driver
{
/// <summary>
/// Test driver for the service host
/// </summary>
public class ServiceTestDriver : TestDriverBase
{
public const string ServiceCodeCoverageEnvironmentVariable = "SERVICECODECOVERAGE";
public const string CodeCoverageToolEnvironmentVariable = "CODECOVERAGETOOL";
public const string CodeCoverageOutputEnvironmentVariable = "CODECOVERAGEOUTPUT";
public const string ServiceHostEnvironmentVariable = "SQLTOOLSSERVICE_EXE";
public bool IsCoverageRun { get; set; }
private Process[] serviceProcesses;
private DateTime startTime;
public ServiceTestDriver()
{
string serviceHostExecutable = Environment.GetEnvironmentVariable(ServiceHostEnvironmentVariable);
string serviceHostArguments = "--enable-logging";
if (string.IsNullOrWhiteSpace(serviceHostExecutable))
{
// Include a fallback value to for running tests within visual studio
serviceHostExecutable =
@"..\..\src\Microsoft.SqlTools.ServiceLayer\bin\Debug\netcoreapp1.0\win7-x64\Microsoft.SqlTools.ServiceLayer.exe";
}
// Make sure it exists before continuing
if (!File.Exists(serviceHostExecutable))
{
throw new FileNotFoundException($"Failed to find Microsoft.SqlTools.ServiceLayer.exe at provided location '{serviceHostExecutable}'. " +
"Please set SQLTOOLSSERVICE_EXE environment variable to location of exe");
}
//setup the service host for code coverage if the envvar is enabled
if (Environment.GetEnvironmentVariable(ServiceCodeCoverageEnvironmentVariable) == "True")
{
string coverageToolPath = Environment.GetEnvironmentVariable(CodeCoverageToolEnvironmentVariable);
if (!string.IsNullOrWhiteSpace(coverageToolPath))
{
string serviceHostDirectory = Path.GetDirectoryName(serviceHostExecutable);
if (string.IsNullOrWhiteSpace(serviceHostDirectory))
{
serviceHostDirectory = ".";
}
string coverageOutput = Environment.GetEnvironmentVariable(CodeCoverageOutputEnvironmentVariable);
if (string.IsNullOrWhiteSpace(coverageOutput))
{
coverageOutput = "coverage.xml";
}
serviceHostArguments = $"-mergeoutput -target:{serviceHostExecutable} -targetargs:{serviceHostArguments} " +
$"-register:user -oldstyle -filter:\"+[Microsoft.SqlTools.*]* -[xunit*]*\" -output:{coverageOutput} " +
$"-searchdirs:{serviceHostDirectory};";
serviceHostExecutable = coverageToolPath;
this.IsCoverageRun = true;
}
}
this.clientChannel = new StdioClientChannel(serviceHostExecutable, serviceHostArguments);
this.protocolClient = new ProtocolEndpoint(clientChannel, MessageProtocolType.LanguageServer);
}
/// <summary>
/// Start the test driver, and launch the sqltoolsservice executable
/// </summary>
public async Task Start()
{
// Store the time we started
startTime = DateTime.Now;
// Launch the process
await this.protocolClient.Start();
await Task.Delay(1000); // Wait for the service host to start
// If this is a code coverage run, we need access to the service layer separate from open cover
if (IsCoverageRun)
{
CancellationTokenSource cancelSource = new CancellationTokenSource();
Task getServiceProcess = GetServiceProcess(cancelSource.Token);
Task timeoutTask = Task.Delay(TimeSpan.FromSeconds(15), cancelSource.Token);
if (await Task.WhenAny(getServiceProcess, timeoutTask) == timeoutTask)
{
cancelSource.Cancel();
throw new Exception("Failed to capture service process");
}
}
Console.WriteLine("Successfully launched service");
// Setup events to queue for testing
this.QueueEventsForType(ConnectionCompleteNotification.Type);
this.QueueEventsForType(IntelliSenseReadyNotification.Type);
this.QueueEventsForType(QueryCompleteEvent.Type);
this.QueueEventsForType(PublishDiagnosticsNotification.Type);
}
/// <summary>
/// Stop the test driver, and shutdown the sqltoolsservice executable
/// </summary>
public async Task Stop()
{
if (IsCoverageRun)
{
// Kill all the processes in the list
foreach (Process p in serviceProcesses.Where(p => !p.HasExited))
{
p.Kill();
}
ServiceProcess?.WaitForExit();
}
else
{
await this.protocolClient.Stop();
}
}
private async Task GetServiceProcess(CancellationToken token)
{
while (serviceProcesses == null && !token.IsCancellationRequested)
{
var processes = Process.GetProcessesByName("Microsoft.SqlTools.ServiceLayer")
.Where(p => p.StartTime >= startTime).ToArray();
// Wait a second if we can't find the process
if (processes.Any())
{
serviceProcesses = processes;
}
else
{
await Task.Delay(TimeSpan.FromSeconds(1), token);
}
}
}
}
}