Skip to content
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
49 changes: 48 additions & 1 deletion src/Tasks.UnitTests/RoslynCodeTaskFactory_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using VerifyTests;
using VerifyXunit;
using Xunit;
using Xunit.Abstractions;

using static VerifyXunit.Verifier;

Expand All @@ -32,8 +33,11 @@ public class RoslynCodeTaskFactory_Tests

private readonly VerifySettings _verifySettings;

public RoslynCodeTaskFactory_Tests()
private readonly ITestOutputHelper _testOutput;

public RoslynCodeTaskFactory_Tests(ITestOutputHelper testOutput)
{
_testOutput = testOutput;
UseProjectRelativeDirectory("TaskFactorySource");

_verifySettings = new();
Expand Down Expand Up @@ -784,6 +788,49 @@ public override bool Execute()
}
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void ClassDoesNotInheritFromITask(bool forceOutOfProc)
{
const string taskName = "ClassDoesNotInheritFromITask";
string unformattedMessage = ResourceUtilities.GetResourceString("CodeTaskFactory.NeedsITaskInterface");

string projectContent = $$"""
<Project>
<UsingTask TaskName="{{taskName}}" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<Task>
<Code Type="Class">
namespace InlineTask
{
public class {{taskName}}
{
public bool Execute()
{
return true;
}
}
}
</Code>
</Task>
</UsingTask>
<Target Name="Build">
<{{taskName}} />
</Target>
</Project>
""";

using TestEnvironment env = TestEnvironment.Create(_testOutput);
if (forceOutOfProc)
Comment thread
JanProvaznik marked this conversation as resolved.
{
env.SetEnvironmentVariable("MSBUILDFORCEINLINETASKFACTORIESOUTOFPROC", "1");
}

TransientTestProjectWithFiles proj = env.CreateTestProjectWithFiles(projectContent);
MockLogger logger = proj.BuildProjectExpectFailure();
logger.AssertLogContains(unformattedMessage);
}
Comment thread
jankratochvilcz marked this conversation as resolved.

[Fact]
public void EmbedsGeneratedFromSourceFileInBinlog()
{
Expand Down
13 changes: 12 additions & 1 deletion src/Tasks/RoslynCodeTaskFactory/RoslynCodeTaskFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,18 @@ public ITask CreateTask(IBuildEngine taskFactoryLoggingHost)
{
// The type of the task has already been determined and the assembly is already loaded after compilation so
// just create an instance of the type and return it.
return Activator.CreateInstance(TaskType) as ITask;
ITask taskInstance = Activator.CreateInstance(TaskType) as ITask;
if (taskInstance is null)
{
TaskLoggingHelper taskInvocationLog = new TaskLoggingHelper(taskFactoryLoggingHost, _taskName)
{
TaskResources = AssemblyResources.PrimaryResources,
HelpKeywordPrefix = "MSBuild."
};
taskInvocationLog.LogErrorWithCodeFromResources("CodeTaskFactory.NeedsITaskInterface", _taskName);
}

return taskInstance;
}

/// <inheritdoc cref="ITaskFactory.GetTaskParameters"/>
Expand Down