Skip to content

Commit db6ed87

Browse files
committed
Make it easier to run the integration test locally
1 parent bd3bd7b commit db6ed87

1 file changed

Lines changed: 45 additions & 3 deletions

File tree

src/IntegrationTests.HostV4/When_starting_the_function_host.cs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
using System;
44
using System.Diagnostics;
55
using System.IO;
6+
using System.Linq;
67
using System.Net.Http;
78
using System.Threading;
89
using System.Threading.Tasks;
910
using Azure.Messaging.ServiceBus.Administration;
11+
using Microsoft.Extensions.Configuration;
1012
using NUnit.Framework;
1113

1214
[TestFixture]
@@ -15,10 +17,50 @@ public class When_starting_the_function_host
1517
[Test]
1618
public async Task Should_not_blow_up()
1719
{
18-
var pathToFuncExe = Environment.GetEnvironmentVariable("PathToFuncExe");
19-
Assert.IsNotNull(pathToFuncExe, "Environment variable 'PathToFuncExe' should be defined to run tests. When running locally this is usually 'C:\\Users\\MyUser\\AppData\\Local\\AzureFunctionsTools\\Releases\\4.30.0\\cli_x64\\func.exe'");
20+
var configBuilder = new ConfigurationBuilder();
21+
configBuilder.SetBasePath(Directory.GetCurrentDirectory());
22+
configBuilder.AddEnvironmentVariables();
23+
configBuilder.AddJsonFile("local.settings.json", true);
2024

21-
var connectionString = Environment.GetEnvironmentVariable("AzureWebJobsServiceBus");
25+
var config = configBuilder.Build();
26+
27+
var pathToFuncExe = config.GetValue<string>("PathToFuncExe");
28+
29+
if (pathToFuncExe == null)
30+
{
31+
Console.WriteLine("Environment variable 'PathToFuncExe' not defined. Going to try to find the latest version.");
32+
33+
var userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
34+
var sdkPath = Path.Combine(userProfile, "AppData", "Local", "AzureFunctionsTools", "Releases");
35+
if (Directory.Exists(sdkPath))
36+
{
37+
var mostRecent = Directory.GetDirectories(sdkPath)
38+
.Select(path =>
39+
{
40+
var name = Path.GetFileName(path);
41+
Version.TryParse(name, out var version);
42+
return new { Name = name, Version = version };
43+
})
44+
.Where(x => x.Version is not null)
45+
.OrderByDescending(x => x.Version)
46+
.FirstOrDefault()
47+
?.Name;
48+
49+
if (mostRecent is not null)
50+
{
51+
var exePath = Path.Combine(sdkPath, mostRecent, "cli_x64", "func.exe");
52+
if (File.Exists(exePath))
53+
{
54+
Console.WriteLine("Found " + exePath);
55+
pathToFuncExe = exePath;
56+
}
57+
}
58+
}
59+
}
60+
61+
Assert.IsNotNull(pathToFuncExe, "Environment variable 'PathToFuncExe' should be defined to run tests. When running locally this is usually 'C:\\Users\\<username>\\AppData\\Local\\AzureFunctionsTools\\Releases\\<version>\\cli_x64\\func.exe'");
62+
63+
var connectionString = config.GetValue<string>("AzureWebJobsServiceBus") ?? config.GetValue<string>("Values:AzureWebJobsServiceBus");
2264
Assert.IsNotNull(connectionString, "Environment variable 'AzureWebJobsServiceBus' should be defined to run tests.");
2365

2466
var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(60));

0 commit comments

Comments
 (0)