Skip to content

Commit

Permalink
Merge pull request #4213 from rainersigwald/newlines-in-task-arguments
Browse files Browse the repository at this point in the history
Support multi-line Exec Commands, fixing #4210.
  • Loading branch information
rainersigwald authored Mar 8, 2019
2 parents a8dc7f1 + 873c127 commit 01bae62
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 18 deletions.
19 changes: 4 additions & 15 deletions src/Build/Xml/XmlReaderExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,10 @@ private static XmlReader GetXmlReader(string file, StreamReader input, bool load

XmlReader reader;

if (loadAsReadOnly)
{
XmlReaderSettings xrs = new XmlReaderSettings
{
DtdProcessing = DtdProcessing.Ignore,
IgnoreComments = true,
IgnoreWhitespace = true,
};
reader = XmlReader.Create(input, xrs, uri);
}
else
{
reader = new XmlTextReader(uri, input) { DtdProcessing = DtdProcessing.Ignore };
}

// Ignore loadAsReadOnly for now; using XmlReader.Create results in whitespace changes
// of attribute text, specifically newline removal.
// https://github.com/Microsoft/msbuild/issues/4210
reader = new XmlTextReader(uri, input) { DtdProcessing = DtdProcessing.Ignore };

reader.Read();
encoding = input.CurrentEncoding;
Expand Down
67 changes: 64 additions & 3 deletions src/Tasks.UnitTests/Exec_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
using System.IO;
using System.Reflection;
using System.Text;
using Microsoft.Build.UnitTests;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Tasks;
using Microsoft.Build.Utilities;
using Microsoft.Build.Shared;
using Shouldly;
using Xunit;
using Xunit.Abstractions;
using System.Collections.Generic;
using Microsoft.Build.Evaluation;

namespace Microsoft.Build.UnitTests
{
Expand All @@ -20,9 +23,16 @@ namespace Microsoft.Build.UnitTests
/// </summary>
sealed public class Exec_Tests
{
private readonly ITestOutputHelper _output;

public Exec_Tests(ITestOutputHelper output)
{
_output = output;
}

private Exec PrepareExec(string command)
{
IBuildEngine2 mockEngine = new MockEngine(true);
IBuildEngine2 mockEngine = new MockEngine(_output);
Exec exec = new Exec();
exec.BuildEngine = mockEngine;
exec.Command = command;
Expand All @@ -31,7 +41,7 @@ private Exec PrepareExec(string command)

private ExecWrapper PrepareExecWrapper(string command)
{
IBuildEngine2 mockEngine = new MockEngine(true);
IBuildEngine2 mockEngine = new MockEngine(_output);
ExecWrapper exec = new ExecWrapper();
exec.BuildEngine = mockEngine;
exec.Command = command;
Expand Down Expand Up @@ -841,6 +851,57 @@ public void CanEncodeTest()
Assert.False(Exec.CanEncodeString(defaultEncoding.CodePage, nonAnsiCharacters));
Assert.True(Exec.CanEncodeString(defaultEncoding.CodePage, pathWithAnsiCharacters));
}

[Fact]
public void EndToEndMultilineExec()
{
using (var env = TestEnvironment.Create(_output))
{
var testProject = env.CreateTestProjectWithFiles(@"<Project>
<Target Name=""MultilineExec"">
<Exec Command=""echo line 1
echo line 2
echo line 3"" />
</Target>
</Project>");

using (var buildManager = new BuildManager())
{
MockLogger logger = new MockLogger(_output, profileEvaluation: false, printEventsToStdout: false);
var parameters = new BuildParameters()
{
Loggers = new[] { logger },
};

var collection = new ProjectCollection(
new Dictionary<string, string>(),
new[] { logger },
remoteLoggers: null,
ToolsetDefinitionLocations.Default,
maxNodeCount: 1,
onlyLogCriticalEvents: false,
loadProjectsReadOnly: true);

var project = collection.LoadProject(testProject.ProjectFile).CreateProjectInstance();

var request = new BuildRequestData(
project,
targetsToBuild: new[] { "MultilineExec" },
hostServices: null);

var result = buildManager.Build(parameters, request);

logger.AssertLogContains("line 2");
logger.AssertLogContains("line 3");

// To be correct, these need to be on separate lines, not
// all together on one.
logger.AssertLogDoesntContain("1 echo line");

result.OverallResult.ShouldBe(BuildResultCode.Success);
}
}
}
}

internal class ExecWrapper : Exec
Expand Down

0 comments on commit 01bae62

Please sign in to comment.