Files
sqltoolsservice/test/Microsoft.SqlTools.ServiceLayer.UnitTests/Utility/CommandOptionsTests.cs
ranasaria 09652cccd1 Enhanced Logging for sqltoolsservice (#695)
This change modifies the logging framework within sqltoolservice.
Moves away from custom Logger object to start using .Net tracing framework. It supports for the static Trace and TraceSource way of logging. For all new code it is recommend that we log the log messages using the existing static Logger class, while the code changes will continue to route the older Trace.Write* calls from the process to same log listeners (and thus the log targets) as used by the Logger class. Thus tracing in SMO code that uses Trace.Write* methods gets routed to the same file as the messages from rest of SQLTools Service code.
Make changes to start using .Net Frameworks codebase for all logging to unify our logging story.
Allows parameter to set tracingLevel filters that controls what kinds of message make it to the log file.
Allows a parameter to set a specific log file name so if these gets set by external code (the UI code using the tools service for example) then the external code is aware of the current log file in use.
Adding unittests to test out the existing and improved logging capabilities.


Sequences of checkins in development branch:
* Saving v1 of logging to prepare for code review. Minor cleanup and some end to end testing still remains
* Removing local launchSettings.json files
* added support for lazy listener to sqltoolsloglistener and removed incorrect changes to comments across files in previous checkin
* Converting time to local time when writing entries to the log
* move the hosting.v2 to new .net based logging code
* removing *.dgml files and addding them to .gitignore
* fixing typo of defaultTraceSource
* Addressing pull request feedback
* Adding a test to verify logging from SMO codebase
* propogating changes to v1 sqltools.hosting commandoptions.cs to the v2 version
* Fixing comments on start and stop callstack methods and whitespaces
* Commenting a test that got uncommented by mistake
* addding .gitattributes file as .sql file was observed to be misconstrued as a binary file
2018-09-24 23:55:59 -07:00

173 lines
6.9 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.IO;
using Microsoft.SqlTools.ServiceLayer.Utility;
using Xunit;
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
{
/// <summary>
/// Tests for the CommandOptions class
/// </summary>
public class CommandOptionsTests
{
[Fact]
public void UsageIsShownWhenHelpFlagProvided()
{
var args = new string[] {"--help"};
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
Assert.NotNull(options);
Assert.True(options.ShouldExit);
Assert.Equal(options.Locale, string.Empty);
}
[Fact]
public void UsageIsShownWhenBadArgumentsProvided()
{
var args = new string[] {"--unknown-argument", "/bad-argument"};
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
Assert.NotNull(options);
Assert.True(options.ShouldExit);
Assert.Equal(options.Locale, string.Empty);
}
[Fact]
public void DefaultValuesAreUsedWhenNoArgumentsAreProvided()
{
int? testNo = 1;
// Test 1: All defaults, no options specified
{
var args = new string[] { };
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
VerifyCommandOptions(options, testNo++);
}
// Test 2: All defaults, -logDir as null
{
var args = new string[] { "--log-dir", null };
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
VerifyCommandOptions(options, testNo++);
}
// Test 3: All defaults, -logDir as empty string
{
var args = new string[] { "--log-dir", string.Empty };
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
VerifyCommandOptions(options, testNo++);
}
// Test 4: All defaults, -log-file as null
{
var args = new string[] { "--log-file", null };
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
VerifyCommandOptions(options, testNo++, logFilePath: null);
}
// Test 5: All defaults, -log-file as empty string
{
var args = new string[] { "--log-file", string.Empty };
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
VerifyCommandOptions(options, testNo++, logFilePath: string.Empty);
}
}
private static void VerifyCommandOptions(ServiceLayerCommandOptions options, int? testNo = null, string errorMessage = "", string tracingLevel = null, string logFilePath = null, bool shouldExit = false, string locale = "", string logDirectory = null)
{
Assert.NotNull(options);
string MsgPrefix = testNo != null ? $"TestNo:{testNo} ::" : string.Empty;
Assert.True(errorMessage == options.ErrorMessage, $"{MsgPrefix} options:{nameof(errorMessage)} should be '{errorMessage}'");
Assert.True(tracingLevel == options.TracingLevel, $"{MsgPrefix} options:{nameof(tracingLevel)} should be '{tracingLevel}'");
Assert.True(logFilePath == options.LogFilePath, $"{MsgPrefix} options:{nameof(logFilePath)} should be '{logFilePath}'");
Assert.True(shouldExit == options.ShouldExit, $"{MsgPrefix} options:{nameof(shouldExit)} should be '{shouldExit}'");
Assert.False(string.IsNullOrWhiteSpace(options.LoggingDirectory));
if (string.IsNullOrWhiteSpace(logDirectory))
{
logDirectory = Path.Combine(options.DefaultLogRoot, options.ServiceName);
}
Assert.True(logDirectory == options.LoggingDirectory, $"{MsgPrefix} options:{nameof(logDirectory)} should be '{logDirectory}'");
Assert.True(options.Locale == locale, $"{MsgPrefix} options:{nameof(locale)} should be '{locale}'");
}
[Theory]
[InlineData("en")]
[InlineData("es")]
public void LocaleSetWhenProvided(string locale)
{
var args = new string[] {"--locale", locale};
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args); ;
// Asserting all options were properly set
Assert.NotNull(options);
Assert.False(options.ShouldExit);
Assert.Equal(options.Locale, locale);
}
[Fact]
public void ShouldExitNotSetWhenInvalidLocale()
{
string locale = "invalid";
var args = new string[] { "--locale", locale };
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
// Asserting all options were properly set
Assert.NotNull(options);
Assert.False(options.ShouldExit);
}
[Fact]
public void LocaleNotSetWhenNotProvided()
{
var args = new string[] {};
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
// Asserting all options were properly set
Assert.NotNull(options);
Assert.False(options.ShouldExit);
Assert.Equal(options.Locale, string.Empty);
}
[Fact]
public void LoggingDirectorySet()
{
string logDir = Directory.GetCurrentDirectory();
var args = new string[] { "--log-dir", logDir };
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
// Asserting all options were properly set
Assert.NotNull(options);
Assert.False(options.ShouldExit);
Assert.Equal(options.LoggingDirectory, logDir);
}
[Fact]
public void TracingLevelSet()
{
string expectedLevel = "Critical";
var args = new string[] { "--tracing-level", expectedLevel };
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
// Asserting all options were properly set
Assert.NotNull(options);
Assert.False(options.ShouldExit);
Assert.Equal(options.TracingLevel, expectedLevel);
}
[Fact]
public void LogFilePathSet()
{
string expectedFilePath = Path.GetRandomFileName();
var args = new string[] { "--log-file", expectedFilePath };
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
// Asserting all options were properly set
Assert.NotNull(options);
Assert.False(options.ShouldExit);
Assert.Equal(options.LogFilePath, expectedFilePath);
}
}
}