Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test MSBuild Custom Task without "Task attempted to log before it was initialized" error

I have written a few MSBuild custom tasks that work well and are use in our CruiseControl.NET build process.

I am modifying one, and wish to unit test it by calling the Task's Execute() method.

However, if it encounters a line containing

Log.LogMessage("some message here"); 

it throws an InvalidOperationException:

Task attempted to log before it was initialized. Message was...

Any suggestions? (In the past I have mostly unit-tested Internal static methods on my custom tasks to avoid such problems.)

like image 752
David White Avatar asked Nov 04 '08 04:11

David White


1 Answers

You need to set the .BuildEngine property of the custom task you are calling.

You can set it to the same BuildEngine your current task is using to include the output seamlessly.

Task myCustomTask = new CustomTask(); myCustomTask.BuildEngine = this.BuildEngine; myCustomTask.Execute(); 
like image 168
Branstar Avatar answered Sep 24 '22 12:09

Branstar