Skip to content

Log verbosity #1107

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

Merged
merged 8 commits into from
Nov 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/GitVersionCore.Tests/ConfigProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public void WarnOnExistingGitVersionConfigYamlFile(string path)

var logOutput = string.Empty;
Action<string> action = info => { logOutput = info; };
using (Logger.AddLoggersTemporarily(action, action, action))
using (Logger.AddLoggersTemporarily(action, action, action, action))
{
ConfigurationProvider.Verify(workingPath, repoPath, fileSystem);
}
Expand All @@ -256,9 +256,10 @@ public void WarnOnAmbiguousConfigFilesAtTheSameProjectRootDirectory(string path)

var logOutput = string.Empty;
Action<string> action = info => { logOutput = info; };
Logger.SetLoggers(action, action, action);

ConfigurationProvider.Verify(workingPath, repoPath, fileSystem);
using (Logger.AddLoggersTemporarily(action, action, action, action))
{
ConfigurationProvider.Verify(workingPath, repoPath, fileSystem);
}

var configFileDeprecatedWarning = string.Format("Ambiguous config files at '{0}'", path);
logOutput.Contains(configFileDeprecatedWarning).ShouldBe(true);
Expand Down Expand Up @@ -286,7 +287,7 @@ public void NoWarnOnGitVersionYmlFile()

var s = string.Empty;
Action<string> action = info => { s = info; };
using (Logger.AddLoggersTemporarily(action, action, action))
using (Logger.AddLoggersTemporarily(action, action, action, action))
{
ConfigurationProvider.Provide(repoPath, fileSystem);
}
Expand Down
35 changes: 28 additions & 7 deletions src/GitVersionCore.Tests/ExecuteCoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ public void CacheFileExistsOnDisk()

var versionAndBranchFinder = new ExecuteCore(fileSystem);

var info = RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
var logs = RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
{
fileSystem.WriteAllText(vv.FileName, versionCacheFileContent);
vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);
vv.AssemblySemVer.ShouldBe("4.10.3.0");
});

info.ShouldContain("Deserializing version variables from cache file", () => info);
logs.Info.ShouldContain("Deserializing version variables from cache file", () => logs.Info);
}


Expand Down Expand Up @@ -139,8 +139,8 @@ public void CacheFileExistsOnDiskWhenOverrideConfigIsSpecifiedVersionShouldBeDyn
[Test]
public void CacheFileIsMissing()
{
var info = RepositoryScope();
info.ShouldContain("yml not found", () => info);
var logsMessages = RepositoryScope();
logsMessages.Info.ShouldContain("yml not found", () => logsMessages.Info);
}


Expand Down Expand Up @@ -260,19 +260,34 @@ public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
});
}

string RepositoryScope(ExecuteCore executeCore = null, Action<EmptyRepositoryFixture, VersionVariables> fixtureAction = null)
LogMessages RepositoryScope(ExecuteCore executeCore = null, Action<EmptyRepositoryFixture, VersionVariables> fixtureAction = null)
{
// Make sure GitVersion doesn't trigger build server mode when we are running the tests
Environment.SetEnvironmentVariable(AppVeyor.EnvironmentVariableName, null);
Environment.SetEnvironmentVariable(TravisCI.EnvironmentVariableName, null);
var debugBuilder = new StringBuilder();
Action<string> debugLogger = s =>
{
debugBuilder.AppendLine(s);
};
var infoBuilder = new StringBuilder();
Action<string> infoLogger = s =>
{
infoBuilder.AppendLine(s);
};
var warnBuilder = new StringBuilder();
Action<string> warnLogger = s =>
{
warnBuilder.AppendLine(s);
};
var errorBuilder = new StringBuilder();
Action<string> errorLogger = s =>
{
errorBuilder.AppendLine(s);
};
executeCore = executeCore ?? new ExecuteCore(fileSystem);

using (Logger.AddLoggersTemporarily(infoLogger, s => {}, s => { }))
using (Logger.AddLoggersTemporarily(debugLogger, infoLogger, warnLogger, errorLogger))
using (var fixture = new EmptyRepositoryFixture())
{
fixture.Repository.MakeACommit();
Expand All @@ -287,6 +302,12 @@ string RepositoryScope(ExecuteCore executeCore = null, Action<EmptyRepositoryFix
}
}

return infoBuilder.ToString();
return new LogMessages
{
Debug = debugBuilder.ToString(),
Info = infoBuilder.ToString(),
Warn = warnBuilder.ToString(),
Error = errorBuilder.ToString()
};
}
}
1 change: 1 addition & 0 deletions src/GitVersionCore.Tests/GitVersionCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
<Compile Include="GitVersionContextTests.cs" />
<Compile Include="Helpers\DirectoryHelper.cs" />
<Compile Include="IntegrationTests\MainlineDevelopmentMode.cs" />
<Compile Include="LogMessages.cs" />
<Compile Include="Mocks\MockThreadSleep.cs" />
<Compile Include="OperationWithExponentialBackoffTests.cs" />
<Compile Include="Init\InitScenarios.cs" />
Expand Down
7 changes: 7 additions & 0 deletions src/GitVersionCore.Tests/LogMessages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
internal class LogMessages
{
public string Debug { get; internal set; }
public object Error { get; internal set; }
public string Info { get; internal set; }
public string Warn { get; internal set; }
}
4 changes: 2 additions & 2 deletions src/GitVersionCore.Tests/LoggerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void LoggerObscuresPassword(string protocol)
const string password = "password";
var s = string.Empty;
Action<string> action = info => { s = info; };
using (Logger.AddLoggersTemporarily(action, action, action))
using (Logger.AddLoggersTemporarily(action, action, action, action))
Logger.WriteInfo(string.Format("{0}://{1}:{2}@workspace.visualstudio.com/DefaultCollection/_git/CAS",protocol,username,password));

s.Contains(password).ShouldBe(false);
Expand All @@ -29,7 +29,7 @@ public void UsernameWithoutPassword()
var s = string.Empty;
Action<string> action = info => { s = info; };
const string repoUrl = "http://[email protected]/DefaultCollection/_git/CAS";
using (Logger.AddLoggersTemporarily(action, action, action))
using (Logger.AddLoggersTemporarily(action, action, action, action))
Logger.WriteInfo(repoUrl);

s.Contains(repoUrl).ShouldBe(true);
Expand Down
1 change: 1 addition & 0 deletions src/GitVersionCore.Tests/ModuleInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static class ModuleInitializer
public static void Initialize()
{
Logger.SetLoggers(
s => Console.WriteLine(s),
s => Console.WriteLine(s),
s => Console.WriteLine(s),
s => Console.WriteLine(s));
Expand Down
1 change: 1 addition & 0 deletions src/GitVersionCore/GitVersionCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
<Compile Include="OutputVariables\VersionVariables.cs" />
<Compile Include="SemanticVersionExtensions.cs" />
<Compile Include="SemanticVersionFormatValues.cs" />
<Compile Include="VerbosityLevel.cs" />
<Compile Include="VersionFilters\MinDateVersionFilter.cs" />
<Compile Include="VersionFilters\IVersionFilter.cs" />
<Compile Include="VersionFilters\ShaVersionFilter.cs" />
Expand Down
18 changes: 14 additions & 4 deletions src/GitVersionCore/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public static class Logger
static readonly Regex ObscurePasswordRegex = new Regex("(https?://)(.+)(:.+@)", RegexOptions.Compiled);
static string indent = string.Empty;


static Logger()
{
Reset();
}

public static Action<string> WriteDebug { get; private set; }
public static Action<string> WriteInfo { get; private set; }
public static Action<string> WriteWarning { get; private set; }
public static Action<string> WriteError { get; private set; }
Expand All @@ -41,23 +41,31 @@ static Action<string> ObscurePassword(Action<string> info)
return logAction;
}

public static void SetLoggers(Action<string> info, Action<string> warn, Action<string> error)
public static void SetLoggers(Action<string> debug, Action<string> info, Action<string> warn, Action<string> error)
{
if (debug == null) throw new ArgumentNullException("debug");
if (info == null) throw new ArgumentNullException("info");
if (warn == null) throw new ArgumentNullException("warn");
if (error == null) throw new ArgumentNullException("error");

WriteDebug = LogMessage(ObscurePassword(debug), "DEBUG");
WriteInfo = LogMessage(ObscurePassword(info), "INFO");
WriteWarning = LogMessage(ObscurePassword(warn), "WARN");
WriteError = LogMessage(ObscurePassword(error), "ERROR");
}

public static IDisposable AddLoggersTemporarily(Action<string> info, Action<string> warn, Action<string> error)
public static IDisposable AddLoggersTemporarily(Action<string> debug, Action<string> info, Action<string> warn, Action<string> error)
{
var currentDebug = WriteDebug;
var currentInfo = WriteInfo;
var currentWarn = WriteWarning;
var currentError = WriteError;
SetLoggers(s => {
SetLoggers(s =>
{
debug(s);
currentDebug(s);
}, s =>
{
info(s);
currentInfo(s);
}, s =>
Expand All @@ -72,6 +80,7 @@ public static IDisposable AddLoggersTemporarily(Action<string> info, Action<stri

return new ActionDisposable(() =>
{
WriteDebug = currentDebug;
WriteInfo = currentInfo;
WriteWarning = currentWarn;
WriteError = currentError;
Expand All @@ -85,6 +94,7 @@ static Action<string> LogMessage(Action<string> logAction, string level)

public static void Reset()
{
WriteDebug = s => { throw new Exception("Debug logger not defined. Attempted to log: " + s); };
WriteInfo = s => { throw new Exception("Info logger not defined. Attempted to log: " + s); };
WriteWarning = s => { throw new Exception("Warning logger not defined. Attempted to log: " + s); };
WriteError = s => { throw new Exception("Error logger not defined. Attempted to log: " + s); };
Expand Down
11 changes: 11 additions & 0 deletions src/GitVersionCore/VerbosityLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace GitVersion
{
public enum VerbosityLevel
{
None,
Error,
Warn,
Info,
Debug
}
}
Loading