Files
sqltoolsservice/test/Microsoft.SqlTools.ServiceLayer.Test.Common/TestUtilities.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

91 lines
3.7 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;
namespace Microsoft.SqlTools.ServiceLayer.Test.Common
{
public class TestUtilities
{
public static void CompareTestFiles(FileInfo baselinePath, FileInfo outputPath, int maxDiffLines = -1 /* unlimited */)
{
if (!baselinePath.Exists)
{
throw new ComparisonFailureException("echo Test Failed: Baseline file " + baselinePath.FullName + " does not exist" +
Environment.NewLine + Environment.NewLine + "echo test > \"" + baselinePath.FullName + "\"");
}
if (!outputPath.Exists)
{
throw new ComparisonFailureException("Test Failed: output file " + outputPath.FullName + " doesn't exist.");
}
string baseline = ReadTextAndNormalizeLineEndings(baselinePath.FullName);
string actual = ReadTextAndNormalizeLineEndings(outputPath.FullName);
if (baseline.CompareTo(actual) != 0)
{
string header = "Test Failed: Baseline file " + baselinePath.FullName + " differs from output file " + outputPath.FullName + "\r\n\r\n";
string editAndCopyMessage =
"\r\n\r\n copy \"" + outputPath.FullName + "\" \"" + baselinePath.FullName + "\"" +
"\r\n\r\n";
string diffCmdMessage =
"code --diff \"" + baselinePath.FullName + "\" \"" + outputPath.FullName + "\"" +
"\r\n\r\n";
string diffContents = FindFirstDifference(baseline, actual);
throw new ComparisonFailureException(header + diffCmdMessage + editAndCopyMessage + diffContents, editAndCopyMessage);
}
}
private static string FindFirstDifference(string baseline, string actual)
{
int index = 0;
int minEnd = Math.Min(baseline.Length, actual.Length);
while (index < minEnd && baseline[index] == actual[index])
index++;
int firstDiffIndex = (index == minEnd && baseline.Length == actual.Length) ? -1 : index;
int startRange = Math.Max(firstDiffIndex - 50, 0);
int endRange = Math.Min(firstDiffIndex + 50, minEnd);
string baselineDiff = ShowWhitespace(baseline.Substring(startRange, endRange));
string actualDiff = ShowWhitespace(actual.Substring(startRange, endRange));
return "\r\nFirst Diff:\r\n===== Baseline =====\r\n"
+ baselineDiff
+ "\r\n===== Actual =====\r\n"
+ actualDiff;
}
private static string ShowWhitespace(string input)
{
return input.Replace("\r", "\\r").Replace("\n", "\\n");
}
/// <summary>
/// Normalizes line endings in a file to facilitate comparisons regardless of OS. On Windows line endings are \r\n, while
/// on other systems only \n is used
/// </summary>
public static string ReadTextAndNormalizeLineEndings(string filePath)
{
string text = File.ReadAllText(filePath);
return NormalizeLineEndings(text);
}
public static string NormalizeLineEndings(string text)
{
// To work on all platforms, we first stript \r and then replace any remaining \n characters
// with a newline string. This helps keep things consistent with file formats for each OS
string noCrs = text.Replace("\r", "");
return noCrs.Replace("\n", Environment.NewLine);
}
}
}