Files
sqltoolsservice/test/Microsoft.SqlTools.ServiceLayer.Test/Formatter/FormatterUnitTestBase.cs
Kevin Cunnane 7477642854 TSQL Formatter Service (#229)
- TSqlFormatterService with support for formatting document and text range inside document
- Settings support for all formatting options.
- Extensibility support so that the service can be initialized using MEF extensibility, and can find all necessary TSqlFormatters using the same process

Fix Initialize request error on startup
- Messages were being read from the input channel before all request handlers were registered
- In particular, the Initialize request which is key for any server to talk to the client was getting lost because the message reader thread begins consuming, and we take an extra few hundred milliseconds due to MEF startup before we register the handler
- The solution is to initialize the message handler so request handlers can register, but not actually start processing incoming messages until all handers are ready. This is a safer way to go and should improve reliability overall

Improvements from internal prototype:
- Normalizing baselines to handle the line ending differences on Mac & Linux vs. Windows
- Significantly shortened most lines by implementing base class methods to wrap common objects from Visitor.Context and removing unnecessary "this." syntax
- Refactored the SqlCommonTableExpressionFormatter and related classes to reduce code count significantly. This provides a pattern to follow when refactoring other classes for similar clarity. It's likely a lot of common logic could be found and reused across these.
- Reduced overall code size by adding utility methods
2017-02-14 23:40:17 -08:00

90 lines
3.4 KiB
C#

//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.IO;
using System.Reflection;
using Microsoft.SqlTools.ServiceLayer.Extensibility;
using Microsoft.SqlTools.ServiceLayer.Formatter;
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
using Microsoft.SqlTools.ServiceLayer.SqlContext;
using Microsoft.SqlTools.ServiceLayer.Test.Common;
using Microsoft.SqlTools.ServiceLayer.Workspace;
using Moq;
namespace Microsoft.SqlTools.ServiceLayer.Test.Formatter
{
public class FormatterUnitTestsBase
{
public FormatterUnitTestsBase()
{
HostMock = new Mock<IProtocolEndpoint>();
WorkspaceServiceMock = new Mock<WorkspaceService<SqlToolsSettings>>();
ServiceProvider = ExtensionServiceProvider.CreateDefaultServiceProvider();
ServiceProvider.RegisterSingleService(WorkspaceServiceMock.Object);
HostLoader.InitializeHostedServices(ServiceProvider, HostMock.Object);
FormatterService = ServiceProvider.GetService<TSqlFormatterService>();
}
protected ExtensionServiceProvider ServiceProvider { get; private set; }
protected Mock<IProtocolEndpoint> HostMock { get; private set; }
protected Mock<WorkspaceService<SqlToolsSettings>> WorkspaceServiceMock { get; private set; }
protected TSqlFormatterService FormatterService { get; private set; }
protected void LoadAndFormatAndCompare(string testName, FileInfo inputFile, FileInfo baselineFile, FormatOptions options, bool verifyFormat)
{
string inputSql = TestUtilities.ReadTextAndNormalizeLineEndings(inputFile.FullName);
string formattedSql = string.Empty;
formattedSql = FormatterService.Format(inputSql, options, verifyFormat);
formattedSql = TestUtilities.NormalizeLineEndings(formattedSql);
string assemblyPath = GetType().GetTypeInfo().Assembly.Location;
string directory = Path.Combine(Path.GetDirectoryName(assemblyPath), "FormatterTests");
Directory.CreateDirectory(directory);
FileInfo outputFile = new FileInfo(Path.Combine(directory, testName + ".out"));
File.WriteAllText(outputFile.FullName, formattedSql);
TestUtilities.CompareTestFiles(baselineFile, outputFile);
}
public FileInfo GetInputFile(string fileName)
{
return new FileInfo(Path.Combine(InputFileDirectory.FullName, fileName));
}
public FileInfo GetBaselineFile(string fileName)
{
return new FileInfo(Path.Combine(BaselineDirectory.FullName, fileName));
}
public DirectoryInfo BaselineDirectory
{
get
{
string d = Path.Combine(TestLocationDirectory, "BaselineFiles");
return new DirectoryInfo(d);
}
}
public DirectoryInfo InputFileDirectory
{
get
{
string d = Path.Combine(TestLocationDirectory, "TestFiles");
return new DirectoryInfo(d);
}
}
private static string TestLocationDirectory
{
get
{
return Path.Combine(RunEnvironmentInfo.GetTestDataLocation(), "TSqlFormatter");
}
}
}
}