-
-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add APIs to read logs from a Stream, not just file path.
- Loading branch information
1 parent
be3bdca
commit b06d2ce
Showing
13 changed files
with
410 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using Microsoft.Build.Logging; | ||
using Microsoft.Build.Logging.StructuredLogger; | ||
using StructuredLogger.Tests; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.Build.UnitTests | ||
{ | ||
public class BinaryLoggerTests : IDisposable | ||
{ | ||
private static string s_testProject = @" | ||
<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'> | ||
<PropertyGroup> | ||
<TestProperty>Test</TestProperty> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<TestItem Include=""Test"" /> | ||
</ItemGroup> | ||
<Target Name='Target1'> | ||
<Message Text='MessageOutputText'/> | ||
</Target> | ||
<Target Name='Target2' AfterTargets='Target1'> | ||
<Exec Command='echo a'/> | ||
</Target> | ||
</Project>"; | ||
|
||
public BinaryLoggerTests(ITestOutputHelper output) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public void TestBinaryLoggerRoundtrip() | ||
{ | ||
var binLog = "1.binlog"; | ||
var binaryLogger = new BinaryLogger(); | ||
binaryLogger.Parameters = binLog; | ||
MSBuild.BuildProject(s_testProject, binaryLogger); | ||
|
||
var build = Serialization.Read(binLog); | ||
Assert.Equal("", GetProperty(build)); | ||
Serialization.Write(build, "1.xml"); | ||
|
||
Serialization.Write(build, "1.buildlog"); | ||
build = Serialization.Read("1.buildlog"); | ||
Assert.Equal("", GetProperty(build)); | ||
Serialization.Write(build, "2.xml"); | ||
|
||
Assert.False(Differ.AreDifferent("1.xml", "2.xml")); | ||
|
||
build = XlinqLogReader.ReadFromXml("1.xml"); | ||
Assert.Equal("", GetProperty(build)); | ||
Serialization.Write(build, "3.xml"); | ||
Assert.False(Differ.AreDifferent("1.xml", "3.xml")); | ||
|
||
build = Serialization.Read("1.xml"); | ||
Assert.Equal("", GetProperty(build)); | ||
Serialization.Write(build, "4.xml"); | ||
|
||
Assert.False(Differ.AreDifferent("1.xml", "4.xml")); | ||
} | ||
|
||
private static string GetProperty(Logging.StructuredLogger.Build build) | ||
{ | ||
var property = build.FindFirstDescendant<Project>().FindChild<Folder>("Properties").FindChild<Property>(p => p.Name == "FrameworkSDKRoot").Value; | ||
return property; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.IO; | ||
using System.Xml; | ||
using Microsoft.Build.Evaluation; | ||
using Microsoft.Build.Framework; | ||
|
||
namespace StructuredLogger.Tests | ||
{ | ||
public class MSBuild | ||
{ | ||
public static bool BuildProject(string projectText, params ILogger[] loggers) | ||
{ | ||
Project project = CreateInMemoryProject(projectText, loggers: loggers); | ||
bool success = project.Build(loggers); | ||
return success; | ||
} | ||
|
||
public static Project CreateInMemoryProject(string projectText, ProjectCollection projectCollection = null, params ILogger[] loggers) | ||
{ | ||
XmlReaderSettings readerSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore }; | ||
projectCollection = projectCollection ?? new ProjectCollection(); | ||
|
||
Project project = new Project( | ||
XmlReader.Create(new StringReader(CleanupFileContents(projectText)), readerSettings), | ||
null, | ||
toolsVersion: null, | ||
projectCollection: projectCollection); | ||
|
||
Guid guid = Guid.NewGuid(); | ||
project.FullPath = Path.GetFullPath("Temporary" + guid.ToString("N") + ".csproj"); | ||
project.ReevaluateIfNecessary(); | ||
|
||
return project; | ||
} | ||
|
||
const string msbuildNamespace = "http://schemas.microsoft.com/developer/msbuild/2003"; | ||
|
||
/// <summary> | ||
/// Does certain replacements in a string representing the project file contents. | ||
/// This makes it easier to write unit tests because the author doesn't have | ||
/// to worry about escaping double-quotes, etc. | ||
/// </summary> | ||
/// <param name="projectFileContents"></param> | ||
/// <returns></returns> | ||
internal static string CleanupFileContents(string projectFileContents) | ||
{ | ||
// Replace reverse-single-quotes with double-quotes. | ||
projectFileContents = projectFileContents.Replace("`", "\""); | ||
|
||
// Place the correct MSBuild namespace into the <Project> tag. | ||
projectFileContents = projectFileContents.Replace("msbuildnamespace", msbuildNamespace); | ||
projectFileContents = projectFileContents.Replace("msbuilddefaulttoolsversion", "15.0"); | ||
projectFileContents = projectFileContents.Replace("msbuildassemblyversion", "15.1.0.0"); | ||
|
||
return projectFileContents; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.