Skip to content

Commit 968b474

Browse files
jonathanpeppersjonpryor
authored andcommitted
[tests] various fixes for Windows (#194)
This gets all the tests projects on Windows passing that *should* pass without compiling a Windows binary/equivalent of `libjava-interop.dylib`. In total, these test projects are passing: - `generator-Tests` - `Java.Interop.Tools.JavaCallableWrappers-Tests` - `LogcatParse-Tests` - `Xamarin.Android.Tools.ApiXmlAdjuster-Tests` - `Xamarin.Android.Tools.Bytecode-Tests` ~~ Changes per test project ~~ `Xamarin.Android.Tools.Bytecode-Tests` - Changes to normalize line endings, `ClassPath` should not hardcode `NewLineChars` to `\n` - The test for `POSITIVE_INFINITY` was failing: Expected: "Double(Infinity)" But was: "Double(∞)" Fix this failure by always using `CultureInfo.InvariantCulture`. We doubt that anyone is going to desire that `.ToString()` on `ConstantPool` subclasses return anything in the current culture; they're largely for debugging aids. generator-Tests: - `CSharpCodeProvider` needs a `using` statement or the generated assembly file is locked for future tests. This causes many test failures. - .NET's `CSharpCodeProvider` won't support C# 6 on Windows. Use `Microsoft.CodeDom.Providers.DotNetCompilerPlatform` to use C#6 on Windows, and continue using `CSharpCodeProvider` elsewhere. - The Roslyn package seems to require an environment variable to locate `csc.exe`. See: https://stackoverflow.com/a/40311406/132442 - When running on Mono, the use of the `partial` keyword is generated as `partial_`. This does not happen on Windows, so included a set of "expected" files for Windows. This runs a different file comparison if the tests are running on Windows. - `--noshadow` is needed for `generator-Tests` to pass on Windows. Later an upgrade to NUnit3 is ideal instead, but some test rework is needed due to the use of the current directory in tests. We should do this after tests are passing reliably on Windows.
1 parent ec16749 commit 968b474

File tree

9 files changed

+187
-27
lines changed

9 files changed

+187
-27
lines changed

build-tools/scripts/RunNUnitTests.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</ItemGroup>
1515
<Target Name="RunTests">
1616
<Exec
17-
Command="$(_NUnit) $(NUNIT_EXTRA) %(_TestAssembly.Identity) $(_Run) --result=&quot;TestResult-%(Filename).xml&quot; --output=&quot;bin\Test$(Configuration)\TestOutput-%(Filename).txt&quot;"
17+
Command="$(_NUnit) $(NUNIT_EXTRA) %(_TestAssembly.Identity) $(_Run) --noshadow --result=&quot;TestResult-%(Filename).xml&quot; --output=&quot;bin\Test$(Configuration)\TestOutput-%(Filename).txt&quot;"
1818
WorkingDirectory="$(_TopDir)"
1919
ContinueOnError="True"
2020
/>

src/Xamarin.Android.Tools.Bytecode/ClassPath.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ public void SaveXmlDescription (TextWriter textWriter)
334334
var settings = new XmlWriterSettings () {
335335
Indent = true,
336336
OmitXmlDeclaration = true,
337-
NewLineChars = "\n",
338337
NewLineOnAttributes = true,
339338
};
340339
var contents = ToXElement ();

src/Xamarin.Android.Tools.Bytecode/ConstantPool.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
4+
using System.Globalization;
45
using System.IO;
56
using System.Linq;
67
using System.Text;
@@ -112,7 +113,7 @@ public ConstantPoolUtf8Item Name {
112113

113114
public override string ToString ()
114115
{
115-
return string.Format ("Class(nameIndex={0} Name=\"{1}\")", nameIndex, Name.Value);
116+
return string.Format (CultureInfo.InvariantCulture, "Class(nameIndex={0} Name=\"{1}\")", nameIndex, Name.Value);
116117
}
117118
}
118119

@@ -138,7 +139,7 @@ public ConstantPoolNameAndTypeItem NameAndType {
138139

139140
public override string ToString ()
140141
{
141-
return string.Format ("{0}(classIndex={1} nameAndTypeIndex={2} Class='{3}' Name='{4}' Descriptor='{5}')",
142+
return string.Format (CultureInfo.InvariantCulture, "{0}(classIndex={1} nameAndTypeIndex={2} Class='{3}' Name='{4}' Descriptor='{5}')",
142143
Type, classIndex, nameAndTypeIndex, Class.Name, NameAndType.Name.Value, NameAndType.Descriptor.Value);
143144
}
144145
}
@@ -201,7 +202,7 @@ public ConstantPoolUtf8Item StringData {
201202

202203
public override string ToString ()
203204
{
204-
return string.Format ("String(stringIndex={0} Utf8=\"{1}\")",
205+
return string.Format (CultureInfo.InvariantCulture, "String(stringIndex={0} Utf8=\"{1}\")",
205206
stringIndex, StringData.Value);
206207
}
207208
}
@@ -227,7 +228,7 @@ public int Value {
227228

228229
public override string ToString ()
229230
{
230-
return string.Format ("Integer({0})", Value);
231+
return string.Format (CultureInfo.InvariantCulture, "Integer({0})", Value);
231232
}
232233
}
233234

@@ -259,7 +260,7 @@ public float Value {
259260

260261
public override string ToString ()
261262
{
262-
return string.Format ("Float({0})", Value);
263+
return string.Format (CultureInfo.InvariantCulture, "Float({0})", Value);
263264
}
264265
}
265266

@@ -288,7 +289,7 @@ public long Value {
288289

289290
public override string ToString ()
290291
{
291-
return string.Format ("Long({0})", Value);
292+
return string.Format (CultureInfo.InvariantCulture, "Long({0})", Value);
292293
}
293294
}
294295

@@ -328,7 +329,7 @@ public double Value {
328329

329330
public override string ToString ()
330331
{
331-
return string.Format ("Double({0})", Value);
332+
return string.Format (CultureInfo.InvariantCulture, "Double({0})", Value);
332333
}
333334
}
334335

@@ -359,7 +360,7 @@ public ConstantPoolUtf8Item Descriptor {
359360

360361
public override string ToString ()
361362
{
362-
return string.Format ("NameAndType(nameIndex={0} descriptorIndex={1} Name=\"{2}\" Descriptor=\"{3}\")",
363+
return string.Format (CultureInfo.InvariantCulture, "NameAndType(nameIndex={0} descriptorIndex={1} Name=\"{2}\" Descriptor=\"{3}\")",
363364
nameIndex, descriptorIndex, Name.Value, Descriptor.Value);
364365
}
365366
}
@@ -414,7 +415,7 @@ public string Value {
414415

415416
public override string ToString ()
416417
{
417-
return string.Format ("Utf8(\"{0}\")", Value);
418+
return string.Format (CultureInfo.InvariantCulture, "Utf8(\"{0}\")", Value);
418419
}
419420
}
420421

@@ -457,7 +458,7 @@ public ConstantPoolUtf8Item Descriptor {
457458

458459
public override string ToString ()
459460
{
460-
return string.Format ("MethodType(descriptorIndex={0} Descriptor=\"{1}\")",
461+
return string.Format (CultureInfo.InvariantCulture, "MethodType(descriptorIndex={0} Descriptor=\"{1}\")",
461462
descriptorIndex, Descriptor.Value);
462463
}
463464
}

tools/generator/Tests/Compiler.cs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,34 @@
33
using System.CodeDom.Compiler;
44
using System.IO;
55
using System.Linq;
6-
using Microsoft.CSharp;
76
using System.Collections.Generic;
87
using NUnit.Framework;
9-
using Xamarin.Android.Binder;
108

119
namespace generatortests
1210
{
1311
public static class Compiler
1412
{
15-
13+
const string RoslynEnvironmentVariable = "ROSLYN_COMPILER_LOCATION";
1614
private static string unitTestFrameworkAssemblyPath = typeof(Assert).Assembly.Location;
1715
private static string supportFilePath = typeof(Compiler).Assembly.Location;
1816

17+
static CodeDomProvider GetCodeDomProvider ()
18+
{
19+
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
20+
//NOTE: there is an issue where Roslyn's csc.exe isn't copied to output for non-ASP.NET projects
21+
// Comments on this here: https://stackoverflow.com/a/40311406/132442
22+
// They added an environment variable as a workaround: https://github.com/aspnet/RoslynCodeDomProvider/pull/12
23+
if (string.IsNullOrEmpty (Environment.GetEnvironmentVariable (RoslynEnvironmentVariable, EnvironmentVariableTarget.Process))) {
24+
string roslynPath = Path.GetFullPath (Path.Combine (unitTestFrameworkAssemblyPath, "..", "..", "..", "packages", "Microsoft.Net.Compilers.2.1.0", "tools"));
25+
Environment.SetEnvironmentVariable (RoslynEnvironmentVariable, roslynPath, EnvironmentVariableTarget.Process);
26+
}
27+
28+
return new Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider ();
29+
} else {
30+
return new Microsoft.CSharp.CSharpCodeProvider ();
31+
}
32+
}
33+
1934
public static Assembly Compile (Xamarin.Android.Binder.CodeGeneratorOptions options,
2035
string assemblyFileName, IEnumerable<string> AdditionalSourceDirectories,
2136
out bool hasErrors, out string output)
@@ -52,17 +67,18 @@ public static Assembly Compile (Xamarin.Android.Binder.CodeGeneratorOptions opti
5267
parameters.IncludeDebugInformation = false;
5368
#endif
5469

55-
CSharpCodeProvider codeProvider = new CSharpCodeProvider ();
56-
CompilerResults results = codeProvider.CompileAssemblyFromFile (parameters,sourceFiles.ToArray ());
70+
using (var codeProvider = GetCodeDomProvider ()) {
71+
CompilerResults results = codeProvider.CompileAssemblyFromFile (parameters, sourceFiles.ToArray ());
5772

58-
hasErrors = false;
73+
hasErrors = false;
5974

60-
foreach (CompilerError message in results.Errors) {
61-
hasErrors = hasErrors || (!message.IsWarning);
62-
}
63-
output = string.Join (Environment.NewLine, results.Output.Cast<string> ());
75+
foreach (CompilerError message in results.Errors) {
76+
hasErrors = hasErrors || (!message.IsWarning);
77+
}
78+
output = string.Join (Environment.NewLine, results.Output.Cast<string> ());
6479

65-
return results.CompiledAssembly;
80+
return results.CompiledAssembly;
81+
}
6682
}
6783

6884
static string GetFacadesPath ()
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Android.Runtime;
4+
using Java.Interop;
5+
6+
namespace Xamarin.Test {
7+
8+
// Metadata.xml XPath class reference: path="/api/package[@name='xamarin.test']/class[@name='CSharpKeywords']"
9+
[global::Android.Runtime.Register ("xamarin/test/CSharpKeywords", DoNotGenerateAcw=true)]
10+
public partial class CSharpKeywords : global::Java.Lang.Object {
11+
12+
internal new static readonly JniPeerMembers _members = new JniPeerMembers ("xamarin/test/CSharpKeywords", typeof (CSharpKeywords));
13+
internal static new IntPtr class_ref {
14+
get {
15+
return _members.JniPeerType.PeerReference.Handle;
16+
}
17+
}
18+
19+
public override global::Java.Interop.JniPeerMembers JniPeerMembers {
20+
get { return _members; }
21+
}
22+
23+
protected override IntPtr ThresholdClass {
24+
get { return _members.JniPeerType.PeerReference.Handle; }
25+
}
26+
27+
protected override global::System.Type ThresholdType {
28+
get { return _members.ManagedPeerType; }
29+
}
30+
31+
protected CSharpKeywords (IntPtr javaReference, JniHandleOwnership transfer) : base (javaReference, transfer) {}
32+
33+
static Delegate cb_usePartial_I;
34+
#pragma warning disable 0169
35+
static Delegate GetUsePartial_IHandler ()
36+
{
37+
if (cb_usePartial_I == null)
38+
cb_usePartial_I = JNINativeWrapper.CreateDelegate ((Func<IntPtr, IntPtr, int, IntPtr>) n_UsePartial_I);
39+
return cb_usePartial_I;
40+
}
41+
42+
static IntPtr n_UsePartial_I (IntPtr jnienv, IntPtr native__this, int partial)
43+
{
44+
global::Xamarin.Test.CSharpKeywords __this = global::Java.Lang.Object.GetObject<global::Xamarin.Test.CSharpKeywords> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
45+
return JNIEnv.NewString (__this.UsePartial (partial));
46+
}
47+
#pragma warning restore 0169
48+
49+
// Metadata.xml XPath method reference: path="/api/package[@name='xamarin.test']/class[@name='CSharpKeywords']/method[@name='usePartial' and count(parameter)=1 and parameter[1][@type='int']]"
50+
[Register ("usePartial", "(I)Ljava/lang/String;", "GetUsePartial_IHandler")]
51+
public virtual unsafe string UsePartial (int partial)
52+
{
53+
const string __id = "usePartial.(I)Ljava/lang/String;";
54+
try {
55+
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
56+
__args [0] = new JniArgumentValue (partial);
57+
var __rm = _members.InstanceMethods.InvokeVirtualObjectMethod (__id, this, __args);
58+
return JNIEnv.GetString (__rm.Handle, JniHandleOwnership.TransferLocalRef);
59+
} finally {
60+
}
61+
}
62+
63+
}
64+
}

tools/generator/Tests/expected.targets

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@
4242
<Content Include='expected\CSharpKeywords\CSharpKeywords.xml'>
4343
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4444
</Content>
45-
<Content Include='expected\CSharpKeywords\Xamarin.Test.CSharpKeywords.cs'>
45+
<Content Include='expected\CSharpKeywords\Xamarin.Test.CSharpKeywords.cs' Condition=" '$(OS)' != 'Windows_NT' ">
46+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
47+
</Content>
48+
<Content Include='expected\CSharpKeywords\Xamarin.Test.CSharpKeywords-windows.cs' Condition=" '$(OS)' == 'Windows_NT' ">
49+
<Link>Xamarin.Test.CSharpKeywords.cs</Link>
4650
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4751
</Content>
4852
<Content Include='expected\EnumerationFixup\EnumerationFixupMap.xml'>
@@ -210,7 +214,11 @@
210214
<Content Include='expected.ji\CSharpKeywords\Mono.Android.projitems'>
211215
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
212216
</Content>
213-
<Content Include='expected.ji\CSharpKeywords\Xamarin.Test.CSharpKeywords.cs'>
217+
<Content Include='expected.ji\CSharpKeywords\Xamarin.Test.CSharpKeywords.cs' Condition=" '$(OS)' != 'Windows_NT' ">
218+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
219+
</Content>
220+
<Content Include='expected.ji\CSharpKeywords\Xamarin.Test.CSharpKeywords-windows.cs' Condition=" '$(OS)' == 'Windows_NT' ">
221+
<Link>Xamarin.Test.CSharpKeywords.cs</Link>
214222
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
215223
</Content>
216224
<Content Include='expected.ji\java.lang.Enum\enumlist'>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Android.Runtime;
4+
5+
namespace Xamarin.Test {
6+
7+
// Metadata.xml XPath class reference: path="/api/package[@name='xamarin.test']/class[@name='CSharpKeywords']"
8+
[global::Android.Runtime.Register ("xamarin/test/CSharpKeywords", DoNotGenerateAcw=true)]
9+
public partial class CSharpKeywords : global::Java.Lang.Object {
10+
11+
internal static new IntPtr java_class_handle;
12+
internal static new IntPtr class_ref {
13+
get {
14+
return JNIEnv.FindClass ("xamarin/test/CSharpKeywords", ref java_class_handle);
15+
}
16+
}
17+
18+
protected override IntPtr ThresholdClass {
19+
get { return class_ref; }
20+
}
21+
22+
protected override global::System.Type ThresholdType {
23+
get { return typeof (CSharpKeywords); }
24+
}
25+
26+
protected CSharpKeywords (IntPtr javaReference, JniHandleOwnership transfer) : base (javaReference, transfer) {}
27+
28+
static Delegate cb_usePartial_I;
29+
#pragma warning disable 0169
30+
static Delegate GetUsePartial_IHandler ()
31+
{
32+
if (cb_usePartial_I == null)
33+
cb_usePartial_I = JNINativeWrapper.CreateDelegate ((Func<IntPtr, IntPtr, int, IntPtr>) n_UsePartial_I);
34+
return cb_usePartial_I;
35+
}
36+
37+
static IntPtr n_UsePartial_I (IntPtr jnienv, IntPtr native__this, int partial)
38+
{
39+
global::Xamarin.Test.CSharpKeywords __this = global::Java.Lang.Object.GetObject<global::Xamarin.Test.CSharpKeywords> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
40+
return JNIEnv.NewString (__this.UsePartial (partial));
41+
}
42+
#pragma warning restore 0169
43+
44+
static IntPtr id_usePartial_I;
45+
// Metadata.xml XPath method reference: path="/api/package[@name='xamarin.test']/class[@name='CSharpKeywords']/method[@name='usePartial' and count(parameter)=1 and parameter[1][@type='int']]"
46+
[Register ("usePartial", "(I)Ljava/lang/String;", "GetUsePartial_IHandler")]
47+
public virtual unsafe string UsePartial (int partial)
48+
{
49+
if (id_usePartial_I == IntPtr.Zero)
50+
id_usePartial_I = JNIEnv.GetMethodID (class_ref, "usePartial", "(I)Ljava/lang/String;");
51+
try {
52+
JValue* __args = stackalloc JValue [1];
53+
__args [0] = new JValue (partial);
54+
55+
if (((object) this).GetType () == ThresholdType)
56+
return JNIEnv.GetString (JNIEnv.CallObjectMethod (((global::Java.Lang.Object) this).Handle, id_usePartial_I, __args), JniHandleOwnership.TransferLocalRef);
57+
else
58+
return JNIEnv.GetString (JNIEnv.CallNonvirtualObjectMethod (((global::Java.Lang.Object) this).Handle, ThresholdClass, JNIEnv.GetMethodID (ThresholdClass, "usePartial", "(I)Ljava/lang/String;"), __args), JniHandleOwnership.TransferLocalRef);
59+
} finally {
60+
}
61+
}
62+
63+
}
64+
}

tools/generator/Tests/generator-Tests.csproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -32,6 +32,9 @@
3232
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
3333
</PropertyGroup>
3434
<ItemGroup>
35+
<Reference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
36+
<HintPath>..\..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.7\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
37+
</Reference>
3538
<Reference Include="System" />
3639
<Reference Include="System.Xml" />
3740
<Reference Include="nunit.framework">
@@ -151,5 +154,8 @@
151154
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
152155
</Content>
153156
</ItemGroup>
157+
<ItemGroup>
158+
<None Include="packages.config" />
159+
</ItemGroup>
154160
<Import Project="expected.targets" />
155-
</Project>
161+
</Project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.7" targetFramework="net45" />
4+
<package id="Microsoft.Net.Compilers" version="2.1.0" developmentDependency="true" />
35
<package id="NUnit" version="2.6.3" targetFramework="net40" />
46
</packages>

0 commit comments

Comments
 (0)