Skip to content

Commit

Permalink
[tests] Use correct test result for adb hosts (#1365)
Browse files Browse the repository at this point in the history
Certain deployment targets require us to fix up an external storage
path before we can pull it via adb (see `ToAdbPath()` comments for
more details). We should only attempt to fix up this path *after*
writing our test result, as our application context will not
recognize it as a valid file path.

Fixes the following error:

	Error: System.IO.DirectoryNotFoundException: Could not find a part of the path "/mnt/shell/emulated/0/Android/data/Xamarin.Android.Bcl_Tests/files/Documents/TestResults.xUnit.xml".
	  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x00177] in /Users/builder/jenkins/workspace/xamarin-android-d15-7/xamarin-android/external/mono/mcs/class/corlib/System.IO/FileStream.cs:223 
	  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.IO.FileOptions options) [0x00000] in /Users/builder/jenkins/workspace/xamarin-android-d15-7/xamarin-android/external/mono/mcs/class/corlib/System.IO/FileStream.cs:106 
	  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean useAsync) [0x00000] in /Users/builder/jenkins/workspace/xamarin-android-d15-7/xamarin-android/external/mono/mcs/class/corlib/System.IO/FileStream.cs:101 
	  at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,bool)
	  at System.Xml.XmlWriterSettings.CreateWriter (System.String outputFileName) [0x00051] in /Users/builder/jenkins/workspace/xamarin-android-d15-7/xamarin-android/external/mono/mcs/class/referencesource/System.Xml/System/Xml/Core/XmlWriterSettings.cs:464 
	  at System.Xml.XmlWriter.Create (System.String outputFileName, System.Xml.XmlWriterSettings settings) [0x0000a] in /Users/builder/jenkins/workspace/xamarin-android-d15-7/xamarin-android/external/mono/mcs/class/referencesource/System.Xml/System/Xml/Core/XmlWriter.cs:610 
	  at Xamarin.Android.UnitTests.XUnit.XUnitTestRunner.WriteResultsToFile ()
	  at Xamarin.Android.UnitTests.TestInstrumentation`1[TRunner].RunTests (Android.OS.Bundle& results)
	  at Xamarin.Android.UnitTests.TestInstrumentation`1[TRunner].OnStart ()
	UNHANDLED EXCEPTION:
	Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
	  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/jenkins/workspace/xamarin-android-d15-7/xamarin-android/external/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:152 
	  at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00089] in <1c16e67a812b475a9ae6b6e26eed9097>:0 
	  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0005d] in <1c16e67a812b475a9ae6b6e26eed9097>:0 
	  at Android.App.Instrumentation.Finish (Android.App.Result resultCode, Android.OS.Bundle results) [0x0003a] in /Users/builder/data/lanes/5945/28b08da2/source/monodroid/external/xamarin-android/src/Mono.Android/obj/Release/android-27/mcw/Android.App.Instrumentation.cs:1366 
	  at Xamarin.Android.UnitTests.TestInstrumentation`1[TRunner].OnStart ()
	  at Android.App.Instrumentation.n_OnStart (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in /Users/builder/data/lanes/5945/28b08da2/source/monodroid/external/xamarin-android/src/Mono.Android/obj/Release/android-27/mcw/Android.App.Instrumentation.cs:1729 
	  at (wrapper dynamic-method) System.Object.60d1c0b0-3a21-410a-9b3a-12e201b965fe(intptr,intptr)
	  --- End of managed Java.Lang.NullPointerException stack trace ---
	java.lang.NullPointerException
		at android.app.Instrumentation.finish(Instrumentation.java:213)
		at md58bfc2187967f55be2f50e366b0d6b91f.TestInstrumentation_1.n_onStart(Native Method)
		at md58bfc2187967f55be2f50e366b0d6b91f.TestInstrumentation_1.onStart(TestInstrumentation_1.java:35)
		at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1792)
  • Loading branch information
pjcollins authored and jonpryor committed Mar 6, 2018
1 parent b6c4909 commit 7e42664
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
23 changes: 22 additions & 1 deletion tests/TestRunner.Core/TestInstrumentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ bool RunTests (ref Bundle results)
results.PutLong (ResultInconclusiveTests, runner.InconclusiveTests);
results.PutLong (ResultTotalTests, runner.TotalTests);
results.PutLong (ResultFilteredTests, runner.FilteredTests);
results.PutString (ResultResultsFilePath, resultsFilePath);
results.PutString (ResultResultsFilePath, ToAdbPath (resultsFilePath));

Log.Info (LogTag, $"Passed: {runner.PassedTests}, Failed: {runner.FailedTests}, Skipped: {runner.SkippedTests}, Inconclusive: {runner.InconclusiveTests}, Total: {runner.TotalTests}, Filtered: {runner.FilteredTests}");

Expand Down Expand Up @@ -364,5 +364,26 @@ protected HashSet<string> LoadExcludedTests (TextReader reader)
excludedTestNames.Add (line);
} while (true);
}

// On some Android targets, the external storage directory is "emulated",
// in which case the paths used on-device by the application are *not*
// paths that can be used off-device with `adb pull`.
// For example, `Contxt.GetExternalFilesDir()` may return `/storage/emulated/foo`,
// but `adb pull /storage/emulated/foo` will *fail*; instead, we may need
// `adb pull /mnt/shell/emulated/foo`.
// The `$EMULATED_STORAGE_SOURCE` and `$EMULATED_STORAGE_TARGET` environment
// variables control the "on-device" (`$EMULATED_STORAGE_TARGET`) and
// "off-device" (`$EMULATED_STORAGE_SOURCE`) directory prefixes
string ToAdbPath (string path)
{
string source = global::System.Environment.GetEnvironmentVariable ("EMULATED_STORAGE_SOURCE")?.Trim ();
string target = global::System.Environment.GetEnvironmentVariable ("EMULATED_STORAGE_TARGET")?.Trim ();

if (!String.IsNullOrEmpty (source) && !String.IsNullOrEmpty (target) && path.StartsWith (target, StringComparison.Ordinal)) {
return path.Replace (target, source);
}

return path;
}
}
}
23 changes: 1 addition & 22 deletions tests/TestRunner.Core/TestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,7 @@ protected virtual string GetResultsFilePath ()
if (!usePathFile && !Directory.Exists (resultsPath))
Directory.CreateDirectory (resultsPath);

return ToAdbPath (Path.Combine (resultsPath, ResultsFileName));
}

// On some Android targets, the external storage directory is "emulated",
// in which case the paths used on-device by the application are *not*
// paths that can be used off-device with `adb pull`.
// For example, `Contxt.GetExternalFilesDir()` may return `/storage/emulated/foo`,
// but `adb pull /storage/emulated/foo` will *fail*; instead, we may need
// `adb pull /mnt/shell/emulated/foo`.
// The `$EMULATED_STORAGE_SOURCE` and `$EMULATED_STORAGE_TARGET` environment
// variables control the "on-device" (`$EMULATED_STORAGE_TARGET`) and
// "off-device" (`$EMULATED_STORAGE_SOURCE`) directory prefixes
string ToAdbPath (string path)
{
string source = global::System.Environment.GetEnvironmentVariable ("EMULATED_STORAGE_SOURCE")?.Trim ();
string target = global::System.Environment.GetEnvironmentVariable ("EMULATED_STORAGE_TARGET")?.Trim ();

if (!String.IsNullOrEmpty (source) && !String.IsNullOrEmpty (target) && path.StartsWith (target, StringComparison.Ordinal)) {
return path.Replace (target, source);
}

return path;
return Path.Combine (resultsPath, ResultsFileName);
}
}
}

0 comments on commit 7e42664

Please sign in to comment.