Last active
November 12, 2017 11:40
-
-
Save dariuszparzygnat/c414c7f4aab5e9017c0e8cac5a8f2717 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.Azure.WebJobs.Host; | |
using System.IO; | |
using System.Reflection; | |
namespace TestsViaFunction | |
{ | |
public static class RetrieveTypes | |
{ | |
[FunctionName("RetrieveTypes")] | |
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, [Queue("queue-with-tests", Connection = "storage_Conn")] ICollector<TestTypeInfo> outputQueueItem, TraceWriter log) | |
{ | |
string fileName = GetHeader(req, "fileName"); | |
Guid executiondIdentifier = Guid.NewGuid(); | |
var dllLocation = ConfigurationManager.AppSettings["testDllDirectory"]; | |
var testAssemblyPath = Path.Combine(dllLocation, fileName); | |
var loadedAssembly = Assembly.LoadFile(testAssemblyPath); | |
var testTypes = GetLoadableTypes(loadedAssembly).Where(type => type.IsClass && type.Name.Contains("Test")).Select(e => new TestTypeInfo() { DllPath = testAssemblyPath, TypeName = e.FullName, Guid = executiondIdentifier }).ToList(); | |
if (!testTypes.Any()) | |
{ | |
return req.CreateResponse(HttpStatusCode.BadRequest, "Assembly has no test types"); | |
} | |
testTypes.ForEach(e => outputQueueItem.Add(e)); | |
return req.CreateResponse(HttpStatusCode.OK, testTypes); | |
} | |
public static string GetHeader(HttpRequestMessage request, string key) | |
{ | |
IEnumerable<string> keys = null; | |
if (!request.Headers.TryGetValues(key, out keys)) | |
return null; | |
return keys.First(); | |
} | |
public static IEnumerable<Type> GetLoadableTypes(Assembly assembly) | |
{ | |
try | |
{ | |
return assembly.GetTypes(); | |
} | |
catch (ReflectionTypeLoadException e) | |
{ | |
return e.Types.Where(t => t != null); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment