[msbuild] Avoid an NRE in BtouchTask when an invalid extra argument is provided (#5041)

The task itself should not throw. An invalid argument is an error that
should (and is) reported by `btouch` itself (and the task picks it up).
This makes the error reporting much more useful and the way an exception
is reported, from Windows, is also confusing
```
MessagingRemoteException: An error occured on client Build4110732 while executing a reply for topic xvs/Build/4.11.0.732/execute-task/ClassLibrary1/6e85b94002fBTouch ArgumentNullException: Value cannot be null.
```

Unit tests added.

Fixes https://devdiv.visualstudio.com/DevDiv/_workitems/edit/656983
This commit is contained in:
Sebastien Pouliot 2018-10-26 13:18:05 -04:00 коммит произвёл GitHub
Родитель 12262c4cba
Коммит eaa6435e89
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 31 добавлений и 25 удалений

Просмотреть файл

@ -202,15 +202,20 @@ namespace Xamarin.MacDev.Tasks {
{ "projectdir", projectDir },
// Apparently msbuild doesn't propagate the solution path, so we can't get it.
// { "solutiondir", proj.ParentSolution != null ? proj.ParentSolution.BaseDirectory : proj.BaseDirectory },
{ "targetpath", Path.Combine (Path.GetDirectoryName (target), Path.GetFileName (target)) },
{ "targetdir", Path.GetDirectoryName (target) },
{ "targetname", Path.GetFileName (target) },
{ "targetext", Path.GetExtension (target) },
};
// OutputAssembly is optional so it can be null
if (target != null) {
var d = Path.GetDirectoryName (target);
var n = Path.GetFileName (target);
customTags.Add ("targetpath", Path.Combine (d, n));
customTags.Add ("targetdir", d);
customTags.Add ("targetname", n);
customTags.Add ("targetext", Path.GetExtension (target));
}
for (int i = 0; i < extraArgs.Length; i++) {
var argument = extraArgs[i];
cmd.AppendTextUnquoted (" ");
cmd.AppendTextUnquoted (StringParserService.Parse (argument, customTags));
}
}

Просмотреть файл

@ -9,11 +9,7 @@ namespace Xamarin.iOS.Tasks
{
class CustomBTouchTask : BTouch
{
public CustomBTouchTask ()
{
}
public new string GenerateCommandLineCommands ()
public string GetCommandLineCommands ()
{
return base.GenerateCommandLineCommands ();
}
@ -22,29 +18,34 @@ namespace Xamarin.iOS.Tasks
[TestFixture]
public class BTouchTaskTests : TestBase
{
CustomBTouchTask Task {
get; set;
}
public override void Setup ()
{
base.Setup ();
Task = CreateTask<CustomBTouchTask> ();
Task.ApiDefinitions = new [] { new TaskItem ("apidefinition.cs") };
Task.References = new [] { new TaskItem ("a.dll"), new TaskItem ("b.dll"), new TaskItem ("c.dll") };
}
[Test]
public void StandardCommandline ()
{
var args = Task.GenerateCommandLineCommands ();
var task = CreateTask<CustomBTouchTask> ();
task.ApiDefinitions = new[] { new TaskItem ("apidefinition.cs") };
task.References = new[] { new TaskItem ("a.dll"), new TaskItem ("b.dll"), new TaskItem ("c.dll") };
var args = task.GetCommandLineCommands ();
Assert.IsTrue (args.Contains ("-r " + Path.Combine (Environment.CurrentDirectory, "a.dll")), "#1a");
Assert.IsTrue (args.Contains ("-r " + Path.Combine (Environment.CurrentDirectory, "b.dll")), "#1b");
Assert.IsTrue (args.Contains ("-r " + Path.Combine (Environment.CurrentDirectory, "c.dll")), "#1c");
}
[Test]
public void Bug656983 ()
{
var task = CreateTask<CustomBTouchTask> ();
task.ApiDefinitions = new[] { new TaskItem ("apidefinition.cs") };
task.References = new[] { new TaskItem ("a.dll"), new TaskItem ("b.dll"), new TaskItem ("c.dll") };
task.ProjectDir = "~/"; // not important, but required (so can't be null)
task.OutputAssembly = null; // default, but important for the bug (in case that default changes)
task.ExtraArgs = "-invalid";
var args = task.GetCommandLineCommands ();
Assert.That (args.Contains (" -invalid"), "incorrect ExtraArg not causing an exception");
}
}
}