Implemented NUnitTester, NuGetter.Push; refactoring

dependabot/npm_and_yarn/BuildServer/eslint-7.2.0
Inga 🏳‍🌈 11 years ago
parent 2f7f4432db
commit c70385dff2
  1. 40
      DotNetBuilder/MicroBuildServer.DotNetBuilder.csproj
  2. 210
      DotNetBuilder/NUnitTester.cs
  3. 2
      DotNetBuilder/NuGetPackRequest.cs
  4. 11
      DotNetBuilder/NuGetPushRequest.cs
  5. 42
      DotNetBuilder/NuGetter.cs
  6. 15
      DotNetBuilder/Program.cs
  7. 7
      DotNetBuilder/Response.cs
  8. 4
      DotNetBuilder/TestRequest.cs
  9. 11
      DotNetBuilder/app.config
  10. 3
      DotNetBuilder/packages.config

@ -34,6 +34,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="log4net">
<HintPath>..\packages\NUnit.Runners.2.6.3\tools\lib\log4net.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Build" />
<Reference Include="Microsoft.Build.Framework" />
<Reference Include="Microsoft.Build.Utilities.v4.0" />
@ -41,7 +44,37 @@
<HintPath>..\packages\Newtonsoft.Json.5.0.8\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NuGet">
<HintPath>..\.nuget\NuGet.exe</HintPath>
<HintPath>..\packages\NuGet.CommandLine.2.7.3\tools\NuGet.exe</HintPath>
</Reference>
<Reference Include="nunit-console-runner">
<HintPath>..\packages\NUnit.Runners.2.6.3\tools\lib\nunit-console-runner.dll</HintPath>
</Reference>
<Reference Include="nunit-gui-runner">
<HintPath>..\packages\NUnit.Runners.2.6.3\tools\lib\nunit-gui-runner.dll</HintPath>
</Reference>
<Reference Include="nunit.core">
<HintPath>..\packages\NUnit.Runners.2.6.3\tools\lib\nunit.core.dll</HintPath>
</Reference>
<Reference Include="nunit.core.interfaces">
<HintPath>..\packages\NUnit.Runners.2.6.3\tools\lib\nunit.core.interfaces.dll</HintPath>
</Reference>
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.5.7.10213\lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="nunit.mocks">
<HintPath>..\packages\NUnit.2.5.7.10213\lib\nunit.mocks.dll</HintPath>
</Reference>
<Reference Include="nunit.uiexception">
<HintPath>..\packages\NUnit.Runners.2.6.3\tools\lib\nunit.uiexception.dll</HintPath>
</Reference>
<Reference Include="nunit.uikit">
<HintPath>..\packages\NUnit.Runners.2.6.3\tools\lib\nunit.uikit.dll</HintPath>
</Reference>
<Reference Include="nunit.util">
<HintPath>..\packages\NUnit.Runners.2.6.3\tools\lib\nunit.util.dll</HintPath>
</Reference>
<Reference Include="pnunit.framework">
<HintPath>..\packages\NUnit.2.5.7.10213\lib\pnunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
@ -54,14 +87,17 @@
<ItemGroup>
<Compile Include="Compiler.cs" />
<Compile Include="CompileRequest.cs" />
<Compile Include="NuGetRequest.cs" />
<Compile Include="NuGetPackRequest.cs" />
<Compile Include="NuGetPushRequest.cs" />
<Compile Include="NuGetter.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Response.cs" />
<Compile Include="NUnitTester.cs" />
<Compile Include="TestRequest.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

@ -0,0 +1,210 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using NuGet;
using NUnit.Core;
using NUnit.Framework;
using System.Reflection;
using NUnit.Util;
namespace MicroBuildServer.DotNetBuilder
{
class NUnitTester
{
private const string DATA_TEST_RESULTS_KEY = "TestResults";
private class Listener : EventListener
{
public readonly List<Response.Message> Messages = new List<Response.Message>();
private bool runFail;
private bool suiteFail;
private static string FormatResult(TestResult result)
{
var additional = new List<string>();
if (result.IsSuccess)
{
additional.Add("success");
}
else if (result.IsError)
{
additional.Add("error");
}
else if (result.IsFailure)
{
additional.Add("fail");
}
else
{
additional.Add("status unknown");
}
if (!string.IsNullOrEmpty(result.Message))
{
additional.Add("message = " + result.Message);
}
return additional.Any() ? result.Name + "; " + string.Join(", ", additional) : result.Name;
}
public void RunFinished(Exception exception)
{
Messages.Add(Response.Message.CreateError("Run finished: " + exception));
runFail = true;
}
public void RunFinished(TestResult result)
{
var message = string.Format("Run finished: {0}", FormatResult(result));
if (!result.IsSuccess && !runFail)
{
Messages.Add(Response.Message.CreateError(message));
}
else
{
Messages.Add(Response.Message.CreateInfo(message));
}
}
public void RunStarted(string name, int testCount)
{
Messages.Add(Response.Message.CreateInfo("Run started: " + name));
runFail = false;
}
public void SuiteFinished(TestResult result)
{
var message = string.Format("Suite finished: {0}", FormatResult(result));
if (!result.IsSuccess && !suiteFail)
{
Messages.Add(Response.Message.CreateError(message));
}
else
{
Messages.Add(Response.Message.CreateInfo(message));
}
if (!result.IsSuccess)
{
runFail = true;
}
}
public void SuiteStarted(TestName testName)
{
Messages.Add(Response.Message.CreateInfo("Suite started: " + testName.Name));
suiteFail = false;
}
public void TestFinished(TestResult result)
{
var message = string.Format("Test finished: {0}", FormatResult(result));
if (!result.IsSuccess)
{
Messages.Add(Response.Message.CreateError(message));
}
else
{
Messages.Add(Response.Message.CreateInfo(message));
}
suiteFail = true;
}
public void TestOutput(TestOutput testOutput)
{
Messages.Add(Response.Message.CreateInfo("Test output: " + testOutput.Text));
}
public void TestStarted(TestName testName)
{
Messages.Add(Response.Message.CreateInfo("Test started: " + testName.Name));
}
public void UnhandledException(Exception exception)
{
Messages.Add(Response.Message.CreateError("Unhandled exception: " + exception));
suiteFail = true;
runFail = true;
}
}
[Serializable]
private class TestWorker
{
public string TestLibraryPath { get; set; }
public void DoTest()
{
var listener = new Listener();
CoreExtensions.Host.InitializeService();
var package = new TestPackage(TestLibraryPath);
//package.AutoBinPath = true;
//package.BasePath = Path.GetDirectoryName(TestLibraryPath);
//package.ConfigurationFile = TestLibraryPath + ".config";
TestRunner runner = new SimpleTestRunner();
if (runner.Load(package))
{
runner.Run(listener, TestFilter.Empty, true, LoggingThreshold.All);
}
//DebugTestResult(Console.Out, result);
var messages = listener.Messages.Any() ? listener.Messages.ToArray() : new[] {Response.Message.CreateError("No messages from listener")};
AppDomain.CurrentDomain.SetData(DATA_TEST_RESULTS_KEY, new Response { Messages = messages });
}
}
/*private static void DebugTestResult(object rawResult, int level = 0)
{
if (rawResult == null)
{
Console.WriteLine("Result is null");
return;
}
var prefix = new string('\t', level);
Console.WriteLine("{0}RESULT START", prefix);
Console.WriteLine("{0}Type: {1}", prefix, rawResult);
if (rawResult is TestResult)
{
var result = (TestResult) rawResult;
Console.WriteLine("{0}Full name: {1}", prefix, result.FullName);
Console.WriteLine("{0}Has results: {1}", prefix, result.HasResults);
Console.WriteLine("{0}Success? {1}", prefix, result.IsSuccess);
Console.WriteLine("{0}Message: {1}", prefix, result.Message);
Console.WriteLine("{0}Test: {1}", prefix, result.Test.TestName);
if (result.Results != null)
{
Console.WriteLine("{0}Results: {1}", prefix, result.Results.Count);
foreach (var v in result.Results)
{
DebugTestResult(v, level+1);
}
}
}
Console.WriteLine("{0}RESULT END", prefix);
}*/
public static Response Test(TestRequest request)
{
AppDomainSetup setup = new AppDomainSetup();
setup.ConfigurationFile = request.TestLibraryPath + ".config";
AppDomain tester = AppDomain.CreateDomain("tester", AppDomain.CurrentDomain.Evidence, setup);
var worker = new TestWorker();
worker.TestLibraryPath = request.TestLibraryPath;
var del = new CrossAppDomainDelegate(worker.DoTest);
tester.DoCallBack(del);
return (Response)(tester.GetData(DATA_TEST_RESULTS_KEY));
}
}
}

@ -5,7 +5,7 @@ using System.Text;
namespace MicroBuildServer.DotNetBuilder
{
class NuGetRequest
class NuGetPackRequest
{
public string BaseDirectory { get; set; }

@ -0,0 +1,11 @@
namespace MicroBuildServer.DotNetBuilder
{
class NuGetPushRequest
{
public string Package { get; set; }
public string NugetHost { get; set; }
public string ApiKey { get; set; }
}
}

@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NuGet;
using NuGet.Commands;
using NuGet.Common;
namespace MicroBuildServer.DotNetBuilder
@ -57,7 +57,7 @@ namespace MicroBuildServer.DotNetBuilder
public Verbosity Verbosity
{
get { throw new NotImplementedException(); }
get { return Verbosity.Detailed; }
set { throw new NotImplementedException(); }
}
@ -99,7 +99,7 @@ namespace MicroBuildServer.DotNetBuilder
public void WriteLine(ConsoleColor color, string value, params object[] args)
{
throw new NotImplementedException();
WriteLine(value, args);
}
public void WriteLine(string format, params object[] args)
@ -163,10 +163,10 @@ namespace MicroBuildServer.DotNetBuilder
}
}
public static Response Publish(NuGetRequest request)
public static Response Pack(NuGetPackRequest request)
{
var console = new Console();
var command = new NuGet.Commands.PackCommand
var command = new PackCommand
{
BasePath = request.BaseDirectory,
OutputDirectory = request.OutputDirectory,
@ -176,7 +176,39 @@ namespace MicroBuildServer.DotNetBuilder
Rules = new IPackageRule[0],
};
command.Arguments.Add(request.SpecPath);
try
{
command.Execute();
}
catch (Exception e)
{
console.WriteError(e);
}
return new Response { Messages = console.Messages.ToArray() };
}
public static Response Push(NuGetPushRequest request)
{
var console = new Console();
var command = new PushCommand
{
Source = request.NugetHost,
ApiKey = request.ApiKey,
Console = console,
Verbosity = Verbosity.Detailed,
};
command.Arguments.Add(request.Package);
try
{
command.Execute();
}
catch (Exception e)
{
console.WriteError(e);
}
return new Response {Messages = console.Messages.ToArray()};
}

@ -8,11 +8,6 @@ namespace MicroBuildServer.DotNetBuilder
{
class Program
{
private static Response Test(TestRequest request)
{
throw new NotImplementedException();
}
private static Response Process(string input, string[] args)
{
try
@ -21,10 +16,12 @@ namespace MicroBuildServer.DotNetBuilder
{
case "compile":
return Compiler.Compile(JsonConvert.DeserializeObject<CompileRequest>(input));
case "test":
return Test(JsonConvert.DeserializeObject<TestRequest>(input));
case "nuget":
return NuGetter.Publish(JsonConvert.DeserializeObject<NuGetRequest>(input));
case "nunit":
return NUnitTester.Test(JsonConvert.DeserializeObject<TestRequest>(input));
case "nugetpack":
return NuGetter.Pack(JsonConvert.DeserializeObject<NuGetPackRequest>(input));
case "nugetpush":
return NuGetter.Push(JsonConvert.DeserializeObject<NuGetPushRequest>(input));
default:
throw new ApplicationException("Unsupported type '" + args[0] + "'");
}

@ -5,8 +5,10 @@ using System.Text;
namespace MicroBuildServer.DotNetBuilder
{
[Serializable]
class Response
{
[Serializable]
public class Message
{
public string Type { get; set; }
@ -26,6 +28,11 @@ namespace MicroBuildServer.DotNetBuilder
{
return new Message { Type = "error", Body = body };
}
public override string ToString()
{
return string.Format("{0}: {1}", Type, Body);
}
}
public Message[] Messages { get; set; }

@ -7,8 +7,6 @@ namespace MicroBuildServer.DotNetBuilder
{
class TestRequest
{
public string CodebasePath { get; set; }
public string TestLibraryName { get; set; }
public string TestLibraryPath { get; set; }
}
}

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="nunit.core.interfaces" publicKeyToken="96d09a1eb7f44a77" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.6.3.13283" newVersion="2.6.3.13283" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

@ -1,4 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="5.0.8" targetFramework="net40" />
<package id="NuGet.CommandLine" version="2.7.3" targetFramework="net40" />
<package id="NUnit" version="2.6.3" targetFramework="net40" />
<package id="NUnit.Runners" version="2.6.3" targetFramework="net40" />
</packages>
Loading…
Cancel
Save