[C#] Cleaned up .NET testing script for Mono (#6016)
authorDerek Bailey <derekbailey@google.com>
Mon, 17 Aug 2020 20:10:10 +0000 (13:10 -0700)
committerGitHub <noreply@github.com>
Mon, 17 Aug 2020 20:10:10 +0000 (13:10 -0700)
* Cleaned up .NET testing script for Mono

Cleaned up the .NET testing script to make it a little better. It
purposefully doesn't delete the .NET installer and SDk after running the
script, so that they can be used in subsequent invocations. This greatly
speeds up the script.

The downloaded files are ignored by git by default. They can be
explicitly cleaned up by runnning the clean script (clean.sh).

* Trying using older Version indicator

* Remove lins to monsterdata and reference it directly.

* Updated appveryor script to remove copying of files no longer needed

* Continue to update appveyor script to work. Disabled CS0169 in ByteBufferTest

appveyor.yml
tests/FlatBuffers.Test/.gitignore [new file with mode: 0644]
tests/FlatBuffers.Test/ByteBufferTests.cs
tests/FlatBuffers.Test/FlatBuffers.Test.csproj
tests/FlatBuffers.Test/FlatBuffersExampleTests.cs
tests/FlatBuffers.Test/NetTest.sh
tests/FlatBuffers.Test/README.md [new file with mode: 0644]
tests/FlatBuffers.Test/Resources/.gitkeep [deleted file]
tests/FlatBuffers.Test/clean.sh [new file with mode: 0755]

index 634a521..6655f3c 100644 (file)
@@ -107,16 +107,15 @@ test_script:
   # Have to compile this here rather than in "build" above because AppVeyor only
   # supports building one project??
   - "cd FlatBuffers.Test"
-  - "copy ..\\monsterdata_test.mon Resources\\"
-  - "copy ..\\monsterdata_test.json Resources\\"
   - "dotnet new sln"
   - "dotnet sln add FlatBuffers.Test.csproj"
   - "nuget restore"
-  - "msbuild.exe /property:Configuration=Release;OutputPath=tempcs /verbosity:minimal FlatBuffers.Test.csproj"
-  - "tempcs\\FlatBuffers.Test.exe"
+  - "mkdir .tmp"
+  - "msbuild.exe /property:Configuration=Release;OutputPath=.tmp /verbosity:minimal FlatBuffers.Test.csproj"
+  - ".tmp\\FlatBuffers.Test.exe"
   # Run tests with UNSAFE_BYTEBUFFER
-  - "msbuild.exe /property:Configuration=Release;UnsafeByteBuffer=true;OutputPath=tempcsUnsafe /verbosity:minimal FlatBuffers.Test.csproj"
-  - "tempcsUnsafe\\FlatBuffers.Test.exe"
+  - "msbuild.exe /property:Configuration=Release;UnsafeByteBuffer=true;OutputPath=.tmp /verbosity:minimal FlatBuffers.Test.csproj"
+  - ".tmp\\FlatBuffers.Test.exe"
   # TODO: add more languages.
   - "cd ..\\.."
 
diff --git a/tests/FlatBuffers.Test/.gitignore b/tests/FlatBuffers.Test/.gitignore
new file mode 100644 (file)
index 0000000..badc827
--- /dev/null
@@ -0,0 +1,4 @@
+dotnet-install.sh
+.dotnet_tmp/
+.tmp/
+packages/
\ No newline at end of file
index faa6a6a..98e917c 100644 (file)
@@ -591,11 +591,14 @@ namespace FlatBuffers.Test
             Assert.Throws<ArgumentException>(() => uut.Put(1024, data));
         }
 
+        #pragma warning disable 0169
+        // These are purposely not used and the warning is suppress
         private struct dummyStruct
         {
             int a;
             float b;
         }
+        #pragma warning restore 0169
 
         [FlatBuffersTestMethod]
         public void ByteBuffer_Put_Array_IncorrectType_Throws()
index c9abb35..e26f918 100644 (file)
@@ -9,6 +9,7 @@
     <RootNamespace>FlatBuffers.Test</RootNamespace>
     <AssemblyName>FlatBuffers.Test</AssemblyName>
     <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+    <RuntimeIdentifier>win</RuntimeIdentifier>
     <LangVersion>4</LangVersion>
     <FileAlignment>512</FileAlignment>
   </PropertyGroup>
     <DefineConstants>$(DefineConstants);UNSAFE_BYTEBUFFER</DefineConstants>
   </PropertyGroup>
   <ItemGroup>
+    <PackageReference Include="Newtonsoft.Json">
+      <Version>12.0.3</Version>
+    </PackageReference>
+  </ItemGroup>
+  <ItemGroup>
     <Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
       <HintPath>packages\Newtonsoft.Json.12.0.3\lib\net35\Newtonsoft.Json.dll</HintPath>
     </Reference>
index 97a32ae..1324c8c 100644 (file)
@@ -114,13 +114,13 @@ namespace FlatBuffers.Test
             // Dump to output directory so we can inspect later, if needed
             #if ENABLE_SPAN_T
             var data = fbb.DataBuffer.ToSizedArray();
-            string filename = @"Resources/monsterdata_cstest" + (sizePrefix ? "_sp" : "") + ".mon";
+            string filename = @".tmp/monsterdata_cstest" + (sizePrefix ? "_sp" : "") + ".mon";
             File.WriteAllBytes(filename, data);
             #else
             using (var ms = fbb.DataBuffer.ToMemoryStream(fbb.DataBuffer.Position, fbb.Offset))
             {
                 var data = ms.ToArray();
-                string filename = @"Resources/monsterdata_cstest" + (sizePrefix ? "_sp" : "") + ".mon";
+                string filename = @".tmp/monsterdata_cstest" + (sizePrefix ? "_sp" : "") + ".mon";
                 File.WriteAllBytes(filename, data);
             }
             #endif
@@ -282,7 +282,7 @@ namespace FlatBuffers.Test
         [FlatBuffersTestMethod]
         public void CanReadCppGeneratedWireFile()
         {
-            var data = File.ReadAllBytes(@"Resources/monsterdata_test.mon");
+            var data = File.ReadAllBytes(@"../monsterdata_test.mon");
             var bb = new ByteBuffer(data);
             TestBuffer(bb);
             TestObjectAPI(Monster.GetRootAsMonster(bb));
@@ -291,7 +291,7 @@ namespace FlatBuffers.Test
         [FlatBuffersTestMethod]
         public void CanReadJsonFile()
         {
-            var jsonText = File.ReadAllText(@"Resources/monsterdata_test.json");
+            var jsonText = File.ReadAllText(@"../monsterdata_test.json");
             var mon = MonsterT.DeserializeFromJson(jsonText);
             var fbb = new FlatBufferBuilder(1);
             fbb.Finish(Monster.Pack(fbb, mon).Value);
index ce36081..44e77e1 100755 (executable)
@@ -1,41 +1,31 @@
 #!/bin/sh
 
-# Restore nuget packages
-mkdir dotnet_tmp
-curl -OL https://dot.net/v1/dotnet-install.sh
-chmod +x dotnet-install.sh
-./dotnet-install.sh --version latest --install-dir dotnet_tmp
-dotnet_tmp/dotnet new sln
-dotnet_tmp/dotnet sln add FlatBuffers.Test.csproj
-curl -OL https://dist.nuget.org/win-x86-commandline/v5.5.1/nuget.exe
-mono nuget.exe restore
-
-# Copy Test Files
-cp ../monsterdata_test.mon Resources/
-cp ../monsterdata_test.json Resources/
+PROJ_FILE=FlatBuffers.Test.csproj
 
-# Testing C# on Linux using Mono.
+TEMP_DOTNET_DIR=.dotnet_tmp
+TEMP_BIN=.tmp
+
+[ -d $TEMP_DOTNET_DIR ] || mkdir $TEMP_DOTNET_DIR
+
+[ -f dotnet-install.sh ] || curl -OL https://dot.net/v1/dotnet-install.sh
+
+./dotnet-install.sh --version latest --install-dir $TEMP_DOTNET_DIR
 
-msbuild -property:Configuration=Release,OutputPath=tempcs -verbosity:minimal FlatBuffers.Test.csproj
-mono tempcs/FlatBuffers.Test.exe
-rm -fr tempcs 
-rm Resources/monsterdata_cstest.mon
-rm Resources/monsterdata_cstest_sp.mon
+DOTNET=$TEMP_DOTNET_DIR/dotnet
+
+$DOTNET new sln
+$DOTNET sln add $PROJ_FILE
+$DOTNET restore -r linux-x64 $PROJ_FILE
+
+# Testing C# on Linux using Mono.
+msbuild -property:Configuration=Release,OutputPath=$TEMP_BIN -verbosity:minimal $PROJ_FILE
+mono $TEMP_BIN/FlatBuffers.Test.exe
+rm -fr $TEMP_BIN
 
 # Repeat with unsafe versions
+msbuild -property:Configuration=Release,UnsafeByteBuffer=true,OutputPath=$TEMP_BIN -verbosity:minimal $PROJ_FILE
+mono $TEMP_BIN/FlatBuffers.Test.exe
+rm -fr $TEMP_BIN
 
-msbuild -property:Configuration=Release,UnsafeByteBuffer=true,OutputPath=tempcsUnsafe -verbosity:minimal FlatBuffers.Test.csproj
-mono tempcsUnsafe/FlatBuffers.Test.exe
-rm -fr tempcsUnsafe 
-rm Resources/monsterdata_cstest.mon
-rm Resources/monsterdata_cstest_sp.mon
-
-# Remove Temp Files
-rm -fr dotnet_tmp
-rm -fr packages
-rm dotnet-install.sh
-rm nuget.exe
 rm FlatBuffers.Test.sln
-rm Resources/monsterdata_test.mon
-rm Resources/monsterdata_test.json
-
+rm -rf obj
diff --git a/tests/FlatBuffers.Test/README.md b/tests/FlatBuffers.Test/README.md
new file mode 100644 (file)
index 0000000..41b6983
--- /dev/null
@@ -0,0 +1,38 @@
+# .NET Tests
+
+## Running on Linux
+
+### Prerequisites
+To run the tests on a Linux a few prerequisites are needed:
+
+1) mono
+2) msbuild
+
+### Running
+
+To run the tests:
+
+```
+./NetTest.sh
+```
+
+This will download the .NET installer and core SDK if those are not already
+installed. Then it will build the tests using `msbuild` and run the resulting
+test binary with `mono`.
+
+After running the tests, the downloaded .NET installer and SDK are *not* removed
+as they can be reused in subsequent invocations. The files are ignored by git by
+default, and can remain in the working directory.
+
+### Cleaning
+
+If you want to clean up the downloaded .NET installer and SDK, run:
+
+```
+./clean.sh
+```
+
+This will wipe away the downloaded files and directories. Those will be
+automatically re-downloaded when running `NetTest.sh`.
+
+
diff --git a/tests/FlatBuffers.Test/Resources/.gitkeep b/tests/FlatBuffers.Test/Resources/.gitkeep
deleted file mode 100644 (file)
index e69de29..0000000
diff --git a/tests/FlatBuffers.Test/clean.sh b/tests/FlatBuffers.Test/clean.sh
new file mode 100755 (executable)
index 0000000..41f6a4a
--- /dev/null
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+# Remove files and directory that are needed to build and run the .NET tests.
+# The script NetTest.sh installs these as needed.
+
+[ -d .dotnet_tmp ] && rm -rf .dotnet_tmp
+[ -d packages ] && rm -rf packages
+[ -d .tmp ] && rm -rf .tmp
+[ -f nuget.exe ] && rm nuget.exe
+[ -f dotnet-intall.sh ] && rm dotnet-install.sh