-
Notifications
You must be signed in to change notification settings - Fork 636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DYN-2680] Write logs to std out instead of creating new files for each test. #10651
Changes from 3 commits
6b4f125
e4e69bc
5d3f162
6a124a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
using Dynamo.Logging; | ||
using Dynamo.Updates; | ||
using Moq; | ||
using NUnit.Framework; | ||
using DynUpdateManager = Dynamo.Updates.UpdateManager; | ||
using System; | ||
using System.Collections.Generic; | ||
using Dynamo.Configuration; | ||
using Dynamo.Core; | ||
using System.Collections.Generic; | ||
using System; | ||
using Dynamo.Interfaces; | ||
using Dynamo.Logging; | ||
using NUnit.Framework; | ||
|
||
namespace Dynamo.Tests.Loggings | ||
{ | ||
|
@@ -17,6 +13,28 @@ class DynamoLoggerTest : DynamoModelTestBase | |
private const string DOWNLOAD_SOURCE_PATH_S = "http://downloadsourcepath/"; | ||
private const string SIGNATURE_SOURCE_PATH_S = "http://SignatureSourcePath/"; | ||
private PathManager pathManager; | ||
internal DynamoLogger logger; | ||
|
||
// Returns the dynamo logger object in non-test mode. | ||
internal DynamoLogger GetDynamoLoggerWithTestModeFalse() | ||
{ | ||
pathManager = new PathManager(new PathManagerParams{ }); | ||
|
||
// Ensure we have all directories in place. | ||
var exceptions = new List<Exception>(); | ||
pathManager.EnsureDirectoryExistence(exceptions); | ||
|
||
//By setting the VerboseLogging we will enable a piece of code inside the method:: | ||
//private void Log(string message, LogLevel level, bool reportModification) | ||
var debugSettings = new DebugSettings | ||
{ | ||
VerboseLogging = true | ||
}; | ||
|
||
var logger = new DynamoLogger(debugSettings, pathManager.LogDirectory, false); | ||
|
||
return logger; | ||
} | ||
|
||
/// <summary> | ||
/// This test method will check the LogEventArgs.LogEventArgs | ||
|
@@ -45,27 +63,9 @@ public void test_dynamologger_LogEventArgs() | |
[Category("UnitTests")] | ||
public void test_dynamologger_logger() | ||
{ | ||
//Arrange | ||
//This has been done in the base class but we need to do it again because the pathManager is private | ||
var config = CreateStartConfiguration(dynamoSettings); | ||
pathManager = new PathManager(new PathManagerParams | ||
{ | ||
CorePath = config.DynamoCorePath, | ||
HostPath = config.DynamoHostPath, | ||
PathResolver = config.PathResolver | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to confirm, these params were not needed, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, these were null values. |
||
|
||
// Ensure we have all directories in place. | ||
var exceptions = new List<Exception>(); | ||
pathManager.EnsureDirectoryExistence(exceptions); | ||
|
||
//By setting the VerboseLogging we will enable a piece of code inside the method:: | ||
//private void Log(string message, LogLevel level, bool reportModification) | ||
var debugSettings = new DebugSettings | ||
{ | ||
VerboseLogging = true | ||
}; | ||
var logger = new DynamoLogger(debugSettings, pathManager.LogDirectory); | ||
// Get the dynamo logger in non-test mode, as we write the logs | ||
// to dynamo console and to a file only in non-test mode. | ||
logger = GetDynamoLoggerWithTestModeFalse(); | ||
|
||
//Act | ||
logger.LogWarning("Testing Logging Warning", WarningLevel.Error); | ||
|
@@ -77,7 +77,7 @@ public void test_dynamologger_logger() | |
logger.LogError("Testing Logging Error","Error Details"); | ||
|
||
//Assert | ||
//Check if the logged info was inserted inside the logged text (the log file cannot be opened due that is being used by the base class) | ||
//Check if the logged info is stored in the LogText property (the log file cannot be opened due that is being used by the base class) | ||
Assert.IsTrue(logger.LogText.Contains("Testing Logging Warning")); | ||
|
||
logger.Dispose(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
using Dynamo.Migration; | ||
using NUnit.Framework; | ||
using System; | ||
using System.IO; | ||
using System.Xml; | ||
using Dynamo.Migration; | ||
using NUnit.Framework; | ||
|
||
namespace Dynamo.Tests.Migrations | ||
{ | ||
|
@@ -15,21 +16,31 @@ class WorkspaceMigrationsTests : DynamoModelTestBase | |
[Category("UnitTests")] | ||
public void TestWorkspaceMigrations() | ||
{ | ||
string consoleOutput; | ||
|
||
//Arrange | ||
string openPath = Path.Combine(TestDirectory, @"core\NodeStates.dyn"); | ||
RunModel(openPath); | ||
string logFileText = string.Empty; | ||
XmlDocument xmlDoc = new XmlDocument(); | ||
|
||
//Act | ||
//Create a instance of WorkspaceMigration and call the function that writes to the log | ||
var workMigration = new WorkspaceMigrations(CurrentDynamoModel); | ||
workMigration.Migrate_0_5_3_to_0_6_0(xmlDoc); | ||
var currentLoggerPath = CurrentDynamoModel.Logger.LogPath; | ||
using (StringWriter stringWriter = new StringWriter()) | ||
{ | ||
Console.SetOut(stringWriter); | ||
|
||
//Act | ||
//Create a instance of WorkspaceMigration and call the function that writes to the log | ||
var workMigration = new WorkspaceMigrations(CurrentDynamoModel); | ||
workMigration.Migrate_0_5_3_to_0_6_0(xmlDoc); | ||
|
||
consoleOutput = stringWriter.ToString(); | ||
|
||
StreamWriter sw = new StreamWriter(Console.OpenStandardOutput()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should you call dispose on StreamWriter? I believe it implements IDisposable! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, looked too quickly, didn't see this was one test, not the logger! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can avoid creating the StreamWriter object. Saving the std out beforehand and setting it back. |
||
Console.SetOut(sw); | ||
} | ||
|
||
//Assert | ||
Assert.IsTrue(File.Exists(currentLoggerPath));//Check that the log was created successfully | ||
Assert.IsTrue(CurrentDynamoModel.Logger.LogText.Contains("migration from 0.5.3.x to 0.6.1.x"));//Checks that the log text contains the migration info | ||
//Checks that the console output text contains the migration info | ||
Assert.IsTrue(consoleOutput.Contains("migration from 0.5.3.x to 0.6.1.x")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe for this test we actually want to use the non-test version of the logger? I'm thinking that having a persistent log after a migration is done is desired and should be tested to exist. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The thing is, the dynamo logger is created from the dynamo model when the dynamo model is initialized from the tests. It is a read only property and we cannot create an instance of logger in non test mode here, then add it to the model(only then this log message will be written to a file). |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just leverage on the existing code for the console logging by setting the
level
variable toLogLevel.Console
iftestMode
is true?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The LogLevel.Console is configured to write to both the dynamo console and to the log file. So couldn't use it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are indeed right. The fact that it does both things having that name is confusing :)