Skip to content

Commit

Permalink
[tests] fix for first launch of adb (dotnet#1900)
Browse files Browse the repository at this point in the history
Context: dotnet@77cf939

Since 77cf939, we have been killing adb after tests complete.

This causes an issue if you are running a test in an IDE that uses
`BaseTest.HasDevices`. The first run will likely work, but the second
one fails saying `Test Skipped no devices or emulators found.`.

If you run a test, and adb is not already running, consider the
following code:

    var result = RunAdbCommand ($"{adbTarget} shell getprop ro.build.version.sdk");

It will return with:

    27* daemon not running; starting now at tcp:5037
    * daemon started successfully

Since we pass the result to `int.TryParse`, we are treating the
command as if it had failed, meaning no devices were attached.

To make this more robust, we can split on the `*` character and take
the first item. This allows this command to work even if adb is being
launched for the first time.

Other changes:
- We also do not need to call `Trim ()` from the result of
  `RunAdbCommand` since it already calls `Trim ()`.
  • Loading branch information
jonathanpeppers authored and dellis1972 committed Jun 29, 2018
1 parent 89e3fef commit acacd7e
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ public void BeforeAllTests ()
try {
var adbTarget = Environment.GetEnvironmentVariable ("ADB_TARGET");
int sdkVersion = -1;
HasDevices = int.TryParse (RunAdbCommand ($"{adbTarget} shell getprop ro.build.version.sdk").Trim (), out sdkVersion) && sdkVersion != -1;
var result = RunAdbCommand ($"{adbTarget} shell getprop ro.build.version.sdk");
if (result.Contains ("*")) {
//NOTE: We may get a result of:
//
//27* daemon not running; starting now at tcp:5037
//* daemon started successfully
result = result.Split ('*').First ().Trim ();
}
HasDevices = int.TryParse (result, out sdkVersion) && sdkVersion != -1;
} catch (Exception ex) {
Console.Error.WriteLine ("Failed to determine whether there is Android target emulator or not: " + ex);
}
Expand Down

0 comments on commit acacd7e

Please sign in to comment.