Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tests] Prevent build logs locking up IDEs (#1876)
Browse files Browse the repository at this point in the history
When running tests in Visual Studio's test runner UI (Windows and
macOS), a test failure causes an unpleasant experience:

Since the entire MSBuild diagnostic log is contained within a
`FailedBuildException`, the UI will hang anywhere from 30 seconds to
1 minute while it tries to render the exception details in the IDE.

The solution here is to:

  - Detect if the unit test is running inside an IDE
  - Don't include the build log in the exception details in this case

On Windows, we can detect the test running by the process name, which
is either `testhost.x86` or `testhost.x64`.

On macOS, we can detect VS for Mac by looking for the
`MONO_GAC_PREFIX` environment variable, which points to
`/Applications/Visual Studio.app/Contents/Resources` by default.

If neither of these cases are met, such as command line, the exception
remains unchanged.
  • Loading branch information
jonathanpeppers authored and jonpryor committed Jun 25, 2018
1 parent 2fbe4f5 commit 0b5ce8f
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,36 @@ protected bool BuildInternal (string projectOrSolution, string target, string []
}
if (!result && ThrowOnBuildFailure) {
string message = "Build failure: " + Path.GetFileName (projectOrSolution) + (BuildLogFile != null && File.Exists (buildLogFullPath) ? "Build log recorded at " + buildLogFullPath : null);
throw new FailedBuildException (message, null, File.ReadAllText (buildLogFullPath));
//NOTE: enormous logs will lock up IDE's UI
if (IsRunningInIDE) {
throw new FailedBuildException (message);
} else {
throw new FailedBuildException (message, null, File.ReadAllText (buildLogFullPath));
}
}

return result;
}

bool IsRunningInIDE {
get {
//Check for Windows, process is testhost.x86 in VS 2017
using (var p = Process.GetCurrentProcess ()) {
if (p.ProcessName.IndexOf ("testhost", StringComparison.OrdinalIgnoreCase) != -1) {
return true;
}
}

//Check for macOS, value is normally /Applications/Visual Studio.app/Contents/Resources
var gac_prefix = Environment.GetEnvironmentVariable ("MONO_GAC_PREFIX", EnvironmentVariableTarget.Process);
if (!string.IsNullOrEmpty (gac_prefix) && gac_prefix.IndexOf ("Visual Studio", StringComparison.OrdinalIgnoreCase) != -1) {
return true;
}

return false;
}
}

string QuoteFileName (string fileName)
{
return fileName.Contains (" ") ? $"\"{fileName}\"" : fileName;
Expand Down

0 comments on commit 0b5ce8f

Please sign in to comment.